You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by ht...@apache.org on 2012/11/29 04:30:56 UTC
svn commit: r1415035 - in /openjpa/branches/2.0.x:
openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/
openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/
openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Author: hthomann
Date: Thu Nov 29 03:30:55 2012
New Revision: 1415035
URL: http://svn.apache.org/viewvc?rev=1415035&view=rev
Log:
OPENJPA-2244: Applied Helen's patch to 2.0.x.
Modified:
openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/AbstractTask.java
openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java
openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
Modified: openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/AbstractTask.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/AbstractTask.java?rev=1415035&r1=1415034&r2=1415035&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/AbstractTask.java (original)
+++ openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/AbstractTask.java Thu Nov 29 03:30:55 2012
@@ -22,13 +22,18 @@ import java.io.File;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import java.util.MissingResourceException;
+import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.lib.conf.Configuration;
import org.apache.openjpa.lib.conf.ConfigurationImpl;
import org.apache.openjpa.lib.conf.ConfigurationProvider;
+import org.apache.openjpa.lib.conf.Configurations;
import org.apache.openjpa.lib.conf.ProductDerivations;
import org.apache.openjpa.lib.util.J2DoPrivHelper;
import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.lib.util.MultiClassLoader;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
@@ -92,8 +97,10 @@ public abstract class AbstractTask exten
* The task configuration.
*/
public Configuration getConfiguration() {
- if (_conf == null)
+ if (_conf == null) {
_conf = newConfiguration();
+ _conf.setDeferResourceLoading(true);
+ }
return _conf;
}
@@ -158,13 +165,18 @@ public abstract class AbstractTask exten
// if the user didn't supply a conf file, load the default
if (_conf == null)
_conf = newConfiguration();
- if (_conf.getPropertiesResource() == null) {
- ConfigurationProvider cp = ProductDerivations.loadDefaults
- (AccessController.doPrivileged(
- J2DoPrivHelper.getClassLoaderAction(_conf.getClass())));
- if (cp != null)
- cp.setInto(_conf);
+ ConfigurationProvider cp = null;
+ String propertiesResource = _conf.getPropertiesResource();
+ if ( propertiesResource == null) {
+ cp = ProductDerivations.loadDefaults(getConfigPropertiesResourceLoader());
+ } else if (_conf.isDeferResourceLoading() && !StringUtils.isEmpty(propertiesResource)) {
+ Map<String, String> result = Configurations.parseConfigResource(propertiesResource);
+ String path = result.get(Configurations.CONFIG_RESOURCE_PATH);
+ String anchor = result.get(Configurations.CONFIG_RESOURCE_ANCHOR);
+ cp = ProductDerivations.load(path, anchor, getConfigPropertiesResourceLoader());
}
+ if (cp != null)
+ cp.setInto(_conf);
String[] files = getFiles();
try {
@@ -179,6 +191,15 @@ public abstract class AbstractTask exten
}
}
+ private MultiClassLoader getConfigPropertiesResourceLoader() {
+ MultiClassLoader loader = AccessController
+ .doPrivileged(J2DoPrivHelper.newMultiClassLoaderAction());
+ loader.addClassLoader(getClassLoader());
+ loader.addClassLoader(AccessController.doPrivileged(
+ J2DoPrivHelper.getClassLoaderAction(_conf.getClass())));
+ return loader;
+ }
+
private String[] getFiles() {
List<String> files = new ArrayList<String>();
for(FileSet fs : fileSets) {
Modified: openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java?rev=1415035&r1=1415034&r2=1415035&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java (original)
+++ openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java Thu Nov 29 03:30:55 2012
@@ -126,6 +126,9 @@ public class ConfigurationImpl
// cache descriptors
private PropertyDescriptor[] _pds = null;
private MethodDescriptor[] _mds = null;
+ //Ant task needs to defer the resource loading
+ //until the classpath setting is loaded properly
+ private boolean _deferResourceLoading = false;
/**
* Default constructor. Attempts to load default properties through
@@ -270,6 +273,14 @@ public class ConfigurationImpl
_readOnlyState = newState;
}
}
+
+ public boolean isDeferResourceLoading() {
+ return _deferResourceLoading;
+ }
+
+ public void setDeferResourceLoading(boolean deferResourceLoading) {
+ this._deferResourceLoading = deferResourceLoading;
+ }
public void instantiateAll() {
StringWriter errs = null;
@@ -840,8 +851,10 @@ public class ConfigurationImpl
* <code>properties</code> value with the name of a resource.
*/
public void setProperties(String resourceName) throws IOException {
- ProductDerivations.load(resourceName, null,
- getClass().getClassLoader()).setInto(this);
+ if (!_deferResourceLoading) {
+ ProductDerivations.load(resourceName, null,
+ getClass().getClassLoader()).setInto(this);
+ }
_auto = resourceName;
}
@@ -853,6 +866,7 @@ public class ConfigurationImpl
public void setPropertiesFile(File file) throws IOException {
ProductDerivations.load(file, null, getClass().getClassLoader()).
setInto(this);
+ setDeferResourceLoading(false);
_auto = file.toString();
}
Modified: openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java?rev=1415035&r1=1415034&r2=1415035&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java (original)
+++ openjpa/branches/2.0.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java Thu Nov 29 03:30:55 2012
@@ -23,6 +23,7 @@ import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -40,7 +41,6 @@ import org.apache.commons.lang.exception
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.lib.util.J2DoPrivHelper;
import org.apache.openjpa.lib.util.Localizer;
-import org.apache.openjpa.lib.util.MultiClassLoader;
import org.apache.openjpa.lib.util.Options;
import org.apache.openjpa.lib.util.ParseException;
import org.apache.openjpa.lib.util.StringDistance;
@@ -64,6 +64,9 @@ public class Configurations {
ConcurrentReferenceHashMap.HARD);
private static final Object NULL_LOADER = "null-loader";
+
+ public static final String CONFIG_RESOURCE_PATH = "configResourcePath";
+ public static final String CONFIG_RESOURCE_ANCHOR = "configResourceAnchor";
/**
* Return the class name from the given plugin string, or null if none.
@@ -320,19 +323,9 @@ public class Configurations {
String props = opts.removeProperty("properties", "p", null);
ConfigurationProvider provider;
if (!StringUtils.isEmpty(props)) {
- String path = props;
- String anchor = null;
- int idx = path.lastIndexOf('#');
- if (idx != -1) {
- if (idx < path.length() - 1)
- anchor = path.substring(idx + 1);
- path = path.substring(0, idx);
- if (path.length() == 0)
- throw new MissingResourceException(_loc.get("anchor-only",
- props).getMessage(), Configurations.class.getName(),
- props);
- }
-
+ Map<String, String> result = parseConfigResource(props);
+ String path = result.get(CONFIG_RESOURCE_PATH);
+ String anchor = result.get(CONFIG_RESOURCE_ANCHOR);
File file = new File(path);
if ((AccessController.doPrivileged(J2DoPrivHelper
.isFileAction(file))).booleanValue())
@@ -358,6 +351,25 @@ public class Configurations {
}
opts.setInto(conf);
}
+
+ public static Map<String, String> parseConfigResource(String props) {
+ String path = props;
+ String anchor = null;
+ int idx = path.lastIndexOf('#');
+ if (idx != -1) {
+ if (idx < path.length() - 1)
+ anchor = path.substring(idx + 1);
+ path = path.substring(0, idx);
+ if (path.length() == 0)
+ throw new MissingResourceException(_loc.get("anchor-only",
+ props).getMessage(), Configurations.class.getName(),
+ props);
+ }
+ Map <String, String> result = new HashMap<String, String>();
+ result.put(CONFIG_RESOURCE_PATH, path);
+ result.put(CONFIG_RESOURCE_ANCHOR, anchor);
+ return result;
+ }
/**
* Helper method to throw an informative description on instantiation error.
Modified: openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=1415035&r1=1415034&r2=1415035&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java (original)
+++ openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java Thu Nov 29 03:30:55 2012
@@ -61,6 +61,7 @@ import org.apache.openjpa.lib.meta.XMLMe
import org.apache.openjpa.lib.meta.XMLVersionParser;
import org.apache.openjpa.lib.util.J2DoPrivHelper;
import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.lib.util.MultiClassLoader;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@@ -510,8 +511,11 @@ public class PersistenceProductDerivatio
private Boolean load(ConfigurationProviderImpl cp, String rsrc,
String name, Map m, ClassLoader loader, boolean explicit)
throws IOException {
- if (loader == null)
- loader = AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
+ ClassLoader contextLoader = null;
+ if (loader == null) {
+ contextLoader = AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
+ loader = contextLoader;
+ }
List<URL> urls = getResourceURLs(rsrc, loader);
if (urls == null || urls.size() == 0)
@@ -535,7 +539,19 @@ public class PersistenceProductDerivatio
}
// Process jar-file references after confirming OpenJPA is the desired JPA provider.
+ if ( loader != contextLoader && loader instanceof MultiClassLoader) {
+ // combine the MultiClassLoader and set to the context
+ // so that it could be used in the jar validation
+ MultiClassLoader mutliClassLoader = (MultiClassLoader) loader;
+ contextLoader = (contextLoader != null) ? contextLoader
+ : AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
+ mutliClassLoader.addClassLoader(contextLoader);
+ AccessController.doPrivileged(J2DoPrivHelper.setContextClassLoaderAction(mutliClassLoader));
+ }
pinfo.processJarFileNames();
+ if (contextLoader != null)
+ //restore the context loader
+ AccessController.doPrivileged(J2DoPrivHelper.setContextClassLoaderAction(contextLoader));
cp.addProperties(pinfo.toOpenJPAProperties());
cp.setSource(pinfo.getPersistenceXmlFileUrl().toString());
Modified: openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java?rev=1415035&r1=1415034&r2=1415035&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java (original)
+++ openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java Thu Nov 29 03:30:55 2012
@@ -19,6 +19,7 @@
package org.apache.openjpa.persistence;
import java.io.File;
+import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@@ -41,7 +42,6 @@ import javax.persistence.spi.Persistence
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
-import org.apache.openjpa.datacache.DataCacheMode;
import org.apache.openjpa.lib.conf.Configuration;
import org.apache.openjpa.lib.conf.Configurations;
import org.apache.openjpa.lib.conf.ProductDerivations;
@@ -244,8 +244,10 @@ public class PersistenceUnitInfoImpl
}
public void validateJarFileName(String name) {
+ ClassLoader contextClassLoader = AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
MultiClassLoader loader = AccessController
.doPrivileged(J2DoPrivHelper.newMultiClassLoaderAction());
+ loader.addClassLoader(contextClassLoader);
loader.addClassLoader(getClass().getClassLoader());
loader.addClassLoader(MultiClassLoader.THREAD_LOADER);
URL url = AccessController.doPrivileged(
@@ -256,9 +258,27 @@ public class PersistenceUnitInfoImpl
}
// jar file is not a resource; check classpath
- String[] cp = (AccessController.doPrivileged(
- J2DoPrivHelper.getPropertyAction("java.class.path")))
- .split(J2DoPrivHelper.getPathSeparator());
+ String classPath = null;
+
+ //first check if the classpath is set from ant class loader
+ if (contextClassLoader instanceof MultiClassLoader) {
+ for (ClassLoader classLoader : ((MultiClassLoader) contextClassLoader).getClassLoaders()){
+ try {
+ Method getClassPathMethod = classLoader.getClass().getMethod("getClasspath", new Class[]{});
+ classPath = (String) getClassPathMethod.invoke(classLoader, new Object[]{});
+ if (classPath != null)
+ break;
+ } catch (Exception e) {
+ //do nothing
+ }
+ }
+ }
+
+ if (classPath == null) {
+ classPath = AccessController.doPrivileged(
+ J2DoPrivHelper.getPropertyAction("java.class.path"));
+ }
+ String[] cp = classPath.split(J2DoPrivHelper.getPathSeparator());
for (int i = 0; i < cp.length; i++) {
if (cp[i].equals(name)
|| cp[i].endsWith(File.separatorChar + name)) {