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.asset;
016    
017    import java.util.Locale;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.Resource;
022    import org.apache.tapestry.IAsset;
023    import org.apache.tapestry.IRequestCycle;
024    import org.apache.tapestry.l10n.ResourceLocalizer;
025    import org.apache.tapestry.web.WebContext;
026    import org.apache.tapestry.web.WebContextResource;
027    
028    /**
029     * All "context:" prefixed asset paths are interpreted relative to the web context (the web
030     * application's root folder).
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    public class ContextAssetFactory implements AssetFactory
036    {
037        private String _contextPath;
038    
039        private AssetFactory _classpathAssetFactory;
040    
041        private WebContext _webContext;
042    
043        private ResourceLocalizer _localizer;
044    
045        private IRequestCycle _requestCycle;
046    
047        public void setWebContext(WebContext webContext)
048        {
049            _webContext = webContext;
050        }
051    
052        public IAsset createAsset(Resource baseResource, String path, Locale locale, Location location)
053        {
054            // We always create a new asset relative to an existing resource; the type of resource
055            // will jive with the type of asset returned. Path may start with a leading slash, which
056            // yields an absolute, not relative, path to the resource.
057    
058            Resource assetResource = baseResource.getRelativeResource(path);
059    
060            // Here's the thing; In Tapestry 3.0 and earlier, you could specify
061            // library path like /org/apache/tapestry/contrib/Contrib.library. In the new scheme
062            // of things, that should be "classpath:/org/apache/tapestry/contrib/Contrib.library".
063            // But to keep a lot of things from breaking, we'll kludgely allow that here.
064    
065            if (assetResource.getResourceURL() == null && path.startsWith("/"))
066                return _classpathAssetFactory.createAbsoluteAsset(path, locale, location);
067    
068            Resource localized = _localizer.findLocalization(assetResource, locale);
069    
070            if (localized == null)
071                throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource),
072                        location, null);
073    
074            return createAsset(localized, location);
075        }
076    
077        public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
078        {
079            Resource base = new WebContextResource(_webContext, path);
080            Resource localized = _localizer.findLocalization(base, locale);
081    
082            if (localized == null)
083                throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path),
084                        location, null);
085    
086            return createAsset(localized, location);
087        }
088    
089        public IAsset createAsset(Resource resource, Location location)
090        {
091            return new ContextAsset(_contextPath, resource, location, _requestCycle);
092        }
093    
094        public void setContextPath(String contextPath)
095        {
096            _contextPath = contextPath;
097        }
098    
099        public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
100        {
101            _classpathAssetFactory = classpathAssetFactory;
102        }
103    
104        public void setLocalizer(ResourceLocalizer localizer)
105        {
106            _localizer = localizer;
107        }
108    
109        public void setRequestCycle(IRequestCycle cycle)
110        {
111            _requestCycle = cycle;
112        }
113    }