1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.asset; |
16 | |
|
17 | |
import org.apache.hivemind.Location; |
18 | |
import org.apache.hivemind.Resource; |
19 | |
import org.apache.hivemind.util.Defense; |
20 | |
import org.apache.tapestry.IAsset; |
21 | |
import org.apache.tapestry.spec.IComponentSpecification; |
22 | |
|
23 | |
import java.util.*; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class AssetSourceImpl implements AssetSource |
30 | |
{ |
31 | 0 | private Map _assetFactoryByPrefix = new HashMap(); |
32 | |
|
33 | |
private List _contributions; |
34 | |
|
35 | |
private AssetFactory _defaultAssetFactory; |
36 | |
|
37 | |
private AssetFactory _lookupAssetFactory; |
38 | |
|
39 | |
private AssetFactory _classpathAssetFactory; |
40 | |
|
41 | |
private AssetFactory _contextAssetFactory; |
42 | |
|
43 | |
public void initializeService() |
44 | |
{ |
45 | 0 | Iterator i = _contributions.iterator(); |
46 | 0 | while (i.hasNext()) |
47 | |
{ |
48 | 0 | AssetFactoryContribution c = (AssetFactoryContribution) i.next(); |
49 | |
|
50 | 0 | _assetFactoryByPrefix.put(c.getPrefix(), c.getFactory()); |
51 | 0 | } |
52 | 0 | } |
53 | |
|
54 | |
public IAsset findAsset(Resource base, String path, Locale locale, Location location) |
55 | |
{ |
56 | 0 | return findAsset(base, null, path, locale, location); |
57 | |
} |
58 | |
|
59 | |
public IAsset findAsset(Resource base, IComponentSpecification spec, String path, Locale locale, Location location) |
60 | |
{ |
61 | 0 | Defense.notNull(path, "path"); |
62 | 0 | Defense.notNull(location, "location"); |
63 | |
|
64 | 0 | int colonx = path.indexOf(':'); |
65 | |
|
66 | 0 | String prefix = colonx > -1 ? path.substring(0, colonx) : null; |
67 | 0 | String truePath = colonx > -1 ? path.substring(colonx + 1) : path; |
68 | |
|
69 | 0 | AssetFactory factory = null; |
70 | |
|
71 | 0 | if (prefix != null) { |
72 | |
|
73 | 0 | factory = (AssetFactory) _assetFactoryByPrefix.get(prefix); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | if (factory == null && prefix == null) { |
79 | |
|
80 | 0 | factory = findAssetFactory(spec, base, truePath, locale); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 0 | if (factory == null) |
87 | |
{ |
88 | 0 | factory = _defaultAssetFactory; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 0 | truePath = path; |
94 | |
} |
95 | |
|
96 | 0 | if (truePath.startsWith("/")) |
97 | 0 | return factory.createAbsoluteAsset(truePath, locale, location); |
98 | |
|
99 | |
|
100 | |
|
101 | 0 | return factory.createAsset(base, spec, truePath, locale, location); |
102 | |
} |
103 | |
|
104 | |
AssetFactory findAssetFactory(IComponentSpecification spec, Resource baseResource, String path, Locale locale) |
105 | |
{ |
106 | |
|
107 | |
|
108 | 0 | if (_classpathAssetFactory.assetExists(spec, baseResource, path, locale)) |
109 | 0 | return _classpathAssetFactory; |
110 | |
|
111 | 0 | if (_contextAssetFactory.assetExists(spec, baseResource, path, locale)) |
112 | 0 | return _contextAssetFactory; |
113 | |
|
114 | 0 | for (int i=0; i < _contributions.size(); i++) { |
115 | |
|
116 | 0 | AssetFactoryContribution c = (AssetFactoryContribution)_contributions.get(i); |
117 | |
|
118 | 0 | if (c.getFactory().assetExists(spec, baseResource, path, locale)) |
119 | 0 | return c.getFactory(); |
120 | |
} |
121 | |
|
122 | 0 | return null; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void setLookupAssetFactory(AssetFactory lookupAssetFactory) |
131 | |
{ |
132 | 0 | _lookupAssetFactory = lookupAssetFactory; |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public void setContributions(List contributions) |
140 | |
{ |
141 | 0 | _contributions = contributions; |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void setDefaultAssetFactory(AssetFactory defaultAssetFactory) |
149 | |
{ |
150 | 0 | _defaultAssetFactory = defaultAssetFactory; |
151 | 0 | } |
152 | |
|
153 | |
public void setClasspathAssetFactory(AssetFactory classpathAssetFactory) |
154 | |
{ |
155 | 0 | _classpathAssetFactory = classpathAssetFactory; |
156 | 0 | } |
157 | |
|
158 | |
public void setContextAssetFactory(AssetFactory contextAssetFactory) |
159 | |
{ |
160 | 0 | _contextAssetFactory = contextAssetFactory; |
161 | 0 | } |
162 | |
} |