openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1494168 - in /openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone: OwbSWClassLoader.java OwbStandaloneConfiguration.java OwbStandaloneContainer.java
Date Tue, 18 Jun 2013 15:14:16 GMT
Author: rmannibucau
Date: Tue Jun 18 15:14:16 2013
New Revision: 1494168

URL: http://svn.apache.org/r1494168
Log:
OWB-876 better handling if resource name doesn't have a / + adding a property to skip AppClassLoader
resources

Modified:
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneConfiguration.java
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java?rev=1494168&r1=1494167&r2=1494168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
Tue Jun 18 15:14:16 2013
@@ -35,17 +35,24 @@ import java.net.URLClassLoader;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.List;
 
 public class OwbSWClassLoader extends URLClassLoader implements Closeable
 {
     private final List<InputStream> openedStreams = new ArrayList<InputStream>();
     private final String prefix;
+    private final boolean useOnlyArchiveResources;
+    private final Archive<?> archive;
 
-    public OwbSWClassLoader(final ClassLoader parent, final Archive<?> archive)
+    public OwbSWClassLoader(final ClassLoader parent, final Archive<?> archive, final
boolean useOnlyArchiveResources)
     {
         super(new URL[0], parent);
 
+        this.useOnlyArchiveResources = useOnlyArchiveResources;
+        this.archive = archive;
+
         if (WebArchive.class.isInstance(archive))
         {
             prefix = "/WEB-INF/classes";
@@ -56,58 +63,8 @@ public class OwbSWClassLoader extends UR
         }
 
         try
-        {
-            addURL(new URL(null, "archive:" + archive.getName() + "/", new URLStreamHandler()
-            {
-                @Override
-                protected URLConnection openConnection(final URL u) throws IOException
-                {
-                    return new URLConnection(u)
-                    {
-                        @Override
-                        public void connect() throws IOException
-                        {
-                            // no-op
-                        }
-
-                        @Override
-                        public InputStream getInputStream() throws IOException
-                        {
-                            final ArchivePath path = convertToArchivePath(u);
-                            Node node = archive.get(prefix + path.get());
-                            if (node == null && !prefix.isEmpty())
-                            { // WEB-INF/lib/x.jar!*
-                                node = archive.get(path);
-                            }
-
-                            // SHRINKWRAP-308
-                            if (node == null)
-                            {
-                                throw new FileNotFoundException("Requested path: " + path
+ " does not exist in " + archive.toString());
-                            }
-
-                            final Asset asset = node.getAsset();
-                            if (asset == null)
-                            {
-                                return null;
-                            }
-
-                            final InputStream input = asset.openStream();
-                            synchronized (this)
-                            {
-                                openedStreams.add(input);
-                            }
-                            return input;
-
-                        }
-
-                        private ArchivePath convertToArchivePath(final URL url)
-                        {
-                            return ArchivePaths.create(url.getPath().replace(archive.getName(),
""));
-                        }
-                    };
-                }
-            }));
+        { // add it to find classes if used this way
+            addURL(new URL(null, "archive:" + archive.getName() + "/", new ArchiveStreamHandler()));
         }
         catch (final MalformedURLException e)
         {
@@ -115,6 +72,73 @@ public class OwbSWClassLoader extends UR
         }
     }
 
+    @Override
+    public URL findResource(final String name)
+    {
+        final Node node = findNode(name);
+        if (node != null)
+        {
+            try
+            {
+                return new URL(null, "archive:" + archive.getName() + "/" + name, new ArchiveStreamHandler());
+            }
+            catch (final MalformedURLException e)
+            {
+                // no-op: let reuse parent method
+            }
+        }
+        if (useOnlyArchiveResources)
+        {
+            return null;
+        }
+        return super.findResource(name);
+    }
+
+    @Override
+    public Enumeration<URL> findResources(final String name) throws IOException
+    {
+        final Node node = findNode(name);
+        if (node != null)
+        {
+            return Collections.enumeration(Collections.singleton(new URL(null, "archive:"
+ archive.getName() + "/" + name, new ArchiveStreamHandler())));
+        }
+        if (useOnlyArchiveResources)
+        {
+            return Collections.emptyEnumeration();
+        }
+
+        return super.findResources(name);
+    }
+
+    private Node findNode(final String name)
+    {
+        ArchivePath path = ArchivePaths.create(path(prefix, name));
+        Node node = archive.get(path);
+        if (node == null)
+        {
+            path = ArchivePaths.create(name);
+            node = archive.get(path);
+
+
+        }
+        return node;
+    }
+
+    private String path(final String... parts)
+    {
+        final StringBuilder builder = new StringBuilder(parts[0]);
+        for (int i = 1; i < parts.length; i++)
+        {
+            if (!parts[i - 1].endsWith("/") && !parts[i].startsWith("/"))
+            {
+                builder.append("/");
+            }
+            builder.append(parts[i]);
+        }
+        return builder.toString();
+    }
+
+
     public void close() throws IOException
     {
         synchronized (this)
@@ -133,4 +157,56 @@ public class OwbSWClassLoader extends UR
             openedStreams.clear();
         }
     }
+
+    protected class ArchiveStreamHandler extends URLStreamHandler
+    {
+        @Override
+        protected URLConnection openConnection(final URL u) throws IOException
+        {
+            return new URLConnection(u)
+            {
+                @Override
+                public void connect() throws IOException
+                {
+                    // no-op
+                }
+
+                @Override
+                public InputStream getInputStream() throws IOException
+                {
+                    final ArchivePath path = convertToArchivePath(u);
+                    Node node = archive.get(prefix + path.get());
+                    if (node == null && !prefix.isEmpty())
+                    { // WEB-INF/lib/x.jar!*
+                        node = archive.get(path);
+                    }
+
+                    // SHRINKWRAP-308
+                    if (node == null)
+                    {
+                        throw new FileNotFoundException("Requested path: " + path + " does
not exist in " + archive.toString());
+                    }
+
+                    final Asset asset = node.getAsset();
+                    if (asset == null)
+                    {
+                        return null;
+                    }
+
+                    final InputStream input = asset.openStream();
+                    synchronized (this)
+                    {
+                        openedStreams.add(input);
+                    }
+                    return input;
+
+                }
+
+                private ArchivePath convertToArchivePath(final URL url)
+                {
+                    return ArchivePaths.create(url.getPath().replace(archive.getName(), ""));
+                }
+            };
+        }
+    }
 }

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneConfiguration.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneConfiguration.java?rev=1494168&r1=1494167&r2=1494168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneConfiguration.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneConfiguration.java
Tue Jun 18 15:14:16 2013
@@ -26,9 +26,25 @@ import org.jboss.arquillian.container.sp
  */
 public class OwbStandaloneConfiguration implements ContainerConfiguration
 {
+    /**
+     * used to skip classpath resources which are not in the archive,
+     * can be useful when a resource should appear a single time in an app
+     */
+    private boolean useOnlyArchiveResources = false;
+
     @Override
     public void validate() throws ConfigurationException
     {
         // not needed right now
     }
+
+    public boolean isUseOnlyArchiveResources()
+    {
+        return useOnlyArchiveResources;
+    }
+
+    public void setUseOnlyArchiveResources(final boolean useOnlyArchiveResources)
+    {
+        this.useOnlyArchiveResources = useOnlyArchiveResources;
+    }
 }

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java?rev=1494168&r1=1494167&r2=1494168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java
Tue Jun 18 15:14:16 2013
@@ -56,6 +56,7 @@ public class OwbStandaloneContainer impl
     private WebBeansContext webBeansContext;
 
     private final ThreadLocal<ClassLoader> originalLoader = new ThreadLocal<ClassLoader>();
+    private boolean useOnlyArchiveResources;
 
     @Override
     public Class<OwbStandaloneConfiguration> getConfigurationClass()
@@ -77,6 +78,8 @@ public class OwbStandaloneContainer impl
         singletonService = new OwbArquillianSingletonService();
         WebBeansFinder.setSingletonService(singletonService);
 
+        useOnlyArchiveResources = owbStandaloneConfiguration.isUseOnlyArchiveResources();
+
     }
 
     @Override
@@ -113,7 +116,7 @@ public class OwbStandaloneContainer impl
 
         final ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
         originalLoader.set(parentLoader);
-        Thread.currentThread().setContextClassLoader(new OwbSWClassLoader(parentLoader, archive));
+        Thread.currentThread().setContextClassLoader(new OwbSWClassLoader(parentLoader, archive,
useOnlyArchiveResources));
 
         return new ProtocolMetaData();
     }



Mime
View raw message