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 |
4 |
public class AssetSourceImpl implements AssetSource |
30 |
|
{ |
31 |
4 |
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 |
2 |
Iterator i = _contributions.iterator(); |
46 |
4 |
while (i.hasNext()) |
47 |
|
{ |
48 |
2 |
AssetFactoryContribution c = (AssetFactoryContribution) i.next(); |
49 |
|
|
50 |
2 |
_assetFactoryByPrefix.put(c.getPrefix(), c.getFactory()); |
51 |
2 |
} |
52 |
2 |
} |
53 |
|
|
54 |
|
public IAsset findAsset(Resource base, String path, Locale locale, Location location) |
55 |
|
{ |
56 |
4 |
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 |
4 |
Defense.notNull(path, "path"); |
62 |
4 |
Defense.notNull(location, "location"); |
63 |
|
|
64 |
4 |
int colonx = path.indexOf(':'); |
65 |
|
|
66 |
4 |
String prefix = colonx > -1 ? path.substring(0, colonx) : null; |
67 |
4 |
String truePath = colonx > -1 ? path.substring(colonx + 1) : path; |
68 |
|
|
69 |
4 |
AssetFactory factory = null; |
70 |
|
|
71 |
4 |
if (prefix != null) { |
72 |
|
|
73 |
3 |
factory = (AssetFactory) _assetFactoryByPrefix.get(prefix); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
4 |
if (factory == null && prefix == null) { |
79 |
|
|
80 |
1 |
factory = findAssetFactory(spec, base, truePath, locale); |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
4 |
if (factory == null) |
87 |
|
{ |
88 |
1 |
factory = _defaultAssetFactory; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
1 |
truePath = path; |
94 |
|
} |
95 |
|
|
96 |
4 |
if (truePath.startsWith("/")) |
97 |
0 |
return factory.createAbsoluteAsset(truePath, locale, location); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
4 |
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 |
1 |
if (_classpathAssetFactory.assetExists(spec, baseResource, path, locale)) |
109 |
1 |
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 |
2 |
_contributions = contributions; |
142 |
2 |
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
public void setDefaultAssetFactory(AssetFactory defaultAssetFactory) |
149 |
|
{ |
150 |
1 |
_defaultAssetFactory = defaultAssetFactory; |
151 |
1 |
} |
152 |
|
|
153 |
|
public void setClasspathAssetFactory(AssetFactory classpathAssetFactory) |
154 |
|
{ |
155 |
1 |
_classpathAssetFactory = classpathAssetFactory; |
156 |
1 |
} |
157 |
|
|
158 |
|
public void setContextAssetFactory(AssetFactory contextAssetFactory) |
159 |
|
{ |
160 |
0 |
_contextAssetFactory = contextAssetFactory; |
161 |
0 |
} |
162 |
|
} |