Skip to content

Commit abfe3f2

Browse files
committed
DefaultPersistenceUnitManager can determine persistence unit root from orm.xml location
Issue: SPR-14246
1 parent a979885 commit abfe3f2

File tree

2 files changed

+68
-55
lines changed

2 files changed

+68
-55
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -553,8 +553,20 @@ else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
553553
scannedUnit.addMappingFileName(mappingFileName);
554554
}
555555
}
556-
else if (useOrmXmlForDefaultPersistenceUnit()) {
557-
scannedUnit.addMappingFileName(DEFAULT_ORM_XML_RESOURCE);
556+
else {
557+
Resource ormXml = getOrmXmlForDefaultPersistenceUnit();
558+
if (ormXml != null) {
559+
scannedUnit.addMappingFileName(DEFAULT_ORM_XML_RESOURCE);
560+
if (scannedUnit.getPersistenceUnitRootUrl() == null) {
561+
try {
562+
scannedUnit.setPersistenceUnitRootUrl(
563+
PersistenceUnitReader.determinePersistenceUnitRootUrl(ormXml));
564+
}
565+
catch (IOException ex) {
566+
logger.debug("Failed to determine persistence unit root URL from orm.xml location", ex);
567+
}
568+
}
569+
}
558570
}
559571

560572
return scannedUnit;
@@ -593,27 +605,27 @@ private URL determineDefaultPersistenceUnitRootUrl() {
593605
}
594606

595607
/**
596-
* Determine whether to register JPA's default "META-INF/orm.xml" with
597-
* Spring's default persistence unit, if any.
598-
* <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and
599-
* uses it if it is not co-located with a "META-INF/persistence.xml" file.
608+
* Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default
609+
* persistence unit, if any.
610+
* <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and uses it
611+
* if it is not co-located with a "META-INF/persistence.xml" file.
600612
*/
601-
private boolean useOrmXmlForDefaultPersistenceUnit() {
613+
private Resource getOrmXmlForDefaultPersistenceUnit() {
602614
Resource ormXml = this.resourcePatternResolver.getResource(
603615
this.defaultPersistenceUnitRootLocation + DEFAULT_ORM_XML_RESOURCE);
604616
if (ormXml.exists()) {
605617
try {
606618
Resource persistenceXml = ormXml.createRelative(PERSISTENCE_XML_FILENAME);
607619
if (!persistenceXml.exists()) {
608-
return true;
620+
return ormXml;
609621
}
610622
}
611623
catch (IOException ex) {
612624
// Cannot resolve relative persistence.xml file - let's assume it's not there.
613-
return true;
625+
return ormXml;
614626
}
615627
}
616-
return false;
628+
return null;
617629
}
618630

619631

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.java

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@
5151
* @author Juergen Hoeller
5252
* @since 2.0
5353
*/
54-
class PersistenceUnitReader {
54+
final class PersistenceUnitReader {
5555

5656
private static final String PERSISTENCE_VERSION = "version";
5757

@@ -84,7 +84,7 @@ class PersistenceUnitReader {
8484
private static final String META_INF = "META-INF";
8585

8686

87-
private final Log logger = LogFactory.getLog(getClass());
87+
private static final Log logger = LogFactory.getLog(PersistenceUnitReader.class);
8888

8989
private final ResourcePatternResolver resourcePatternResolver;
9090

@@ -185,47 +185,6 @@ protected List<SpringPersistenceUnitInfo> parseDocument(
185185
return infos;
186186
}
187187

188-
/**
189-
* Determine the persistence unit root URL based on the given resource
190-
* (which points to the {@code persistence.xml} file we're reading).
191-
* @param resource the resource to check
192-
* @return the corresponding persistence unit root URL
193-
* @throws IOException if the checking failed
194-
*/
195-
protected URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
196-
URL originalURL = resource.getURL();
197-
198-
// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
199-
if (ResourceUtils.isJarURL(originalURL)) {
200-
return ResourceUtils.extractJarFileURL(originalURL);
201-
}
202-
203-
// check META-INF folder
204-
String urlToString = originalURL.toExternalForm();
205-
if (!urlToString.contains(META_INF)) {
206-
if (logger.isInfoEnabled()) {
207-
logger.info(resource.getFilename() +
208-
" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
209-
resource);
210-
}
211-
return null;
212-
}
213-
if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
214-
if (logger.isInfoEnabled()) {
215-
logger.info(resource.getFilename() +
216-
" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
217-
resource);
218-
}
219-
return null;
220-
}
221-
222-
String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
223-
if (persistenceUnitRoot.endsWith("/")) {
224-
persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
225-
}
226-
return new URL(persistenceUnitRoot);
227-
}
228-
229188
/**
230189
* Parse the unit info DOM element.
231190
*/
@@ -365,4 +324,46 @@ protected void parseJarFiles(Element persistenceUnit, SpringPersistenceUnitInfo
365324
}
366325
}
367326

327+
328+
/**
329+
* Determine the persistence unit root URL based on the given resource
330+
* (which points to the {@code persistence.xml} file we're reading).
331+
* @param resource the resource to check
332+
* @return the corresponding persistence unit root URL
333+
* @throws IOException if the checking failed
334+
*/
335+
static URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
336+
URL originalURL = resource.getURL();
337+
338+
// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
339+
if (ResourceUtils.isJarURL(originalURL)) {
340+
return ResourceUtils.extractJarFileURL(originalURL);
341+
}
342+
343+
// Check META-INF folder
344+
String urlToString = originalURL.toExternalForm();
345+
if (!urlToString.contains(META_INF)) {
346+
if (logger.isInfoEnabled()) {
347+
logger.info(resource.getFilename() +
348+
" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
349+
resource);
350+
}
351+
return null;
352+
}
353+
if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
354+
if (logger.isInfoEnabled()) {
355+
logger.info(resource.getFilename() +
356+
" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
357+
resource);
358+
}
359+
return null;
360+
}
361+
362+
String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
363+
if (persistenceUnitRoot.endsWith("/")) {
364+
persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
365+
}
366+
return new URL(persistenceUnitRoot);
367+
}
368+
368369
}

0 commit comments

Comments
 (0)