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