1 22 package org.jboss.ejb3.entity; 23 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Properties ; 30 import javax.naming.InitialContext ; 31 import javax.persistence.EntityManager; 32 import javax.persistence.EntityManagerFactory; 33 import javax.persistence.spi.PersistenceProvider; 34 import javax.persistence.spi.PersistenceUnitTransactionType; 35 import org.hibernate.ejb.HibernatePersistence; 36 import org.hibernate.ejb.packaging.PersistenceMetadata; 37 import org.jboss.ejb3.DependencyPolicy; 38 import org.jboss.ejb3.DeploymentUnit; 39 import org.jboss.ejb3.Ejb3Deployment; 40 import org.jboss.ejb3.NonSerializableFactory; 41 import org.jboss.logging.Logger; 42 43 49 public class PersistenceUnitDeployment 50 { 51 private static final Logger log = Logger.getLogger(PersistenceUnitDeployment.class); 52 53 protected InitialContext initialContext; 54 protected DeploymentUnit di; 55 protected List <String > explicitEntityClasses = new ArrayList <String >(); 56 protected ManagedEntityManagerFactory managedFactory; 57 protected EntityManagerFactory actualFactory; 58 protected URL persistenceXmlUrl; 59 protected PersistenceMetadata xml; 60 protected String kernelName; 61 protected Ejb3Deployment deployment; 62 protected boolean scoped; 63 64 public PersistenceUnitDeployment(InitialContext initialContext, Ejb3Deployment deployment, List <String > explicitEntityClasses, URL persistenceXmlUrl, PersistenceMetadata metadata, String ear, String jar, boolean isScoped) 65 { 66 this.scoped = isScoped; 67 this.deployment = deployment; 68 this.initialContext = initialContext; 69 this.di = deployment.getDeploymentUnit(); 70 this.explicitEntityClasses = explicitEntityClasses; 71 this.persistenceXmlUrl = persistenceXmlUrl; 72 xml = metadata; 73 kernelName = "persistence.units:"; 74 String name = getEntityManagerName(); 75 if (name == null || name.equals("")) 76 throw new RuntimeException ("Empty string is not allowed for a persistence unit. Fix your persistence.xml file"); 77 if (ear != null) 78 { 79 kernelName += "ear=" + ear; 80 if (!ear.endsWith(".ear")) kernelName += ".ear"; 81 kernelName += ","; 82 } 83 if (isScoped) 84 { 85 kernelName += "jar=" + jar; 86 if (!jar.endsWith(".jar")) kernelName += ".jar"; 87 kernelName += ","; 88 } 89 kernelName += "unitName=" + name; 90 } 91 92 public static String getDefaultKernelName(String unitName) 93 { 94 int hashIndex = unitName.indexOf('#'); 95 if (hashIndex != -1) 96 { 97 String relativePath = unitName.substring(3, hashIndex); 98 String name = unitName.substring(hashIndex + 1); 99 return "persistence.units:jar=" + relativePath + "," + "unitName=" + name; 100 } 101 return "persistence.units:unitName=" + unitName; 102 } 103 104 public boolean isScoped() 105 { 106 return scoped; 107 } 108 109 public Ejb3Deployment getDeployment() 110 { 111 return deployment; 112 } 113 114 protected String getJaccContextId() 115 { 116 return di.getShortName(); 117 } 118 119 public EntityManagerFactory getActualFactory() 120 { 121 return actualFactory; 122 } 123 124 public PersistenceMetadata getXml() 125 { 126 return xml; 127 } 128 129 public String getKernelName() 130 { 131 return kernelName; 132 } 133 134 public String getEntityManagerName() 135 { 136 return xml.getName() == null ? "" : xml.getName(); 137 } 138 139 public ManagedEntityManagerFactory getManagedFactory() 140 { 141 return managedFactory; 142 } 143 144 public void addDependencies(DependencyPolicy policy) 145 { 146 Properties props = getXml().getProps(); 147 if (!props.containsKey("jboss.no.implicit.datasource.dependency")) 148 { 149 if (getXml().getJtaDatasource() != null) 150 { 151 String ds = getXml().getJtaDatasource(); 152 policy.addDatasource(ds); 153 } 154 if (getXml().getNonJtaDatasource() != null) 155 { 156 String ds = getXml().getNonJtaDatasource(); 157 policy.addDatasource(ds); 158 } 159 } 160 for (Object prop : props.keySet()) 161 { 162 String property = (String )prop; 163 if (property.startsWith("jboss.depends")) 164 { 165 policy.addDependency(props.getProperty(property)); 166 } 167 } 168 169 } 170 171 172 public void start() 173 throws Exception 174 { 175 Properties props = new Properties (); 176 props.putAll(di.getDefaultPersistenceProperties()); 177 props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId()); 178 179 PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl(); 180 pi.setClassLoader(di.getClassLoader()); 181 182 ArrayList <URL > jarFiles = new ArrayList <URL >(); 183 pi.setJarFiles(jarFiles); 184 pi.setPersistenceProviderClassName(HibernatePersistence.class.getName()); 185 log.debug("Found persistence.xml file in EJB3 jar"); 186 props.putAll(xml.getProps()); 187 pi.setManagedClassnames(xml.getClasses()); 188 pi.setPersistenceUnitName(xml.getName()); 189 pi.setMappingFileNames(xml.getMappingFiles()); 190 pi.setExcludeUnlistedClasses(xml.getExcludeUnlistedClasses()); 191 pi.setPersistenceUnitRootUrl(di.getUrl()); 192 PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.JTA; 193 if ("RESOURCE_LOCAL".equals(xml.getTransactionType())) 194 transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL; 195 pi.setTransactionType(transactionType); 196 197 for (String jar : xml.getJarFiles()) 198 { 199 jarFiles.add(deployment.getDeploymentUnit().getRelativeURL(jar)); 200 } 201 202 203 if (xml.getProvider() != null) pi.setPersistenceProviderClassName(xml.getProvider()); 204 if (explicitEntityClasses.size() > 0) 205 { 206 List <String > classes = pi.getManagedClassNames(); 207 if (classes == null) classes = explicitEntityClasses; 208 else classes.addAll(explicitEntityClasses); 209 pi.setManagedClassnames(classes); 210 } 211 if (xml.getJtaDatasource() != null) 212 { 213 pi.setJtaDataSource((javax.sql.DataSource ) initialContext.lookup(xml.getJtaDatasource())); 214 } 215 else if (transactionType == PersistenceUnitTransactionType.JTA) 216 { 217 throw new RuntimeException ("You have not defined a jta-data-source for a JTA enabled persistence context named: " + xml.getName()); 218 } 219 if (xml.getNonJtaDatasource() != null) 220 { 221 pi.setNonJtaDataSource((javax.sql.DataSource ) initialContext.lookup(xml.getNonJtaDatasource())); 222 } 223 else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL) 224 { 225 throw new RuntimeException ("You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: " + xml.getName()); 226 } 227 pi.setProperties(props); 228 229 if (pi.getPersistenceUnitName() == null) 230 { 231 throw new RuntimeException ("you must specify a name in persistence.xml"); 232 } 233 234 Class providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName()); 235 236 PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance(); 237 actualFactory = pp.createContainerEntityManagerFactory(pi, null); 238 239 managedFactory = new ManagedEntityManagerFactory(actualFactory, kernelName); 240 241 String entityManagerJndiName = (String ) props.get("jboss.entity.manager.jndi.name"); 242 if (entityManagerJndiName != null) 243 { 244 EntityManager injectedManager = new TransactionScopedEntityManager(managedFactory); 245 NonSerializableFactory.rebind(initialContext, entityManagerJndiName, injectedManager); 246 } 247 String entityManagerFactoryJndiName = (String ) props.get("jboss.entity.manager.factory.jndi.name"); 248 if (entityManagerFactoryJndiName != null) 249 { 250 EntityManagerFactory injectedFactory = new InjectedEntityManagerFactory(managedFactory); 251 NonSerializableFactory.rebind(initialContext, entityManagerFactoryJndiName, injectedFactory); 252 } 253 } 254 255 public void stop() throws Exception 256 { 257 String entityManagerJndiName = (String ) xml.getProps().get("jboss.entity.manager.jndi.name"); 258 if (entityManagerJndiName != null) 259 { 260 NonSerializableFactory.unbind(initialContext, entityManagerJndiName); 261 } 262 String entityManagerFactoryJndiName = (String ) xml.getProps().get("jboss.entity.manager.factory.jndi.name"); 263 if (entityManagerFactoryJndiName != null) 264 { 265 NonSerializableFactory.unbind(initialContext, entityManagerFactoryJndiName); 266 } 267 managedFactory.destroy(); 268 } 269 270 271 } 272 | Popular Tags |