myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r882702 - in /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces: application/ResourceHandlerImpl.java resource/ClassLoaderResourceLoader.java resource/ExternalContextResourceLoader.java resource/ResourceLoader.java
Date Fri, 20 Nov 2009 20:16:07 GMT
Author: lu4242
Date: Fri Nov 20 20:16:07 2009
New Revision: 882702

URL: http://svn.apache.org/viewvc?rev=882702&view=rev
Log:
MYFACES-2414 ResourceHandlerImpl should scan first with locale prefix and then without it

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ExternalContextResourceLoader.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java?rev=882702&r1=882701&r2=882702&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
(original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
Fri Nov 20 20:16:07 2009
@@ -24,6 +24,7 @@
 import java.net.URL;
 import java.util.Locale;
 import java.util.Map;
+import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -38,6 +39,7 @@
 import org.apache.myfaces.resource.ResourceImpl;
 import org.apache.myfaces.resource.ResourceLoader;
 import org.apache.myfaces.resource.ResourceMeta;
+import org.apache.myfaces.shared_impl.util.ClassUtils;
 import org.apache.myfaces.shared_impl.util.StringUtils;
 
 /**
@@ -111,61 +113,111 @@
         String localePrefix = getLocalePrefixForLocateResource();
         String resourceVersion = null;
         String libraryVersion = null;
-        ResourceMeta resourceId;
+        ResourceMeta resourceId = null;
+        
+        log.info("Derive Resource:"+localePrefix+" "+resourceName+" "+libraryName);
 
-        String prefix = "";
+        //1. Try to locate resource in a localized path
         if (localePrefix != null)
         {
-            prefix = localePrefix;
-        }
-        
-        boolean prependPrefix = !"".equals(prefix);
+            if (null != libraryName)
+            {
+                String pathToLib = localePrefix + '/' + libraryName;
+                libraryVersion = resourceLoader.getLibraryVersion(pathToLib);
 
-        if (null != libraryName)
-        {
-            String pathToLib = prependPrefix ? prefix + '/' + libraryName
-                    : libraryName;
-            libraryVersion = resourceLoader.getLibraryVersion(pathToLib);
+                if (null != libraryVersion)
+                {
+                    String pathToResource = localePrefix + '/'
+                            + libraryName + '/' + libraryVersion + '/'
+                            + resourceName;
+                    resourceVersion = resourceLoader
+                            .getResourceVersion(pathToResource);
+                }
+                else
+                {
+                    String pathToResource = localePrefix + '/'
+                            + libraryName + '/' + resourceName;
+                    resourceVersion = resourceLoader
+                            .getResourceVersion(pathToResource);
+                }
 
-            if (null != libraryVersion)
-            {
-                String pathToResource = (prependPrefix ? prefix + '/'
-                        + libraryName + '/' + libraryVersion + '/'
-                        + resourceName : libraryName + '/' + libraryVersion
-                        + '/' + resourceName);
-                resourceVersion = resourceLoader
-                        .getResourceVersion(pathToResource);
+                if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion)))
+                {
+                    resourceId = resourceLoader.createResourceMeta(localePrefix, libraryName,
+                            libraryVersion, resourceName, resourceVersion);
+                }
             }
             else
             {
-                String pathToResource = (prependPrefix ? prefix + '/'
-                        + libraryName + '/' + resourceName : libraryName + '/'
-                        + resourceName);
                 resourceVersion = resourceLoader
-                        .getResourceVersion(pathToResource);
+                        .getResourceVersion(localePrefix + '/'+ resourceName);
+                if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion)))
+                {               
+                    resourceId = resourceLoader.createResourceMeta(localePrefix, null, null,
+                            resourceName, resourceVersion);
+                }
             }
 
-            resourceId = resourceLoader.createResourceMeta(prefix, libraryName,
-                    libraryVersion, resourceName, resourceVersion);
+            if (resourceId != null)
+            {
+                URL url = resourceLoader.getResourceURL(resourceId);
+                if (url == null)
+                {
+                    resourceId = null;
+                }
+            }            
         }
-        else
+        
+        //2. Try to localize resource in a non localized path
+        if (resourceId == null)
         {
-            resourceVersion = resourceLoader
-                    .getResourceVersion(prependPrefix ? prefix + '/'
-                            + resourceName : resourceName);
-            resourceId = resourceLoader.createResourceMeta(prefix, null, null,
-                    resourceName, resourceVersion);
-        }
+            if (null != libraryName)
+            {
+                libraryVersion = resourceLoader.getLibraryVersion(libraryName);
 
-        if (resourceId != null)
-        {
-            URL url = resourceLoader.getResourceURL(resourceId);
-            if (url == null)
+                if (null != libraryVersion)
+                {
+                    String pathToResource = (libraryName + '/' + libraryVersion
+                            + '/' + resourceName);
+                    resourceVersion = resourceLoader
+                            .getResourceVersion(pathToResource);
+                }
+                else
+                {
+                    String pathToResource = (libraryName + '/'
+                            + resourceName);
+                    resourceVersion = resourceLoader
+                            .getResourceVersion(pathToResource);
+                }
+
+                if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion)))
+                {               
+                    resourceId = resourceLoader.createResourceMeta(null, libraryName,
+                            libraryVersion, resourceName, resourceVersion);
+                }
+            }
+            else
             {
-                resourceId = null;
+                resourceVersion = resourceLoader
+                        .getResourceVersion(resourceName);
+                
+                if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion)))
+                {               
+                    resourceId = resourceLoader.createResourceMeta(null, null, null,
+                            resourceName, resourceVersion);
+                }
             }
-        }
 
+            if (resourceId != null)
+            {
+                URL url = resourceLoader.getResourceURL(resourceId);
+                if (url == null)
+                {
+                    resourceId = null;
+                }
+            }            
+        }
+        
         return resourceId;
     }
 
@@ -386,12 +438,44 @@
                     .calculateLocale(context);
 
             ResourceBundle bundle = ResourceBundle
-                    .getBundle(bundleName, locale);
+                    .getBundle(bundleName, locale, ClassUtils.getContextClassLoader());
 
-            localePrefix = bundle.getString(ResourceHandler.LOCALE_PREFIX);
+            if (bundle != null)
+            {
+                localePrefix = bundle.getString(ResourceHandler.LOCALE_PREFIX);         
      
+            }
         }
         return localePrefix;
     }
+    
+    private static ResourceBundle getBundle(FacesContext facesContext, Locale locale, String
bundleName)
+    {
+        try
+        {
+            // First we try the JSF implementation class loader
+            return ResourceBundle.getBundle(bundleName, locale, facesContext.getClass().getClassLoader());
+        }
+        catch (MissingResourceException ignore1)
+        {
+            try
+            {
+                // Next we try the JSF API class loader
+                return ResourceBundle.getBundle(bundleName, locale, ResourceHandlerImpl.class.getClassLoader());
+            }
+            catch (MissingResourceException ignore2)
+            {
+                try
+                {
+                    // Last resort is the context class loader
+                    return ResourceBundle.getBundle(bundleName, locale, ClassUtils.getContextClassLoader());
+                }
+                catch (MissingResourceException damned)
+                {
+                    return null;
+                }
+            }
+        }
+    }
 
     protected boolean isResourceIdentifierExcluded(FacesContext context,
             String resourceIdentifier)
@@ -426,23 +510,32 @@
         String localePrefix = getLocalePrefixForLocateResource();
 
         String pathToLib = null;
+        
         if (localePrefix != null)
         {
+            //Check with locale
             pathToLib = localePrefix + '/' + libraryName;
-        }
-        else
-        {
-            pathToLib = libraryName;
+            
+            for (ResourceLoader loader : getResourceHandlerSupport()
+                    .getResourceLoaders())
+            {
+                if (loader.libraryExists(pathToLib))
+                {
+                    return true;
+                }
+            }            
         }
 
+        //Check without locale
         for (ResourceLoader loader : getResourceHandlerSupport()
                 .getResourceLoaders())
         {
-            if (loader.libraryExists(pathToLib))
+            if (loader.libraryExists(libraryName))
             {
                 return true;
             }
         }
+
         return false;
     }
 

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java?rev=882702&r1=882701&r2=882702&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
(original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
Fri Nov 20 20:16:07 2009
@@ -28,6 +28,8 @@
 import java.util.Enumeration;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.regex.Pattern;
 
 import org.apache.myfaces.shared_impl.util.ClassUtils;
@@ -136,6 +138,11 @@
             {
                 // Just return null, because library version cannot be
                 // resolved.
+                Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());

+                if (log.isLoggable(Level.WARNING))
+                {
+                    log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to
uri: "+e.getMessage(), e);
+                }
             }
         }
         else if (isJarResourceProtocol(url.getProtocol()))
@@ -222,6 +229,11 @@
             {
                 // Just return null, because library version cannot be
                 // resolved.
+                Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());

+                if (log.isLoggable(Level.WARNING))
+                {
+                    log.log(Level.WARNING, "IOException when scanning for resource in jar
file:", e);
+                }
             }
         }
         return libraryVersion;
@@ -290,11 +302,20 @@
                             resourceVersion = version;
                         }
                     }
+                    //Since it is a directory and no version found set resourceVersion as
invalid
+                    if (resourceVersion == null)
+                    {
+                        resourceVersion = VERSION_INVALID;
+                    }
                 }
             }
             catch (URISyntaxException e)
             {
-                e.printStackTrace();
+                Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());

+                if (log.isLoggable(Level.WARNING))
+                {
+                    log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to
uri: "+e.getMessage(), e);
+                }
             }
         }
         else if (isJarResourceProtocol(url.getProtocol()))
@@ -343,6 +364,10 @@
                                     }
                                 }
                             }
+                            if (resourceVersion == null)
+                            {
+                                resourceVersion = VERSION_INVALID;
+                            }
                         }
                     }
                     finally
@@ -367,6 +392,11 @@
             {
                 // Just return null, because library version cannot be
                 // resolved.
+                Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());

+                if (log.isLoggable(Level.WARNING))
+                {
+                    log.log(Level.WARNING, "IOException when scanning for resource in jar
file:", e);
+                }
             }
         }
         return resourceVersion;
@@ -395,7 +425,7 @@
     {
         if (getPrefix() != null && !"".equals(getPrefix()))
         {
-            URL url = getClassLoader().getResource(libraryName);
+            URL url = getClassLoader().getResource(getPrefix() + '/' + libraryName);
             if (url != null)
             {
                 return true;
@@ -403,7 +433,7 @@
         }
         else
         {
-            URL url = getClassLoader().getResource(getPrefix() + '/' + libraryName);
+            URL url = getClassLoader().getResource(libraryName);
             if (url != null)
             {
                 return true;

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ExternalContextResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ExternalContextResourceLoader.java?rev=882702&r1=882701&r2=882702&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ExternalContextResourceLoader.java
(original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ExternalContextResourceLoader.java
Fri Nov 20 20:16:07 2009
@@ -91,6 +91,11 @@
                     }
                 }
             }
+            //Since it is a directory and no version was found, set as invalid
+            if (resourceVersion == null)
+            {
+                resourceVersion = VERSION_INVALID;
+            }
         }
         return resourceVersion;
     }
@@ -166,7 +171,9 @@
         {
             try
             {
-                URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(libraryName);
+                URL url =
+                    FacesContext.getCurrentInstance().getExternalContext().getResource(
+                        getPrefix() + '/' + libraryName);
                 if (url != null)
                 {
                     return true;
@@ -181,9 +188,9 @@
         {
             try
             {
-                URL url =
-                        FacesContext.getCurrentInstance().getExternalContext().getResource(
-                            getPrefix() + '/' + libraryName);
+
+                URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(libraryName);
+
                 if (url != null)
                 {
                     return true;

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java?rev=882702&r1=882701&r2=882702&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
(original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/ResourceLoader.java
Fri Nov 20 20:16:07 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.myfaces.resource;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Comparator;
@@ -32,6 +33,8 @@
 public abstract class ResourceLoader
 {
     
+    public static final String VERSION_INVALID = "INVALID";
+    
     private String _prefix;
     
     public ResourceLoader(String prefix)



Mime
View raw message