Author: dan.j.allen
Date: 2009-05-27 16:44:49 -0400 (Wed, 27 May 2009)
New Revision: 11016
Added:
modules/trunk/resources/
modules/trunk/resources/pom.xml
modules/trunk/resources/src/
modules/trunk/resources/src/main/
modules/trunk/resources/src/main/java/
modules/trunk/resources/src/main/java/org/
modules/trunk/resources/src/main/java/org/jboss/
modules/trunk/resources/src/main/java/org/jboss/seam/
modules/trunk/resources/src/main/java/org/jboss/seam/resources/
modules/trunk/resources/src/main/java/org/jboss/seam/resources/DefaultResourceLoader.java
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoader.java
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoaderProducer.java
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoadingException.java
modules/trunk/resources/src/main/resources/
modules/trunk/resources/src/main/resources/META-INF/
modules/trunk/resources/src/main/resources/META-INF/beans.xml
modules/trunk/resources/src/test/
modules/trunk/resources/src/test/java/
modules/trunk/resources/src/test/java/org/
modules/trunk/resources/src/test/java/org/jboss/
modules/trunk/resources/src/test/java/org/jboss/seam/
modules/trunk/resources/src/test/java/org/jboss/seam/resources/
modules/trunk/resources/src/test/java/org/jboss/seam/resources/DefaultResourceLoaderTest.java
modules/trunk/resources/src/test/resources/
modules/trunk/resources/src/test/resources/org/
modules/trunk/resources/src/test/resources/org/jboss/
modules/trunk/resources/src/test/resources/org/jboss/testharness/
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/packaging/
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/beans.xml
Modified:
modules/trunk/version-matrix/pom.xml
Log:
add a resources module
Property changes on: modules/trunk/resources
___________________________________________________________________
Name: svn:ignore
+ .classpath
.project
.settings
attic
target
temp-testng-customsuite.xml
test-output
Added: modules/trunk/resources/pom.xml
===================================================================
--- modules/trunk/resources/pom.xml (rev 0)
+++ modules/trunk/resources/pom.xml 2009-05-27 20:44:49 UTC (rev 11016)
@@ -0,0 +1,58 @@
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>seam-parent</artifactId>
+ <groupId>org.jboss.seam</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>seam-resources</artifactId>
+ <packaging>jar</packaging>
+ <version>3.0.0-SNAPSHOT</version>
+ <name>Seam Resources Module</name>
+ <description>A shared module which provides services for loading resources from
the classpath.</description>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>${seam.groupId}</groupId>
+ <artifactId>seam-beans</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>${webbeans.groupId}</groupId>
+ <artifactId>jsr299-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${webbeans.groupId}</groupId>
+ <artifactId>webbeans-logging</artifactId>
+ <!-- assumes use of Web Beans impl -->
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+
+ <!--
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <suiteXmlFiles>
+
<suiteXmlFile>src/test/resources/unit-test-suite.xml</suiteXmlFile>
+ </suiteXmlFiles>
+ </configuration>
+ </plugin>
+ -->
+
+ </plugins>
+ </build>
+
+</project>
Added:
modules/trunk/resources/src/main/java/org/jboss/seam/resources/DefaultResourceLoader.java
===================================================================
---
modules/trunk/resources/src/main/java/org/jboss/seam/resources/DefaultResourceLoader.java
(rev 0)
+++
modules/trunk/resources/src/main/java/org/jboss/seam/resources/DefaultResourceLoader.java 2009-05-27
20:44:49 UTC (rev 11016)
@@ -0,0 +1,245 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.seam.resources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.MissingResourceException;
+
+import org.jboss.seam.beans.Default;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * A simple resource loader.
+ *
+ * Uses {@link DefaultResourceLoader}'s classloader if the Thread Context
+ * Classloader isn't available
+ *
+ * @author Pete Muir
+ */
+public
+@Default
+class DefaultResourceLoader implements ResourceLoader
+{
+ private static final LogProvider log =
Logging.getLogProvider(DefaultResourceLoader.class);
+
+ public Class<?> getClassForName(String name)
+ {
+ try
+ {
+ if (Thread.currentThread().getContextClassLoader() != null)
+ {
+ return Thread.currentThread().getContextClassLoader().loadClass(name);
+ }
+ else
+ {
+ return Class.forName(name);
+ }
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new ResourceLoadingException(e);
+ }
+ catch (NoClassDefFoundError e)
+ {
+ throw new ResourceLoadingException(e);
+ }
+ }
+
+ /**
+ * Load a resource bundle by name (may be overridden by subclasses
+ * who want to use non-standard resource bundle types).
+ *
+ * @param bundleName the name of the resource bundle
+ * @return an instance of java.util.ResourceBundle
+ */
+ public java.util.ResourceBundle loadBundle(String bundleName)
+ {
+ try
+ {
+ java.util.ResourceBundle bundle = loadBundleInternal(bundleName);
+ log.debug("loaded resource bundle: " + bundleName);
+ return bundle;
+ }
+ catch (MissingResourceException mre)
+ {
+ log.debug("resource bundle missing: " + bundleName);
+ return null;
+ }
+ }
+
+ public URL getResource(String name)
+ {
+ return getResourceInternal(toAbsolutePath(name), toRelativePath(name));
+ }
+
+ public InputStream getResourceAsStream(String name)
+ {
+ return getResourceAsStreamInternal(toAbsolutePath(name), toRelativePath(name));
+ }
+
+ public Iterable<URL> getResources(String name)
+ {
+ try
+ {
+ return new
EnumerationIterable<URL>(getResourcesInternal(toAbsolutePath(name),
toRelativePath(name)));
+ }
+ catch (IOException e)
+ {
+ throw new ResourceLoadingException(e);
+ }
+ }
+
+ protected java.util.ResourceBundle loadBundleInternal(String bundleName)
+ {
+ return java.util.ResourceBundle.getBundle(bundleName, Locale.getDefault(),
Thread.currentThread().getContextClassLoader());
+ }
+
+ protected InputStream getResourceAsStreamInternal(String absolutePath, String
relativePath)
+ {
+ InputStream stream = null;
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (classLoader != null)
+ {
+ stream = classLoader.getResourceAsStream(relativePath);
+ if (stream != null)
+ {
+ log.debug("Loaded resource from context classloader: " +
relativePath);
+ return stream;
+ }
+ }
+
+ stream = getClass().getResourceAsStream(absolutePath);
+ if (stream != null)
+ {
+ log.debug("Loaded resource from Seam classloader: " + absolutePath);
+ return stream;
+ }
+
+ stream = getClass().getClassLoader().getResourceAsStream(relativePath);
+ if (stream != null)
+ {
+ log.debug("Loaded resource from Seam classloader: " + relativePath);
+ return stream;
+ }
+
+ return stream;
+ }
+
+ protected URL getResourceInternal(String absolutePath, String relativePath)
+ {
+ URL url = null;
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (classLoader != null)
+ {
+ url = classLoader.getResource(relativePath);
+ if (url != null)
+ {
+ log.debug("Loaded resource from context classloader: " + url);
+ return url;
+ }
+ }
+
+ url = getClass().getResource(absolutePath);
+ if (url != null)
+ {
+ log.debug("Loaded resource from " + getClass() + " classloader:
" + url);
+ return url;
+ }
+
+ url = getClass().getClassLoader().getResource(relativePath);
+ if (url != null)
+ {
+ log.debug("Loaded resource from " + getClass() + " classloader:
" + url);
+ return url;
+ }
+
+ return url;
+ }
+
+
+ protected Enumeration<URL> getResourcesInternal(String absolutePath, String
relativePath) throws IOException
+ {
+ Enumeration<URL> urls = null;
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (classLoader != null)
+ {
+ urls = classLoader.getResources(relativePath);
+ if (urls != null && urls.hasMoreElements())
+ {
+ log.debug("Loaded resources from context classloader: " + urls);
+ return urls;
+ }
+ }
+
+ urls = getClass().getClassLoader().getResources(relativePath);
+ if (urls != null)
+ {
+ log.debug("Loaded resources from " + getClass() + " classloader:
" + urls);
+ return urls;
+ }
+
+ return urls;
+ }
+
+ protected String toAbsolutePath(String name)
+ {
+ return name.startsWith("/") ? name : "/" + name;
+ }
+
+ protected String toRelativePath(String name)
+ {
+ return name.startsWith("/") ? name.substring(1) : name;
+ }
+
+ private class EnumerationIterable<T> implements Iterable<T>
+ {
+ // The enumeration as a list
+ private final List<T> list = new ArrayList<T>();
+
+ /**
+ * Constructor
+ *
+ * @param enumeration The enumeration
+ */
+ public EnumerationIterable(Enumeration<T> enumeration)
+ {
+ while (enumeration.hasMoreElements())
+ {
+ list.add(enumeration.nextElement());
+ }
+ }
+
+ /**
+ * Gets an iterator
+ *
+ * @return The iterator
+ */
+ public Iterator<T> iterator()
+ {
+ return Collections.unmodifiableList(list).iterator();
+ }
+ }
+}
Added: modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoader.java
===================================================================
--- modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoader.java
(rev 0)
+++
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoader.java 2009-05-27
20:44:49 UTC (rev 11016)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.seam.resources;
+
+import java.net.URL;
+
+/**
+ * Resource loading/class creation services for Seam. By default an
+ * implementation which uses the Thread Context ClassLoader if available,
+ * otherwise the classloading of the implementation is used
+ *
+ * @author Pete Muir
+ */
+public interface ResourceLoader
+{
+ /**
+ * Creates a class from a given FQCN
+ *
+ * @param name The name of the clsas
+ * @return The class
+ */
+ public Class<?> getClassForName(String name);
+
+ /**
+ * Gets a resource as a URL by name
+ *
+ * @param name The name of the resource
+ * @return An URL to the resource
+ */
+ public URL getResource(String name);
+
+ /**
+ * Gets resources as URLs by name
+ *
+ * @param name The name of the resource
+ * @return An iterable reference to the URLS
+ */
+ public Iterable<URL> getResources(String name);
+
+}
Added:
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoaderProducer.java
===================================================================
---
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoaderProducer.java
(rev 0)
+++
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoaderProducer.java 2009-05-27
20:44:49 UTC (rev 11016)
@@ -0,0 +1,36 @@
+package org.jboss.seam.resources;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Initializer;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.BeanManager;
+
+import org.jboss.seam.beans.RuntimeBeanSelector;
+
+public
+@ApplicationScoped
+class ResourceLoaderProducer extends RuntimeBeanSelector<ResourceLoader>
+{
+ ResourceLoaderProducer() {}
+
+ public @Initializer ResourceLoaderProducer(BeanManager manager)
+ {
+ super(manager);
+ }
+
+ @Override
+ public Class<ResourceLoader> getType()
+ {
+ return ResourceLoader.class;
+ }
+
+ @Override
+ public
+ @Produces
+ //@RequestScoped
+ ResourceLoader selectImplementation()
+ {
+ return super.selectImplementation();
+ }
+
+}
Added:
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoadingException.java
===================================================================
---
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoadingException.java
(rev 0)
+++
modules/trunk/resources/src/main/java/org/jboss/seam/resources/ResourceLoadingException.java 2009-05-27
20:44:49 UTC (rev 11016)
@@ -0,0 +1,56 @@
+/**
+ *
+ */
+package org.jboss.seam.resources;
+
+import javax.inject.ExecutionException;
+
+/**
+ * Exception thrown when errors occur while loading resource
+ *
+ * @author Pete Muir
+ */
+public class ResourceLoadingException extends ExecutionException
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Constructor
+ */
+ public ResourceLoadingException()
+ {
+ super();
+ }
+
+ /**
+ * Constructor
+ *
+ * @param message The message
+ * @param throwable The exception
+ */
+ public ResourceLoadingException(String message, Throwable throwable)
+ {
+ super(message, throwable);
+ }
+
+ /**
+ * Constructor
+ *
+ * @param message The message
+ */
+ public ResourceLoadingException(String message)
+ {
+ super(message);
+ }
+
+ /**
+ * Constructor
+ *
+ * @param throwable The exception
+ */
+ public ResourceLoadingException(Throwable throwable)
+ {
+ super(throwable);
+ }
+
+}
\ No newline at end of file
Added: modules/trunk/resources/src/main/resources/META-INF/beans.xml
===================================================================
Added:
modules/trunk/resources/src/test/java/org/jboss/seam/resources/DefaultResourceLoaderTest.java
===================================================================
---
modules/trunk/resources/src/test/java/org/jboss/seam/resources/DefaultResourceLoaderTest.java
(rev 0)
+++
modules/trunk/resources/src/test/java/org/jboss/seam/resources/DefaultResourceLoaderTest.java 2009-05-27
20:44:49 UTC (rev 11016)
@@ -0,0 +1,45 @@
+package org.jboss.seam.resources;
+
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+
+import java.net.URL;
+
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.jsr299.JSR299ArtifactDescriptor;
+import org.jboss.webbeans.test.AbstractWebBeansTest;
+import org.testng.annotations.Test;
+
+@Artifact
+public class DefaultResourceLoaderTest extends AbstractWebBeansTest
+{
+ @Test
+ public void testGetClassForName()
+ {
+ assertSame(DefaultResourceLoader.class,
getResourceLoaderInstance().getClassForName(DefaultResourceLoader.class.getName()));
+ }
+
+ @Test
+ public void testGetResourceFromRelativePath()
+ {
+ URL beansXml =
getResourceLoaderInstance().getResource(JSR299ArtifactDescriptor.STANDARD_BEANS_XML_FILE_NAME);
+ assertNotNull(beansXml);
+
assertTrue(beansXml.toString().endsWith(JSR299ArtifactDescriptor.STANDARD_BEANS_XML_FILE_NAME));
+ }
+
+ @Test
+ public void testGetResourceFromAbsolutePath()
+ {
+ URL beansXml = getResourceLoaderInstance().getResource("/" +
JSR299ArtifactDescriptor.STANDARD_BEANS_XML_FILE_NAME);
+ assertNotNull(beansXml);
+
assertTrue(beansXml.toString().endsWith(JSR299ArtifactDescriptor.STANDARD_BEANS_XML_FILE_NAME));
+ }
+
+ private ResourceLoader getResourceLoaderInstance()
+ {
+ ResourceLoader resourceLoader =
getCurrentManager().getInstanceByType(ResourceLoader.class);
+ assertNotNull(resourceLoader);
+ return resourceLoader;
+ }
+}
Added:
modules/trunk/resources/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/beans.xml
===================================================================
Modified: modules/trunk/version-matrix/pom.xml
===================================================================
--- modules/trunk/version-matrix/pom.xml 2009-05-27 13:30:16 UTC (rev 11015)
+++ modules/trunk/version-matrix/pom.xml 2009-05-27 20:44:49 UTC (rev 11016)
@@ -306,6 +306,12 @@
<artifactId>seam-security</artifactId>
<version>${seam.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>${seam.groupId}</groupId>
+ <artifactId>seam-resources</artifactId>
+ <version>${seam.version}</version>
+ </dependency>
<dependency>
<groupId>${seam.groupId}</groupId>