001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.web;
016    
017    import org.apache.hivemind.Resource;
018    import org.apache.hivemind.util.AbstractResource;
019    import org.apache.hivemind.util.LocalizedResource;
020    
021    import javax.servlet.ServletContext;
022    import java.net.URL;
023    import java.util.Locale;
024    
025    /**
026     * Implementation of {@link org.apache.hivemind.Resource} for resources found within a
027     * {@link org.apache.tapestry.web.WebContext}.
028     * 
029     * @author Howard Lewis Ship
030     * @since 4.0
031     */
032    
033    public class WebContextResource extends AbstractResource
034    {
035        private WebContext _context;
036    
037        public WebContextResource(WebContext context, String path)
038        {
039            this(context, path, null);
040        }
041    
042        public WebContextResource(WebContext context, String path, Locale locale)
043        {
044            super(path, locale);
045    
046            _context = context;
047        }
048    
049        /**
050         * Locates the resource using {@link LocalizedWebContextResourceFinder}and
051         * {@link ServletContext#getResource(java.lang.String)}.
052         */
053    
054        public Resource getLocalization(Locale locale)
055        {
056            LocalizedWebContextResourceFinder finder = new LocalizedWebContextResourceFinder(_context);
057    
058            String path = getPath();
059            LocalizedResource localizedResource = finder.resolve(path, locale);
060    
061            if (localizedResource == null)
062                return null;
063    
064            String localizedPath = localizedResource.getResourcePath();
065            Locale pathLocale = localizedResource.getResourceLocale();
066    
067            if (localizedPath == null)
068                return null;
069    
070            if (path.equals(localizedPath))
071                return this;
072    
073            return new WebContextResource(_context, localizedPath, pathLocale);
074        }
075    
076        public URL getResourceURL()
077        {
078            return _context.getResource(getPath());
079        }
080    
081        public String toString()
082        {
083            return "context:" + getPath();
084        }
085    
086        public int hashCode()
087        {
088            return 2387 & getPath().hashCode();
089        }
090    
091        protected Resource newResource(String path)
092        {
093            return new WebContextResource(_context, path);
094        }
095    
096    }