You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2011/10/22 19:59:37 UTC
svn commit: r1187741 [3/11] - in /myfaces/shared/trunk: ./
core/src/main/java/org/apache/myfaces/shared/
core/src/main/java/org/apache/myfaces/shared/application/
core/src/main/java/org/apache/myfaces/shared/component/
core/src/main/java/org/apache/myf...
Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java?rev=1187741&r1=1187740&r2=1187741&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java Sat Oct 22 17:59:32 2011
@@ -1,229 +1,229 @@
-/*
- * 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.util.Map;
-
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
-import org.apache.myfaces.shared.application.FacesServletMapping;
-import org.apache.myfaces.shared.util.WebConfigParamUtils;
-
-/**
- * A ResourceHandlerSupport implementation for use with standard Java Servlet engines,
- * ie an engine that supports javax.servlet, and uses a standard web.xml file.
- *
- * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
- * @version $Revision: 946779 $ $Date: 2010-05-20 15:31:42 -0500 (Jue, 20 May 2010) $
- */
-public class BaseResourceHandlerSupport extends ResourceHandlerSupport
-{
-
- /**
- * Set the max time in miliseconds set on the "Expires" header for a resource.
- * (default to one week in miliseconds or 604800000)
- */
- @JSFWebConfigParam(since="2.0", defaultValue="604800000")
- public static final String RESOURCE_MAX_TIME_EXPIRES = "org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES";
-
- /**
- * Identifies the FacesServlet mapping in the current request map.
- */
- private static final String CACHED_SERVLET_MAPPING =
- BaseResourceHandlerSupport.class.getName() + ".CACHED_SERVLET_MAPPING";
-
- private Long _startupTime;
-
- private Long _maxTimeExpires;
-
- public BaseResourceHandlerSupport()
- {
- _startupTime = System.currentTimeMillis();
- }
-
- public ResourceLoader[] getResourceLoaders()
- {
- return null;
- }
-
- public String calculateResourceBasePath(FacesContext facesContext)
- {
- FacesServletMapping mapping = getFacesServletMapping(facesContext);
- ExternalContext externalContext = facesContext.getExternalContext();
-
- if (mapping != null)
- {
- String resourceBasePath = null;
- if (mapping.isExtensionMapping())
- {
- // Mapping using a suffix. In this case we have to strip
- // the suffix. If we have a url like:
- // http://localhost:8080/testjsf20/javax.faces.resource/imagen.jpg.jsf?ln=dojo
- //
- // The servlet path is /javax.faces.resource/imagen.jpg.jsf
- //
- // For obtain the resource name we have to remove the .jsf suffix and
- // the prefix ResourceHandler.RESOURCE_IDENTIFIER
- resourceBasePath = externalContext.getRequestServletPath();
- int stripPoint = resourceBasePath.lastIndexOf('.');
- if (stripPoint > 0)
- {
- resourceBasePath = resourceBasePath.substring(0, stripPoint);
- }
- }
- else
- {
- // Mapping using prefix. In this case we have to strip
- // the prefix used for mapping. If we have a url like:
- // http://localhost:8080/testjsf20/faces/javax.faces.resource/imagen.jpg?ln=dojo
- //
- // The servlet path is /faces
- // and the path info is /javax.faces.resource/imagen.jpg
- //
- // For obtain the resource name we have to remove the /faces prefix and
- // then the prefix ResourceHandler.RESOURCE_IDENTIFIER
- resourceBasePath = externalContext.getRequestPathInfo();
- }
- return resourceBasePath;
- }
- else
- {
- //If no mapping is detected, just return the
- //information follows the servlet path but before
- //the query string
- return externalContext.getRequestPathInfo();
- }
- }
-
- public boolean isExtensionMapping()
- {
- FacesServletMapping mapping = getFacesServletMapping(
- FacesContext.getCurrentInstance());
- if (mapping != null)
- {
- if (mapping.isExtensionMapping())
- {
- return true;
- }
- }
- return false;
- }
-
- public String getMapping()
- {
- FacesServletMapping mapping = getFacesServletMapping(
- FacesContext.getCurrentInstance());
- if (mapping != null)
- {
- if (mapping.isExtensionMapping())
- {
- return mapping.getExtension();
- }
- else
- {
- return mapping.getPrefix();
- }
- }
- return "";
- }
-
- /**
- * Read the web.xml file that is in the classpath and parse its internals to
- * figure out how the FacesServlet is mapped for the current webapp.
- */
- protected FacesServletMapping getFacesServletMapping(FacesContext context)
- {
- Map<Object, Object> attributes = context.getAttributes();
-
- // Has the mapping already been determined during this request?
- FacesServletMapping mapping = (FacesServletMapping) attributes.get(CACHED_SERVLET_MAPPING);
- if (mapping == null)
- {
- ExternalContext externalContext = context.getExternalContext();
- mapping = calculateFacesServletMapping(externalContext.getRequestServletPath(),
- externalContext.getRequestPathInfo());
-
- attributes.put(CACHED_SERVLET_MAPPING, mapping);
- }
- return mapping;
- }
-
- /**
- * Determines the mapping of the FacesServlet in the web.xml configuration
- * file. However, there is no need to actually parse this configuration file
- * as runtime information is sufficient.
- *
- * @param servletPath The servletPath of the current request
- * @param pathInfo The pathInfo of the current request
- * @return the mapping of the FacesServlet in the web.xml configuration file
- */
- protected static FacesServletMapping calculateFacesServletMapping(
- String servletPath, String pathInfo)
- {
- if (pathInfo != null)
- {
- // If there is a "extra path", it's definitely no extension mapping.
- // Now we just have to determine the path which has been specified
- // in the url-pattern, but that's easy as it's the same as the
- // current servletPath. It doesn't even matter if "/*" has been used
- // as in this case the servletPath is just an empty string according
- // to the Servlet Specification (SRV 4.4).
- return FacesServletMapping.createPrefixMapping(servletPath);
- }
- else
- {
- // In the case of extension mapping, no "extra path" is available.
- // Still it's possible that prefix-based mapping has been used.
- // Actually, if there was an exact match no "extra path"
- // is available (e.g. if the url-pattern is "/faces/*"
- // and the request-uri is "/context/faces").
- int slashPos = servletPath.lastIndexOf('/');
- int extensionPos = servletPath.lastIndexOf('.');
- if (extensionPos > -1 && extensionPos > slashPos)
- {
- String extension = servletPath.substring(extensionPos);
- return FacesServletMapping.createExtensionMapping(extension);
- }
- else
- {
- // There is no extension in the given servletPath and therefore
- // we assume that it's an exact match using prefix-based mapping.
- return FacesServletMapping.createPrefixMapping(servletPath);
- }
- }
- }
-
- public long getStartupTime()
- {
- return _startupTime;
- }
-
- public long getMaxTimeExpires()
- {
- if (_maxTimeExpires == null)
- {
- _maxTimeExpires = WebConfigParamUtils.getLongInitParameter(
- FacesContext.getCurrentInstance().getExternalContext(),
- RESOURCE_MAX_TIME_EXPIRES, 604800000L);
- }
- return _maxTimeExpires;
- }
-}
+/*
+ * 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.util.Map;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.shared.application.FacesServletMapping;
+import org.apache.myfaces.shared.util.WebConfigParamUtils;
+
+/**
+ * A ResourceHandlerSupport implementation for use with standard Java Servlet engines,
+ * ie an engine that supports javax.servlet, and uses a standard web.xml file.
+ *
+ * @author Leonardo Uribe (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class BaseResourceHandlerSupport extends ResourceHandlerSupport
+{
+
+ /**
+ * Set the max time in miliseconds set on the "Expires" header for a resource.
+ * (default to one week in miliseconds or 604800000)
+ */
+ @JSFWebConfigParam(since="2.0", defaultValue="604800000")
+ public static final String RESOURCE_MAX_TIME_EXPIRES = "org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES";
+
+ /**
+ * Identifies the FacesServlet mapping in the current request map.
+ */
+ private static final String CACHED_SERVLET_MAPPING =
+ BaseResourceHandlerSupport.class.getName() + ".CACHED_SERVLET_MAPPING";
+
+ private Long _startupTime;
+
+ private Long _maxTimeExpires;
+
+ public BaseResourceHandlerSupport()
+ {
+ _startupTime = System.currentTimeMillis();
+ }
+
+ public ResourceLoader[] getResourceLoaders()
+ {
+ return null;
+ }
+
+ public String calculateResourceBasePath(FacesContext facesContext)
+ {
+ FacesServletMapping mapping = getFacesServletMapping(facesContext);
+ ExternalContext externalContext = facesContext.getExternalContext();
+
+ if (mapping != null)
+ {
+ String resourceBasePath = null;
+ if (mapping.isExtensionMapping())
+ {
+ // Mapping using a suffix. In this case we have to strip
+ // the suffix. If we have a url like:
+ // http://localhost:8080/testjsf20/javax.faces.resource/imagen.jpg.jsf?ln=dojo
+ //
+ // The servlet path is /javax.faces.resource/imagen.jpg.jsf
+ //
+ // For obtain the resource name we have to remove the .jsf suffix and
+ // the prefix ResourceHandler.RESOURCE_IDENTIFIER
+ resourceBasePath = externalContext.getRequestServletPath();
+ int stripPoint = resourceBasePath.lastIndexOf('.');
+ if (stripPoint > 0)
+ {
+ resourceBasePath = resourceBasePath.substring(0, stripPoint);
+ }
+ }
+ else
+ {
+ // Mapping using prefix. In this case we have to strip
+ // the prefix used for mapping. If we have a url like:
+ // http://localhost:8080/testjsf20/faces/javax.faces.resource/imagen.jpg?ln=dojo
+ //
+ // The servlet path is /faces
+ // and the path info is /javax.faces.resource/imagen.jpg
+ //
+ // For obtain the resource name we have to remove the /faces prefix and
+ // then the prefix ResourceHandler.RESOURCE_IDENTIFIER
+ resourceBasePath = externalContext.getRequestPathInfo();
+ }
+ return resourceBasePath;
+ }
+ else
+ {
+ //If no mapping is detected, just return the
+ //information follows the servlet path but before
+ //the query string
+ return externalContext.getRequestPathInfo();
+ }
+ }
+
+ public boolean isExtensionMapping()
+ {
+ FacesServletMapping mapping = getFacesServletMapping(
+ FacesContext.getCurrentInstance());
+ if (mapping != null)
+ {
+ if (mapping.isExtensionMapping())
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String getMapping()
+ {
+ FacesServletMapping mapping = getFacesServletMapping(
+ FacesContext.getCurrentInstance());
+ if (mapping != null)
+ {
+ if (mapping.isExtensionMapping())
+ {
+ return mapping.getExtension();
+ }
+ else
+ {
+ return mapping.getPrefix();
+ }
+ }
+ return "";
+ }
+
+ /**
+ * Read the web.xml file that is in the classpath and parse its internals to
+ * figure out how the FacesServlet is mapped for the current webapp.
+ */
+ protected FacesServletMapping getFacesServletMapping(FacesContext context)
+ {
+ Map<Object, Object> attributes = context.getAttributes();
+
+ // Has the mapping already been determined during this request?
+ FacesServletMapping mapping = (FacesServletMapping) attributes.get(CACHED_SERVLET_MAPPING);
+ if (mapping == null)
+ {
+ ExternalContext externalContext = context.getExternalContext();
+ mapping = calculateFacesServletMapping(externalContext.getRequestServletPath(),
+ externalContext.getRequestPathInfo());
+
+ attributes.put(CACHED_SERVLET_MAPPING, mapping);
+ }
+ return mapping;
+ }
+
+ /**
+ * Determines the mapping of the FacesServlet in the web.xml configuration
+ * file. However, there is no need to actually parse this configuration file
+ * as runtime information is sufficient.
+ *
+ * @param servletPath The servletPath of the current request
+ * @param pathInfo The pathInfo of the current request
+ * @return the mapping of the FacesServlet in the web.xml configuration file
+ */
+ protected static FacesServletMapping calculateFacesServletMapping(
+ String servletPath, String pathInfo)
+ {
+ if (pathInfo != null)
+ {
+ // If there is a "extra path", it's definitely no extension mapping.
+ // Now we just have to determine the path which has been specified
+ // in the url-pattern, but that's easy as it's the same as the
+ // current servletPath. It doesn't even matter if "/*" has been used
+ // as in this case the servletPath is just an empty string according
+ // to the Servlet Specification (SRV 4.4).
+ return FacesServletMapping.createPrefixMapping(servletPath);
+ }
+ else
+ {
+ // In the case of extension mapping, no "extra path" is available.
+ // Still it's possible that prefix-based mapping has been used.
+ // Actually, if there was an exact match no "extra path"
+ // is available (e.g. if the url-pattern is "/faces/*"
+ // and the request-uri is "/context/faces").
+ int slashPos = servletPath.lastIndexOf('/');
+ int extensionPos = servletPath.lastIndexOf('.');
+ if (extensionPos > -1 && extensionPos > slashPos)
+ {
+ String extension = servletPath.substring(extensionPos);
+ return FacesServletMapping.createExtensionMapping(extension);
+ }
+ else
+ {
+ // There is no extension in the given servletPath and therefore
+ // we assume that it's an exact match using prefix-based mapping.
+ return FacesServletMapping.createPrefixMapping(servletPath);
+ }
+ }
+ }
+
+ public long getStartupTime()
+ {
+ return _startupTime;
+ }
+
+ public long getMaxTimeExpires()
+ {
+ if (_maxTimeExpires == null)
+ {
+ _maxTimeExpires = WebConfigParamUtils.getLongInitParameter(
+ FacesContext.getCurrentInstance().getExternalContext(),
+ RESOURCE_MAX_TIME_EXPIRES, 604800000L);
+ }
+ return _maxTimeExpires;
+ }
+}
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java?rev=1187741&r1=1187740&r2=1187741&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java Sat Oct 22 17:59:32 2011
@@ -1,513 +1,513 @@
-/*
- * 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 javax.faces.application.ProjectStage;
-import javax.faces.context.FacesContext;
-
-import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
-import org.apache.myfaces.shared.util.ClassUtils;
-
-/**
- * A resource loader implementation which loads resources from the thread ClassLoader.
- *
- */
-public class ClassLoaderResourceLoader 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}*)*\\..*");
-
- /*
- private FileFilter _libraryFileFilter = new FileFilter()
- {
- public boolean accept(File pathname)
- {
- if (pathname.isDirectory() && VERSION_CHECKER.matcher(pathname.getName()).matches())
- {
- return true;
- }
- return false;
- }
- };*/
-
- /*
- private FileFilter _resourceFileFilter = new FileFilter()
- {
- public boolean accept(File pathname)
- {
- if (pathname.isDirectory() && RESOURCE_VERSION_CHECKER.matcher(pathname.getName()).matches())
- {
- return true;
- }
- return false;
- }
- };*/
-
- private final boolean _developmentStage;
-
- public ClassLoaderResourceLoader(String prefix)
- {
- super(prefix);
- _developmentStage = FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Development);
- }
-
- @Override
- public String getLibraryVersion(String path)
- {
- return null;
- /*
- String libraryVersion = null;
- if (getPrefix() != null)
- path = getPrefix() + '/' + path;
-
- URL url = getClassLoader().getResource(path);
-
- if (url == null)
- {
- // This library does not exists for this
- // ResourceLoader
- return null;
- }
-
- // The problem here is how to scan the directory. When a ClassLoader
- // is used two cases could occur
- // 1. The files are unpacked so we can use Url.toURI and crawl
- // the directory using the api for files.
- // 2. The files are packed in a jar. This case is more tricky,
- // because we only have a URL. Checking the jar api we can use
- // JarURLConnection (Sounds strange, but the api of
- // URL.openConnection says that for a jar connection a
- // JarURLConnection is returned). From this point we can access
- // to the jar api and solve the algoritm.
- if (url.getProtocol().equals("file"))
- {
- try
- {
- File directory = new File(url.toURI());
- if (directory.isDirectory())
- {
- File[] versions = directory.listFiles(_libraryFileFilter);
- for (int i = 0; i < versions.length; i++)
- {
- String version = versions[i].getName();
- if (VERSION_CHECKER.matcher(version).matches())
- {
- if (libraryVersion == null)
- {
- libraryVersion = version;
- }
- else if (getVersionComparator().compare(libraryVersion, version) < 0)
- {
- libraryVersion = version;
- }
- }
- }
- }
- }
- catch (URISyntaxException e)
- {
- // Just return null, because library version cannot be
- // resolved.
- Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
- if (log.isLoggable(Level.WARNING))
- {
- log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to uri: "+e.getMessage(), e);
- }
- }
- }
- else if (isJarResourceProtocol(url.getProtocol()))
- {
- try
- {
- url = getClassLoader().getResource(path + '/');
-
- if (url != null)
- {
- JarURLConnection conn = (JarURLConnection)url.openConnection();
- // See DIGESTER-29 for related problem
- conn.setUseCaches(false);
-
- try
- {
- if (conn.getJarEntry().isDirectory())
- {
- // Unfortunately, we have to scan all entry files
- // because there is no proper api to scan it as a
- // directory tree.
- JarFile file = conn.getJarFile();
- for (Enumeration<JarEntry> en = file.entries(); en.hasMoreElements();)
- {
- JarEntry entry = en.nextElement();
- String entryName = entry.getName();
-
- if (entryName.startsWith(path + '/'))
- {
- if (entryName.length() == path.length() + 1)
- {
- // the same string, just skip it
- continue;
- }
-
- if (entryName.charAt(entryName.length() - 1) != '/')
- {
- // Skip files
- continue;
- }
-
- entryName = entryName.substring(path.length() + 1, entryName.length() - 1);
-
- if (entryName.indexOf('/') >= 0)
- {
- // Inner Directory
- continue;
- }
-
- String version = entryName;
- if (VERSION_CHECKER.matcher(version).matches())
- {
- if (libraryVersion == null)
- {
- libraryVersion = version;
- }
- else if (getVersionComparator().compare(libraryVersion, version) < 0)
- {
- libraryVersion = version;
- }
- }
- }
- }
- }
- }
- finally
- {
- //See TRINIDAD-73
- //just close the input stream again if
- //by inspecting the entries the stream
- //was let open.
- try
- {
- conn.getInputStream().close();
- }
- catch (Exception exception)
- {
- // Ignored
- }
- }
- }
- }
- catch (IOException e)
- {
- // Just return null, because library version cannot be
- // resolved.
- Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
- if (log.isLoggable(Level.WARNING))
- {
- log.log(Level.WARNING, "IOException when scanning for resource in jar file:", e);
- }
- }
- }
- return libraryVersion;
- */
- }
-
- @Override
- public InputStream getResourceInputStream(ResourceMeta resourceMeta)
- {
- InputStream is = null;
- if (getPrefix() != null && !"".equals(getPrefix()))
- {
- String name = getPrefix() + '/' + resourceMeta.getResourceIdentifier();
- is = getClassLoader().getResourceAsStream(name);
- if (is == null)
- {
- is = this.getClass().getClassLoader().getResourceAsStream(name);
- }
- return is;
- }
- else
- {
- is = getClassLoader().getResourceAsStream(resourceMeta.getResourceIdentifier());
- if (is == null)
- {
- is = this.getClass().getClassLoader().getResourceAsStream(resourceMeta.getResourceIdentifier());
- }
- return is;
- }
- }
-
- @Override
- public URL getResourceURL(ResourceMeta resourceMeta)
- {
- URL url = null;
- if (getPrefix() != null && !"".equals(getPrefix()))
- {
- String name = getPrefix() + '/' + resourceMeta.getResourceIdentifier();
- url = getClassLoader().getResource(name);
- if (url == null)
- {
- url = this.getClass().getClassLoader().getResource(name);
- }
- return url;
- }
- else
- {
- url = getClassLoader().getResource(resourceMeta.getResourceIdentifier());
- if (url == null)
- {
- url = this.getClass().getClassLoader().getResource(resourceMeta.getResourceIdentifier());
- }
- return url;
- }
- }
-
- @Override
- public String getResourceVersion(String path)
- {
- return null;
- /*
- String resourceVersion = null;
-
- if (getPrefix() != null)
- path = getPrefix() + '/' + path;
-
- URL url = getClassLoader().getResource(path);
-
- if (url == null)
- {
- // This library does not exists for this
- // ResourceLoader
- return null;
- }
-
- if (url.getProtocol().equals("file"))
- {
- try
- {
- File directory = new File(url.toURI());
- if (directory.isDirectory())
- {
- File[] versions = directory.listFiles(_resourceFileFilter);
- for (int i = 0; i < versions.length; i++)
- {
- String version = versions[i].getName();
- if (resourceVersion == null)
- {
- resourceVersion = version;
- }
- else if (getVersionComparator().compare(resourceVersion, version) < 0)
- {
- resourceVersion = version;
- }
- }
- //Since it is a directory and no version found set resourceVersion as invalid
- if (resourceVersion == null)
- {
- resourceVersion = VERSION_INVALID;
- }
- }
- }
- catch (URISyntaxException e)
- {
- Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
- if (log.isLoggable(Level.WARNING))
- {
- log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to uri: "+e.getMessage(), e);
- }
- }
- }
- else if (isJarResourceProtocol(url.getProtocol()))
- {
- try
- {
- url = getClassLoader().getResource(path + '/');
-
- if (url != null)
- {
- JarURLConnection conn = (JarURLConnection)url.openConnection();
- // See DIGESTER-29 for related problem
- conn.setUseCaches(false);
-
- try
- {
- if (conn.getJarEntry().isDirectory())
- {
- // Unfortunately, we have to scan all entry files
- JarFile file = conn.getJarFile();
- for (Enumeration<JarEntry> en = file.entries(); en.hasMoreElements();)
- {
- JarEntry entry = en.nextElement();
- String entryName = entry.getName();
-
- if (entryName.startsWith(path + '/'))
- {
- if (entryName.length() == path.length() + 1)
- {
- // the same string, just skip it
- continue;
- }
-
- entryName = entryName.substring(path.length());
- if (RESOURCE_VERSION_CHECKER.matcher(entryName).matches())
- {
- String version = entryName.substring(1, entryName.lastIndexOf('.'));
- if (resourceVersion == null)
- {
- resourceVersion = version;
- }
- else if (getVersionComparator().compare(resourceVersion, version) < 0)
- {
- resourceVersion = version;
- }
- }
- }
- }
- if (resourceVersion == null)
- {
- resourceVersion = VERSION_INVALID;
- }
- }
- }
- finally
- {
- //See TRINIDAD-73
- //just close the input stream again if
- //by inspecting the entries the stream
- //was let open.
- try
- {
- conn.getInputStream().close();
- }
- catch (Exception exception)
- {
- // Ignored
- }
- }
-
- }
- }
- catch (IOException e)
- {
- // Just return null, because library version cannot be
- // resolved.
- Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
- if (log.isLoggable(Level.WARNING))
- {
- log.log(Level.WARNING, "IOException when scanning for resource in jar file:", e);
- }
- }
- }
- return resourceVersion;
- */
- }
-
- @Override
- public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
- String resourceName, String resourceVersion)
- {
- if (_developmentStage && libraryName != null &&
- ResourceUtils.JAVAX_FACES_LIBRARY_NAME.equals(libraryName) &&
- ResourceUtils.JSF_JS_RESOURCE_NAME.equals(resourceName))
- {
- // InternalClassLoaderResourceLoader will serve it, so return null in this case.
- return null;
- } else if (_developmentStage && libraryName != null &&
- ResourceUtils.MYFACES_LIBRARY_NAME.equals(libraryName) &&
- ResourceUtils.MYFACES_JS_RESOURCE_NAME.equals(resourceName)) {
- // InternalClassLoaderResourceLoader will serve it, so return null in this case.
- return null;
- } else
- {
- return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
- }
- }
-
- /**
- * Returns the ClassLoader to use when looking up resources under the top level package. By default, this is the
- * context class loader.
- *
- * @return the ClassLoader used to lookup resources
- */
- protected ClassLoader getClassLoader()
- {
- return ClassUtils.getContextClassLoader();
- }
-
- @Override
- public boolean libraryExists(String libraryName)
- {
- if (getPrefix() != null && !"".equals(getPrefix()))
- {
- URL url = getClassLoader().getResource(getPrefix() + '/' + libraryName);
- if (url == null)
- {
- url = this.getClass().getClassLoader().getResource(getPrefix() + '/' + libraryName);
- }
- if (url != null)
- {
- return true;
- }
- }
- else
- {
- URL url = getClassLoader().getResource(libraryName);
- if (url == null)
- {
- url = this.getClass().getClassLoader().getResource(libraryName);
- }
- if (url != null)
- {
- return true;
- }
- }
- return false;
- }
-
- /**
- * <p>Determines whether the given URL resource protocol refers to a JAR file. Note that
- * BEA WebLogic and IBM WebSphere don't use the "jar://" protocol for some reason even
- * though you can treat these resources just like normal JAR files, i.e. you can ignore
- * the difference between these protocols after this method has returned.</p>
- *
- * @param protocol the URL resource protocol you want to check
- *
- * @return <code>true</code> if the given URL resource protocol refers to a JAR file,
- * <code>false</code> otherwise
- */
- /*
- private static boolean isJarResourceProtocol(String protocol)
- {
- // Websphere uses the protocol "wsjar://" and Weblogic uses the protocol "zip://".
- return "jar".equals(protocol) || "wsjar".equals(protocol) || "zip".equals(protocol);
- }*/
-
-}
+/*
+ * 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 javax.faces.application.ProjectStage;
+import javax.faces.context.FacesContext;
+
+import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
+import org.apache.myfaces.shared.util.ClassUtils;
+
+/**
+ * A resource loader implementation which loads resources from the thread ClassLoader.
+ *
+ */
+public class ClassLoaderResourceLoader 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}*)*\\..*");
+
+ /*
+ private FileFilter _libraryFileFilter = new FileFilter()
+ {
+ public boolean accept(File pathname)
+ {
+ if (pathname.isDirectory() && VERSION_CHECKER.matcher(pathname.getName()).matches())
+ {
+ return true;
+ }
+ return false;
+ }
+ };*/
+
+ /*
+ private FileFilter _resourceFileFilter = new FileFilter()
+ {
+ public boolean accept(File pathname)
+ {
+ if (pathname.isDirectory() && RESOURCE_VERSION_CHECKER.matcher(pathname.getName()).matches())
+ {
+ return true;
+ }
+ return false;
+ }
+ };*/
+
+ private final boolean _developmentStage;
+
+ public ClassLoaderResourceLoader(String prefix)
+ {
+ super(prefix);
+ _developmentStage = FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Development);
+ }
+
+ @Override
+ public String getLibraryVersion(String path)
+ {
+ return null;
+ /*
+ String libraryVersion = null;
+ if (getPrefix() != null)
+ path = getPrefix() + '/' + path;
+
+ URL url = getClassLoader().getResource(path);
+
+ if (url == null)
+ {
+ // This library does not exists for this
+ // ResourceLoader
+ return null;
+ }
+
+ // The problem here is how to scan the directory. When a ClassLoader
+ // is used two cases could occur
+ // 1. The files are unpacked so we can use Url.toURI and crawl
+ // the directory using the api for files.
+ // 2. The files are packed in a jar. This case is more tricky,
+ // because we only have a URL. Checking the jar api we can use
+ // JarURLConnection (Sounds strange, but the api of
+ // URL.openConnection says that for a jar connection a
+ // JarURLConnection is returned). From this point we can access
+ // to the jar api and solve the algoritm.
+ if (url.getProtocol().equals("file"))
+ {
+ try
+ {
+ File directory = new File(url.toURI());
+ if (directory.isDirectory())
+ {
+ File[] versions = directory.listFiles(_libraryFileFilter);
+ for (int i = 0; i < versions.length; i++)
+ {
+ String version = versions[i].getName();
+ if (VERSION_CHECKER.matcher(version).matches())
+ {
+ if (libraryVersion == null)
+ {
+ libraryVersion = version;
+ }
+ else if (getVersionComparator().compare(libraryVersion, version) < 0)
+ {
+ libraryVersion = version;
+ }
+ }
+ }
+ }
+ }
+ catch (URISyntaxException e)
+ {
+ // Just return null, because library version cannot be
+ // resolved.
+ Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
+ if (log.isLoggable(Level.WARNING))
+ {
+ log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to uri: "+e.getMessage(), e);
+ }
+ }
+ }
+ else if (isJarResourceProtocol(url.getProtocol()))
+ {
+ try
+ {
+ url = getClassLoader().getResource(path + '/');
+
+ if (url != null)
+ {
+ JarURLConnection conn = (JarURLConnection)url.openConnection();
+ // See DIGESTER-29 for related problem
+ conn.setUseCaches(false);
+
+ try
+ {
+ if (conn.getJarEntry().isDirectory())
+ {
+ // Unfortunately, we have to scan all entry files
+ // because there is no proper api to scan it as a
+ // directory tree.
+ JarFile file = conn.getJarFile();
+ for (Enumeration<JarEntry> en = file.entries(); en.hasMoreElements();)
+ {
+ JarEntry entry = en.nextElement();
+ String entryName = entry.getName();
+
+ if (entryName.startsWith(path + '/'))
+ {
+ if (entryName.length() == path.length() + 1)
+ {
+ // the same string, just skip it
+ continue;
+ }
+
+ if (entryName.charAt(entryName.length() - 1) != '/')
+ {
+ // Skip files
+ continue;
+ }
+
+ entryName = entryName.substring(path.length() + 1, entryName.length() - 1);
+
+ if (entryName.indexOf('/') >= 0)
+ {
+ // Inner Directory
+ continue;
+ }
+
+ String version = entryName;
+ if (VERSION_CHECKER.matcher(version).matches())
+ {
+ if (libraryVersion == null)
+ {
+ libraryVersion = version;
+ }
+ else if (getVersionComparator().compare(libraryVersion, version) < 0)
+ {
+ libraryVersion = version;
+ }
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ //See TRINIDAD-73
+ //just close the input stream again if
+ //by inspecting the entries the stream
+ //was let open.
+ try
+ {
+ conn.getInputStream().close();
+ }
+ catch (Exception exception)
+ {
+ // Ignored
+ }
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ // Just return null, because library version cannot be
+ // resolved.
+ Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
+ if (log.isLoggable(Level.WARNING))
+ {
+ log.log(Level.WARNING, "IOException when scanning for resource in jar file:", e);
+ }
+ }
+ }
+ return libraryVersion;
+ */
+ }
+
+ @Override
+ public InputStream getResourceInputStream(ResourceMeta resourceMeta)
+ {
+ InputStream is = null;
+ if (getPrefix() != null && !"".equals(getPrefix()))
+ {
+ String name = getPrefix() + '/' + resourceMeta.getResourceIdentifier();
+ is = getClassLoader().getResourceAsStream(name);
+ if (is == null)
+ {
+ is = this.getClass().getClassLoader().getResourceAsStream(name);
+ }
+ return is;
+ }
+ else
+ {
+ is = getClassLoader().getResourceAsStream(resourceMeta.getResourceIdentifier());
+ if (is == null)
+ {
+ is = this.getClass().getClassLoader().getResourceAsStream(resourceMeta.getResourceIdentifier());
+ }
+ return is;
+ }
+ }
+
+ @Override
+ public URL getResourceURL(ResourceMeta resourceMeta)
+ {
+ URL url = null;
+ if (getPrefix() != null && !"".equals(getPrefix()))
+ {
+ String name = getPrefix() + '/' + resourceMeta.getResourceIdentifier();
+ url = getClassLoader().getResource(name);
+ if (url == null)
+ {
+ url = this.getClass().getClassLoader().getResource(name);
+ }
+ return url;
+ }
+ else
+ {
+ url = getClassLoader().getResource(resourceMeta.getResourceIdentifier());
+ if (url == null)
+ {
+ url = this.getClass().getClassLoader().getResource(resourceMeta.getResourceIdentifier());
+ }
+ return url;
+ }
+ }
+
+ @Override
+ public String getResourceVersion(String path)
+ {
+ return null;
+ /*
+ String resourceVersion = null;
+
+ if (getPrefix() != null)
+ path = getPrefix() + '/' + path;
+
+ URL url = getClassLoader().getResource(path);
+
+ if (url == null)
+ {
+ // This library does not exists for this
+ // ResourceLoader
+ return null;
+ }
+
+ if (url.getProtocol().equals("file"))
+ {
+ try
+ {
+ File directory = new File(url.toURI());
+ if (directory.isDirectory())
+ {
+ File[] versions = directory.listFiles(_resourceFileFilter);
+ for (int i = 0; i < versions.length; i++)
+ {
+ String version = versions[i].getName();
+ if (resourceVersion == null)
+ {
+ resourceVersion = version;
+ }
+ else if (getVersionComparator().compare(resourceVersion, version) < 0)
+ {
+ resourceVersion = version;
+ }
+ }
+ //Since it is a directory and no version found set resourceVersion as invalid
+ if (resourceVersion == null)
+ {
+ resourceVersion = VERSION_INVALID;
+ }
+ }
+ }
+ catch (URISyntaxException e)
+ {
+ Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
+ if (log.isLoggable(Level.WARNING))
+ {
+ log.log(Level.WARNING, "url "+url.toString()+" cannot be translated to uri: "+e.getMessage(), e);
+ }
+ }
+ }
+ else if (isJarResourceProtocol(url.getProtocol()))
+ {
+ try
+ {
+ url = getClassLoader().getResource(path + '/');
+
+ if (url != null)
+ {
+ JarURLConnection conn = (JarURLConnection)url.openConnection();
+ // See DIGESTER-29 for related problem
+ conn.setUseCaches(false);
+
+ try
+ {
+ if (conn.getJarEntry().isDirectory())
+ {
+ // Unfortunately, we have to scan all entry files
+ JarFile file = conn.getJarFile();
+ for (Enumeration<JarEntry> en = file.entries(); en.hasMoreElements();)
+ {
+ JarEntry entry = en.nextElement();
+ String entryName = entry.getName();
+
+ if (entryName.startsWith(path + '/'))
+ {
+ if (entryName.length() == path.length() + 1)
+ {
+ // the same string, just skip it
+ continue;
+ }
+
+ entryName = entryName.substring(path.length());
+ if (RESOURCE_VERSION_CHECKER.matcher(entryName).matches())
+ {
+ String version = entryName.substring(1, entryName.lastIndexOf('.'));
+ if (resourceVersion == null)
+ {
+ resourceVersion = version;
+ }
+ else if (getVersionComparator().compare(resourceVersion, version) < 0)
+ {
+ resourceVersion = version;
+ }
+ }
+ }
+ }
+ if (resourceVersion == null)
+ {
+ resourceVersion = VERSION_INVALID;
+ }
+ }
+ }
+ finally
+ {
+ //See TRINIDAD-73
+ //just close the input stream again if
+ //by inspecting the entries the stream
+ //was let open.
+ try
+ {
+ conn.getInputStream().close();
+ }
+ catch (Exception exception)
+ {
+ // Ignored
+ }
+ }
+
+ }
+ }
+ catch (IOException e)
+ {
+ // Just return null, because library version cannot be
+ // resolved.
+ Logger log = Logger.getLogger(ClassLoaderResourceLoader.class.getName());
+ if (log.isLoggable(Level.WARNING))
+ {
+ log.log(Level.WARNING, "IOException when scanning for resource in jar file:", e);
+ }
+ }
+ }
+ return resourceVersion;
+ */
+ }
+
+ @Override
+ public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
+ String resourceName, String resourceVersion)
+ {
+ if (_developmentStage && libraryName != null &&
+ ResourceUtils.JAVAX_FACES_LIBRARY_NAME.equals(libraryName) &&
+ ResourceUtils.JSF_JS_RESOURCE_NAME.equals(resourceName))
+ {
+ // InternalClassLoaderResourceLoader will serve it, so return null in this case.
+ return null;
+ } else if (_developmentStage && libraryName != null &&
+ ResourceUtils.MYFACES_LIBRARY_NAME.equals(libraryName) &&
+ ResourceUtils.MYFACES_JS_RESOURCE_NAME.equals(resourceName)) {
+ // InternalClassLoaderResourceLoader will serve it, so return null in this case.
+ return null;
+ } else
+ {
+ return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
+ }
+ }
+
+ /**
+ * Returns the ClassLoader to use when looking up resources under the top level package. By default, this is the
+ * context class loader.
+ *
+ * @return the ClassLoader used to lookup resources
+ */
+ protected ClassLoader getClassLoader()
+ {
+ return ClassUtils.getContextClassLoader();
+ }
+
+ @Override
+ public boolean libraryExists(String libraryName)
+ {
+ if (getPrefix() != null && !"".equals(getPrefix()))
+ {
+ URL url = getClassLoader().getResource(getPrefix() + '/' + libraryName);
+ if (url == null)
+ {
+ url = this.getClass().getClassLoader().getResource(getPrefix() + '/' + libraryName);
+ }
+ if (url != null)
+ {
+ return true;
+ }
+ }
+ else
+ {
+ URL url = getClassLoader().getResource(libraryName);
+ if (url == null)
+ {
+ url = this.getClass().getClassLoader().getResource(libraryName);
+ }
+ if (url != null)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * <p>Determines whether the given URL resource protocol refers to a JAR file. Note that
+ * BEA WebLogic and IBM WebSphere don't use the "jar://" protocol for some reason even
+ * though you can treat these resources just like normal JAR files, i.e. you can ignore
+ * the difference between these protocols after this method has returned.</p>
+ *
+ * @param protocol the URL resource protocol you want to check
+ *
+ * @return <code>true</code> if the given URL resource protocol refers to a JAR file,
+ * <code>false</code> otherwise
+ */
+ /*
+ private static boolean isJarResourceProtocol(String protocol)
+ {
+ // Websphere uses the protocol "wsjar://" and Weblogic uses the protocol "zip://".
+ return "jar".equals(protocol) || "wsjar".equals(protocol) || "zip".equals(protocol);
+ }*/
+
+}
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ClassLoaderResourceLoader.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java?rev=1187741&r1=1187740&r2=1187741&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java Sat Oct 22 17:59:32 2011
@@ -1,206 +1,206 @@
-/*
- * 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.MalformedURLException;
-import java.net.URL;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.regex.Pattern;
-
-import javax.faces.context.FacesContext;
-
-/**
- * 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.shared.resource;
+
+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;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * 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;
+ }
+}
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ExternalContextResourceLoader.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL