You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by li...@apache.org on 2011/01/17 18:13:21 UTC

svn commit: r1059995 - in /shindig/trunk/java/common/src/main/java/org/apache/shindig/common: servlet/GuiceServletContextListener.java util/ResourceLoader.java

Author: lindner
Date: Mon Jan 17 17:13:21 2011
New Revision: 1059995

URL: http://svn.apache.org/viewvc?rev=1059995&view=rev
Log:
SHINDIG-1487 | Patch from Ryan Baxter | The class loaders some of the classes in Shindig use can cause problems in an OSGi enviornment

Modified:
    shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java
    shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/ResourceLoader.java

Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java?rev=1059995&r1=1059994&r2=1059995&view=diff
==============================================================================
--- shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java (original)
+++ shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java Mon Jan 17 17:13:21 2011
@@ -44,7 +44,7 @@ public class GuiceServletContextListener
   // From guice-servlet-2.0
   public static final String INJECTOR_NAME = Injector.class.getName();
   
-  //HNN- constant name matched system.properties <contextparam> specified in the web.xml
+  // HNN- constant name matched system.properties <contextparam> specified in the web.xml
   private static final String SYSTEM_PROPERTIES = "system.properties";
 
   public void contextInitialized(ServletContextEvent event) {
@@ -58,7 +58,14 @@ public class GuiceServletContextListener
         try {
           moduleName = moduleName.trim();
           if (moduleName.length() > 0) {
-            modules.add((Module)Class.forName(moduleName).newInstance());
+            try {
+              modules.add((Module)Class.forName(moduleName).newInstance());
+            } catch (Throwable t) {
+              // If we cannot find the class using forName try the current
+              // threads class loader
+              modules.add((Module)Thread.currentThread().getContextClassLoader()
+                  .loadClass(moduleName).newInstance());
+            }
           }
         } catch (InstantiationException e) {
           throw new RuntimeException(e);
@@ -71,7 +78,6 @@ public class GuiceServletContextListener
     }
     Injector injector = Guice.createInjector(Stage.PRODUCTION, modules);
     context.setAttribute(INJECTOR_ATTRIBUTE, injector);
-
   }
 
   public void contextDestroyed(ServletContextEvent event) {

Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/ResourceLoader.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/ResourceLoader.java?rev=1059995&r1=1059994&r2=1059995&view=diff
==============================================================================
--- shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/ResourceLoader.java (original)
+++ shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/ResourceLoader.java Mon Jan 17 17:13:21 2011
@@ -50,12 +50,33 @@ public final class ResourceLoader {
   }
 
   /**
+   * Opens a resource
    * @param resource
    * @return An input stream for the given named resource
    * @throws FileNotFoundException
    */
-  public static InputStream openResource(String resource) throws IOException {
+  public static InputStream openResource(String resource) throws FileNotFoundException  {
     ClassLoader cl = ResourceLoader.class.getClassLoader();
+    try {
+      return openResource(cl, resource);
+    } catch (FileNotFoundException e) {
+      // If we cannot find the resource using the current classes class loader
+      // try the current threads
+      cl = Thread.currentThread().getContextClassLoader();
+      return openResource(cl, resource);
+    }
+  }
+
+  /**
+   * Opens a resource
+   * @param cl The classloader to use to find the resource
+   * @param resource The resource to open
+   * @return An input stream for the given named resource
+   * @throws FileNotFoundException
+   */
+
+  private static InputStream openResource(ClassLoader cl, String resource)
+      throws FileNotFoundException {
     InputStream is = cl.getResourceAsStream(resource.trim());
     if (is == null) {
       throw new FileNotFoundException("Can not locate resource: " + resource);
@@ -72,7 +93,7 @@ public final class ResourceLoader {
    */
   public static String getContent(String resource) throws IOException {
     InputStream is = openResource(resource);
-    try{
+    try {
       return IOUtils.toString(is, "UTF-8");
     } finally {
       IOUtils.closeQuietly(is);
@@ -86,7 +107,7 @@ public final class ResourceLoader {
    */
   public static String getContent(File file) throws IOException {
     InputStream is = new FileInputStream(file);
-    try{
+    try {
       return IOUtils.toString(is, "UTF-8");
     } finally {
       IOUtils.closeQuietly(is);