deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject git commit: DELTASPIKE-399 Added support for multi InputStream and some general clean up of how closing is handled when throwing an error.
Date Sat, 28 Dec 2013 15:41:22 GMT
Updated Branches:
  refs/heads/master bcf5909dc -> 585339f89


DELTASPIKE-399 Added support for multi InputStream and some general clean up of how closing
is handled when throwing an error.


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/585339f8
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/585339f8
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/585339f8

Branch: refs/heads/master
Commit: 585339f89030b2c316e48546ea1f0fe279d64a54
Parents: bcf5909
Author: John D. Ament <john.d.ament@gmail.com>
Authored: Sat Dec 28 10:41:11 2013 -0500
Committer: John D. Ament <john.d.ament@gmail.com>
Committed: Sat Dec 28 10:41:11 2013 -0500

----------------------------------------------------------------------
 .../ExternalResourceProvider.java               |  3 +
 .../resourceloader/BaseResourceProvider.java    |  8 ++
 .../ClasspathResourceProvider.java              | 85 ++++++++++++++++----
 .../ExternalResourceProducer.java               | 10 +++
 .../core/impl/resourceloader/Cdi10Bean.java     |  7 ++
 .../core/impl/resourceloader/Cdi11Bean.java     | 12 +++
 .../resourceloader/ClasspathResourceTest.java   | 12 +++
 .../impl/resourceloader/TestResourceHolder.java |  3 +
 8 files changed, 123 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java
index 43dca6d..824e4d7 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java
@@ -21,6 +21,7 @@ package org.apache.deltaspike.core.spi.resourceloader;
 import org.apache.deltaspike.core.api.resourceloader.ExternalResource;
 
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 /**
@@ -32,6 +33,8 @@ public interface ExternalResourceProvider
 
     InputStream readStream(final ExternalResource externalResource);
 
+    List<InputStream> readStreams(final ExternalResource externalResource);
+
     Properties readProperties(final ExternalResource externalResource);
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java
b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java
index f7bc2fc..7319213 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java
@@ -28,6 +28,8 @@ import javax.inject.Inject;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.List;
 import java.util.Properties;
 import java.util.Set;
 import java.util.logging.Level;
@@ -89,4 +91,10 @@ public abstract class BaseResourceProvider implements ExternalResourceProvider
         this.loadInputStreamToProperties(inputStream, properties, name);
         return properties;
     }
+
+    @Override
+    public List<InputStream> readStreams(ExternalResource externalResource)
+    {
+        return Collections.singletonList(this.readStream(externalResource));
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java
b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java
index 67f9d81..a2cf0d7 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java
@@ -27,7 +27,9 @@ import javax.enterprise.context.ApplicationScoped;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -45,7 +47,8 @@ public class ClasspathResourceProvider extends BaseResourceProvider
     {
         try
         {
-            return readClassPath(externalResource.location());
+            List<InputStream> matchedStreams = this.readClassPath(externalResource.location(),true);
+            return matchedStreams.get(0);
         }
         catch (IOException e)
         {
@@ -57,35 +60,83 @@ public class ClasspathResourceProvider extends BaseResourceProvider
         }
     }
 
-    private InputStream readClassPath(final String name) throws IOException
+    @Override
+    public List<InputStream> readStreams(ExternalResource externalResource)
     {
-        Enumeration<URL> urls = ClassUtils.getClassLoader(null).getResources(name);
+        try
+        {
+            return readClassPath(externalResource.location(),false);
+        }
+        catch (IOException e)
+        {
+            throw new IllegalStateException("Error while trying to load resources from classpath
",e);
+        }
+    }
 
-        InputStream result = null;
-        URL firstURL = null;
+    /**
+     * Reads all possibly matching classpath entries for the given name.
+     *
+     * If requireUnique is true, then validates that 1 element is present before returning
+     *
+     * @param name
+     * @param requireUnique
+     * @return
+     * @throws IOException
+     * @throws IllegalStateException
+     */
+    private List<InputStream> readClassPath(final String name, final boolean requireUnique)
+        throws IllegalStateException,IOException
+    {
+        Enumeration<URL> urls = ClassUtils.getClassLoader(null).getResources(name);
+        List<URL> urlList = new ArrayList<URL>();
+        List<InputStream> results = new ArrayList<InputStream>();
         while (urls.hasMoreElements())
         {
             URL url = urls.nextElement();
             InputStream is = url.openStream();
             if (is != null)
             {
-                if (firstURL != null)
+                results.add(is);
+                urlList.add(url);
+            }
+        }
+        if (requireUnique && results.size() != 1)
+        {
+            String msg = urlsToString(urlList,name);
+            for (InputStream is : results)
+            {
+                try
                 {
-                    try
-                    {
-                        result.close();
-                    }
-                    finally
+                    is.close();
+                }
+                catch (IOException e)
+                {
+                    if (logger.isLoggable(Level.FINE))
                     {
-                        is.close();
+                        logger.log(Level.FINE,"Unable to close stream",e);
                     }
-                    throw new IllegalStateException("multiple files found for '" + name +
-                        "' (" + firstURL.toExternalForm() + ", " + url.toExternalForm() +
")");
                 }
-                firstURL = url;
-                result = is;
             }
+            throw new IllegalStateException(msg);
+        }
+        return results;
+    }
+
+    private String urlsToString(List<URL> urls, String name)
+    {
+        if (urls.size() == 0)
+        {
+            return String.format("No resources found for '%s'",name);
+        }
+        else
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.append(String.format("multiple resources found for '%s'",name));
+            for (URL u : urls)
+            {
+                sb.append(" Match : ").append(u.toExternalForm());
+            }
+            return sb.toString();
         }
-        return result;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java
b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java
index 57430c7..019b42a 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java
@@ -34,6 +34,7 @@ import javax.inject.Inject;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.util.List;
 import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -70,6 +71,15 @@ public class ExternalResourceProducer
 
     @Produces
     @ExternalResource(storage = ExternalResourceStorage.class,location = "")
+    public List<InputStream> getInputStreams(final InjectionPoint injectionPoint)
+    {
+        ExternalResource externalResource = getAnnotation(injectionPoint);
+        ExternalResourceProvider provider = getProvider(externalResource.storage());
+        return provider.readStreams(externalResource);
+    }
+
+    @Produces
+    @ExternalResource(storage = ExternalResourceStorage.class,location = "")
     public Properties getProperties(final InjectionPoint injectionPoint) throws IOException
     {
         ExternalResource externalResource = getAnnotation(injectionPoint);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java
index df5be1b..45c390b 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java
@@ -20,6 +20,7 @@ package org.apache.deltaspike.test.core.impl.resourceloader;
 
 import javax.enterprise.inject.Instance;
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 public class Cdi10Bean implements TestResourceHolder
@@ -47,4 +48,10 @@ public class Cdi10Bean implements TestResourceHolder
     {
         throw new UnsupportedOperationException();
     }
+
+    @Override
+    public List<InputStream> getInputStreams()
+    {
+        throw new UnsupportedOperationException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java
index 908cc38..47b7df6 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java
@@ -21,12 +21,15 @@ package org.apache.deltaspike.test.core.impl.resourceloader;
 import org.apache.deltaspike.core.api.resourceloader.ClasspathStorage;
 import org.apache.deltaspike.core.api.resourceloader.ExternalResource;
 
+import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.Any;
 import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
+@Dependent
 public class Cdi11Bean implements TestResourceHolder
 {
     @Inject
@@ -38,6 +41,10 @@ public class Cdi11Bean implements TestResourceHolder
     private Properties properties;
 
     @Inject
+    @ExternalResource(storage = ClasspathStorage.class,location="META-INF/beans.xml")
+    private List<InputStream> inputStreams;
+
+    @Inject
     @Any
     private Instance<InputStream> inputStreamInstance;
 
@@ -64,4 +71,9 @@ public class Cdi11Bean implements TestResourceHolder
     {
         return propertiesInstance;
     }
+
+    @Override
+    public List<InputStream> getInputStreams() {
+        return inputStreams;
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java
index a95f6c6..694bc47 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java
@@ -38,6 +38,8 @@ import org.junit.runner.RunWith;
 
 import javax.inject.Inject;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 @RunWith(Arquillian.class)
@@ -93,6 +95,16 @@ public class ClasspathResourceTest
             .select(new ExternalResourceLiteral(ClasspathStorage.class, "META-INF/beans.xml")).get();
     }
 
+    @Test
+    public void testSuccessfulAmbiguousLookup()
+    {
+        Assume.assumeTrue(!isOwbForCdi10());
+        //note, we only test this on classpath, since File impl is always getting 1.
+        List<InputStream> streams = testResourceHolder.getInputStreams();
+        Assert.assertEquals(4,streams.size());
+
+    }
+
     private static boolean isOwbForCdi10()
     {
         return CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB11) || CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB12);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/585339f8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java
index cf059d2..730efba 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java
@@ -20,6 +20,7 @@ package org.apache.deltaspike.test.core.impl.resourceloader;
 
 import javax.enterprise.inject.Instance;
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 //just needed to build different archives - otherwise injection would fail with owb implementing
cdi 1.0
@@ -32,4 +33,6 @@ public interface TestResourceHolder
     Instance<InputStream> getInputStreamInstance();
 
     Instance<Properties> getPropertiesInstance();
+
+    List<InputStream> getInputStreams();
 }


Mime
View raw message