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 org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.ClassResolver; 019 import org.apache.hivemind.Location; 020 import org.apache.hivemind.Resource; 021 import org.apache.hivemind.util.ClasspathResource; 022 import org.apache.tapestry.IAsset; 023 import org.apache.tapestry.engine.IEngineService; 024 import org.apache.tapestry.l10n.ResourceLocalizer; 025 import org.apache.tapestry.spec.IComponentSpecification; 026 027 import java.util.Locale; 028 029 /** 030 * Creates instances of {@link org.apache.tapestry.asset.PrivateAsset}, which are the holders of 031 * classpath: resources. 032 * 033 * @author Howard M. Lewis Ship 034 * @since 4.0 035 */ 036 public class ClasspathAssetFactory implements AssetFactory 037 { 038 private ClassResolver _classResolver; 039 040 private IEngineService _assetService; 041 042 private ResourceLocalizer _localizer; 043 044 private Resource _rootClassPath; 045 046 public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale) 047 { 048 Resource assetResource = null; 049 if (path.startsWith("/")) { 050 051 if (_rootClassPath == null) { 052 _rootClassPath = new ClasspathResource(_classResolver, ""); 053 } 054 055 assetResource = _rootClassPath.getRelativeResource(path); 056 } else { 057 058 assetResource = baseResource.getRelativeResource(path); 059 } 060 061 Resource localized = _localizer.findLocalization(assetResource, locale); 062 063 return localized != null; 064 } 065 066 public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location) 067 { 068 Resource asset = baseResource.getRelativeResource(path); 069 Resource localized = _localizer.findLocalization(asset, locale); 070 071 if (localized == null) 072 throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource), 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 ClasspathResource(_classResolver, path); 080 Resource localized = _localizer.findLocalization(base, locale); 081 082 if (localized == null) 083 throw new ApplicationRuntimeException(AssetMessages.missingClasspathResource(path), location, null); 084 085 return createAsset(localized, location); 086 } 087 088 public IAsset createAsset(Resource resource, Location location) 089 { 090 return new PrivateAsset(resource, _assetService, location); 091 } 092 093 public void setAssetService(IEngineService assetService) 094 { 095 _assetService = assetService; 096 } 097 098 public void setClassResolver(ClassResolver classResolver) 099 { 100 _classResolver = classResolver; 101 } 102 103 public void setLocalizer(ResourceLocalizer localizer) 104 { 105 _localizer = localizer; 106 } 107 }