myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r1178222 - in /myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler: ./ resource/
Date Sun, 02 Oct 2011 19:02:39 GMT
Author: lu4242
Date: Sun Oct  2 19:02:38 2011
New Revision: 1178222

URL: http://svn.apache.org/viewvc?rev=1178222&view=rev
Log:
MFCOMMONS-38 Resource ending with .css is not processed for value expressions when gzip compression
is enabled and cache disk is true

Added:
    myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ValueExpressionFilterInputStream.java
Modified:
    myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedDefaultResourceHandlerSupport.java
    myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedResourceImpl.java
    myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/GZIPResourceLoader.java
    myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ResourceImpl.java

Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedDefaultResourceHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedDefaultResourceHandlerSupport.java?rev=1178222&r1=1178221&r2=1178222&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedDefaultResourceHandlerSupport.java
(original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedDefaultResourceHandlerSupport.java
Sun Oct  2 19:02:38 2011
@@ -140,6 +140,10 @@ public class ExtendedDefaultResourceHand
             createWebConfigProvider(context);
         
         _webConfigProvider.init(context);
+        
+        // GZIPResourceLoader does some file operations to clear the cache, so this call
must be done
+        // to ensure only one thread is cleaning it up (at setup time)
+        getResourceLoaders();
     }
     
     public MyFacesResourcesConfig getMyFacesResourcesConfig()
@@ -303,6 +307,10 @@ public class ExtendedDefaultResourceHand
                     mapping = calculatedMapping;
                 }
             }
+            else
+            {
+                mapping = calculatedMapping;
+            }
 
             attributes.put(CACHED_SERVLET_MAPPING, mapping);
         }
@@ -394,7 +402,7 @@ public class ExtendedDefaultResourceHand
             //   - ProjectStage != Development
             //   - a compressed version is available (created in constructor)
             //   - the user agent supports compresssion
-            if (!_developmentStage && isGzipResourcesEnabled() && isCacheDiskGzipResources())
+            if (/*!_developmentStage && */isGzipResourcesEnabled() && isCacheDiskGzipResources())
             {
                 _resourceLoaders = new ResourceLoader[] {
                         new GZIPResourceLoader(new ExtendedResourceLoaderWrapper(new ExternalContextResourceLoader("/resources")),
this),

Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedResourceImpl.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedResourceImpl.java?rev=1178222&r1=1178221&r2=1178222&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedResourceImpl.java
(original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/ExtendedResourceImpl.java
Sun Oct  2 19:02:38 2011
@@ -18,6 +18,8 @@
  */
 package org.apache.myfaces.commons.resourcehandler;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Locale;
@@ -34,6 +36,7 @@ import javax.faces.context.FacesContext;
 import org.apache.myfaces.commons.resourcehandler.config.element.Library;
 import org.apache.myfaces.commons.resourcehandler.resource.ResourceImpl;
 import org.apache.myfaces.commons.resourcehandler.resource.ResourceLoader;
+import org.apache.myfaces.commons.resourcehandler.resource.ValueExpressionFilterInputStream;
 
 /**
  * 
@@ -61,6 +64,23 @@ public class ExtendedResourceImpl extend
             setLibraryName(expectedLibraryName);
         }
     }
+    
+    @Override
+    public InputStream getInputStream() throws IOException
+    {
+        if ( getExtendedDefaultResourceHandlerSupport().isGzipResourcesEnabled() &&
+             getExtendedDefaultResourceHandlerSupport().isCacheDiskGzipResources() &&
+             getExtendedDefaultResourceHandlerSupport().isCompressable(this) &&
+             getExtendedDefaultResourceHandlerSupport().userAgentSupportsCompression(FacesContext.getCurrentInstance()))
+        {
+            //GZIPResourceLoader will take care of resources containing ValueExpressions

+            return getResourceLoader().getResourceInputStream(getResourceMeta());
+        }
+        else
+        {
+            return super.getInputStream();
+        }
+    }
 
     @Override
     public String getRequestPath()

Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/GZIPResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/GZIPResourceLoader.java?rev=1178222&r1=1178221&r2=1178222&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/GZIPResourceLoader.java
(original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/GZIPResourceLoader.java
Sun Oct  2 19:02:38 2011
@@ -38,6 +38,7 @@ import javax.faces.context.FacesContext;
 import org.apache.myfaces.commons.resourcehandler.resource.ResourceLoader;
 import org.apache.myfaces.commons.resourcehandler.resource.ResourceLoaderWrapper;
 import org.apache.myfaces.commons.resourcehandler.resource.ResourceMeta;
+import org.apache.myfaces.commons.resourcehandler.resource.ValueExpressionFilterInputStream;
 
 /**
  * 
@@ -91,12 +92,35 @@ public class GZIPResourceLoader extends 
         {
             imagesDir.mkdirs();
         }
+        else
+        {
+            //Clear the cache
+            deleteDir(imagesDir);
+            imagesDir.mkdirs();
+        }
         _tempDir = imagesDir;
 
         //2. Create map for register compressed resources
         Map<String, FileProducer> compressedFilesMap = new ConcurrentHashMap<String,
FileProducer>();
         facesContext.getExternalContext().getApplicationMap().put(COMPRESSED_FILES_MAP, compressedFilesMap);
-    }    
+    }
+
+    private static boolean deleteDir(File dir)
+    {
+        if (dir.isDirectory())
+        {
+            String[] children = dir.list();
+            for (int i = 0; i < children.length; i++)
+            {
+                boolean success = deleteDir(new File(dir, children[i]));
+                if (!success)
+                {
+                    return false;
+                }
+            }
+        }
+        return dir.delete();
+    }
     
     @Override
     public URL getResourceURL(ResourceMeta resourceMeta)
@@ -165,7 +189,7 @@ public class GZIPResourceLoader extends 
     @SuppressWarnings("unchecked")
     private File createOrGetCompressedFile(FacesContext facesContext, ResourceMeta resourceMeta)
     {
-        String identifier = resourceMeta.toString();
+        String identifier = resourceMeta.getResourceIdentifier();
         File file = getCompressedFile(resourceMeta);
         if (!file.exists())
         {
@@ -198,9 +222,15 @@ public class GZIPResourceLoader extends 
     
     private File getCompressedFile(ResourceMeta resourceMeta)
     {
-        return new File(_tempDir, COMPRESSION_BASE_DIR + resourceMeta.toString() + COMPRESSED_FILE_SUFFIX);
+        return new File(_tempDir, resourceMeta.getResourceIdentifier() + COMPRESSED_FILE_SUFFIX);
     }
 
+    private boolean couldResourceContainValueExpressions(ResourceMeta resourceMeta)
+    {
+        return resourceMeta.couldResourceContainValueExpressions() || resourceMeta.getResourceName().endsWith(".css");
+    }
+    
+    
     /**
      * Uses GZIPOutputStream to compress this resource.
      * It will be stored where getCompressedFile() points to.
@@ -220,7 +250,17 @@ public class GZIPResourceLoader extends 
         GZIPOutputStream gzipOutputStream = null;
         try
         {
-            inputStream = getWrapped().getResourceInputStream(resourceMeta);
+            if (couldResourceContainValueExpressions(resourceMeta))
+            {
+                inputStream = new ValueExpressionFilterInputStream(
+                        getWrapped().getResourceInputStream(resourceMeta),
+                        resourceMeta.getLibraryName(), 
+                        resourceMeta.getResourceName());
+            }
+            else
+            {
+                inputStream = getWrapped().getResourceInputStream(resourceMeta);
+            }
             fileOutputStream = new FileOutputStream(target);
             gzipOutputStream = new GZIPOutputStream(fileOutputStream);
             byte[] buffer = new byte[BUFFER_SIZE];

Modified: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ResourceImpl.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ResourceImpl.java?rev=1178222&r1=1178221&r2=1178222&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ResourceImpl.java
(original)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ResourceImpl.java
Sun Oct  2 19:02:38 2011
@@ -78,7 +78,7 @@ public class ResourceImpl extends Resour
         if (couldResourceContainValueExpressions())
         {
             return new ValueExpressionFilterInputStream(
-                    getResourceLoader().getResourceInputStream(_resourceMeta)); 
+                    getResourceLoader().getResourceInputStream(_resourceMeta), getLibraryName(),
getResourceName()); 
         }
         else
         {
@@ -101,119 +101,6 @@ public class ResourceImpl extends Resour
         }
     }
 
-    private class ValueExpressionFilterInputStream extends InputStream
-    {
-        private PushbackInputStream delegate;
-        
-        public ValueExpressionFilterInputStream(InputStream in)
-        {
-            super();
-            delegate = new PushbackInputStream(in,255);
-        }
-
-        @Override
-        public int read() throws IOException
-        {
-            int c1 = delegate.read();
-            
-            if (c1 == -1) return -1;
-            
-            if ( ((char)c1) == '#')
-            {
-                int c2 = delegate.read();
-                if (c2 == -1) return -1;
-                if (((char)c2) == '{')
-                {
-                    //It is a value expression. We need
-                    //to look for a occurrence of } to 
-                    //extract the expression and evaluate it,
-                    //the result should be unread.
-                    List<Integer> expressionList = new ArrayList<Integer>();
-                    int c3 = delegate.read();
-                    while ( c3 != -1 && ((char)c3) != '}' )
-                    {
-                        expressionList.add(c3);
-                        c3 = delegate.read();
-                    }
-                    
-                    if (c3 == -1)
-                    {
-                        //get back the data, because we can't
-                        //extract any value expression
-                        for (int i = 0; i < expressionList.size(); i++)
-                        {
-                            delegate.unread(expressionList.get(i));
-                        }
-                        delegate.unread(c2);
-                        return c1;
-                    }
-                    else
-                    {
-                        //EL expression found. Evaluate it and pushback
-                        //the result into the stream
-                        FacesContext context = FacesContext.getCurrentInstance();
-                        ELContext elContext = context.getELContext();
-                        try
-                        {
-                            ValueExpression ve = context.getApplication().
-                                getExpressionFactory().createValueExpression(
-                                        elContext,
-                                        "#{"+convertToExpression(expressionList)+"}",
-                                        String.class);
-                            String value = (String) ve.getValue(elContext);
-                            
-                            for (int i = value.length()-1; i >= 0 ; i--)
-                            {
-                                delegate.unread((int) value.charAt(i));
-                            }
-                        }
-                        catch(ELException e)
-                        {
-                            ExceptionQueuedEventContext equecontext = new ExceptionQueuedEventContext
(context, e, null);
-                            context.getApplication().publishEvent (context, ExceptionQueuedEvent.class,
equecontext);
-                            
-                            Logger log = Logger.getLogger(ResourceImpl.class.getName());
-                            if (log.isLoggable(Level.SEVERE))
-                                log.severe("Cannot evaluate EL expression "+convertToExpression(expressionList)+
" in resource " + getLibraryName()+":"+getResourceName());
-                            
-                            delegate.unread(c3);
-                            for (int i = expressionList.size()-1; i >= 0; i--)
-                            {
-                                delegate.unread(expressionList.get(i));
-                            }
-                            delegate.unread(c2);
-                            return c1;
-                        }
-                        
-                        //read again
-                        return delegate.read();
-                    }
-                }
-                else
-                {
-                    delegate.unread(c2);
-                    return c1;
-                }
-            }
-            else
-            {
-                //just continue
-                return c1;
-            }
-        }
-        
-        private String convertToExpression(List<Integer> expressionList)
-        {
-            char[] exprArray = new char[expressionList.size()];
-            
-            for (int i = 0; i < expressionList.size(); i++)
-            {
-                exprArray[i] = (char) expressionList.get(i).intValue();
-            }
-            return String.valueOf(exprArray);
-        }
-    }
-
     @Override
     public String getRequestPath()
     {

Added: myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ValueExpressionFilterInputStream.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ValueExpressionFilterInputStream.java?rev=1178222&view=auto
==============================================================================
--- myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ValueExpressionFilterInputStream.java
(added)
+++ myfaces/commons/branches/jsf_20/myfaces-commons-resourcehandler/src/main/java/org/apache/myfaces/commons/resourcehandler/resource/ValueExpressionFilterInputStream.java
Sun Oct  2 19:02:38 2011
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.commons.resourcehandler.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+import javax.faces.event.ExceptionQueuedEvent;
+import javax.faces.event.ExceptionQueuedEventContext;
+
+public class ValueExpressionFilterInputStream extends InputStream
+{
+    private PushbackInputStream delegate;
+    private String libraryName;
+    private String resourceName;
+    
+    public ValueExpressionFilterInputStream(InputStream in, String libraryName, String resourceName)
+    {
+        super();
+        delegate = new PushbackInputStream(in,255);
+    }
+
+    @Override
+    public int read() throws IOException
+    {
+        int c1 = delegate.read();
+        
+        if (c1 == -1) return -1;
+        
+        if ( ((char)c1) == '#')
+        {
+            int c2 = delegate.read();
+            if (c2 == -1) return -1;
+            if (((char)c2) == '{')
+            {
+                //It is a value expression. We need
+                //to look for a occurrence of } to 
+                //extract the expression and evaluate it,
+                //the result should be unread.
+                List<Integer> expressionList = new ArrayList<Integer>();
+                int c3 = delegate.read();
+                while ( c3 != -1 && ((char)c3) != '}' )
+                {
+                    expressionList.add(c3);
+                    c3 = delegate.read();
+                }
+                
+                if (c3 == -1)
+                {
+                    //get back the data, because we can't
+                    //extract any value expression
+                    for (int i = 0; i < expressionList.size(); i++)
+                    {
+                        delegate.unread(expressionList.get(i));
+                    }
+                    delegate.unread(c2);
+                    return c1;
+                }
+                else
+                {
+                    //EL expression found. Evaluate it and pushback
+                    //the result into the stream
+                    FacesContext context = FacesContext.getCurrentInstance();
+                    ELContext elContext = context.getELContext();
+                    try
+                    {
+                        ValueExpression ve = context.getApplication().
+                            getExpressionFactory().createValueExpression(
+                                    elContext,
+                                    "#{"+convertToExpression(expressionList)+"}",
+                                    String.class);
+                        String value = (String) ve.getValue(elContext);
+                        
+                        for (int i = value.length()-1; i >= 0 ; i--)
+                        {
+                            delegate.unread((int) value.charAt(i));
+                        }
+                    }
+                    catch(ELException e)
+                    {
+                        ExceptionQueuedEventContext equecontext = new ExceptionQueuedEventContext
(context, e, null);
+                        context.getApplication().publishEvent (context, ExceptionQueuedEvent.class,
equecontext);
+                        
+                        Logger log = Logger.getLogger(ResourceImpl.class.getName());
+                        if (log.isLoggable(Level.SEVERE))
+                            log.severe("Cannot evaluate EL expression "+convertToExpression(expressionList)+
" in resource " + (libraryName == null ? "" : libraryName)+":"+resourceName);
+                        
+                        delegate.unread(c3);
+                        for (int i = expressionList.size()-1; i >= 0; i--)
+                        {
+                            delegate.unread(expressionList.get(i));
+                        }
+                        delegate.unread(c2);
+                        return c1;
+                    }
+                    
+                    //read again
+                    return delegate.read();
+                }
+            }
+            else
+            {
+                delegate.unread(c2);
+                return c1;
+            }
+        }
+        else
+        {
+            //just continue
+            return c1;
+        }
+    }
+    
+    private String convertToExpression(List<Integer> expressionList)
+    {
+        char[] exprArray = new char[expressionList.size()];
+        
+        for (int i = 0; i < expressionList.size(); i++)
+        {
+            exprArray[i] = (char) expressionList.get(i).intValue();
+        }
+        return String.valueOf(exprArray);
+    }
+}
\ No newline at end of file



Mime
View raw message