Author: swd847
Date: 2010-06-27 06:52:02 -0400 (Sun, 27 Jun 2010)
New Revision: 6579
Added:
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/DelegatingResourceLoader.java
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ResourceListener.java
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ServletContextLoader.java
extensions/trunk/src/main/resources/META-INF/web-fragment.xml
Modified:
extensions/trunk/pom.xml
extensions/trunk/src/main/resources/META-INF/services/org.jboss.weld.extensions.resourceLoader.ResourceLoader
Log:
Added ability to load resources from the ServletContext
Modified: extensions/trunk/pom.xml
===================================================================
--- extensions/trunk/pom.xml 2010-06-27 06:06:05 UTC (rev 6578)
+++ extensions/trunk/pom.xml 2010-06-27 10:52:02 UTC (rev 6579)
@@ -63,6 +63,7 @@
<slf4j.version>1.5.10</slf4j.version>
<testng.version>5.12.1</testng.version>
<jboss.test.harness.version>1.1.0-CR3</jboss.test.harness.version>
+ <servlet.api.version>2.5</servlet.api.version>
</properties>
<!-- Dependency management, including extra repositories if needed -->
@@ -137,6 +138,12 @@
<artifactId>jboss-test-harness-tomcat</artifactId>
<version>${jboss.test.harness.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>${servlet.api.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
@@ -193,6 +200,13 @@
<artifactId>jboss-ejb3-api</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <optional>true</optional>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<scm>
Added:
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/DelegatingResourceLoader.java
===================================================================
---
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/DelegatingResourceLoader.java
(rev 0)
+++
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/DelegatingResourceLoader.java 2010-06-27
10:52:02 UTC (rev 6579)
@@ -0,0 +1,74 @@
+package org.jboss.weld.extensions.resourceLoader;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Resource loader that delegates to a static list of resource loaders.
+ *
+ * @author Stuart Douglas
+ *
+ */
+public class DelegatingResourceLoader implements ResourceLoader
+{
+ private static final Logger log =
LoggerFactory.getLogger("org.jboss.weld.extensions.resources");
+
+ // TODO: get rid of the static
+ private static final List<ResourceLoader> resourceLoaders = new
ArrayList<ResourceLoader>();
+
+ public static void addResourceLoader(ResourceLoader loader)
+ {
+ resourceLoaders.add(loader);
+ }
+
+ public URL getResource(String resource)
+ {
+ for (ResourceLoader r : resourceLoaders)
+ {
+ URL res = r.getResource(resource);
+ if (res != null)
+ {
+ log.trace("Loaded resource " + resource + " from " +
r.toString());
+ return res;
+ }
+ }
+ return null;
+ }
+
+ public InputStream getResourceAsStream(String resource)
+ {
+ for (ResourceLoader r : resourceLoaders)
+ {
+ InputStream res = r.getResourceAsStream(resource);
+ if (res != null)
+ {
+ log.trace("Loaded resource " + resource + " from " +
r.toString());
+ return res;
+ }
+ }
+ return null;
+ }
+
+ public Set<URL> getResources(String name)
+ {
+ Set<URL> ret = new HashSet<URL>();
+ for (ResourceLoader r : resourceLoaders)
+ {
+ ret.addAll(r.getResources(name));
+ }
+ return ret;
+ }
+
+ public int getPrecedence()
+ {
+ return 5;
+ }
+
+}
Added:
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ResourceListener.java
===================================================================
---
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ResourceListener.java
(rev 0)
+++
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ResourceListener.java 2010-06-27
10:52:02 UTC (rev 6579)
@@ -0,0 +1,27 @@
+package org.jboss.weld.extensions.resourceLoader.servlet;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.jboss.weld.extensions.resourceLoader.DelegatingResourceLoader;
+
+/**
+ * Servlet context listener than creates and registers a new
+ * ServletContextLoader for each context
+ *
+ * @author Stuart Douglas
+ *
+ */
+public class ResourceListener implements ServletContextListener
+{
+
+ public void contextDestroyed(ServletContextEvent sce)
+ {
+ }
+
+ public void contextInitialized(ServletContextEvent sce)
+ {
+ DelegatingResourceLoader.addResourceLoader(new
ServletContextLoader(sce.getServletContext()));
+ }
+
+}
Added:
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ServletContextLoader.java
===================================================================
---
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ServletContextLoader.java
(rev 0)
+++
extensions/trunk/src/main/java/org/jboss/weld/extensions/resourceLoader/servlet/ServletContextLoader.java 2010-06-27
10:52:02 UTC (rev 6579)
@@ -0,0 +1,77 @@
+package org.jboss.weld.extensions.resourceLoader.servlet;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+
+import org.jboss.weld.extensions.resourceLoader.ResourceLoader;
+
+/**
+ * Implementation of ResourceLoader that can load from the servlet context. It
+ * is not used directly but is called by DelegatingResourceLoader
+ *
+ * @author stuart
+ *
+ */
+public class ServletContextLoader implements ResourceLoader
+{
+
+ private final ServletContext context;
+
+ public ServletContextLoader(ServletContext context)
+ {
+ this.context = context;
+ }
+
+ public URL getResource(String resource)
+ {
+ if (!resource.startsWith("/"))
+ {
+ resource = "/" + resource;
+ }
+ try
+ {
+ return context.getResource(resource);
+ }
+ catch (MalformedURLException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public InputStream getResourceAsStream(String resource)
+ {
+ if (!resource.startsWith("/"))
+ {
+ resource = "/" + resource;
+ }
+ return context.getResourceAsStream(resource);
+ }
+
+ public Set<URL> getResources(String name)
+ {
+ URL r = getResource(name);
+ if (r != null)
+ {
+ return Collections.singleton(r);
+ }
+ return Collections.emptySet();
+ }
+
+ // not used
+ public int getPrecedence()
+ {
+ return 0;
+ }
+
+ @Override
+ public String toString()
+ {
+ return getClass().getName() + " [" + context.getContextPath() +
"]";
+ }
+
+}
Modified:
extensions/trunk/src/main/resources/META-INF/services/org.jboss.weld.extensions.resourceLoader.ResourceLoader
===================================================================
---
extensions/trunk/src/main/resources/META-INF/services/org.jboss.weld.extensions.resourceLoader.ResourceLoader 2010-06-27
06:06:05 UTC (rev 6578)
+++
extensions/trunk/src/main/resources/META-INF/services/org.jboss.weld.extensions.resourceLoader.ResourceLoader 2010-06-27
10:52:02 UTC (rev 6579)
@@ -1 +1,2 @@
-org.jboss.weld.extensions.resourceLoader.ClasspathResourceLoader
\ No newline at end of file
+org.jboss.weld.extensions.resourceLoader.ClasspathResourceLoader
+org.jboss.weld.extensions.resourceLoader.DelegatingResourceLoader
\ No newline at end of file
Added: extensions/trunk/src/main/resources/META-INF/web-fragment.xml
===================================================================
--- extensions/trunk/src/main/resources/META-INF/web-fragment.xml
(rev 0)
+++ extensions/trunk/src/main/resources/META-INF/web-fragment.xml 2010-06-27 10:52:02 UTC
(rev 6579)
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-fragment version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
+ <name>weldextensions</name>
+
+ <listener>
+
<listener-class>org.jboss.weld.extensions.resourceLoader.servlet.ResourceListener</listener-class>
+ </listener>
+
+</web-fragment>
\ No newline at end of file