[jboss-cvs] jboss-seam/src/main/org/jboss/seam/util ...

Peter Muir peter at bleepbleep.org.uk
Wed Jan 24 13:54:41 EST 2007


  User: pmuir   
  Date: 07/01/24 13:54:41

  Modified:    src/main/org/jboss/seam/util  Resources.java
  Log:
  Add methods to get a resource as a URL
  
  Revision  Changes    Path
  1.6       +128 -69   jboss-seam/src/main/org/jboss/seam/util/Resources.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Resources.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/util/Resources.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- Resources.java	14 Nov 2006 00:15:02 -0000	1.5
  +++ Resources.java	24 Jan 2007 18:54:41 -0000	1.6
  @@ -1,6 +1,7 @@
   package org.jboss.seam.util;
   
   import java.io.InputStream;
  +import java.net.URL;
   
   import javax.faces.context.FacesContext;
   import javax.servlet.ServletContext;
  @@ -66,4 +67,62 @@
         return stream;
      }
   
  +   public static URL getResource(String resource) 
  +   {
  +      String stripped = resource.startsWith("/") ? 
  +               resource.substring(1) : resource;
  +      
  +         URL url = null; 
  +
  +         try
  +         {
  +            url = FacesContext.getCurrentInstance().getExternalContext()
  +                  .getResource(resource);
  +         }
  +         catch (Exception e) {}
  +         
  +         if (url==null)
  +         {
  +            url = getResource(resource, stripped);
  +         }
  +         
  +         return url;
  +   }
  +   
  +   public static URL getResource(String resource, ServletContext servletContext) {
  +      String stripped = resource.startsWith("/") ? 
  +            resource.substring(1) : resource;
  +   
  +      URL url  = null; 
  +
  +      try
  +      {
  +         url = servletContext.getResource(resource);
  +      }
  +      catch (Exception e) {}
  +      
  +      if (url==null)
  +      {
  +        url = getResource(resource, stripped);
  +      }
  +      
  +      return url;
  +   }
  +   
  +   private static URL  getResource(String resource, String stripped)
  +   {
  +      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  +      URL url = null;
  +      if (classLoader!=null) {
  +         url = classLoader.getResource(stripped);
  +      }
  +      if ( url == null ) {
  +        url = Seam.class.getResource(resource);
  +      }
  +      if ( url == null ) {
  +         url = Seam.class.getClassLoader().getResource(stripped);
  +      }
  +      return url;
  +   }
  +
   }
  
  
  



More information about the jboss-cvs-commits mailing list