KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > assembler > classic > EnterpriseBeanBuilder


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EnterpriseBeanBuilder.java 2095 2005-08-23 04:22:57Z dblevins $
44  */

45
46 package org.openejb.alt.assembler.classic;
47
48 import org.openejb.OpenEJBException;
49 import org.openejb.SystemException;
50 import org.openejb.core.DeploymentContext;
51 import org.openejb.core.DeploymentInfo;
52 import org.openejb.core.ivm.naming.IvmContext;
53 import org.openejb.util.Messages;
54 import org.openejb.util.SafeToolkit;
55
56 import java.io.File JavaDoc;
57 import java.lang.reflect.Method JavaDoc;
58 import java.net.MalformedURLException JavaDoc;
59 import java.net.URL JavaDoc;
60 import java.net.URLClassLoader JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 /**
64  * @version $Revision: 2095 $ $Date: 2005-08-22 21:22:57 -0700 (Mon, 22 Aug 2005) $
65  */

66 class EnterpriseBeanBuilder {
67     protected static final Messages messages = new Messages("org.openejb.util.resources");
68     private final EnterpriseBeanInfo bean;
69     private final EjbType ejbType;
70     private final ClassLoader JavaDoc cl;
71
72     public EnterpriseBeanBuilder(ClassLoader JavaDoc cl, EnterpriseBeanInfo bean) {
73         this.bean = bean;
74
75         if (bean.type == EnterpriseBeanInfo.STATEFUL) {
76             ejbType = EjbType.STATEFUL;
77         } else if (bean.type == EnterpriseBeanInfo.STATELESS) {
78             ejbType = EjbType.STATELESS;
79         } else if (bean.type == EnterpriseBeanInfo.ENTITY) {
80             String JavaDoc persistenceType = ((EntityBeanInfo) bean).persistenceType;
81             ejbType = (persistenceType.equalsIgnoreCase("Container")) ? EjbType.CMP_ENTITY : EjbType.BMP_ENTITY;
82         } else {
83             throw new UnsupportedOperationException JavaDoc("No building support for bean type: " + bean);
84         }
85         this.cl = cl;
86     }
87
88     static class Loader {
89         protected static final Messages messages = new Messages("org.openejb.util.resources");
90         private final ClassLoader JavaDoc classLoader;
91         private final String JavaDoc ejbDeploymentId;
92
93         public Loader(String JavaDoc codebase, String JavaDoc ejbDeploymentId) throws OpenEJBException {
94             try {
95                 this.classLoader = new URLClassLoader JavaDoc(new URL JavaDoc[]{new File JavaDoc(codebase).toURL()});
96             } catch (MalformedURLException JavaDoc e) {
97                 throw new OpenEJBException(messages.format("cl0001", codebase, e.getMessage()));
98             }
99             this.ejbDeploymentId = ejbDeploymentId;
100         }
101
102         public Class JavaDoc load(String JavaDoc className, String JavaDoc artifact) throws OpenEJBException {
103             try {
104                 return classLoader.loadClass(className);
105             } catch (ClassNotFoundException JavaDoc e) {
106                 throw new OpenEJBException(messages.format("classNotFound." + artifact, className, ejbDeploymentId, e.getMessage()));
107             }
108         }
109     }
110
111
112     public Object JavaDoc build() throws OpenEJBException {
113         Class JavaDoc ejbClass = loadClass(bean.ejbClass, "classNotFound.ejbClass");
114
115         Class JavaDoc home = null;
116         Class JavaDoc remote = null;
117         if (bean.home != null) {
118             home = loadClass(bean.home, "classNotFound.home");
119             remote = loadClass(bean.remote, "classNotFound.remote");
120         }
121
122         Class JavaDoc localhome = null;
123         Class JavaDoc local = null;
124         if (bean.localHome != null) {
125             localhome = loadClass(bean.localHome, "classNotFound.localHome");
126             local = loadClass(bean.local, "classNotFound.local");
127         }
128
129         Class JavaDoc primaryKey = null;
130         if (ejbType.isEntity() && ((EntityBeanInfo) bean).primKeyClass != null) {
131             String JavaDoc className = ((EntityBeanInfo) bean).primKeyClass;
132             primaryKey = loadClass(className, "classNotFound.primaryKey");
133         }
134         final String JavaDoc transactionType = bean.transactionType;
135
136         JndiEncBuilder jndiEncBuilder = new JndiEncBuilder(bean.jndiEnc, transactionType, ejbType);
137         IvmContext root = (IvmContext) jndiEncBuilder.build();
138
139         DeploymentContext deploymentContext = new DeploymentContext(bean.ejbDeploymentId, ejbClass.getClassLoader(), root);
140         DeploymentInfo deployment = new DeploymentInfo(deploymentContext, home, remote, localhome, local, ejbClass, primaryKey, ejbType.getType());
141
142         if (ejbType.isSession()) {
143             deployment.setBeanManagedTransaction("Bean".equalsIgnoreCase(bean.transactionType));
144         }
145
146         if (ejbType.isEntity()) {
147             EntityBeanInfo entity = (EntityBeanInfo) bean;
148
149             deployment.setIsReentrant(entity.reentrant.equalsIgnoreCase("true"));
150
151             if (ejbType == EjbType.CMP_ENTITY) {
152                 QueryInfo[] queries = (entity.queries == null)? new QueryInfo[]{}:entity.queries;
153                 for (int i = 0; i < queries.length; i++) {
154                     QueryInfo query = queries[i];
155
156                     Vector JavaDoc finderMethods = new Vector JavaDoc();
157
158                     if (home != null ){
159                         AssemblerTool.resolveMethods(finderMethods, home, query.method);
160                     }
161                     if (localhome != null ){
162                         AssemblerTool.resolveMethods(finderMethods, localhome, query.method);
163                     }
164                     for (int j = 0; j < finderMethods.size(); j++) {
165                         deployment.addQuery((Method JavaDoc) finderMethods.elementAt(j), query.queryStatement);
166                     }
167                 }
168                 deployment.setCmrFields(entity.cmpFieldNames);
169
170                 if (entity.primKeyField != null) {
171                     try {
172                         deployment.setPrimKeyField(entity.primKeyField);
173                     } catch (NoSuchFieldException JavaDoc e) {
174                         throw new SystemException("Can not set prim-key-field on deployment " + entity.ejbDeploymentId, e);
175                     }
176                 }
177             }
178         }
179         return deployment;
180     }
181
182     private Class JavaDoc loadClass(String JavaDoc className, String JavaDoc messageCode) throws OpenEJBException {
183         try {
184             return cl.loadClass(className);
185         } catch (ClassNotFoundException JavaDoc cnfe) {
186             String JavaDoc message = SafeToolkit.messages.format("cl0007", className, bean.codebase);
187             throw new OpenEJBException(AssemblerTool.messages.format(messageCode, className, bean.ejbDeploymentId, message));
188         }
189     }
190
191
192 }
193
Popular Tags