[weld-commits] Weld SVN: r6084 - in extensions/trunk: src/main/java/org/jboss/weld/extensions and 4 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Fri Apr 9 09:07:09 EDT 2010


Author: swd847
Date: 2010-04-09 09:07:07 -0400 (Fri, 09 Apr 2010)
New Revision: 6084

Added:
   extensions/trunk/src/main/java/org/jboss/weld/extensions/ResourceListener.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextDestroyedEvent.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextInitializedEvent.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletResourceExtension.java
Modified:
   extensions/trunk/pom.xml
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoader.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoaderImpl.java
   extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
added loading resources from the ServletContext



Modified: extensions/trunk/pom.xml
===================================================================
--- extensions/trunk/pom.xml	2010-04-09 11:28:53 UTC (rev 6083)
+++ extensions/trunk/pom.xml	2010-04-09 13:07:07 UTC (rev 6084)
@@ -154,6 +154,12 @@
             </exclusion>
          </exclusions>
       </dependency>
+      
+      <dependency>
+         <groupId>javax.servlet</groupId>
+         <artifactId>servlet-api</artifactId>
+         <optional>true</optional>
+      </dependency>
 
       <dependency>
          <groupId>javax.enterprise</groupId>

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/ResourceListener.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/ResourceListener.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/ResourceListener.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -0,0 +1,81 @@
+package org.jboss.weld.extensions;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.jboss.weld.extensions.resources.servlet.ServletContextDestroyedEvent;
+import org.jboss.weld.extensions.resources.servlet.ServletContextInitializedEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * ServletContextListener that fires a CDI event when the ServletContext is
+ * created or destroyed
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+//@WebListener
+public class ResourceListener implements ServletContextListener
+{
+   private BeanManager beanManager;
+
+   private Logger log = LoggerFactory.getLogger(ResourceListener.class);
+   // FIXME: hack to work around invalid binding in JBoss AS 6 M2
+   private static final List<String> beanManagerLocations = new ArrayList<String>()
+   {
+      private static final long serialVersionUID = 1L;
+      {
+         add("java:comp/BeanManager");
+         add("java:app/BeanManager");
+      }
+   };
+
+   public ResourceListener()
+   {
+      beanManager = lookupBeanManager();
+   }
+
+   private BeanManager lookupBeanManager()
+   {
+      for (String location : beanManagerLocations)
+      {
+         try
+         {
+            log.trace("Looking for Bean Manager at JNDI location #0", location);
+            return (BeanManager) new InitialContext().lookup(location);
+         }
+         catch (NamingException e)
+         {
+            // No panic, keep trying
+            log.debug("Bean Manager not found at JNDI location #0", location);
+         }
+      }
+      // OK, panic
+      throw new IllegalArgumentException("Could not find BeanManager in " + beanManagerLocations);
+   }
+
+   private void fireEvent(Object payload, Annotation... qualifiers)
+   {
+      log.trace("Firing event #0 with qualifiers #1", payload, qualifiers);
+      beanManager.fireEvent(payload, qualifiers);
+   }
+
+   public void contextDestroyed(ServletContextEvent sce)
+   {
+      fireEvent(new ServletContextDestroyedEvent(sce.getServletContext()));
+   }
+
+   public void contextInitialized(ServletContextEvent sce)
+   {
+      fireEvent(new ServletContextInitializedEvent(sce.getServletContext()));
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextDestroyedEvent.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextDestroyedEvent.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextDestroyedEvent.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -0,0 +1,25 @@
+package org.jboss.weld.extensions.resources.servlet;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Event class that is fired from a ServletContextListener
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class ServletContextDestroyedEvent
+{
+   ServletContext servletContext;
+
+   public ServletContextDestroyedEvent(ServletContext servletContext)
+   {
+      this.servletContext = servletContext;
+   }
+
+   public ServletContext getServletContext()
+   {
+      return servletContext;
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextInitializedEvent.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextInitializedEvent.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletContextInitializedEvent.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -0,0 +1,25 @@
+package org.jboss.weld.extensions.resources.servlet;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Event class that is fired from a ServletContextListener
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class ServletContextInitializedEvent
+{
+   ServletContext servletContext;
+
+   public ServletContextInitializedEvent(ServletContext servletContext)
+   {
+      this.servletContext = servletContext;
+   }
+
+   public ServletContext getServletContext()
+   {
+      return servletContext;
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletResourceExtension.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletResourceExtension.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/servlet/ServletResourceExtension.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -0,0 +1,37 @@
+package org.jboss.weld.extensions.resources.servlet;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.Extension;
+import javax.servlet.ServletContext;
+
+/**
+ * Portable extension that listens for the creation of ServletContexts and
+ * stored them for other extensions to Consume
+ * 
+ * @author stuart
+ * 
+ */
+public class ServletResourceExtension implements Extension
+{
+   protected Set<ServletContext> servletContexts = new HashSet<ServletContext>();
+
+   public void registerServletContext(@Observes ServletContextInitializedEvent event)
+   {
+      servletContexts.add(event.getServletContext());
+   }
+
+   public void removeServletContext(@Observes ServletContextDestroyedEvent event)
+   {
+      servletContexts.remove(event.getServletContext());
+   }
+
+   public Set<ServletContext> getServletContexts()
+   {
+      return Collections.unmodifiableSet(servletContexts);
+   }
+
+}

Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoader.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoader.java	2010-04-09 11:28:53 UTC (rev 6083)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoader.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -2,6 +2,7 @@
 
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Set;
 
 public interface ResourceLoader
 {
@@ -10,4 +11,6 @@
    
    public InputStream getResourceAsStream(String name);
 
+   public Set<URL> getResources(String name);
+
 }

Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoaderImpl.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoaderImpl.java	2010-04-09 11:28:53 UTC (rev 6083)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/resources/spi/ResourceLoaderImpl.java	2010-04-09 13:07:07 UTC (rev 6084)
@@ -1,18 +1,33 @@
 package org.jboss.weld.extensions.resources.spi;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
 
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+
 import org.jboss.weld.extensions.resources.ResourceProducer;
+import org.jboss.weld.extensions.resources.servlet.ServletResourceExtension;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ResourceLoaderImpl implements ResourceLoader
 {
-   
+
+   private final Set<ServletContext> servletContexts;
+
    private static final Logger log = LoggerFactory.getLogger("org.jboss.weld.extensions.resources");
    
-   private ResourceLoaderImpl() {}
+   @Inject
+   private ResourceLoaderImpl(ServletResourceExtension extension)
+   {
+      servletContexts = extension.getServletContexts();
+   }
 
    public InputStream getResourceAsStream(String name)
    {
@@ -38,12 +53,22 @@
             return stream;
          }
       }
+      String slashedName = getSlashedName(name);
+      for (ServletContext context : servletContexts)
+      {
+         InputStream stream = context.getResourceAsStream(slashedName);
+         if (stream != null)
+         {
+            log.trace("Loaded resource from ServletContext: " + slashedName);
+            return stream;
+         }
+      }
       return null;
    }
 
    public URL getResource(String name)
    {
-   // Always use the strippedName, classloader always assumes no starting /
+      // Always use the strippedName, classloader always assumes no starting /
       String strippedName = getStrippedName(name);
       // Try to load from the TCCL
       if (Thread.currentThread().getContextClassLoader() != null)
@@ -65,12 +90,93 @@
             return url;
          }
       }
+      String slashedName = getSlashedName(name);
+      // Try to load from the ServletContext
+      for (ServletContext context : servletContexts)
+      {
+         try
+         {
+            URL url = context.getResource(slashedName);
+            if (url != null)
+            {
+               log.trace("Loaded resource from ServletContext: " + slashedName);
+               return url;
+            }
+         }
+         catch (MalformedURLException e)
+         {
+            log.error("Malformed URL loading " + name, e);
+         }
+      }
       return null;
    }
    
+
+   public Set<URL> getResources(String name)
+   {
+      Set<URL> urls = new HashSet<URL>();
+      // Always use the strippedName, classloader always assumes no starting /
+      String strippedName = getStrippedName(name);
+      // Try to load from the TCCL
+      if (Thread.currentThread().getContextClassLoader() != null)
+      {
+         try
+         {
+            Enumeration<URL> urlEnum = Thread.currentThread().getContextClassLoader().getResources(strippedName);
+            while (urlEnum.hasMoreElements())
+            {
+               urls.add(urlEnum.nextElement());
+            }
+         }
+         catch (IOException e)
+         {
+            // we are probably not going to recover from an IOException
+            throw new RuntimeException(e);
+         }
+      }
+      // Try to load from the extension's classloader
+      else
+      {
+         try
+         {
+            Enumeration<URL> urlEnum = ResourceProducer.class.getClassLoader().getResources(strippedName);
+            while (urlEnum.hasMoreElements())
+            {
+               urls.add(urlEnum.nextElement());
+            }
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException(e);
+         }
+      }
+      String slashedName = getSlashedName(name);
+      for (ServletContext context : servletContexts)
+      {
+         try
+         {
+            URL url = context.getResource(slashedName);
+            if (url != null)
+            {
+               urls.add(url);
+            }
+         }
+         catch (MalformedURLException e)
+         {
+            log.error("Malformed URL loading " + name, e);
+         }
+      }
+      return urls;
+   }
+
    private static String getStrippedName(String name)
    {
       return name.startsWith("/") ? name.substring(1) : name;
    }
 
+   private static String getSlashedName(String name)
+   {
+      return name.startsWith("/") ? name : "/" + name;
+   }
+
 }

Modified: extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
--- extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-04-09 11:28:53 UTC (rev 6083)
+++ extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-04-09 13:07:07 UTC (rev 6084)
@@ -1 +1,2 @@
-org.jboss.weld.extensions.genericbeans.GenericExtension
\ No newline at end of file
+org.jboss.weld.extensions.genericbeans.GenericExtension
+org.jboss.weld.extensions.resources.servlet.ServletResourceExtension
\ No newline at end of file



More information about the weld-commits mailing list