1 17 18 package org.apache.geronimo.persistence; 19 20 import java.util.List ; 21 import java.util.Properties ; 22 import java.util.Map ; 23 import java.util.ArrayList ; 24 import java.net.URL ; 25 26 import javax.persistence.spi.PersistenceUnitInfo; 27 import javax.persistence.spi.PersistenceUnitTransactionType; 28 import javax.persistence.spi.ClassTransformer; 29 import javax.persistence.spi.PersistenceProvider; 30 import javax.persistence.EntityManagerFactory; 31 import javax.persistence.PersistenceException; 32 import javax.persistence.EntityManager; 33 import javax.sql.DataSource ; 34 35 import org.apache.geronimo.gbean.GBeanInfo; 36 import org.apache.geronimo.gbean.GBeanInfoBuilder; 37 import org.apache.geronimo.gbean.GBeanLifecycle; 38 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 40 import org.apache.geronimo.connector.outbound.ConnectionFactorySource; 41 import org.apache.geronimo.transformer.TransformerAgent; 42 import org.apache.geronimo.kernel.classloader.JarFileClassLoader; 43 44 47 public class PersistenceUnitGBean implements GBeanLifecycle { 48 49 private final PersistenceUnitInfoImpl persistenceUnitInfo; 50 private final EntityManagerFactory entityManagerFactory; 51 private final TransactionManagerImpl transactionManager; 52 53 54 public PersistenceUnitGBean() { 55 persistenceUnitInfo = null; 56 entityManagerFactory = null; 57 transactionManager = null; 58 } 59 60 public PersistenceUnitGBean(String persistenceUnitName, 61 String persistenceProviderClassName, 62 String persistenceUnitTransactionTypeString, 63 ConnectionFactorySource jtaDataSourceWrapper, 64 ConnectionFactorySource nonJtaDataSourceWrapper, 65 List mappingFileNamesUntyped, 66 List jarFileUrlsUntyped, 67 URL persistenceUnitRootUrl, 68 List managedClassNamesUntyped, 69 boolean excludeUnlistedClassesValue, 70 Properties properties, 71 TransactionManagerImpl transactionManager, 72 ClassLoader classLoader) { 73 List <String > mappingFileNames = mappingFileNamesUntyped == null? new ArrayList <String >(): new ArrayList <String >(mappingFileNamesUntyped); 74 List <URL > jarFileUrls = jarFileUrlsUntyped == null? new ArrayList <URL >(): new ArrayList <URL >(jarFileUrlsUntyped); 75 List <String > managedClassNames = managedClassNamesUntyped == null? new ArrayList <String >(): new ArrayList <String >(managedClassNamesUntyped); 76 PersistenceUnitTransactionType persistenceUnitTransactionType = persistenceUnitTransactionTypeString == null? PersistenceUnitTransactionType.JTA: PersistenceUnitTransactionType.valueOf(persistenceUnitTransactionTypeString); 77 78 persistenceUnitInfo = new PersistenceUnitInfoImpl(persistenceUnitName, 79 persistenceProviderClassName, 80 persistenceUnitTransactionType, 81 jtaDataSourceWrapper == null? null: (DataSource )jtaDataSourceWrapper.$getResource(), 82 nonJtaDataSourceWrapper == null? null: (DataSource )nonJtaDataSourceWrapper.$getResource(), 83 mappingFileNames, 84 jarFileUrls, 85 persistenceUnitRootUrl, 86 managedClassNames, 87 excludeUnlistedClassesValue, 88 properties, 89 classLoader); 90 try { 91 Class clazz = classLoader.loadClass(persistenceProviderClassName); 92 PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance(); 93 entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(persistenceUnitInfo, properties); 94 } catch (ClassNotFoundException e) { 95 throw new PersistenceException("Could not locate PersistenceProvider class: " + persistenceProviderClassName + " in classloader " + classLoader, e); 96 } catch (InstantiationException e) { 97 throw new PersistenceException("Could not create PersistenceProvider instance: " + persistenceProviderClassName + " loaded from classloader " + classLoader, e); 98 } catch (IllegalAccessException e) { 99 throw new PersistenceException("Could not create PersistenceProvider instance: " + persistenceProviderClassName + " loaded from classloader " + classLoader, e); 100 } 101 this.transactionManager = transactionManager; 102 } 103 104 public EntityManagerFactory getEntityManagerFactory() { 105 return entityManagerFactory; 106 } 107 108 public EntityManager getEntityManager(boolean transactionScoped, Map properties) { 109 if (transactionScoped) { 110 return new CMPEntityManagerTxScoped(transactionManager, getPersistenceUnitName(), entityManagerFactory, properties); 111 } else { 112 return new CMPEntityManagerExtended(transactionManager, getPersistenceUnitName(), entityManagerFactory, properties); 113 } 114 } 115 116 public String getPersistenceUnitName() { 117 return persistenceUnitInfo.getPersistenceUnitName(); 118 } 119 120 public String getPersistenceProviderClassName() { 121 return persistenceUnitInfo.getPersistenceProviderClassName(); 122 } 123 124 public PersistenceUnitTransactionType getTransactionType() { 125 return persistenceUnitInfo.getTransactionType(); 126 } 127 128 public DataSource getJtaDataSource() { 129 return persistenceUnitInfo.getJtaDataSource(); 130 } 131 132 public DataSource getNonJtaDataSource() { 133 return persistenceUnitInfo.getNonJtaDataSource(); 134 } 135 136 public List <String > getMappingFileNames() { 137 return persistenceUnitInfo.getMappingFileNames(); 138 } 139 140 public List <URL > getJarFileUrls() { 141 return persistenceUnitInfo.getJarFileUrls(); 142 } 143 144 public URL getPersistenceUnitRootUrl() { 145 return persistenceUnitInfo.getPersistenceUnitRootUrl(); 146 } 147 148 public List <String > getManagedClassNames() { 149 return persistenceUnitInfo.getManagedClassNames(); 150 } 151 152 public boolean excludeUnlistedClasses() { 153 return persistenceUnitInfo.excludeUnlistedClasses(); 154 } 155 156 public Properties getProperties() { 157 return persistenceUnitInfo.getProperties(); 158 } 159 160 public ClassLoader getClassLoader() { 161 return persistenceUnitInfo.getClassLoader(); 162 } 163 164 public void addTransformer(ClassTransformer classTransformer) { 165 persistenceUnitInfo.addTransformer(classTransformer); 166 } 167 168 public ClassLoader getNewTempClassLoader() { 169 return persistenceUnitInfo.getNewTempClassLoader(); 170 } 171 172 public void doStart() throws Exception { 173 } 174 175 public void doStop() throws Exception { 176 entityManagerFactory.close(); 178 } 179 180 public void doFail() { 181 entityManagerFactory.close(); 182 } 183 184 private static class PersistenceUnitInfoImpl implements PersistenceUnitInfo { 185 private final String persistenceUnitName; 186 private final String persistenceProviderClassName; 187 private final PersistenceUnitTransactionType persistenceUnitTransactionType; 188 private final DataSource jtaDataSource; 189 private final DataSource nonJtaDataSource; 190 private final List <String > mappingFileNames; 191 private final List <URL > jarFileUrls; 192 private final URL persistenceUnitRootUrl; 193 private final List <String > managedClassNames; 194 private final boolean excludeUnlistedClassesValue; 195 private final Properties properties; 196 private final ClassLoader classLoader; 197 198 199 public PersistenceUnitInfoImpl(String persistenceUnitName, String persistenceProviderClassName, PersistenceUnitTransactionType persistenceUnitTransactionType, DataSource jtaDataSource, DataSource nonJtaDataSource, List <String > mappingFileNames, List <URL > jarFileUrls, URL persistenceUnitRootUrl, List <String > managedClassNames, boolean excludeUnlistedClassesValue, Properties properties, ClassLoader classLoader) { 200 this.persistenceUnitName = persistenceUnitName; 201 this.persistenceProviderClassName = persistenceProviderClassName; 202 this.persistenceUnitTransactionType = persistenceUnitTransactionType; 203 this.jtaDataSource = jtaDataSource; 204 this.nonJtaDataSource = nonJtaDataSource; 205 this.mappingFileNames = mappingFileNames; 206 this.jarFileUrls = jarFileUrls; 207 this.persistenceUnitRootUrl = persistenceUnitRootUrl; 208 this.managedClassNames = managedClassNames; 209 this.excludeUnlistedClassesValue = excludeUnlistedClassesValue; 210 this.properties = properties; 211 this.classLoader = classLoader; 212 } 213 214 public String getPersistenceUnitName() { 215 return persistenceUnitName; 216 } 217 218 public String getPersistenceProviderClassName() { 219 return persistenceProviderClassName; 220 } 221 222 public PersistenceUnitTransactionType getTransactionType() { 223 return persistenceUnitTransactionType; 224 } 225 226 public DataSource getJtaDataSource() { 227 return jtaDataSource; 228 } 229 230 public DataSource getNonJtaDataSource() { 231 return nonJtaDataSource; 232 } 233 234 public List <String > getMappingFileNames() { 235 return mappingFileNames; 236 } 237 238 public List <URL > getJarFileUrls() { 239 return jarFileUrls; 240 } 241 242 public URL getPersistenceUnitRootUrl() { 243 return persistenceUnitRootUrl; 244 } 245 246 public List <String > getManagedClassNames() { 247 return managedClassNames; 248 } 249 250 public boolean excludeUnlistedClasses() { 251 return excludeUnlistedClassesValue; 252 } 253 254 public Properties getProperties() { 255 return properties; 256 } 257 258 public ClassLoader getClassLoader() { 259 return classLoader; 260 } 261 262 public void addTransformer(ClassTransformer classTransformer) { 263 TransformerAgent.addTransformer(new TransformerWrapper(classTransformer, classLoader)); 264 } 265 266 public ClassLoader getNewTempClassLoader() { 267 return JarFileClassLoader.copy(classLoader); 268 } 269 270 } 271 272 public static final GBeanInfo GBEAN_INFO; 273 274 static { 275 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceUnitGBean.class); 276 infoBuilder.setPriority(GBeanInfo.PRIORITY_CLASSLOADER); 277 278 infoBuilder.addAttribute("persistenceUnitName", String .class, true, true); 279 infoBuilder.addAttribute("persistenceProviderClassName", String .class, true, true); 280 infoBuilder.addAttribute("persistenceUnitTransactionType", String .class, true, true); 281 infoBuilder.addAttribute("mappingFileNames", List .class, true, true); 282 infoBuilder.addAttribute("jarFileUrls", List .class, true, true); 283 infoBuilder.addAttribute("persistenceUnitRootUrl", URL .class, true, true); 284 infoBuilder.addAttribute("managedClassNames", List .class, true, true); 285 infoBuilder.addAttribute("excludeUnlistedClasses", boolean.class, true, true); 286 infoBuilder.addAttribute("properties", Properties .class, true, true); 287 infoBuilder.addReference("TransactionManager", TransactionManagerImpl.class, NameFactory.TRANSACTION_MANAGER); 288 infoBuilder.addReference("JtaDataSourceWrapper", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY); 289 infoBuilder.addReference("NonJtaDataSourceWrapper", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY); 290 infoBuilder.addOperation("getEntityManagerFactory"); 291 infoBuilder.addOperation("getEntityManager", new Class [] {boolean.class, Map .class}); 292 293 infoBuilder.setConstructor(new String [] { 294 "persistenceUnitName", 295 "persistenceProviderClassName", 296 "persistenceUnitTransactionType", 297 "JtaDataSourceWrapper", 298 "NonJtaDataSourceWrapper", 299 "mappingFileNames", 300 "jarFileUrls", 301 "persistenceUnitRootUrl", 302 "managedClassNames", 303 "excludeUnlistedClasses", 304 "properties", 305 "TransactionManager", 306 "classLoader" 307 }); 308 309 GBEAN_INFO = infoBuilder.getBeanInfo(); 310 311 } 312 313 public static GBeanInfo getGBeanInfo() { 314 return GBEAN_INFO; 315 } 316 317 } 318 | Popular Tags |