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.ClassResolver;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.Resource;
023    import org.apache.hivemind.util.ClasspathResource;
024    import org.apache.tapestry.IAsset;
025    import org.apache.tapestry.engine.IEngineService;
026    
027    /**
028     * Creates instances of {@link org.apache.tapestry.asset.PrivateAsset}, which are the holders of
029     * classpath: resources.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    public class ClasspathAssetFactory implements AssetFactory
035    {
036        private ClassResolver _classResolver;
037    
038        private IEngineService _assetService;
039    
040        public IAsset createAsset(Resource baseResource, String path, Locale locale, Location location)
041        {
042            Resource asset = baseResource.getRelativeResource(path);
043            Resource localized = asset.getLocalization(locale);
044    
045            if (localized == null)
046                throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource),
047                        location, null);
048    
049            return createAsset(localized, location);
050        }
051    
052        public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
053        {
054            Resource base = new ClasspathResource(_classResolver, path);
055            Resource localized = base.getLocalization(locale);
056    
057            if (localized == null)
058                throw new ApplicationRuntimeException(AssetMessages.missingClasspathResource(path),
059                        location, null);
060    
061            return createAsset(localized, location);
062        }
063    
064        public IAsset createAsset(Resource resource, Location location)
065        {
066            ClasspathResource cr = (ClasspathResource) resource;
067    
068            return new PrivateAsset(cr, _assetService, location);
069        }
070    
071        public void setAssetService(IEngineService assetService)
072        {
073            _assetService = assetService;
074        }
075    
076        public void setClassResolver(ClassResolver classResolver)
077        {
078            _classResolver = classResolver;
079        }
080    }