From 787b57a486877f695c55c54f7709428df18e64d9 Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Fri, 14 Dec 2007 21:26:02 +0000 Subject: [PATCH] OPENJPA-9 Use the same criteria for resolving the configuration file for the purposes of listing all the anchors as we do for actually parsing the file itself. Fixes the problem where specifing the property "persistence.xml" was not actually resolving /META-INF/persistence.xml in order to obtain the list of anchors. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@604300 13f79535-47bb-0310-9956-ffa450edef68 --- .../PersistenceProductDerivation.java | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java index adbb46c6b..c3a535c45 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java @@ -228,7 +228,14 @@ public class PersistenceProductDerivation public List getAnchorsInResource(String resource) throws Exception { ConfigurationParser parser = new ConfigurationParser(null); try { - parser.parse(resource); + ClassLoader loader = (ClassLoader) AccessController.doPrivileged( + J2DoPrivHelper.getContextClassLoaderAction()); + List urls = getResourceURLs(resource, loader); + if (urls != null) { + for (URL url : urls) { + parser.parse(url); + } + } return getUnitNames(parser); } catch (IOException e) { // not all configuration files are XML; return null if unparsable @@ -273,6 +280,27 @@ public class PersistenceProductDerivation return null; } + private static List getResourceURLs(String rsrc, ClassLoader loader) + throws IOException { + Enumeration urls = null; + try { + urls = (Enumeration) AccessController.doPrivileged( + J2DoPrivHelper.getResourcesAction(loader, rsrc)); + if (!urls.hasMoreElements()) { + if (!rsrc.startsWith("META-INF")) + urls = (Enumeration) AccessController.doPrivileged( + J2DoPrivHelper.getResourcesAction( + loader, "META-INF/" + rsrc)); + if (!urls.hasMoreElements()) + return null; + } + } catch (PrivilegedActionException pae) { + throw (IOException) pae.getException(); + } + + return Collections.list(urls); + } + /** * Looks through the resources at rsrc for a configuration * file that matches name (or an unnamed one if @@ -290,21 +318,9 @@ public class PersistenceProductDerivation loader = (ClassLoader) AccessController.doPrivileged( J2DoPrivHelper.getContextClassLoaderAction()); - Enumeration urls = null; - try { - urls = (Enumeration) AccessController.doPrivileged( - J2DoPrivHelper.getResourcesAction(loader, rsrc)); - if (!urls.hasMoreElements()) { - if (!rsrc.startsWith("META-INF")) - urls = (Enumeration) AccessController.doPrivileged( - J2DoPrivHelper.getResourcesAction( - loader, "META-INF/" + rsrc)); - if (!urls.hasMoreElements()) - return null; - } - } catch (PrivilegedActionException pae) { - throw (IOException) pae.getException(); - } + List urls = getResourceURLs(rsrc, loader); + if (urls == null || urls.size() == 0) + return null; ConfigurationParser parser = new ConfigurationParser(m); PersistenceUnitInfoImpl pinfo = parseResources(parser, urls, name, @@ -335,11 +351,11 @@ public class PersistenceProductDerivation * no name given (preferring an unnamed OpenJPA unit to a named one). */ private PersistenceUnitInfoImpl parseResources(ConfigurationParser parser, - Enumeration urls, String name, ClassLoader loader) + List urls, String name, ClassLoader loader) throws IOException { List pinfos = new ArrayList(); - for (URL url : Collections.list(urls)) { + for (URL url : urls) { parser.parse(url); pinfos.addAll((List) parser.getResults()); }