Permalink
Browse files

OPENJPA-OpenJPA must support processing puRoot and jar-file URLs as j…

…ar-formatted InputStreams

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1446294 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent cbc0301 commit 3bdb0b95d8b728447cde2c23d53d673c49e7198b Jody Grassel committed Feb 14, 2013
View
60 openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java
@@ -18,6 +18,7 @@
*/
package org.apache.openjpa.meta;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -759,14 +760,41 @@ public File run() {
throw (IOException) pae.getException();
}
} else {
- if (log.isTraceEnabled())
- log.trace(_loc.get("scanning-url", url));
- clss = cparser.parseTypeNames(new URLMetaDataIterator(url));
- List<String> newNames = Arrays.asList(clss);
- if (log.isTraceEnabled())
- log.trace(_loc.get("scan-found-names", newNames, url));
- names.addAll(newNames);
- mapPersistentTypeNames(url, clss);
+ // Open an InputStream from the URL and sniff for a zip header. If it is, then this is
+ // a URL with a jar-formated InputStream, as per the JPA specification. Otherwise, fall back
+ // to URLMetaDataIterator.
+ BufferedInputStream is = null;
+
+ try {
+ is = new BufferedInputStream((InputStream) AccessController.
+ doPrivileged(J2DoPrivHelper.openStreamAction(url)));
+ } catch (PrivilegedActionException pae) {
+ throw (IOException) pae.getException();
+ }
+
+ // Check for zip header magic 0x50 0x4b 0x03 0x04
+ is.mark(0);
+ boolean zipHeaderMatch = is.read() == 0x50 && is.read() == 0x4b && is.read() == 0x03 &&
+ is.read() == 0x04;
+ is.reset();
+
+ if (zipHeaderMatch) {
+ // The URL provides a Jar-formatted InputStream, consume it with ZipStreamMetaDataIterator
+ if (log.isTraceEnabled())
+ log.trace(_loc.get("scanning-jar-at-url", url));
+ scan(new ZipStreamMetaDataIterator(new ZipInputStream(is), newMetaDataFilter()),
+ cparser, names, true, url);
+ } else {
+ // Fall back to URLMetaDataIterator
+ if (log.isTraceEnabled())
+ log.trace(_loc.get("scanning-url", url));
+ clss = cparser.parseTypeNames(new URLMetaDataIterator(url));
+ List<String> newNames = Arrays.asList(clss);
+ if (log.isTraceEnabled())
+ log.trace(_loc.get("scan-found-names", newNames, url));
+ names.addAll(newNames);
+ mapPersistentTypeNames(url, clss);
+ }
}
}
}
@@ -809,6 +837,16 @@ public File run() {
if (log.isTraceEnabled())
log.trace(_loc.get("pu-root-url", puUrlString));
+ URL puORMUrl = null;
+ try {
+ if (puUrlString != null) {
+ String puORMUrlStr = puUrlString + (puUrlString.endsWith("/") ? "" : "/") + rsrc;
+ puORMUrl = AccessController.doPrivileged(J2DoPrivHelper.createURL(puORMUrlStr));
+ }
+ } catch (PrivilegedActionException e) {
+ throw new IOException("Error generating puORMUrlStr.", e.getCause());
+ }
+
List<URL> urls = new ArrayList<URL>(3);
while (mitr.hasNext()) {
url = (URL) mitr.next();
@@ -817,8 +855,12 @@ public File run() {
log.trace(_loc.get("resource-url", urlString));
if (peMap != null) {
//OPENJPA-2102: decode the URL to remove such things a spaces (' ') encoded as '%20'
- if (puUrlString != null && decode(urlString).indexOf(decode(puUrlString)) != -1)
+ if (puUrlString != null && decode(urlString).indexOf(decode(puUrlString)) != -1) {
urls.add(url);
+ } else if (puORMUrl != null && puORMUrl.equals(url)) {
+ // Check URL equality to support encapsulating URL protocols
+ urls.add(url);
+ }
if (mappingFileNames != null && mappingFileNames.size() != 0) {
for (String mappingFileName : mappingFileNames) {
if (log.isTraceEnabled())
View
8 openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
@@ -1268,4 +1268,12 @@ public ValidatorFactory run() {
}
};
}
+
+ public static final PrivilegedExceptionAction<URL> createURL(final String url) throws MalformedURLException {
+ return new PrivilegedExceptionAction<URL>() {
+ public URL run() throws MalformedURLException {
+ return new URL(url);
+ }
+ };
+ }
}

0 comments on commit 3bdb0b9

Please sign in to comment.