1 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 ; 57 import java.lang.reflect.Method ; 58 import java.net.MalformedURLException ; 59 import java.net.URL ; 60 import java.net.URLClassLoader ; 61 import java.util.Vector ; 62 63 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 cl; 71 72 public EnterpriseBeanBuilder(ClassLoader 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 persistenceType = ((EntityBeanInfo) bean).persistenceType; 81 ejbType = (persistenceType.equalsIgnoreCase("Container")) ? EjbType.CMP_ENTITY : EjbType.BMP_ENTITY; 82 } else { 83 throw new UnsupportedOperationException ("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 classLoader; 91 private final String ejbDeploymentId; 92 93 public Loader(String codebase, String ejbDeploymentId) throws OpenEJBException { 94 try { 95 this.classLoader = new URLClassLoader (new URL []{new File (codebase).toURL()}); 96 } catch (MalformedURLException e) { 97 throw new OpenEJBException(messages.format("cl0001", codebase, e.getMessage())); 98 } 99 this.ejbDeploymentId = ejbDeploymentId; 100 } 101 102 public Class load(String className, String artifact) throws OpenEJBException { 103 try { 104 return classLoader.loadClass(className); 105 } catch (ClassNotFoundException e) { 106 throw new OpenEJBException(messages.format("classNotFound." + artifact, className, ejbDeploymentId, e.getMessage())); 107 } 108 } 109 } 110 111 112 public Object build() throws OpenEJBException { 113 Class ejbClass = loadClass(bean.ejbClass, "classNotFound.ejbClass"); 114 115 Class home = null; 116 Class 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 localhome = null; 123 Class 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 primaryKey = null; 130 if (ejbType.isEntity() && ((EntityBeanInfo) bean).primKeyClass != null) { 131 String className = ((EntityBeanInfo) bean).primKeyClass; 132 primaryKey = loadClass(className, "classNotFound.primaryKey"); 133 } 134 final String 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 finderMethods = new Vector (); 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 ) 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 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 loadClass(String className, String messageCode) throws OpenEJBException { 183 try { 184 return cl.loadClass(className); 185 } catch (ClassNotFoundException cnfe) { 186 String 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 |