1 21 22 23 package com.sun.enterprise.appclient; 24 25 import com.sun.enterprise.deployment.*; 26 import com.sun.enterprise.loader.InstrumentableClassLoader; 27 import com.sun.enterprise.server.PersistenceUnitInfoImpl; 28 import com.sun.logging.LogDomains; 29 30 import javax.persistence.EntityManagerFactory; 31 import javax.persistence.spi.PersistenceProvider; 32 import javax.persistence.spi.PersistenceUnitInfo; 33 import java.util.Collection ; 34 import java.util.List ; 35 import java.util.ArrayList ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 42 public class PersistenceUnitLoaderImpl { 43 44 private static Logger logger = LogDomains.getLogger(LogDomains.ACC_LOGGER); 45 46 private String applicationLocation; 47 48 private InstrumentableClassLoader classLoader; 49 50 private ApplicationClientDescriptor appClient; 51 52 public PersistenceUnitLoaderImpl( 53 String applicationLocation, InstrumentableClassLoader classLoader, 54 ApplicationClientDescriptor appClient) { 55 this.applicationLocation = applicationLocation; 56 this.classLoader = classLoader; 57 this.appClient = appClient; 58 } 59 60 63 public void load() { 64 logger.info("Loading persistence units for application: " + 65 applicationLocation); 66 for (PersistenceUnitDescriptor pu : findReferencedPUs()) { 67 load(pu); 68 } 69 } 70 71 public void unload() { 72 logger.info("Unloading persistence units for application: " + 73 applicationLocation); 74 if (appClient.getApplication() != null) { 76 closeEMFs(appClient.getApplication().getEntityManagerFactories()); 77 } 78 79 logger.info("Unloading persistence units for appclient module called " + 81 appClient.getModuleDescriptor().getArchiveUri() + 82 " in application " + applicationLocation); 83 closeEMFs(appClient.getEntityManagerFactories()); 84 logger.info("Finished unloading persistence units for application: " + 85 applicationLocation); 86 } 87 88 94 private List <PersistenceUnitDescriptor> findReferencedPUs() { 95 List <PersistenceUnitDescriptor> result = 96 new ArrayList <PersistenceUnitDescriptor>(); 97 98 for(EntityManagerFactoryReferenceDescriptor emfRef : 99 appClient.getEntityManagerFactoryReferenceDescriptors()) { 100 final String unitName = emfRef.getUnitName(); 101 PersistenceUnitDescriptor pu = appClient.findReferencedPU(unitName); 102 if(pu == null) { 103 throw new RuntimeException ("No suitable persistence unit " + 104 "called " + unitName + " found in the scope of this " + 105 "application client."); 106 } 107 if(pu.getTransactionType() == "JTA") { 108 throw new RuntimeException ("Application client is referencing " + 109 "a persistence unit called " + unitName + ", but that" + 110 "is of transaction-type JTA. Application client " + 111 "can only use RESOURCE_LOCAL persistence unit."); 112 113 } 114 if (!result.contains(pu)) { 115 result.add(pu); 116 } 117 } 118 return result; 119 } 120 121 127 private void load(PersistenceUnitDescriptor pud) { 128 logger.info("loading pud " + pud.getAbsolutePuRoot()); 129 PersistenceUnitInfo pInfo = new PersistenceUnitInfoImpl(pud, 130 applicationLocation, 131 classLoader); 132 logger.info("PersistenceInfo for this pud is :\n" + pInfo); 133 PersistenceProvider provider; 134 try { 135 provider = 136 PersistenceProvider.class.cast(ClassLoader .class.cast( 137 classLoader) 138 .loadClass(pInfo.getPersistenceProviderClassName()) 139 .newInstance()); 140 } catch (ClassNotFoundException e) { 141 throw new RuntimeException (e); 142 } catch (InstantiationException e) { 143 throw new RuntimeException (e); 144 } catch (IllegalAccessException e) { 145 throw new RuntimeException (e); 146 } 147 EntityManagerFactory emf = provider.createContainerEntityManagerFactory( 148 pInfo, null); 149 logger.logp(Level.INFO, "PersistenceUnitLoaderImpl", "load", 150 "emf = {0}", emf); 151 RootDeploymentDescriptor rootDD = pud.getParent().getParent(); 152 if (rootDD.isApplication()) { 153 Application.class.cast(rootDD).addEntityManagerFactory( 154 pInfo.getPersistenceUnitName(), pud.getPuRoot(), emf); 155 } else { 156 BundleDescriptor.class.cast(rootDD).addEntityManagerFactory( 157 pInfo.getPersistenceUnitName(), emf); 158 } 159 } 160 161 private void closeEMFs( 162 Collection <EntityManagerFactory> entityManagerFactories) { 163 for (EntityManagerFactory emf : entityManagerFactories) { 164 try { 165 emf.close(); 166 } catch (Exception e) { 167 logger.log(Level.WARNING, e.getMessage(), e); 168 } 169 } 170 } 171 } 172 | Popular Tags |