1 16 17 package org.springframework.orm.jpa; 18 19 import javax.persistence.EntityManagerFactory; 20 import javax.persistence.PersistenceException; 21 import javax.persistence.spi.PersistenceProvider; 22 import javax.persistence.spi.PersistenceUnitInfo; 23 import javax.sql.DataSource ; 24 25 import org.springframework.beans.BeanUtils; 26 import org.springframework.beans.factory.BeanClassLoaderAware; 27 import org.springframework.context.ResourceLoaderAware; 28 import org.springframework.core.io.ResourceLoader; 29 import org.springframework.instrument.classloading.LoadTimeWeaver; 30 import org.springframework.jdbc.datasource.lookup.SingleDataSourceLookup; 31 import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; 32 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; 33 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor; 34 import org.springframework.util.ClassUtils; 35 36 80 public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean 81 implements ResourceLoaderAware, BeanClassLoaderAware { 82 83 private PersistenceUnitManager persistenceUnitManager; 84 85 private final DefaultPersistenceUnitManager internalPersistenceUnitManager = 86 new DefaultPersistenceUnitManager(); 87 88 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 89 90 private PersistenceUnitInfo persistenceUnitInfo; 91 92 93 109 public void setPersistenceUnitManager(PersistenceUnitManager persistenceUnitManager) { 110 this.persistenceUnitManager = persistenceUnitManager; 111 } 112 113 123 public void setPersistenceXmlLocation(String persistenceXmlLocation) { 124 this.internalPersistenceUnitManager.setPersistenceXmlLocations(new String [] {persistenceXmlLocation}); 125 } 126 127 139 public void setDataSource(DataSource dataSource) { 140 this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(dataSource)); 141 this.internalPersistenceUnitManager.setDefaultDataSource(dataSource); 142 } 143 144 161 public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) { 162 this.internalPersistenceUnitManager.setLoadTimeWeaver(loadTimeWeaver); 163 } 164 173 public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor[] postProcessors) { 174 this.internalPersistenceUnitManager.setPersistenceUnitPostProcessors(postProcessors); 175 } 176 177 178 public void setResourceLoader(ResourceLoader resourceLoader) { 179 this.internalPersistenceUnitManager.setResourceLoader(resourceLoader); 180 } 181 182 public void setBeanClassLoader(ClassLoader beanClassLoader) { 183 this.beanClassLoader = beanClassLoader; 184 } 185 186 187 @Override 188 protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException { 189 PersistenceUnitManager managerToUse = this.persistenceUnitManager; 190 if (this.persistenceUnitManager == null) { 191 this.internalPersistenceUnitManager.afterPropertiesSet(); 192 managerToUse = this.internalPersistenceUnitManager; 193 } 194 195 this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse); 196 197 PersistenceProvider provider = getPersistenceProvider(); 198 if (provider == null) { 199 String providerClassName = this.persistenceUnitInfo.getPersistenceProviderClassName(); 200 if (providerClassName == null) { 201 throw new IllegalArgumentException ( 202 "No PersistenceProvider specified in EntityManagerFactory configuration, " + 203 "and chosen PersistenceUnitInfo does not specify a provider class name either"); 204 } 205 Class providerClass = ClassUtils.resolveClassName(providerClassName, this.beanClassLoader); 206 provider = (PersistenceProvider) BeanUtils.instantiateClass(providerClass); 207 } 208 if (provider == null) { 209 throw new IllegalStateException ("Unable to determine persistence provider. " + 210 "Please check configuration of " + getClass().getName() + "; " + 211 "ideally specify the appropriate JpaVendorAdapter class for this provider."); 212 } 213 214 this.nativeEntityManagerFactory = 215 provider.createContainerEntityManagerFactory(this.persistenceUnitInfo, getJpaPropertyMap()); 216 postProcessEntityManagerFactory(this.nativeEntityManagerFactory, this.persistenceUnitInfo); 217 218 return this.nativeEntityManagerFactory; 219 } 220 221 222 232 protected PersistenceUnitInfo determinePersistenceUnitInfo(PersistenceUnitManager persistenceUnitManager) { 233 if (getPersistenceUnitName() != null) { 234 return persistenceUnitManager.obtainPersistenceUnitInfo(getPersistenceUnitName()); 235 } 236 else { 237 return persistenceUnitManager.obtainDefaultPersistenceUnitInfo(); 238 } 239 } 240 241 249 protected void postProcessEntityManagerFactory(EntityManagerFactory emf, PersistenceUnitInfo pui) { 250 } 251 252 253 public PersistenceUnitInfo getPersistenceUnitInfo() { 254 return this.persistenceUnitInfo; 255 } 256 257 public String getPersistenceUnitName() { 258 if (this.persistenceUnitInfo != null) { 259 return this.persistenceUnitInfo.getPersistenceUnitName(); 260 } 261 return super.getPersistenceUnitName(); 262 } 263 264 public DataSource getDataSource() { 265 if (this.persistenceUnitInfo != null) { 266 return this.persistenceUnitInfo.getNonJtaDataSource(); 267 } 268 return this.internalPersistenceUnitManager.getDefaultDataSource(); 269 } 270 271 } 272 | Popular Tags |