You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2012/11/28 15:07:37 UTC
svn commit: r1414697 [2/3] - in
/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources:
./ deprecated/
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ExternalContextResourceLoader.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ExternalContextResourceLoader.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ExternalContextResourceLoader.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ExternalContextResourceLoader.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ExternalContextResourceLoader.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ExternalContextResourceLoader.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ExternalContextResourceLoader.java Wed Nov 28 14:07:34 2012
@@ -1,205 +1,205 @@
-/*
- * 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.extensions.scripting.jsf.resources;
-
-import javax.faces.context.FacesContext;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.regex.Pattern;
-
-/**
- * A resource loader implementation which loads resources from the webapp root. It uses the methods on ExternalContext
- * for handle resources.
- *
- */
-public class ExternalContextResourceLoader extends ResourceLoader
-{
- /**
- * It checks version like this: /1/, /1_0/, /1_0_0/, /100_100/
- *
- * Used on getLibraryVersion to filter resource directories
- **/
- protected static Pattern VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*/");
-
- /**
- * It checks version like this: /1.js, /1_0.js, /1_0_0.js, /100_100.js
- *
- * Used on getResourceVersion to filter resources
- **/
- protected static Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*");
-
- public ExternalContextResourceLoader(String prefix)
- {
- super(prefix);
- }
-
- protected Set<String> getResourcePaths(String path)
- {
- return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(getPrefix() + '/' + path);
- }
-
- @Override
- public String getResourceVersion(String path)
- {
- String resourceVersion = null;
- Set<String> resourcePaths = this.getResourcePaths(path);
- if (getPrefix() != null)
- path = getPrefix() + '/' + path;
-
- if (null != resourcePaths && !resourcePaths.isEmpty())
- {
- // resourceVersion = // execute the comment
- // Look in the resourcePaths for versioned resources.
- // If one or more versioned resources are found, take
- // the one with the "highest" version number as the value
- // of resourceVersion. If no versioned libraries
- // are found, let resourceVersion remain null.
- for (String resourcePath : resourcePaths)
- {
- String version = resourcePath.substring(path.length());
-
- if (RESOURCE_VERSION_CHECKER.matcher(version).matches())
- {
- version = version.substring(1, version.lastIndexOf('.'));
- if (resourceVersion == null)
- {
- resourceVersion = version;
- }
- else if (getVersionComparator().compare(resourceVersion, version) < 0)
- {
- resourceVersion = version;
- }
- }
- }
- //Since it is a directory and no version was found, set as invalid
- if (resourceVersion == null)
- {
- resourceVersion = VERSION_INVALID;
- }
- }
- return resourceVersion;
- }
-
- @Override
- public String getLibraryVersion(String path)
- {
- String libraryVersion = null;
- Set<String> libraryPaths = this.getResourcePaths(path);
- path = getPrefix() + '/' + path;
- if (null != libraryPaths && !libraryPaths.isEmpty())
- {
- // Look in the libraryPaths for versioned libraries.
- // If one or more versioned libraries are found, take
- // the one with the "highest" version number as the value
- // of libraryVersion. If no versioned libraries
- // are found, let libraryVersion remain null.
-
- for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();)
- {
- String libraryPath = it.next();
- String version = libraryPath.substring(path.length());
-
- if (VERSION_CHECKER.matcher(version).matches())
- {
- version = version.substring(1, version.length() - 1);
- if (libraryVersion == null)
- {
- libraryVersion = version;
- }
- else if (getVersionComparator().compare(libraryVersion, version) < 0)
- {
- libraryVersion = version;
- }
- }
- }
- }
- return libraryVersion;
- }
-
- @Override
- public URL getResourceURL(ResourceMeta resourceMeta)
- {
- try
- {
- return FacesContext.getCurrentInstance().getExternalContext().getResource(
- getPrefix() + '/' + resourceMeta.getResourceIdentifier());
- }
- catch (MalformedURLException e)
- {
- return null;
- }
- }
-
- @Override
- public InputStream getResourceInputStream(ResourceMeta resourceMeta)
- {
- return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
- getPrefix() + '/' + resourceMeta.getResourceIdentifier());
- }
-
- @Override
- public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
- String resourceName, String resourceVersion)
- {
- return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
- }
-
- @Override
- public boolean libraryExists(String libraryName)
- {
- if (getPrefix() != null && !"".equals(getPrefix()))
- {
- try
- {
- URL url =
- FacesContext.getCurrentInstance().getExternalContext().getResource(
- getPrefix() + '/' + libraryName);
- if (url != null)
- {
- return true;
- }
- }
- catch (MalformedURLException e)
- {
- return false;
- }
- }
- else
- {
- try
- {
-
- URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(libraryName);
-
- if (url != null)
- {
- return true;
- }
- }
- catch (MalformedURLException e)
- {
- return false;
- }
- }
- return false;
- }
-}
+/*
+ * 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.extensions.scripting.jsf.resources.deprecated;
+
+import javax.faces.context.FacesContext;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * A resource loader implementation which loads resources from the webapp root. It uses the methods on ExternalContext
+ * for handle resources.
+ *
+ */
+public class ExternalContextResourceLoader extends ResourceLoader
+{
+ /**
+ * It checks version like this: /1/, /1_0/, /1_0_0/, /100_100/
+ *
+ * Used on getLibraryVersion to filter resource directories
+ **/
+ protected static Pattern VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*/");
+
+ /**
+ * It checks version like this: /1.js, /1_0.js, /1_0_0.js, /100_100.js
+ *
+ * Used on getResourceVersion to filter resources
+ **/
+ protected static Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*");
+
+ public ExternalContextResourceLoader(String prefix)
+ {
+ super(prefix);
+ }
+
+ protected Set<String> getResourcePaths(String path)
+ {
+ return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(getPrefix() + '/' + path);
+ }
+
+ @Override
+ public String getResourceVersion(String path)
+ {
+ String resourceVersion = null;
+ Set<String> resourcePaths = this.getResourcePaths(path);
+ if (getPrefix() != null)
+ path = getPrefix() + '/' + path;
+
+ if (null != resourcePaths && !resourcePaths.isEmpty())
+ {
+ // resourceVersion = // execute the comment
+ // Look in the resourcePaths for versioned resources.
+ // If one or more versioned resources are found, take
+ // the one with the "highest" version number as the value
+ // of resourceVersion. If no versioned libraries
+ // are found, let resourceVersion remain null.
+ for (String resourcePath : resourcePaths)
+ {
+ String version = resourcePath.substring(path.length());
+
+ if (RESOURCE_VERSION_CHECKER.matcher(version).matches())
+ {
+ version = version.substring(1, version.lastIndexOf('.'));
+ if (resourceVersion == null)
+ {
+ resourceVersion = version;
+ }
+ else if (getVersionComparator().compare(resourceVersion, version) < 0)
+ {
+ resourceVersion = version;
+ }
+ }
+ }
+ //Since it is a directory and no version was found, set as invalid
+ if (resourceVersion == null)
+ {
+ resourceVersion = VERSION_INVALID;
+ }
+ }
+ return resourceVersion;
+ }
+
+ @Override
+ public String getLibraryVersion(String path)
+ {
+ String libraryVersion = null;
+ Set<String> libraryPaths = this.getResourcePaths(path);
+ path = getPrefix() + '/' + path;
+ if (null != libraryPaths && !libraryPaths.isEmpty())
+ {
+ // Look in the libraryPaths for versioned libraries.
+ // If one or more versioned libraries are found, take
+ // the one with the "highest" version number as the value
+ // of libraryVersion. If no versioned libraries
+ // are found, let libraryVersion remain null.
+
+ for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();)
+ {
+ String libraryPath = it.next();
+ String version = libraryPath.substring(path.length());
+
+ if (VERSION_CHECKER.matcher(version).matches())
+ {
+ version = version.substring(1, version.length() - 1);
+ if (libraryVersion == null)
+ {
+ libraryVersion = version;
+ }
+ else if (getVersionComparator().compare(libraryVersion, version) < 0)
+ {
+ libraryVersion = version;
+ }
+ }
+ }
+ }
+ return libraryVersion;
+ }
+
+ @Override
+ public URL getResourceURL(ResourceMeta resourceMeta)
+ {
+ try
+ {
+ return FacesContext.getCurrentInstance().getExternalContext().getResource(
+ getPrefix() + '/' + resourceMeta.getResourceIdentifier());
+ }
+ catch (MalformedURLException e)
+ {
+ return null;
+ }
+ }
+
+ @Override
+ public InputStream getResourceInputStream(ResourceMeta resourceMeta)
+ {
+ return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
+ getPrefix() + '/' + resourceMeta.getResourceIdentifier());
+ }
+
+ @Override
+ public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
+ String resourceName, String resourceVersion)
+ {
+ return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
+ }
+
+ @Override
+ public boolean libraryExists(String libraryName)
+ {
+ if (getPrefix() != null && !"".equals(getPrefix()))
+ {
+ try
+ {
+ URL url =
+ FacesContext.getCurrentInstance().getExternalContext().getResource(
+ getPrefix() + '/' + libraryName);
+ if (url != null)
+ {
+ return true;
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ return false;
+ }
+ }
+ else
+ {
+ try
+ {
+
+ URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(libraryName);
+
+ if (url != null)
+ {
+ return true;
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ return false;
+ }
+ }
+ return false;
+ }
+}
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/FacesServletMapping.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/FacesServletMapping.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/FacesServletMapping.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/FacesServletMapping.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/FacesServletMapping.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/FacesServletMapping.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/FacesServletMapping.java Wed Nov 28 14:07:34 2012
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.myfaces.extensions.scripting.jsf.resources;
+package org.apache.myfaces.extensions.scripting.jsf.resources.deprecated;
/**
* Represents a mapping entry of the FacesServlet in the web.xml
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerCache.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerCache.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerCache.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerCache.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerCache.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerCache.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerCache.java Wed Nov 28 14:07:34 2012
@@ -1,241 +1,241 @@
-/*
- * 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.extensions.scripting.jsf.resources;
-
-import javax.faces.application.ProjectStage;
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-public class ResourceHandlerCache
-{
- private static final Logger log = Logger
- .getLogger(ResourceHandlerCache.class.getName());
-
- private Boolean _resourceCacheEnabled = null;
- private Map<ResourceKey, ResourceValue> _resourceCacheMap = null;
-
- private static final String RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.RESOURCE_HANDLER_CACHE_SIZE";
- private static final int RESOURCE_HANDLER_CACHE_DEFAULT_SIZE = 500;
-
- private static final String RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE = "org.apache.myfaces.RESOURCE_HANDLER_CACHE_ENABLED";
- private static final boolean RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT = true;
-
- public ResourceValue getResource(String resourceName, String libraryName,
- String contentType)
- {
- if (!isResourceCachingEnabled() || _resourceCacheMap == null)
- return null;
-
- if (log.isLoggable(Level.FINE))
- log.log(Level.FINE, "Attemping to get resource from cache for "
- + resourceName);
-
- ResourceKey key = new ResourceKey(resourceName, libraryName,
- contentType);
-
- return _resourceCacheMap.get(key);
- }
-
- public boolean containsResource(String resourceName, String libraryName,
- String contentType)
- {
- if (!isResourceCachingEnabled() || _resourceCacheMap == null)
- return false;
- ResourceKey key = new ResourceKey(resourceName, libraryName,
- contentType);
- return _resourceCacheMap.containsKey(key);
- }
-
- public void putResource(String resourceName, String libraryName,
- String contentType, ResourceMeta resource, ResourceLoader loader)
- {
- if (!isResourceCachingEnabled())
- return;
-
- if (log.isLoggable(Level.FINE))
- log.log(Level.FINE, "Attemping to put resource to cache for "
- + resourceName);
-
- if (_resourceCacheMap == null)
- {
- if (log.isLoggable(Level.FINE))
- log.log(Level.FINE, "Initializing resource cache map");
- _resourceCacheMap = Collections
- .synchronizedMap(new _ResourceMap<ResourceKey, ResourceValue>(
- getMaxSize()));
- }
-
- _resourceCacheMap.put(new ResourceKey(resourceName, libraryName,
- contentType), new ResourceValue(resource, loader));
- }
-
- private boolean isResourceCachingEnabled()
- {
- if (_resourceCacheEnabled == null)
- {
- FacesContext facesContext = FacesContext.getCurrentInstance();
-
- //first, check to make sure that ProjectStage is production, if not, skip caching
- if (!facesContext.isProjectStage(ProjectStage.Production))
- {
- return _resourceCacheEnabled = Boolean.FALSE;
- }
-
- ExternalContext externalContext = facesContext.getExternalContext();
- if (externalContext == null)
- return false; //don't cache right now, but don't disable it yet either
-
- //if in production, make sure that the cache is not explicitly disabled via context param
- String configParam = externalContext
- .getInitParameter(ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE);
- _resourceCacheEnabled = configParam == null ? ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT
- : Boolean.parseBoolean(configParam);
-
- if (log.isLoggable(Level.FINE))
- {
- log.log(Level.FINE, "MyFaces Resource Caching Enabled="
- + _resourceCacheEnabled);
- }
- }
- return _resourceCacheEnabled;
- }
-
- private int getMaxSize()
- {
- ExternalContext externalContext = FacesContext.getCurrentInstance()
- .getExternalContext();
-
- String configParam = externalContext == null ? null : externalContext
- .getInitParameter(RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE);
- return configParam == null ? RESOURCE_HANDLER_CACHE_DEFAULT_SIZE
- : Integer.parseInt(configParam);
- }
-
- public static class ResourceKey
- {
- private String resourceName;
- private String libraryName;
- private String contentType;
-
- public ResourceKey(String resourceName, String libraryName,
- String contentType)
- {
- this.resourceName = resourceName;
- this.libraryName = libraryName;
- this.contentType = contentType;
- }
-
- @Override
- public int hashCode()
- {
- final int prime = 31;
- int result = 1;
- result = prime * result
- + ((contentType == null) ? 0 : contentType.hashCode());
- result = prime * result
- + ((libraryName == null) ? 0 : libraryName.hashCode());
- result = prime * result
- + ((resourceName == null) ? 0 : resourceName.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj)
- {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ResourceKey other = (ResourceKey) obj;
- if (contentType == null)
- {
- if (other.contentType != null)
- return false;
- }
- else if (!contentType.equals(other.contentType))
- return false;
- if (libraryName == null)
- {
- if (other.libraryName != null)
- return false;
- }
- else if (!libraryName.equals(other.libraryName))
- return false;
- if (resourceName == null)
- {
- if (other.resourceName != null)
- return false;
- }
- else if (!resourceName.equals(other.resourceName))
- return false;
- return true;
- }
- }
-
- public static class ResourceValue
- {
- private ResourceMeta resourceMeta;
-
- private ResourceLoader resourceLoader;
-
- public ResourceValue(ResourceMeta resourceMeta,
- ResourceLoader resourceLoader)
- {
- super();
- this.resourceMeta = resourceMeta;
- this.resourceLoader = resourceLoader;
- }
-
- public ResourceMeta getResourceMeta()
- {
- return resourceMeta;
- }
-
- public ResourceLoader getResourceLoader()
- {
- return resourceLoader;
- }
- }
-
- private static class _ResourceMap<K, V> extends LinkedHashMap<K, V>
- {
- private static final long serialVersionUID = 1L;
- private int maxCapacity;
-
- public _ResourceMap(int cacheSize)
- {
- // create map at max capacity and 1.1 load factor to avoid rehashing
- super(cacheSize + 1, 1.1f, true);
- maxCapacity = cacheSize;
- }
-
- @Override
- protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
- {
- return size() > maxCapacity;
- }
- }
+/*
+ * 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.extensions.scripting.jsf.resources.deprecated;
+
+import javax.faces.application.ProjectStage;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class ResourceHandlerCache
+{
+ private static final Logger log = Logger
+ .getLogger(ResourceHandlerCache.class.getName());
+
+ private Boolean _resourceCacheEnabled = null;
+ private Map<ResourceKey, ResourceValue> _resourceCacheMap = null;
+
+ private static final String RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.RESOURCE_HANDLER_CACHE_SIZE";
+ private static final int RESOURCE_HANDLER_CACHE_DEFAULT_SIZE = 500;
+
+ private static final String RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE = "org.apache.myfaces.RESOURCE_HANDLER_CACHE_ENABLED";
+ private static final boolean RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT = true;
+
+ public ResourceValue getResource(String resourceName, String libraryName,
+ String contentType)
+ {
+ if (!isResourceCachingEnabled() || _resourceCacheMap == null)
+ return null;
+
+ if (log.isLoggable(Level.FINE))
+ log.log(Level.FINE, "Attemping to get resource from cache for "
+ + resourceName);
+
+ ResourceKey key = new ResourceKey(resourceName, libraryName,
+ contentType);
+
+ return _resourceCacheMap.get(key);
+ }
+
+ public boolean containsResource(String resourceName, String libraryName,
+ String contentType)
+ {
+ if (!isResourceCachingEnabled() || _resourceCacheMap == null)
+ return false;
+ ResourceKey key = new ResourceKey(resourceName, libraryName,
+ contentType);
+ return _resourceCacheMap.containsKey(key);
+ }
+
+ public void putResource(String resourceName, String libraryName,
+ String contentType, ResourceMeta resource, ResourceLoader loader)
+ {
+ if (!isResourceCachingEnabled())
+ return;
+
+ if (log.isLoggable(Level.FINE))
+ log.log(Level.FINE, "Attemping to put resource to cache for "
+ + resourceName);
+
+ if (_resourceCacheMap == null)
+ {
+ if (log.isLoggable(Level.FINE))
+ log.log(Level.FINE, "Initializing resource cache map");
+ _resourceCacheMap = Collections
+ .synchronizedMap(new _ResourceMap<ResourceKey, ResourceValue>(
+ getMaxSize()));
+ }
+
+ _resourceCacheMap.put(new ResourceKey(resourceName, libraryName,
+ contentType), new ResourceValue(resource, loader));
+ }
+
+ private boolean isResourceCachingEnabled()
+ {
+ if (_resourceCacheEnabled == null)
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+
+ //first, check to make sure that ProjectStage is production, if not, skip caching
+ if (!facesContext.isProjectStage(ProjectStage.Production))
+ {
+ return _resourceCacheEnabled = Boolean.FALSE;
+ }
+
+ ExternalContext externalContext = facesContext.getExternalContext();
+ if (externalContext == null)
+ return false; //don't cache right now, but don't disable it yet either
+
+ //if in production, make sure that the cache is not explicitly disabled via context param
+ String configParam = externalContext
+ .getInitParameter(ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE);
+ _resourceCacheEnabled = configParam == null ? ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT
+ : Boolean.parseBoolean(configParam);
+
+ if (log.isLoggable(Level.FINE))
+ {
+ log.log(Level.FINE, "MyFaces Resource Caching Enabled="
+ + _resourceCacheEnabled);
+ }
+ }
+ return _resourceCacheEnabled;
+ }
+
+ private int getMaxSize()
+ {
+ ExternalContext externalContext = FacesContext.getCurrentInstance()
+ .getExternalContext();
+
+ String configParam = externalContext == null ? null : externalContext
+ .getInitParameter(RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE);
+ return configParam == null ? RESOURCE_HANDLER_CACHE_DEFAULT_SIZE
+ : Integer.parseInt(configParam);
+ }
+
+ public static class ResourceKey
+ {
+ private String resourceName;
+ private String libraryName;
+ private String contentType;
+
+ public ResourceKey(String resourceName, String libraryName,
+ String contentType)
+ {
+ this.resourceName = resourceName;
+ this.libraryName = libraryName;
+ this.contentType = contentType;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((contentType == null) ? 0 : contentType.hashCode());
+ result = prime * result
+ + ((libraryName == null) ? 0 : libraryName.hashCode());
+ result = prime * result
+ + ((resourceName == null) ? 0 : resourceName.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ResourceKey other = (ResourceKey) obj;
+ if (contentType == null)
+ {
+ if (other.contentType != null)
+ return false;
+ }
+ else if (!contentType.equals(other.contentType))
+ return false;
+ if (libraryName == null)
+ {
+ if (other.libraryName != null)
+ return false;
+ }
+ else if (!libraryName.equals(other.libraryName))
+ return false;
+ if (resourceName == null)
+ {
+ if (other.resourceName != null)
+ return false;
+ }
+ else if (!resourceName.equals(other.resourceName))
+ return false;
+ return true;
+ }
+ }
+
+ public static class ResourceValue
+ {
+ private ResourceMeta resourceMeta;
+
+ private ResourceLoader resourceLoader;
+
+ public ResourceValue(ResourceMeta resourceMeta,
+ ResourceLoader resourceLoader)
+ {
+ super();
+ this.resourceMeta = resourceMeta;
+ this.resourceLoader = resourceLoader;
+ }
+
+ public ResourceMeta getResourceMeta()
+ {
+ return resourceMeta;
+ }
+
+ public ResourceLoader getResourceLoader()
+ {
+ return resourceLoader;
+ }
+ }
+
+ private static class _ResourceMap<K, V> extends LinkedHashMap<K, V>
+ {
+ private static final long serialVersionUID = 1L;
+ private int maxCapacity;
+
+ public _ResourceMap(int cacheSize)
+ {
+ // create map at max capacity and 1.1 load factor to avoid rehashing
+ super(cacheSize + 1, 1.1f, true);
+ maxCapacity = cacheSize;
+ }
+
+ @Override
+ protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
+ {
+ return size() > maxCapacity;
+ }
+ }
}
\ No newline at end of file
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerImpl.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerImpl.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerImpl.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerImpl.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerImpl.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerImpl.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerImpl.java Wed Nov 28 14:07:34 2012
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.myfaces.extensions.scripting.jsf.resources;
+package org.apache.myfaces.extensions.scripting.jsf.resources.deprecated;
import org.apache.myfaces.extensions.scripting.core.common.util.ClassLoaderUtils;
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerSupport.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerSupport.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerSupport.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerSupport.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerSupport.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceHandlerSupport.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceHandlerSupport.java Wed Nov 28 14:07:34 2012
@@ -1,86 +1,86 @@
-/*
- * 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.extensions.scripting.jsf.resources;
-
-import javax.faces.context.FacesContext;
-
-/**
- * A utility class to isolate a ResourceHandler implementation from its
- * underlying implementation
- *
- * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
- * @version $Revision: 891494 $ $Date: 2009-12-16 19:42:18 -0500 (Mié, 16 Dic 2009) $
- */
-public abstract class ResourceHandlerSupport
-{
-
- /**
- * Calculate the resource base path.
- *
- * It should extract a string like:
- *
- * ResourceHandler.RESOURCE_IDENTIFIER + '/' + getResourceName()
- *
- * For example:
- *
- * /javax.faces.resource/image.jpg
- *
- * This is used on ResourceHandler.handleResourceRequest()
- *
- */
- public abstract String calculateResourceBasePath(FacesContext facesContext);
-
- /**
- * Return an array of resource loaders used to find resources
- * using the standard. The order of ResourceLoaders define
- * its precedence.
- *
- * @return
- */
- public abstract ResourceLoader[] getResourceLoaders();
-
- /**
- * Check if the mapping used is done using extensions (.xhtml, .jsf)
- * or if it is not (/faces/*)
- * @return
- */
- public abstract boolean isExtensionMapping();
-
- /**
- * Get the mapping used as prefix(/faces) or sufix(.jsf)
- *
- * @return
- */
- public abstract String getMapping();
-
- /**
- * Return the time when the app started. This is useful to set the
- * "Last-Modified" header in some specific cases.
- *
- * @return
- */
- public abstract long getStartupTime();
-
- /**
- * Return the time that should be set on "Expires" header in a resource.
- *
- * @return
- */
- public abstract long getMaxTimeExpires();
-}
+/*
+ * 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.extensions.scripting.jsf.resources.deprecated;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * A utility class to isolate a ResourceHandler implementation from its
+ * underlying implementation
+ *
+ * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
+ * @version $Revision: 891494 $ $Date: 2009-12-16 19:42:18 -0500 (Mié, 16 Dic 2009) $
+ */
+public abstract class ResourceHandlerSupport
+{
+
+ /**
+ * Calculate the resource base path.
+ *
+ * It should extract a string like:
+ *
+ * ResourceHandler.RESOURCE_IDENTIFIER + '/' + getResourceName()
+ *
+ * For example:
+ *
+ * /javax.faces.resource/image.jpg
+ *
+ * This is used on ResourceHandler.handleResourceRequest()
+ *
+ */
+ public abstract String calculateResourceBasePath(FacesContext facesContext);
+
+ /**
+ * Return an array of resource loaders used to find resources
+ * using the standard. The order of ResourceLoaders define
+ * its precedence.
+ *
+ * @return
+ */
+ public abstract ResourceLoader[] getResourceLoaders();
+
+ /**
+ * Check if the mapping used is done using extensions (.xhtml, .jsf)
+ * or if it is not (/faces/*)
+ * @return
+ */
+ public abstract boolean isExtensionMapping();
+
+ /**
+ * Get the mapping used as prefix(/faces) or sufix(.jsf)
+ *
+ * @return
+ */
+ public abstract String getMapping();
+
+ /**
+ * Return the time when the app started. This is useful to set the
+ * "Last-Modified" header in some specific cases.
+ *
+ * @return
+ */
+ public abstract long getStartupTime();
+
+ /**
+ * Return the time that should be set on "Expires" header in a resource.
+ *
+ * @return
+ */
+ public abstract long getMaxTimeExpires();
+}
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceImpl.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceImpl.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceImpl.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceImpl.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceImpl.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceImpl.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceImpl.java Wed Nov 28 14:07:34 2012
@@ -1,316 +1,316 @@
-/*
- * 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.extensions.scripting.jsf.resources;
-
-import javax.el.ELContext;
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.faces.application.ProjectStage;
-import javax.faces.application.Resource;
-import javax.faces.application.ResourceHandler;
-import javax.faces.context.FacesContext;
-import javax.faces.event.ExceptionQueuedEvent;
-import javax.faces.event.ExceptionQueuedEventContext;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PushbackInputStream;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * Default implementation for resources
- *
- * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
- * @version $Revision: 957739 $ $Date: 2010-06-24 17:27:40 -0500 (Jue, 24 Jun 2010) $
- */
-public class ResourceImpl extends Resource {
-
-
- private ResourceMeta _resourceMeta;
- private ResourceLoader _resourceLoader;
- private ResourceHandlerSupport _resourceHandlerSupport;
-
- public ResourceImpl(ResourceMeta resourceMeta,
- ResourceLoader resourceLoader, ResourceHandlerSupport support, String contentType) {
- _resourceMeta = resourceMeta;
- _resourceLoader = resourceLoader;
- _resourceHandlerSupport = support;
- setLibraryName(resourceMeta.getLibraryName());
- setResourceName(resourceMeta.getResourceName());
- setContentType(contentType);
- }
-
- public ResourceLoader getResourceLoader() {
- return _resourceLoader;
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- if (couldResourceContainValueExpressions()) {
- return new ValueExpressionFilterInputStream(
- getResourceLoader().getResourceInputStream(_resourceMeta));
- } else {
- return getResourceLoader().getResourceInputStream(_resourceMeta);
- }
- }
-
- private boolean couldResourceContainValueExpressions() {
- if (_resourceMeta.couldResourceContainValueExpressions()) {
- return true;
- } else {
- //By default only css resource contain value expressions
- String contentType = getContentType();
-
- return ("text/css".equals(contentType));
- }
- }
-
- 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() {
- String path;
- if (_resourceHandlerSupport.isExtensionMapping()) {
- path = ResourceHandler.RESOURCE_IDENTIFIER + '/' +
- getResourceName() + _resourceHandlerSupport.getMapping();
- } else {
- String mapping = _resourceHandlerSupport.getMapping();
- path = ResourceHandler.RESOURCE_IDENTIFIER + '/' + getResourceName();
- path = (mapping == null) ? path : mapping + path;
- }
-
- FacesContext facesContext = FacesContext.getCurrentInstance();
- String metadata = null;
- boolean useAmp = false;
- if (getLibraryName() != null) {
- metadata = "?ln=" + getLibraryName();
- path = path + metadata;
- useAmp = true;
-
- if (!facesContext.isProjectStage(ProjectStage.Production)
- && ResourceLoaderUtils.JSF_JS_RESOURCE_NAME.equals(getResourceName())
- && ResourceLoaderUtils.JAVAX_FACES_LIBRARY_NAME.equals(getLibraryName())) {
- // append &stage=?? for all ProjectStages except Production
- path = path + "&stage=" + facesContext.getApplication().getProjectStage().toString();
- }
- }
-
- return facesContext.getApplication().getViewHandler().getResourceURL(facesContext, path);
- }
-
- @Override
- public Map<String, String> getResponseHeaders() {
- FacesContext facesContext = FacesContext.getCurrentInstance();
-
- if (facesContext.getApplication().getResourceHandler().isResourceRequest(facesContext)) {
- Map<String, String> headers = new HashMap<String, String>();
-
- long lastModified;
- try {
- lastModified = ResourceLoaderUtils.getResourceLastModified(this.getURL());
- } catch (IOException e) {
- lastModified = -1;
- }
-
- // Here we have two cases: If the file could contain EL Expressions
- // the last modified time is the greatest value between application startup and
- // the value from file.
- if (this.couldResourceContainValueExpressions() &&
- lastModified < _resourceHandlerSupport.getStartupTime()) {
- lastModified = _resourceHandlerSupport.getStartupTime();
- } else if (_resourceMeta instanceof AliasResourceMetaImpl &&
- lastModified < _resourceHandlerSupport.getStartupTime()) {
- // If the resource meta is aliased, the last modified time is the greatest
- // value between application startup and the value from file.
- lastModified = _resourceHandlerSupport.getStartupTime();
- }
-
- if (lastModified >= 0) {
- headers.put("Last-Modified", ResourceLoaderUtils.formatDateHeader(lastModified));
-
- long expires;
- if (facesContext.isProjectStage(ProjectStage.Development)) {
- // Force to expire now to prevent caching on development time.
- expires = System.currentTimeMillis();
- } else {
- expires = System.currentTimeMillis() + _resourceHandlerSupport.getMaxTimeExpires();
- }
- headers.put("Expires", ResourceLoaderUtils.formatDateHeader(expires));
- }
-
- return headers;
- } else {
- //No need to return headers
- return Collections.emptyMap();
- }
- }
-
- @Override
- public URL getURL() {
- return getResourceLoader().getResourceURL(_resourceMeta);
- }
-
- @Override
- public boolean userAgentNeedsUpdate(FacesContext context) {
- // RFC2616 says related to If-Modified-Since header the following:
- //
- // "... The If-Modified-Since request-header field is used with a method to
- // make it conditional: if the requested variant has not been modified since
- // the time specified in this field, an entity will not be returned from
- // the server; instead, a 304 (not modified) response will be returned
- // without any message-body..."
- //
- // This method is called from ResourceHandlerImpl.handleResourceRequest and if
- // returns false send a 304 Not Modified response.
-
- String ifModifiedSinceString = context.getExternalContext().getRequestHeaderMap().get("If-Modified-Since");
-
- if (ifModifiedSinceString == null) {
- return true;
- }
-
- Long ifModifiedSince = ResourceLoaderUtils.parseDateHeader(ifModifiedSinceString);
-
- if (ifModifiedSince == null) {
- return true;
- }
-
- Long lastModified;
- try {
- lastModified = ResourceLoaderUtils.getResourceLastModified(this.getURL());
- } catch (IOException exception) {
- lastModified = -1L;
- }
-
- if (lastModified >= 0) {
- if (this.couldResourceContainValueExpressions() &&
- lastModified < _resourceHandlerSupport.getStartupTime()) {
- lastModified = _resourceHandlerSupport.getStartupTime();
- }
-
- // If the lastModified date is lower or equal than ifModifiedSince,
- // the agent does not need to update.
- // Note the lastModified time is set at milisecond precision, but when
- // the date is parsed and sent on ifModifiedSince, the exceding miliseconds
- // are trimmed. So, we have to compare trimming this from the calculated
- // lastModified time.
- if ((lastModified - (lastModified % 1000)) <= ifModifiedSince) {
- return false;
- }
- }
-
- return true;
- }
-}
+/*
+ * 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.extensions.scripting.jsf.resources.deprecated;
+
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.application.ProjectStage;
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.faces.context.FacesContext;
+import javax.faces.event.ExceptionQueuedEvent;
+import javax.faces.event.ExceptionQueuedEventContext;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Default implementation for resources
+ *
+ * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
+ * @version $Revision: 957739 $ $Date: 2010-06-24 17:27:40 -0500 (Jue, 24 Jun 2010) $
+ */
+public class ResourceImpl extends Resource {
+
+
+ private ResourceMeta _resourceMeta;
+ private ResourceLoader _resourceLoader;
+ private ResourceHandlerSupport _resourceHandlerSupport;
+
+ public ResourceImpl(ResourceMeta resourceMeta,
+ ResourceLoader resourceLoader, ResourceHandlerSupport support, String contentType) {
+ _resourceMeta = resourceMeta;
+ _resourceLoader = resourceLoader;
+ _resourceHandlerSupport = support;
+ setLibraryName(resourceMeta.getLibraryName());
+ setResourceName(resourceMeta.getResourceName());
+ setContentType(contentType);
+ }
+
+ public ResourceLoader getResourceLoader() {
+ return _resourceLoader;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (couldResourceContainValueExpressions()) {
+ return new ValueExpressionFilterInputStream(
+ getResourceLoader().getResourceInputStream(_resourceMeta));
+ } else {
+ return getResourceLoader().getResourceInputStream(_resourceMeta);
+ }
+ }
+
+ private boolean couldResourceContainValueExpressions() {
+ if (_resourceMeta.couldResourceContainValueExpressions()) {
+ return true;
+ } else {
+ //By default only css resource contain value expressions
+ String contentType = getContentType();
+
+ return ("text/css".equals(contentType));
+ }
+ }
+
+ 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() {
+ String path;
+ if (_resourceHandlerSupport.isExtensionMapping()) {
+ path = ResourceHandler.RESOURCE_IDENTIFIER + '/' +
+ getResourceName() + _resourceHandlerSupport.getMapping();
+ } else {
+ String mapping = _resourceHandlerSupport.getMapping();
+ path = ResourceHandler.RESOURCE_IDENTIFIER + '/' + getResourceName();
+ path = (mapping == null) ? path : mapping + path;
+ }
+
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ String metadata = null;
+ boolean useAmp = false;
+ if (getLibraryName() != null) {
+ metadata = "?ln=" + getLibraryName();
+ path = path + metadata;
+ useAmp = true;
+
+ if (!facesContext.isProjectStage(ProjectStage.Production)
+ && ResourceLoaderUtils.JSF_JS_RESOURCE_NAME.equals(getResourceName())
+ && ResourceLoaderUtils.JAVAX_FACES_LIBRARY_NAME.equals(getLibraryName())) {
+ // append &stage=?? for all ProjectStages except Production
+ path = path + "&stage=" + facesContext.getApplication().getProjectStage().toString();
+ }
+ }
+
+ return facesContext.getApplication().getViewHandler().getResourceURL(facesContext, path);
+ }
+
+ @Override
+ public Map<String, String> getResponseHeaders() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+
+ if (facesContext.getApplication().getResourceHandler().isResourceRequest(facesContext)) {
+ Map<String, String> headers = new HashMap<String, String>();
+
+ long lastModified;
+ try {
+ lastModified = ResourceLoaderUtils.getResourceLastModified(this.getURL());
+ } catch (IOException e) {
+ lastModified = -1;
+ }
+
+ // Here we have two cases: If the file could contain EL Expressions
+ // the last modified time is the greatest value between application startup and
+ // the value from file.
+ if (this.couldResourceContainValueExpressions() &&
+ lastModified < _resourceHandlerSupport.getStartupTime()) {
+ lastModified = _resourceHandlerSupport.getStartupTime();
+ } else if (_resourceMeta instanceof AliasResourceMetaImpl &&
+ lastModified < _resourceHandlerSupport.getStartupTime()) {
+ // If the resource meta is aliased, the last modified time is the greatest
+ // value between application startup and the value from file.
+ lastModified = _resourceHandlerSupport.getStartupTime();
+ }
+
+ if (lastModified >= 0) {
+ headers.put("Last-Modified", ResourceLoaderUtils.formatDateHeader(lastModified));
+
+ long expires;
+ if (facesContext.isProjectStage(ProjectStage.Development)) {
+ // Force to expire now to prevent caching on development time.
+ expires = System.currentTimeMillis();
+ } else {
+ expires = System.currentTimeMillis() + _resourceHandlerSupport.getMaxTimeExpires();
+ }
+ headers.put("Expires", ResourceLoaderUtils.formatDateHeader(expires));
+ }
+
+ return headers;
+ } else {
+ //No need to return headers
+ return Collections.emptyMap();
+ }
+ }
+
+ @Override
+ public URL getURL() {
+ return getResourceLoader().getResourceURL(_resourceMeta);
+ }
+
+ @Override
+ public boolean userAgentNeedsUpdate(FacesContext context) {
+ // RFC2616 says related to If-Modified-Since header the following:
+ //
+ // "... The If-Modified-Since request-header field is used with a method to
+ // make it conditional: if the requested variant has not been modified since
+ // the time specified in this field, an entity will not be returned from
+ // the server; instead, a 304 (not modified) response will be returned
+ // without any message-body..."
+ //
+ // This method is called from ResourceHandlerImpl.handleResourceRequest and if
+ // returns false send a 304 Not Modified response.
+
+ String ifModifiedSinceString = context.getExternalContext().getRequestHeaderMap().get("If-Modified-Since");
+
+ if (ifModifiedSinceString == null) {
+ return true;
+ }
+
+ Long ifModifiedSince = ResourceLoaderUtils.parseDateHeader(ifModifiedSinceString);
+
+ if (ifModifiedSince == null) {
+ return true;
+ }
+
+ Long lastModified;
+ try {
+ lastModified = ResourceLoaderUtils.getResourceLastModified(this.getURL());
+ } catch (IOException exception) {
+ lastModified = -1L;
+ }
+
+ if (lastModified >= 0) {
+ if (this.couldResourceContainValueExpressions() &&
+ lastModified < _resourceHandlerSupport.getStartupTime()) {
+ lastModified = _resourceHandlerSupport.getStartupTime();
+ }
+
+ // If the lastModified date is lower or equal than ifModifiedSince,
+ // the agent does not need to update.
+ // Note the lastModified time is set at milisecond precision, but when
+ // the date is parsed and sent on ifModifiedSince, the exceding miliseconds
+ // are trimmed. So, we have to compare trimming this from the calculated
+ // lastModified time.
+ if ((lastModified - (lastModified % 1000)) <= ifModifiedSince) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
Copied: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceLoader.java (from r1414696, myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceLoader.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceLoader.java?p2=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceLoader.java&p1=myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceLoader.java&r1=1414696&r2=1414697&rev=1414697&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/ResourceLoader.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/resources/deprecated/ResourceLoader.java Wed Nov 28 14:07:34 2012
@@ -1,155 +1,155 @@
-/*
- * 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.extensions.scripting.jsf.resources;
-
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Comparator;
-
-/**
- * Base class for resource loaders. Resource loaders can lookup resources
- * as URLs from arbitrary locations, including JAR files.
- *
- * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
- * @version $Revision: 882702 $ $Date: 2009-11-20 15:16:07 -0500 (Vie, 20 Nov 2009) $
- */
-public abstract class ResourceLoader
-{
-
- public static final String VERSION_INVALID = "INVALID";
-
- private String _prefix;
-
- public ResourceLoader(String prefix)
- {
- _prefix = prefix;
- }
-
- public abstract String getResourceVersion(String path);
-
- /**
- * Return the max available version found (if exists) or
- * return null if no version available.
- */
- public abstract String getLibraryVersion(String path);
-
- /**
- * Return the max available version found (if exists) or
- * return null if no version available.
- */
- public abstract URL getResourceURL(ResourceMeta resourceMeta);
-
- public abstract InputStream getResourceInputStream(ResourceMeta resourceMeta);
-
- public abstract ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
- String resourceName, String resourceVersion);
-
- public abstract boolean libraryExists(String libraryName);
-
- private Comparator<String> _versionComparator = null;
-
- protected Comparator<String> getVersionComparator()
- {
- if (_versionComparator == null)
- {
- _versionComparator = new VersionComparator();
- }
- return _versionComparator;
- }
-
- protected void setVersionComparator(Comparator<String> versionComparator)
- {
- _versionComparator = versionComparator;
- }
-
- public class VersionComparator implements Comparator<String>
- {
-
- public int compare(String s1, String s2)
- {
- int n1 = 0;
- int n2 = 0;
- String o1 = s1;
- String o2 = s2;
-
- boolean p1 = true;
- boolean p2 = true;
-
- while (n1 == n2 && (p1 || p2))
- {
- int i1 = o1.indexOf('_');
- int i2 = o2.indexOf('_');
- if (i1 < 0)
- {
- if (o1.length() > 0)
- {
- p1 = false;
- n1 = Integer.valueOf(o1);
- o1 = "";
- }
- else
- {
- p1 = false;
- n1 = 0;
- }
- }
- else
- {
- n1 = Integer.valueOf(o1.substring(0, i1));
- o1 = o1.substring(i1 + 1);
- }
- if (i2 < 0)
- {
- if (o2.length() > 0)
- {
- p2 = false;
- n2 = Integer.valueOf(o2);
- o2 = "";
- }
- else
- {
- p2 = false;
- n2 = 0;
- }
- }
- else
- {
- n2 = Integer.valueOf(o2.substring(0, i2));
- o2 = o2.substring(i2 + 1);
- }
- }
-
- if (n1 == n2)
- {
- return s1.length() - s2.length();
- }
- return n1 - n2;
- }
- }
-
- public String getPrefix()
- {
- return _prefix;
- }
-
- public void setPrefix(String prefix)
- {
- _prefix = prefix;
- }
-}
+/*
+ * 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.extensions.scripting.jsf.resources.deprecated;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Comparator;
+
+/**
+ * Base class for resource loaders. Resource loaders can lookup resources
+ * as URLs from arbitrary locations, including JAR files.
+ *
+ * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
+ * @version $Revision: 882702 $ $Date: 2009-11-20 15:16:07 -0500 (Vie, 20 Nov 2009) $
+ */
+public abstract class ResourceLoader
+{
+
+ public static final String VERSION_INVALID = "INVALID";
+
+ private String _prefix;
+
+ public ResourceLoader(String prefix)
+ {
+ _prefix = prefix;
+ }
+
+ public abstract String getResourceVersion(String path);
+
+ /**
+ * Return the max available version found (if exists) or
+ * return null if no version available.
+ */
+ public abstract String getLibraryVersion(String path);
+
+ /**
+ * Return the max available version found (if exists) or
+ * return null if no version available.
+ */
+ public abstract URL getResourceURL(ResourceMeta resourceMeta);
+
+ public abstract InputStream getResourceInputStream(ResourceMeta resourceMeta);
+
+ public abstract ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
+ String resourceName, String resourceVersion);
+
+ public abstract boolean libraryExists(String libraryName);
+
+ private Comparator<String> _versionComparator = null;
+
+ protected Comparator<String> getVersionComparator()
+ {
+ if (_versionComparator == null)
+ {
+ _versionComparator = new VersionComparator();
+ }
+ return _versionComparator;
+ }
+
+ protected void setVersionComparator(Comparator<String> versionComparator)
+ {
+ _versionComparator = versionComparator;
+ }
+
+ public class VersionComparator implements Comparator<String>
+ {
+
+ public int compare(String s1, String s2)
+ {
+ int n1 = 0;
+ int n2 = 0;
+ String o1 = s1;
+ String o2 = s2;
+
+ boolean p1 = true;
+ boolean p2 = true;
+
+ while (n1 == n2 && (p1 || p2))
+ {
+ int i1 = o1.indexOf('_');
+ int i2 = o2.indexOf('_');
+ if (i1 < 0)
+ {
+ if (o1.length() > 0)
+ {
+ p1 = false;
+ n1 = Integer.valueOf(o1);
+ o1 = "";
+ }
+ else
+ {
+ p1 = false;
+ n1 = 0;
+ }
+ }
+ else
+ {
+ n1 = Integer.valueOf(o1.substring(0, i1));
+ o1 = o1.substring(i1 + 1);
+ }
+ if (i2 < 0)
+ {
+ if (o2.length() > 0)
+ {
+ p2 = false;
+ n2 = Integer.valueOf(o2);
+ o2 = "";
+ }
+ else
+ {
+ p2 = false;
+ n2 = 0;
+ }
+ }
+ else
+ {
+ n2 = Integer.valueOf(o2.substring(0, i2));
+ o2 = o2.substring(i2 + 1);
+ }
+ }
+
+ if (n1 == n2)
+ {
+ return s1.length() - s2.length();
+ }
+ return n1 - n2;
+ }
+ }
+
+ public String getPrefix()
+ {
+ return _prefix;
+ }
+
+ public void setPrefix(String prefix)
+ {
+ _prefix = prefix;
+ }
+}