From adffaces-commits-return-2051-apmail-incubator-adffaces-commits-archive=incubator.apache.org@incubator.apache.org Tue Feb 20 18:17:15 2007 Return-Path: Delivered-To: apmail-incubator-adffaces-commits-archive@locus.apache.org Received: (qmail 75907 invoked from network); 20 Feb 2007 18:17:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 20 Feb 2007 18:17:15 -0000 Received: (qmail 56987 invoked by uid 500); 20 Feb 2007 18:17:19 -0000 Delivered-To: apmail-incubator-adffaces-commits-archive@incubator.apache.org Received: (qmail 56967 invoked by uid 500); 20 Feb 2007 18:17:18 -0000 Mailing-List: contact adffaces-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: adffaces-dev@incubator.apache.org Delivered-To: mailing list adffaces-commits@incubator.apache.org Received: (qmail 56955 invoked by uid 99); 20 Feb 2007 18:17:18 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Feb 2007 10:17:18 -0800 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Feb 2007 10:17:06 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 594FD1A981A; Tue, 20 Feb 2007 10:16:46 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r509697 - /incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java Date: Tue, 20 Feb 2007 18:16:46 -0000 To: adffaces-commits@incubator.apache.org From: jwaldman@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070220181646.594FD1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jwaldman Date: Tue Feb 20 10:16:45 2007 New Revision: 509697 URL: http://svn.apache.org/viewvc?view=rev&rev=509697 Log: code cleanup in StyleSheetNameResolver. I basically implemented what Adam suggested in this comment and got rid of the servletContext and that code path to look for files. The URL way was sufficient. // =-=AEW This is a holdover from ancient days; it should // be sufficient to use URL-based access ServletContext servletContext = _getServletContext(); Modified: incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java Modified: incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java URL: http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java?view=diff&rev=509697&r1=509696&r2=509697 ============================================================================== --- incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java (original) +++ incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/StyleSheetNameResolver.java Tue Feb 20 10:16:45 2007 @@ -26,7 +26,6 @@ import java.net.URL; import javax.faces.context.FacesContext; -import javax.servlet.ServletContext; import org.apache.myfaces.trinidad.util.ClassLoaderUtils; @@ -66,14 +65,7 @@ return null; } - // Load the ServletContext so we can try getRealPath(); but - // we also use ExternalContext to get URLs - // =-=AEW This is a holdover from ancient days; it should - // be sufficient to use URL-based access - ServletContext servletContext = _getServletContext(); - - return new StyleSheetNameResolver(localStylesDir, - servletContext); + return new StyleSheetNameResolver(localStylesDir); } /** @@ -81,11 +73,9 @@ * styles directories. Note that the constructor is private since * StyleSheetEntry always calls createResolver(). * @param localStylesDirectory The location of the local styles directory - * @param sharedStylesDirectory The location of the shared styles directory */ private StyleSheetNameResolver( - File localStylesDirectory, - ServletContext servletContext + File localStylesDirectory ) { // We should always have some directory @@ -93,21 +83,36 @@ _localStylesDir = localStylesDirectory; - _servletContext = servletContext; } /** * Implementation of NameResolver.getProvider(). + * Given the name of the file, create an InputStreamProvider. I */ public InputStreamProvider getProvider(String name) throws IOException { - File file = _resolveFile(name); + File file = _resolveLocalFile(name); if (file != null) return new FileInputStreamProvider(file); - - URL url = _resolveURL(name); + + // Gets an URL for the specified name. + // Try a few different means to get the file as an url and then create the appropriate + // InputStreamProvider from that URL. + URL url = _resolveNonStaticURL(name); if (url != null) - return new StaticURLInputStreamProvider(url); + return new URLInputStreamProvider(url); + else + { + // see if it is an URL that can be loaded by the ClassLoader. + // We create a StaticURLInputStreamProvider from the url because we consider the + // url static because it can't be changed without restarting the server, so we don't + // need to check if the source has changed. + url = _resolveClassLoaderURL(name); + if (url != null) + return new StaticURLInputStreamProvider(url); + } + + // If we couldn't locate the file, throw an IOException throw new FileNotFoundException(_getFileNotFoundMessage(name)); @@ -119,58 +124,49 @@ public NameResolver getResolver(String name) { URL url = null; - File file = _resolveFile(name); + File file = _resolveLocalFile(name); if (file == null) - url = _resolveURL(name); + { + // Gets an URL for the specified name. + // Try a few different means to get the file as an url: + // new URL, ExternalContext's getResource, ClassLoaderUtils getResource + + url = _resolveNonStaticURL(name); + if (url == null) + url =_resolveClassLoaderURL(name); + } // Just use a DefaultNameResolver to resolve relative files return new DefaultNameResolver(file, url); } // Gets a File for the specified name, or returns null if no file exists - private File _resolveFile(String name) + // Try the local styles directory. + private File _resolveLocalFile(String name) { - // First try to local styles directory - File file = _createFile(_localStylesDir, name); - if (file != null) + // Try the local styles directory + File file = new File(_localStylesDir, name); + if (file.exists()) return file; - - // Finally, try relative to the context root - if (_servletContext != null) - { - // Use ServletContext.getRealPath() to locate the file. - String rootName = _getRootName(name); - String realPath = _servletContext.getRealPath(rootName); - - if (realPath != null) - { - File realFile = new File(realPath); - if (realFile.exists()) - return realFile; - } - } - return null; } - // Creates the File for the specified base directory/file name, - // assuming the file exists. Otherwise, returns null; - private File _createFile(File baseDir, String name) + // Gets an URL for the specified name using ClassLoaderUtils.getResource + private URL _resolveClassLoaderURL(String name) { - File file = new File(baseDir, name); - if (file.exists()) - return file; - - return null; + if (name == null) + return null; + return ClassLoaderUtils.getResource(name); + } - - // Gets an URL for the specified name - private URL _resolveURL(String name) + + // Gets an URL for the non static urls -- that is, urls that could change after the + // server has started. + private URL _resolveNonStaticURL(String name) { if (name == null) return null; - FacesContext fContext = FacesContext.getCurrentInstance(); if (fContext != null) { @@ -189,6 +185,8 @@ else { String rootName = _getRootName(name); + // Return a URL for the application resource mapped to the specified path, + // if it exists; otherwise, return null. URL url = fContext.getExternalContext().getResource(rootName); if (url != null) return url; @@ -200,9 +198,7 @@ ; } } - - - return ClassLoaderUtils.getResource(name); + return null; } // Construct error message for the specified file name @@ -220,14 +216,6 @@ buffer.append("), "); } - - if (_servletContext != null) - { - buffer.append("or in context root ("); - buffer.append(_servletContext.getRealPath("/")); - buffer.append("), "); - } - buffer.append("or on the class path.\n"); buffer.append("Please be sure that this style sheet is installed."); @@ -261,18 +249,6 @@ return null; } - // Private utility method for retrieving a ServletContext from - // a StyleContext - private static ServletContext _getServletContext() - { - FacesContext fContext = FacesContext.getCurrentInstance(); - Object app = fContext.getExternalContext().getContext(); - if (app instanceof ServletContext) - return (ServletContext) app; - - return null; - } - // Returns a name which can be resolved relative to the // ServletContext root. private static String _getRootName(String name) @@ -304,10 +280,6 @@ private File _localStylesDir; - - // We use the ServletContext to search for context-relative - // style sheets. - private ServletContext _servletContext; // Error messages private static final String _STYLES_DIR_ERROR =