1 23 24 25 package com.sun.enterprise.server; 26 27 import com.sun.enterprise.deployment.*; 28 import com.sun.enterprise.loader.InstrumentableClassLoader; 29 import com.sun.logging.LogDomains; 30 31 import javax.persistence.EntityManagerFactory; 32 import javax.persistence.spi.PersistenceProvider; 33 import javax.persistence.spi.PersistenceUnitInfo; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import java.util.Collection ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.util.Collections ; 40 41 46 public class PersistenceUnitLoaderImpl implements PersistenceUnitLoader { 47 48 51 private static Logger logger = LogDomains.getLogger( 52 LogDomains.LOADER_LOGGER); 53 54 private String applicationLocation; 55 56 private InstrumentableClassLoader classLoader; 57 58 private Application application; 59 60 private static Map <String , String > integrationProperties; 61 62 65 public void load(ApplicationInfo appInfo) { 66 application = appInfo.getApplication(); 67 applicationLocation = appInfo.getApplicationLocation(); 68 classLoader = appInfo.getClassLoader(); 69 if(logger.isLoggable(Level.FINE)) { 70 logger.fine("Loading persistence units for application: " + 71 applicationLocation); 72 } 73 for (PersistenceUnitsDescriptor pus : 75 application.getPersistenceUnitsDescriptors()) { 76 for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) { 77 load(pu); 78 } 79 } 80 81 for (Object o : application.getEjbBundleDescriptors()) { 83 EjbBundleDescriptor bundle = EjbBundleDescriptor.class.cast(o); 84 if(logger.isLoggable(Level.FINE)) { 85 logger.fine("Loading persistence units for ejb module called " + 86 bundle.getModuleDescriptor().getArchiveUri() + 87 " in application " + applicationLocation); 88 } 89 for (PersistenceUnitsDescriptor pus : bundle.getPersistenceUnitsDescriptors()) { 90 for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) { 91 load(pu); 92 } 93 } 94 } 95 96 for (Object o : application.getWebBundleDescriptors()) { 98 WebBundleDescriptor bundle = WebBundleDescriptor.class.cast(o); 99 if(logger.isLoggable(Level.FINE)) { 100 logger.fine("Loading persistence units for web module called " + 101 bundle.getModuleDescriptor().getArchiveUri() + 102 " in application " + applicationLocation); 103 } 104 for (PersistenceUnitsDescriptor pus : bundle.getPersistenceUnitsDescriptors()) { 105 for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) { 106 load(pu); 107 } 108 } 109 } 110 if(logger.isLoggable(Level.FINE)) { 111 logger.fine("Finished loading persistence units for application: " + 112 applicationLocation); 113 } 114 } 115 116 119 public void unload(ApplicationInfo appInfo) { 120 application = appInfo.getApplication(); 121 applicationLocation = appInfo.getApplicationLocation(); 122 classLoader = appInfo.getClassLoader(); 123 if(logger.isLoggable(Level.FINE)) { 124 logger.fine("Unloading persistence units for application: " + 125 applicationLocation); 126 } 127 closeEMFs(application.getEntityManagerFactories()); 129 130 for (Object o : application.getEjbBundleDescriptors()) { 132 EjbBundleDescriptor bundle = EjbBundleDescriptor.class.cast(o); 133 if(logger.isLoggable(Level.FINE)) { 134 logger.fine("Unloading persistence units for ejb module called " + 135 bundle.getModuleDescriptor().getArchiveUri() + 136 " in application " + applicationLocation); 137 } 138 closeEMFs(bundle.getEntityManagerFactories()); 139 } 140 141 for (Object o : application.getWebBundleDescriptors()) { 143 WebBundleDescriptor bundle = WebBundleDescriptor.class.cast(o); 144 if(logger.isLoggable(Level.FINE)) { 145 logger.fine("Unloading persistence units for web module called " + 146 bundle.getModuleDescriptor().getArchiveUri() + 147 " in application " + applicationLocation); 148 } 149 closeEMFs(bundle.getEntityManagerFactories()); 150 } 151 if(logger.isLoggable(Level.FINE)) { 152 logger.fine("Finished unloading persistence units for application: " + 153 applicationLocation); 154 } 155 } 156 157 163 private void load(PersistenceUnitDescriptor pud) { 164 if(logger.isLoggable(Level.FINE)) { 165 logger.fine("loading pud " + pud.getAbsolutePuRoot()); 166 } 167 PersistenceUnitInfo pInfo = new PersistenceUnitInfoImpl( 168 pud, 169 applicationLocation, 170 classLoader); 171 if(logger.isLoggable(Level.FINE)) { 172 logger.fine("PersistenceInfo for this pud is :\n" + pInfo); 173 } 174 PersistenceProvider provider; 175 try { 176 provider = 184 PersistenceProvider.class.cast( 185 ClassLoader .class.cast(classLoader) 186 .loadClass(pInfo.getPersistenceProviderClassName()) 187 .newInstance()); 188 } catch (ClassNotFoundException e) { 189 throw new RuntimeException (e); 190 } catch (InstantiationException e) { 191 throw new RuntimeException (e); 192 } catch (IllegalAccessException e) { 193 throw new RuntimeException (e); 194 } 195 EntityManagerFactory emf = provider.createContainerEntityManagerFactory( 196 pInfo, integrationProperties); 197 logger.logp(Level.FINE, "PersistenceUnitLoaderImpl", "load", 198 "emf = {0}", emf); 199 200 RootDeploymentDescriptor rootDD = pud.getParent().getParent(); 201 if (rootDD.isApplication()) { 202 Application.class.cast(rootDD).addEntityManagerFactory( 203 pInfo.getPersistenceUnitName(), pud.getPuRoot(), emf); 204 } else { 205 BundleDescriptor.class.cast(rootDD).addEntityManagerFactory( 206 pInfo.getPersistenceUnitName(), emf); 207 } 208 } 209 210 private void closeEMFs( 211 Collection <EntityManagerFactory> entityManagerFactories) { 212 for (EntityManagerFactory emf : entityManagerFactories) { 213 try { 214 emf.close(); 215 } catch (Exception e) { 216 logger.log(Level.WARNING, e.getMessage(), e); 217 } 218 } 219 } 220 221 static { 222 234 Map <String , String > props = new HashMap <String , String >(); 235 236 final String TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY = 239 "toplink.server.platform.class.name"; props.put(TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY, 241 System.getProperty(TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY, 242 "oracle.toplink.essentials.platform.server.sunas.SunAS9ServerPlatform")); 244 final String TOPLINK_DDL_GENERATION_MODE_PROPERTY = 246 "toplink.ddl-generation.output-mode"; props.put(TOPLINK_DDL_GENERATION_MODE_PROPERTY, 248 System.getProperty(TOPLINK_DDL_GENERATION_MODE_PROPERTY, 249 "none")); 251 final String HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY = 253 "hibernate.transaction.manager_lookup_class"; props.put(HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY, 255 System.getProperty(HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY, 256 "org.hibernate.transaction.SunONETransactionManagerLookup")); 258 integrationProperties = Collections.unmodifiableMap(props); 261 } 262 263 } 264 | Popular Tags |