You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2014/01/22 01:24:03 UTC

svn commit: r1560226 - in /myfaces/core/trunk: impl/src/main/java/org/apache/myfaces/application/ impl/src/main/java/org/apache/myfaces/resource/ shared/src/main/java/org/apache/myfaces/shared/resource/

Author: lu4242
Date: Wed Jan 22 00:24:03 2014
New Revision: 1560226

URL: http://svn.apache.org/r1560226
Log:
MYFACES-3846 Implement temporal resourcehandler cache for JSF 2.2 contracts

Added:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java
      - copied, changed from r1559611, myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java   (with props)
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/DefaultResourceHandlerSupport.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/DefaultResourceHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/DefaultResourceHandlerSupport.java?rev=1560226&r1=1560225&r2=1560226&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/DefaultResourceHandlerSupport.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/DefaultResourceHandlerSupport.java Wed Jan 22 00:24:03 2014
@@ -27,6 +27,7 @@ import org.apache.myfaces.resource.Faces
 
 import org.apache.myfaces.resource.InternalClassLoaderResourceLoader;
 import org.apache.myfaces.resource.RootExternalContextResourceLoader;
+import org.apache.myfaces.resource.TempDirFileCacheContractResourceLoader;
 import org.apache.myfaces.resource.TempDirFileCacheResourceLoader;
 import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
 import org.apache.myfaces.shared.resource.BaseResourceHandlerSupport;
@@ -146,24 +147,23 @@ public class DefaultResourceHandlerSuppo
                 throw new IllegalStateException("javax.faces.WEBAPP_CONTRACTS_DIRECTORY cannot start with '/");
             }
             
-            /* TODO: Implement me!
-            FacesContext facesContext = FacesContext.getCurrentInstance(); 
-
             if (TempDirFileCacheResourceLoader.isValidCreateTemporalFiles(facesContext))
             {
                 _contractResourceLoaders= new ContractResourceLoader[] { 
-                    new ExternalContextContractResourceLoader(CONTRACTS),
-                    new ClassLoaderContractResourceLoader(META_INF_CONTRACTS)
+                    new TempDirFileCacheContractResourceLoader(
+                        new ExternalContextContractResourceLoader("/"+directory)),
+                    new TempDirFileCacheContractResourceLoader(
+                        new ClassLoaderContractResourceLoader(META_INF_CONTRACTS))
                 };
             }
             else
-            {*/
+            {
             
                 _contractResourceLoaders= new ContractResourceLoader[] { 
                     new ExternalContextContractResourceLoader("/"+directory),
                     new ClassLoaderContractResourceLoader(META_INF_CONTRACTS)
                 };
-            //}
+            }
         }
         return _contractResourceLoaders;
     }

Copied: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java (from r1559611, myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java)
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java?p2=myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java&p1=myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java&r1=1559611&r2=1560226&rev=1560226&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheResourceLoader.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/resource/TempDirFileCacheContractResourceLoader.java Wed Jan 22 00:24:03 2014
@@ -34,9 +34,8 @@ import javax.faces.FacesException;
 import javax.faces.application.Resource;
 import javax.faces.context.FacesContext;
 import org.apache.myfaces.application.ResourceHandlerImpl;
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
-import org.apache.myfaces.shared.resource.ResourceLoader;
-import org.apache.myfaces.shared.resource.ResourceLoaderWrapper;
+import org.apache.myfaces.shared.resource.ContractResourceLoader;
+import org.apache.myfaces.shared.resource.ContractResourceLoaderWrapper;
 import org.apache.myfaces.shared.resource.ResourceMeta;
 import org.apache.myfaces.shared.util.WebConfigParamUtils;
 
@@ -47,20 +46,10 @@ import org.apache.myfaces.shared.util.We
  *
  * @author Leonardo Uribe
  */
-public class TempDirFileCacheResourceLoader extends ResourceLoaderWrapper
+public class TempDirFileCacheContractResourceLoader extends ContractResourceLoaderWrapper
 {
-    /**
-     * If this param is set to true (default false), a temporal directory is created and
-     * all files handled by this ResourceLoader are cached there, avoiding the problem
-     * described on MYFACES-3586. (Performance improvement in Resource loading - 
-     * HIGH CPU inflating bytes in ResourceHandlerImpl.handleResourceRequest).
-     */
-    @JSFWebConfigParam(since="2.1.11", expectedValues="true, false", defaultValue="false")
-    public final static String INIT_PARAM_TEMPORAL_RESOURCEHANDLER_CACHE_ENABLED = 
-        "org.apache.myfaces.TEMPORAL_RESOURCEHANDLER_CACHE_ENABLED";
-    public final static boolean INIT_PARAM_TEMPORAL_RESOURCEHANDLER_CACHE_ENABLED_DEFAULT = false;
     
-    public final static String TEMP_FILES_LOCK_MAP = "oam.rh.TEMP_FILES_LOCK_MAP";
+    public final static String TEMP_FILES_LOCK_MAP = "oam.rh.con.TEMP_FILES_LOCK_MAP";
     
     /**
      * Subdir of the ServletContext tmp dir to store temporal resources.
@@ -72,47 +61,18 @@ public class TempDirFileCacheResourceLoa
      */
     private static final String TEMP_FILE_SUFFIX = ".tmp";
     
-    private ResourceLoader delegate;
+    private final ContractResourceLoader delegate;
     
     private volatile File _tempDir;
     
     private int _resourceBufferSize = -1;
     
-    public TempDirFileCacheResourceLoader(ResourceLoader delegate)
+    public TempDirFileCacheContractResourceLoader(ContractResourceLoader delegate)
     {
         this.delegate = delegate;
         initialize();
     }
     
-    public static boolean isValidCreateTemporalFiles(FacesContext facesContext)
-    {
-        if (WebConfigParamUtils.getBooleanInitParameter(facesContext.getExternalContext(),
-            INIT_PARAM_TEMPORAL_RESOURCEHANDLER_CACHE_ENABLED,
-            INIT_PARAM_TEMPORAL_RESOURCEHANDLER_CACHE_ENABLED_DEFAULT))
-        {
-            // Try create a temporal folder to check if is valid to do so, otherwise, disable it.
-            try
-            {
-                Map<String, Object> applicationMap = facesContext.getExternalContext().getApplicationMap();
-                File tempdir = (File) applicationMap.get("javax.servlet.context.tempdir");
-                File imagesDir = new File(tempdir, TEMP_FOLDER_BASE_DIR);
-                if (!imagesDir.exists())
-                {
-                    imagesDir.mkdirs();
-                }
-                return true;
-            }
-            catch (Exception e)
-            {
-                return false;
-            }
-        }
-        else
-        {
-            return false;
-        }
-    }
-    
     protected void initialize()
     {
         //Get startup FacesContext
@@ -218,7 +178,7 @@ public class TempDirFileCacheResourceLoa
     @SuppressWarnings("unchecked")
     private File createOrGetTempFile(FacesContext facesContext, ResourceMeta resourceMeta)
     {
-        String identifier = resourceMeta.getResourceIdentifier();
+        String identifier = resourceMeta.getResourceIdentifier()+"_"+resourceMeta.getContractName();
         File file = getTemporalFile(resourceMeta);
         if (!file.exists())
         {
@@ -251,15 +211,10 @@ public class TempDirFileCacheResourceLoa
     
     private File getTemporalFile(ResourceMeta resourceMeta)
     {
-        return new File(_tempDir, resourceMeta.getResourceIdentifier() + TEMP_FILE_SUFFIX);
+        return new File(_tempDir, 
+            resourceMeta.getResourceIdentifier()+"_"+ resourceMeta.getContractName() + TEMP_FILE_SUFFIX);
     }
 
-    /*
-    private boolean couldResourceContainValueExpressions(ResourceMeta resourceMeta)
-    {
-        return resourceMeta.couldResourceContainValueExpressions() || resourceMeta.getResourceName().endsWith(".css");
-    }*/
-    
     protected void createTemporalFileVersion(FacesContext facesContext, ResourceMeta resourceMeta, File target)
     {
         target.mkdirs();  // ensure necessary directories exist
@@ -269,18 +224,7 @@ public class TempDirFileCacheResourceLoa
         FileOutputStream fileOutputStream;
         try
         {
-            /*
-            if (couldResourceContainValueExpressions(resourceMeta))
-            {
-                inputStream = new ValueExpressionFilterInputStream(
-                        getWrapped().getResourceInputStream(resourceMeta),
-                        resourceMeta.getLibraryName(), 
-                        resourceMeta.getResourceName());
-            }
-            else
-            {*/
-                inputStream = getWrapped().getResourceInputStream(resourceMeta);
-            /*}*/
+            inputStream = getWrapped().getResourceInputStream(resourceMeta);
             fileOutputStream = new FileOutputStream(target);
             byte[] buffer = new byte[this.getResourceBufferSize()];
 
@@ -340,7 +284,7 @@ public class TempDirFileCacheResourceLoa
         }
 
         public synchronized void createFile(FacesContext facesContext, 
-            ResourceMeta resourceMeta, File file, TempDirFileCacheResourceLoader loader)
+            ResourceMeta resourceMeta, File file, TempDirFileCacheContractResourceLoader loader)
         {
             if (!created)
             {
@@ -362,7 +306,7 @@ public class TempDirFileCacheResourceLoa
         return _resourceBufferSize;
     }
     
-    public ResourceLoader getWrapped()
+    public ContractResourceLoader getWrapped()
     {
         return delegate;
     }

Added: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java?rev=1560226&view=auto
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java (added)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java Wed Jan 22 00:24:03 2014
@@ -0,0 +1,132 @@
+/*
+ * 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.shared.resource;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Comparator;
+import javax.faces.FacesWrapper;
+
+/**
+ *
+ */
+public abstract class ContractResourceLoaderWrapper extends ContractResourceLoader
+    implements FacesWrapper<ContractResourceLoader>
+{
+
+    public ContractResourceLoaderWrapper()
+    {
+        super(null);
+    }
+
+    @Override
+    public String getResourceVersion(String path, String contractName)
+    {
+        return getWrapped().getResourceVersion(path, contractName);
+    }
+
+    @Override
+    public String getLibraryVersion(String path, String contractName)
+    {
+        return getWrapped().getLibraryVersion(path, contractName);
+    }
+
+    @Override
+    public ResourceMeta createResourceMeta(String prefix, String libraryName, 
+        String libraryVersion, String resourceName, String resourceVersion, String contractName)
+    {
+        return getWrapped().createResourceMeta(prefix, libraryName, 
+            libraryVersion, resourceName, resourceVersion, contractName);
+    }
+
+    @Override
+    public boolean libraryExists(String libraryName, String contractName)
+    {
+        return getWrapped().libraryExists(libraryName, contractName);
+    }
+
+    @Override
+    public URL getResourceURL(ResourceMeta resourceMeta)
+    {
+        return getWrapped().getResourceURL(resourceMeta);
+    }
+
+    @Override
+    public InputStream getResourceInputStream(ResourceMeta resourceMeta)
+    {
+        return getWrapped().getResourceInputStream(resourceMeta);
+    }
+
+    @Override
+    public boolean libraryExists(String libraryName)
+    {
+        return getWrapped().libraryExists(libraryName);
+    }
+
+    @Override
+    public ResourceMeta createResourceMeta(String prefix, String libraryName, 
+        String libraryVersion, String resourceName, String resourceVersion)
+    {
+        return getWrapped().createResourceMeta(prefix, libraryName, 
+            libraryVersion, resourceName, resourceVersion);
+    }
+
+    @Override
+    public String getLibraryVersion(String path)
+    {
+        return getWrapped().getLibraryVersion(path);
+    }
+
+    @Override
+    public String getResourceVersion(String path)
+    {
+        return getWrapped().getResourceVersion(path);
+    }
+
+    @Override
+    public void setPrefix(String prefix)
+    {
+        getWrapped().setPrefix(prefix);
+    }
+
+    @Override
+    public String getPrefix()
+    {
+        return getWrapped().getPrefix();
+    }
+
+    @Override
+    protected void setVersionComparator(Comparator<String> versionComparator)
+    {
+        getWrapped().setVersionComparator(versionComparator);
+    }
+
+    @Override
+    protected Comparator<String> getVersionComparator()
+    {
+        return getWrapped().getVersionComparator();
+    }
+
+    @Override
+    public boolean resourceExists(ResourceMeta resourceMeta)
+    {
+        return getWrapped().resourceExists(resourceMeta);
+    }
+
+}

Propchange: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ContractResourceLoaderWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native