1 15 package org.apache.tapestry.asset; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.Resource; 25 import org.apache.hivemind.util.Defense; 26 import org.apache.tapestry.IAsset; 27 28 34 public class AssetSourceImpl implements AssetSource 35 { 36 private Map _assetFactoryByPrefix = new HashMap (); 37 38 private List _contributions; 39 40 private AssetFactory _defaultAssetFactory; 41 42 private AssetFactory _lookupAssetFactory; 43 44 public void initializeService() 45 { 46 Iterator i = _contributions.iterator(); 47 while (i.hasNext()) 48 { 49 AssetFactoryContribution c = (AssetFactoryContribution) i.next(); 50 51 _assetFactoryByPrefix.put(c.getPrefix(), c.getFactory()); 52 } 53 } 54 55 public IAsset findAsset(Resource base, String path, Locale locale, Location location) 56 { 57 Defense.notNull(base, "base"); 58 Defense.notNull(path, "path"); 59 Defense.notNull(locale, "locale"); 60 Defense.notNull(location, "location"); 61 62 int colonx = path.indexOf(':'); 63 64 if (colonx < 0) 65 return _lookupAssetFactory.createAsset(base, path, locale, location); 66 67 String prefix = path.substring(0, colonx); 68 String truePath = path.substring(colonx + 1); 69 70 AssetFactory factory = (AssetFactory) _assetFactoryByPrefix.get(prefix); 71 72 75 if (factory == null) 76 { 77 factory = _defaultAssetFactory; 78 79 82 truePath = path; 83 } 84 85 return factory.createAsset(base, truePath, locale, location); 86 } 87 88 92 93 public void setLookupAssetFactory(AssetFactory lookupAssetFactory) 94 { 95 _lookupAssetFactory = lookupAssetFactory; 96 } 97 98 101 102 public void setContributions(List contributions) 103 { 104 _contributions = contributions; 105 } 106 107 110 111 public void setDefaultAssetFactory(AssetFactory defaultAssetFactory) 112 { 113 _defaultAssetFactory = defaultAssetFactory; 114 } 115 116 } | Popular Tags |