1 16 17 package org.springframework.orm.jpa.persistenceunit; 18 19 import java.io.IOException ; 20 import java.net.URL ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.persistence.PersistenceException; 27 import javax.persistence.spi.PersistenceUnitInfo; 28 import javax.sql.DataSource ; 29 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.context.ResourceLoaderAware; 32 import org.springframework.core.io.Resource; 33 import org.springframework.core.io.ResourceLoader; 34 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 35 import org.springframework.core.io.support.ResourcePatternResolver; 36 import org.springframework.core.io.support.ResourcePatternUtils; 37 import org.springframework.instrument.classloading.LoadTimeWeaver; 38 import org.springframework.jdbc.datasource.lookup.DataSourceLookup; 39 import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; 40 import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup; 41 import org.springframework.util.ObjectUtils; 42 43 62 public class DefaultPersistenceUnitManager implements PersistenceUnitManager, ResourceLoaderAware, InitializingBean { 63 64 68 public final static String DEFAULT_PERSISTENCE_XML_LOCATION = "classpath*:META-INF/persistence.xml"; 69 70 74 public final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION = "classpath:"; 75 76 77 78 private String [] persistenceXmlLocations = new String [] {DEFAULT_PERSISTENCE_XML_LOCATION}; 79 80 private String defaultPersistenceUnitRootLocation = ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION; 81 82 private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); 83 84 private DataSource defaultDataSource; 85 86 private LoadTimeWeaver loadTimeWeaver; 87 88 private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors; 89 90 private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); 91 92 private final Set <String > persistenceUnitInfoNames = new HashSet <String >(); 93 94 private final Map <String , PersistenceUnitInfo> persistenceUnitInfos = new HashMap <String , PersistenceUnitInfo>(); 95 96 97 104 public void setPersistenceXmlLocations(String [] persistenceXmlLocations) { 105 this.persistenceXmlLocations = persistenceXmlLocations; 106 } 107 108 115 public void setDefaultPersistenceUnitRootLocation(String defaultPersistenceUnitRootLocation) { 116 this.defaultPersistenceUnitRootLocation = defaultPersistenceUnitRootLocation; 117 } 118 119 129 public void setDataSources(Map <String , DataSource > dataSources) { 130 this.dataSourceLookup = new MapDataSourceLookup(dataSources); 131 } 132 133 149 public void setDataSourceLookup(DataSourceLookup dataSourceLookup) { 150 this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup()); 151 } 152 153 158 public DataSourceLookup getDataSourceLookup() { 159 return dataSourceLookup; 160 } 161 162 171 public void setDefaultDataSource(DataSource defaultDataSource) { 172 this.defaultDataSource = defaultDataSource; 173 } 174 175 180 public DataSource getDefaultDataSource() { 181 return defaultDataSource; 182 } 183 184 199 public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) { 200 this.loadTimeWeaver = loadTimeWeaver; 201 } 202 203 207 public LoadTimeWeaver getLoadTimeWeaver() { 208 return loadTimeWeaver; 209 } 210 211 218 public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor[] postProcessors) { 219 this.persistenceUnitPostProcessors = postProcessors; 220 } 221 222 226 public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors() { 227 return persistenceUnitPostProcessors; 228 } 229 230 public void setResourceLoader(ResourceLoader resourceLoader) { 231 this.resourcePatternResolver = (resourceLoader != null ? 232 ResourcePatternUtils.getResourcePatternResolver(resourceLoader) : 233 new PathMatchingResourcePatternResolver()); 234 } 235 236 237 public void afterPropertiesSet() { 238 preparePersistenceUnitInfos(); 239 } 240 241 250 public void preparePersistenceUnitInfos() { 251 this.persistenceUnitInfoNames.clear(); 252 this.persistenceUnitInfos.clear(); 253 SpringPersistenceUnitInfo[] puis = readPersistenceUnitInfos(); 254 for (int i = 0; i < puis.length; i++) { 255 SpringPersistenceUnitInfo pui = puis[i]; 256 if (pui.getPersistenceUnitRootUrl() == null) { 257 pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl()); 258 } 259 if (pui.getNonJtaDataSource() == null) { 260 pui.setNonJtaDataSource(this.defaultDataSource); 261 } 262 pui.setLoadTimeWeaver(this.loadTimeWeaver); 263 postProcessPersistenceUnitInfo(pui); 264 String name = pui.getPersistenceUnitName(); 265 this.persistenceUnitInfoNames.add(name); 266 this.persistenceUnitInfos.put(name, pui); 267 } 268 } 269 270 274 private SpringPersistenceUnitInfo[] readPersistenceUnitInfos() { 275 PersistenceUnitReader reader = new PersistenceUnitReader(this.resourcePatternResolver, this.dataSourceLookup); 276 return reader.readPersistenceUnitInfos(this.persistenceXmlLocations); 277 } 278 279 285 private URL determineDefaultPersistenceUnitRootUrl() { 286 if (this.defaultPersistenceUnitRootLocation == null) { 287 return null; 288 } 289 try { 290 Resource res = this.resourcePatternResolver.getResource(this.defaultPersistenceUnitRootLocation); 291 return res.getURL(); 292 } 293 catch (IOException ex) { 294 throw new PersistenceException("Unable to resolve persistence unit root URL", ex); 295 } 296 } 297 298 307 protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) { 308 PersistenceUnitPostProcessor[] postProcessors = getPersistenceUnitPostProcessors(); 309 if (postProcessors != null) { 310 for (int i = 0; i < postProcessors.length; i++) { 311 postProcessors[i].postProcessPersistenceUnitInfo(pui); 312 } 313 } 314 } 315 316 317 public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() { 318 if (this.persistenceUnitInfoNames.isEmpty()) { 319 throw new IllegalStateException ("No persistence units parsed from " + 320 ObjectUtils.nullSafeToString(this.persistenceXmlLocations)); 321 } 322 if (this.persistenceUnitInfos.isEmpty()) { 323 throw new IllegalStateException ("All persistence units from " + 324 ObjectUtils.nullSafeToString(this.persistenceXmlLocations) + " already obtained"); 325 } 326 if (this.persistenceUnitInfos.size() > 1) { 327 throw new IllegalStateException ("No single default persistence unit defined in " + 328 ObjectUtils.nullSafeToString(this.persistenceXmlLocations)); 329 } 330 return this.persistenceUnitInfos.values().iterator().next(); 331 } 332 333 public PersistenceUnitInfo obtainPersistenceUnitInfo(String persistenceUnitName) { 334 PersistenceUnitInfo pui = this.persistenceUnitInfos.remove(persistenceUnitName); 335 if (pui == null) { 336 if (!this.persistenceUnitInfoNames.contains(persistenceUnitName)) { 337 throw new IllegalArgumentException ( 338 "No persistence unit with name '" + persistenceUnitName + "' found"); 339 } 340 else { 341 throw new IllegalStateException ( 342 "Persistence unit with name '" + persistenceUnitName + "' already obtained"); 343 } 344 } 345 return pui; 346 } 347 348 } 349 | Popular Tags |