You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2013/06/18 13:21:20 UTC

svn commit: r1494101 - in /openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src: main/java/org/apache/webbeans/arquillian/standalone/ test/java/org/apache/webbeans/arquillian/test/

Author: rmannibucau
Date: Tue Jun 18 11:21:20 2013
New Revision: 1494101

URL: http://svn.apache.org/r1494101
Log:
OWB-876 virtual resource handling in arquillian for webapps

Added:
    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/test/java/org/apache/webbeans/arquillian/test/VirtualResourceInWebappTest.java
      - copied, changed from r1494080, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceTest.java
Modified:
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbStandaloneContainer.java

Added: 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=1494101&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java (added)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java Tue Jun 18 11:21:20 2013
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.arquillian.standalone;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePath;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.Node;
+import org.jboss.shrinkwrap.api.asset.Asset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+
+import java.io.Closeable;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.ArrayList;
+import java.util.List;
+
+public class OwbSWClassLoader extends URLClassLoader implements Closeable
+{
+    private final List<InputStream> openedStreams = new ArrayList<InputStream>();
+    private final String prefix;
+
+    public OwbSWClassLoader(final ClassLoader parent, final Archive<?> archive)
+    {
+        super(new URL[0], parent);
+
+        if (WebArchive.class.isInstance(archive))
+        {
+            prefix = "/WEB-INF/classes";
+        }
+        else
+        {
+            prefix = "";
+        }
+
+        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(), ""));
+                        }
+                    };
+                }
+            }));
+        }
+        catch (final MalformedURLException e)
+        {
+            throw new RuntimeException("Could not create URL for archive: " + archive.getName(), e);
+        }
+    }
+
+    public void close() throws IOException
+    {
+        synchronized (this)
+        {
+            for (final InputStream stream : openedStreams)
+            {
+                try
+                {
+                    stream.close();
+                }
+                catch (final Exception e)
+                {
+                    // no-op
+                }
+            }
+            openedStreams.clear();
+        }
+    }
+}

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=1494101&r1=1494100&r2=1494101&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 11:21:20 2013
@@ -18,10 +18,6 @@
  */
 package org.apache.webbeans.arquillian.standalone;
 
-import javax.enterprise.inject.spi.BeanManager;
-
-import java.util.logging.Logger;
-
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.config.WebBeansFinder;
 import org.apache.webbeans.exception.WebBeansDeploymentException;
@@ -35,9 +31,12 @@ import org.jboss.arquillian.container.sp
 import org.jboss.arquillian.core.api.InstanceProducer;
 import org.jboss.arquillian.core.api.annotation.Inject;
 import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.classloader.ShrinkWrapClassLoader;
 import org.jboss.shrinkwrap.descriptor.api.Descriptor;
 
+import javax.enterprise.inject.spi.BeanManager;
+import java.io.IOException;
+import java.util.logging.Logger;
+
 /**
  */
 public class OwbStandaloneContainer implements DeployableContainer<OwbStandaloneConfiguration>
@@ -114,7 +113,7 @@ public class OwbStandaloneContainer impl
 
         final ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
         originalLoader.set(parentLoader);
-        Thread.currentThread().setContextClassLoader(new ShrinkWrapClassLoader(parentLoader, archive));
+        Thread.currentThread().setContextClassLoader(new OwbSWClassLoader(parentLoader, archive));
 
         return new ProtocolMetaData();
     }
@@ -134,6 +133,18 @@ public class OwbStandaloneContainer impl
             lifecycle.stopApplication(null);
         }
 
+        final ClassLoader current = Thread.currentThread().getContextClassLoader();
+        if (OwbSWClassLoader.class.isInstance(current))
+        { // should be the case
+            try
+            {
+                OwbSWClassLoader.class.cast(current).close();
+            }
+            catch (final IOException e)
+            {
+                // no-op
+            }
+        }
         Thread.currentThread().setContextClassLoader(originalLoader.get());
         originalLoader.remove();
     }

Copied: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceInWebappTest.java (from r1494080, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceTest.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceInWebappTest.java?p2=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceInWebappTest.java&p1=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceTest.java&r1=1494080&r2=1494101&rev=1494101&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceTest.java (original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/VirtualResourceInWebappTest.java Tue Jun 18 11:21:20 2013
@@ -24,6 +24,7 @@ import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
 import org.jboss.shrinkwrap.api.asset.StringAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -36,12 +37,12 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertNotNull;
 
 @RunWith(Arquillian.class)
-public class VirtualResourceTest
+public class VirtualResourceInWebappTest
 {
     @Deployment
-    public static Archive<?> jar()
+    public static Archive<?> war()
     {
-        return ShrinkWrap.create(JavaArchive.class).addAsResource(new StringAsset("virtual"), "resource.txt");
+        return ShrinkWrap.create(WebArchive.class).addAsWebInfResource(new StringAsset("virtual-webapp"), "classes/resource.txt");
     }
 
     @Test
@@ -52,7 +53,7 @@ public class VirtualResourceTest
 
         try
         {
-            assertEquals("virtual", new BufferedReader(new InputStreamReader(is)).readLine());
+            assertEquals("virtual-webapp", new BufferedReader(new InputStreamReader(is)).readLine());
         }
         finally
         {