openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgras...@apache.org
Subject svn commit: r1446295 - in /openjpa/branches/2.2.x: openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
Date Thu, 14 Feb 2013 18:35:20 GMT
Author: jgrassel
Date: Thu Feb 14 18:35:20 2013
New Revision: 1446295

URL: http://svn.apache.org/r1446295
Log:
OPENJPA-OpenJPA must support processing puRoot and jar-file URLs as jar-formatted InputStreams

Modified:
    openjpa/branches/2.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java
    openjpa/branches/2.2.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java

Modified: openjpa/branches/2.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java?rev=1446295&r1=1446294&r2=1446295&view=diff
==============================================================================
--- openjpa/branches/2.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java
(original)
+++ openjpa/branches/2.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractCFMetaDataFactory.java
Thu Feb 14 18:35:20 2013
@@ -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;
@@ -716,28 +717,43 @@ public abstract class AbstractCFMetaData
                         scan(new JarFileURLMetaDataIterator(url,
                             newMetaDataFilter()), cparser, names, true, url);
                     }                   
-                } else if (url.getPath().endsWith(".jar")) {
-                    if (log.isTraceEnabled())
-                        log.trace(_loc.get("scanning-jar-at-url", url));
+                } else {
+                    // 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 {
-                        InputStream is = (InputStream)
-                            AccessController.doPrivileged(
-                                J2DoPrivHelper.openStreamAction(url));
-                        scan(new ZipStreamMetaDataIterator(
-                            new ZipInputStream(is),
-                            newMetaDataFilter()), cparser, names, true, url);
+                        is = new BufferedInputStream((InputStream) AccessController.
+                                doPrivileged(J2DoPrivHelper.openStreamAction(url)));
                     } catch (PrivilegedActionException pae) {
                         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);
+                    
+                    // 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));
+                        
+                        is.close();
+                        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);
+                    }
                 }
             }
         }
@@ -779,7 +795,18 @@ public abstract class AbstractCFMetaData
                     String puUrlString = puUrl == null ? null : puUrl.toString();
                     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();
@@ -787,9 +814,14 @@ public abstract class AbstractCFMetaData
                         if (log.isTraceEnabled())
                             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) 
+                            //OPENJPA-2102: decode the URL to remove such things a spaces
(' ') encoded as '%20'
+                            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())

Modified: openjpa/branches/2.2.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java?rev=1446295&r1=1446294&r2=1446295&view=diff
==============================================================================
--- openjpa/branches/2.2.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
(original)
+++ openjpa/branches/2.2.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
Thu Feb 14 18:35:20 2013
@@ -1268,4 +1268,12 @@ public abstract class J2DoPrivHelper {
             }
         };
     }
+
+    public static final PrivilegedExceptionAction<URL> createURL(final String url)
throws MalformedURLException {
+        return new PrivilegedExceptionAction<URL>() {
+            public URL run() throws MalformedURLException {
+                return new URL(url);
+            }
+        };
+    }
 }



Mime
View raw message