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