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