001 // Copyright 2006, 2007, 2008, 2009, 2010 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.tapestry5.internal.services; 016 017 import org.apache.tapestry5.Asset; 018 import org.apache.tapestry5.ioc.Resource; 019 import org.apache.tapestry5.services.AssetFactory; 020 import org.apache.tapestry5.services.AssetPathConverter; 021 import org.apache.tapestry5.services.Context; 022 import org.apache.tapestry5.services.assets.AssetPathConstructor; 023 024 /** 025 * Implementation of {@link AssetFactory} for assets that are part of the web application context. 026 * 027 * @see org.apache.tapestry5.internal.services.ContextResource 028 */ 029 public class ContextAssetFactory implements AssetFactory 030 { 031 private final AssetPathConstructor assetPathConstructor; 032 033 private final Resource rootResource; 034 035 private final AssetPathConverter converter; 036 037 private final boolean invariant; 038 039 public ContextAssetFactory(AssetPathConstructor assetPathConstructor, Context context, 040 041 AssetPathConverter converter) 042 { 043 this.assetPathConstructor = assetPathConstructor; 044 this.converter = converter; 045 046 rootResource = new ContextResource(context, "/"); 047 invariant = this.converter.isInvariant(); 048 } 049 050 public Asset createAsset(Resource resource) 051 { 052 String defaultPath = assetPathConstructor.constructAssetPath(RequestConstants.CONTEXT_FOLDER, resource.getPath()); 053 054 if (invariant) 055 { 056 return createInvariantAsset(resource, defaultPath); 057 } 058 059 return createVariantAsset(resource, defaultPath); 060 } 061 062 private Asset createInvariantAsset(final Resource resource, final String defaultPath) 063 { 064 return new AbstractAsset(true) 065 { 066 private String clientURL; 067 068 public Resource getResource() 069 { 070 return resource; 071 } 072 073 public String toClientURL() 074 { 075 if (clientURL == null) 076 { 077 clientURL = converter.convertAssetPath(defaultPath); 078 } 079 080 return clientURL; 081 } 082 }; 083 } 084 085 private Asset createVariantAsset(final Resource resource, final String defaultPath) 086 { 087 return new AbstractAsset(false) 088 { 089 public Resource getResource() 090 { 091 return resource; 092 } 093 094 public String toClientURL() 095 { 096 return converter.convertAssetPath(defaultPath); 097 } 098 }; 099 } 100 101 /** 102 * Returns the root {@link org.apache.tapestry5.internal.services.ContextResource}. 103 */ 104 public Resource getRootResource() 105 { 106 return rootResource; 107 } 108 }