1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.spec; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.Location; |
19 |
|
import org.apache.hivemind.Resource; |
20 |
|
import org.apache.hivemind.util.ToStringBuilder; |
21 |
|
import org.apache.tapestry.Tapestry; |
22 |
|
|
23 |
|
import java.util.*; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
53 |
public class LibrarySpecification extends LocatablePropertyHolder implements ILibrarySpecification |
35 |
|
{ |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
private Map _pages; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private Map _components; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private Map _libraries; |
53 |
|
|
54 |
|
private String _description; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private Map _extensions; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private Map _instantiatedExtensions; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private String _publicId; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private Resource _specificationLocation; |
82 |
|
|
83 |
|
public String getLibrarySpecificationPath(String id) |
84 |
|
{ |
85 |
0 |
return (String) get(_libraries, id); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
public void setLibrarySpecificationPath(String id, String path) |
96 |
|
{ |
97 |
3 |
if (_libraries == null) _libraries = new HashMap(); |
98 |
|
|
99 |
3 |
if (_libraries.containsKey(id)) |
100 |
0 |
throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-child-namespace-id", id)); |
101 |
|
|
102 |
3 |
_libraries.put(id, path); |
103 |
3 |
} |
104 |
|
|
105 |
|
public List getLibraryIds() |
106 |
|
{ |
107 |
1 |
return sortedKeys(_libraries); |
108 |
|
} |
109 |
|
|
110 |
|
public String getPageSpecificationPath(String name) |
111 |
|
{ |
112 |
0 |
return (String) get(_pages, name); |
113 |
|
} |
114 |
|
|
115 |
|
public void setPageSpecificationPath(String name, String path) |
116 |
|
{ |
117 |
18 |
if (_pages == null) |
118 |
4 |
_pages = new HashMap(); |
119 |
|
|
120 |
18 |
if (_pages.containsKey(name)) |
121 |
0 |
throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-page-name", name)); |
122 |
|
|
123 |
18 |
_pages.put(name, path); |
124 |
18 |
} |
125 |
|
|
126 |
|
public List getPageNames() |
127 |
|
{ |
128 |
1 |
return sortedKeys(_pages); |
129 |
|
} |
130 |
|
|
131 |
|
public void setComponentSpecificationPath(String alias, String path) |
132 |
|
{ |
133 |
9 |
if (_components == null) |
134 |
5 |
_components = new HashMap(); |
135 |
|
|
136 |
9 |
if (_components.containsKey(alias)) |
137 |
0 |
throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-component-alias", alias)); |
138 |
|
|
139 |
9 |
_components.put(alias, path); |
140 |
9 |
} |
141 |
|
|
142 |
|
public String getComponentSpecificationPath(String alias) |
143 |
|
{ |
144 |
2 |
return (String) get(_components, alias); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
public List getComponentTypes() |
152 |
|
{ |
153 |
2 |
return sortedKeys(_components); |
154 |
|
} |
155 |
|
|
156 |
|
private List sortedKeys(Map map) |
157 |
|
{ |
158 |
4 |
if (map == null) |
159 |
0 |
return Collections.EMPTY_LIST; |
160 |
|
|
161 |
4 |
List result = new ArrayList(map.keySet()); |
162 |
|
|
163 |
4 |
Collections.sort(result); |
164 |
|
|
165 |
4 |
return result; |
166 |
|
} |
167 |
|
|
168 |
|
private Object get(Map map, Object key) |
169 |
|
{ |
170 |
2 |
if (map == null) |
171 |
0 |
return null; |
172 |
|
|
173 |
2 |
return map.get(key); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
public String getDescription() |
181 |
|
{ |
182 |
1 |
return _description; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
public void setDescription(String description) |
190 |
|
{ |
191 |
1 |
_description = description; |
192 |
1 |
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
public Map getExtensionSpecifications() |
201 |
|
{ |
202 |
0 |
if (_extensions == null) |
203 |
0 |
return null; |
204 |
|
|
205 |
0 |
return Collections.unmodifiableMap(_extensions); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
public void addExtensionSpecification(String name, IExtensionSpecification extension) |
215 |
|
{ |
216 |
8 |
if (_extensions == null) |
217 |
8 |
_extensions = new HashMap(); |
218 |
|
|
219 |
8 |
if (_extensions.containsKey(name)) |
220 |
0 |
throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-extension-name", |
221 |
|
this, name)); |
222 |
|
|
223 |
8 |
_extensions.put(name, extension); |
224 |
8 |
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
public synchronized List getExtensionNames() |
232 |
|
{ |
233 |
0 |
return sortedKeys(_instantiatedExtensions); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
public IExtensionSpecification getExtensionSpecification(String name) |
241 |
|
{ |
242 |
9 |
if (_extensions == null) |
243 |
0 |
return null; |
244 |
|
|
245 |
9 |
return (IExtensionSpecification) _extensions.get(name); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
public boolean checkExtension(String name) |
254 |
|
{ |
255 |
0 |
if (_extensions == null) |
256 |
0 |
return false; |
257 |
|
|
258 |
0 |
return _extensions.containsKey(name); |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
public synchronized Object getExtension(String name) |
270 |
|
{ |
271 |
2 |
return getExtension(name, null); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
public synchronized Object getExtension(String name, Class typeConstraint) |
277 |
|
{ |
278 |
5 |
if (_instantiatedExtensions == null) |
279 |
5 |
_instantiatedExtensions = new HashMap(); |
280 |
|
|
281 |
5 |
Object result = _instantiatedExtensions.get(name); |
282 |
5 |
IExtensionSpecification spec = getExtensionSpecification(name); |
283 |
|
|
284 |
5 |
if (spec == null) |
285 |
0 |
throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.no-such-extension", name)); |
286 |
|
|
287 |
5 |
if (result == null) |
288 |
|
{ |
289 |
|
|
290 |
5 |
result = spec.instantiateExtension(); |
291 |
|
|
292 |
5 |
_instantiatedExtensions.put(name, result); |
293 |
|
} |
294 |
|
|
295 |
5 |
if (typeConstraint != null) |
296 |
3 |
applyTypeConstraint(name, result, typeConstraint, spec.getLocation()); |
297 |
|
|
298 |
4 |
return result; |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
protected void applyTypeConstraint(String name, Object extension, |
319 |
|
Class typeConstraint, Location location) |
320 |
|
{ |
321 |
3 |
Class extensionClass = extension.getClass(); |
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
3 |
if (typeConstraint.isAssignableFrom(extensionClass)) |
327 |
2 |
return; |
328 |
|
|
329 |
1 |
String key = typeConstraint.isInterface() |
330 |
|
? "LibrarySpecification.extension-does-not-implement-interface" |
331 |
|
: "LibrarySpecification.extension-not-a-subclass"; |
332 |
|
|
333 |
1 |
throw new ApplicationRuntimeException( |
334 |
|
Tapestry.format(key, name, extensionClass.getName(), typeConstraint.getName()), location, null); |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
public synchronized void instantiateImmediateExtensions() |
343 |
|
{ |
344 |
15 |
if (_extensions == null) |
345 |
7 |
return; |
346 |
|
|
347 |
8 |
Iterator i = _extensions.entrySet().iterator(); |
348 |
|
|
349 |
16 |
while(i.hasNext()) |
350 |
|
{ |
351 |
8 |
Map.Entry entry = (Map.Entry) i.next(); |
352 |
|
|
353 |
8 |
IExtensionSpecification spec = (IExtensionSpecification) entry.getValue(); |
354 |
|
|
355 |
8 |
if (!spec.isImmediate()) |
356 |
7 |
continue; |
357 |
|
|
358 |
1 |
String name = (String) entry.getKey(); |
359 |
|
|
360 |
1 |
getExtension(name); |
361 |
1 |
} |
362 |
|
|
363 |
8 |
} |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
protected Map getExtensions() |
372 |
|
{ |
373 |
0 |
return _extensions; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
protected void setExtensions(Map extension) |
386 |
|
{ |
387 |
0 |
_extensions = extension; |
388 |
0 |
} |
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
protected Map getLibraries() |
397 |
|
{ |
398 |
0 |
return _libraries; |
399 |
|
} |
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
protected void setLibraries(Map libraries) |
411 |
|
{ |
412 |
0 |
_libraries = libraries; |
413 |
0 |
} |
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
protected Map getPages() |
422 |
|
{ |
423 |
0 |
return _pages; |
424 |
|
} |
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
protected void setPages(Map pages) |
436 |
|
{ |
437 |
0 |
_pages = pages; |
438 |
0 |
} |
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
protected Map getComponents() |
447 |
|
{ |
448 |
0 |
return _components; |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
protected void setComponents(Map components) |
460 |
|
{ |
461 |
0 |
_components = components; |
462 |
0 |
} |
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
public String getPublicId() |
474 |
|
{ |
475 |
0 |
return _publicId; |
476 |
|
} |
477 |
|
|
478 |
|
public void setPublicId(String publicId) |
479 |
|
{ |
480 |
0 |
_publicId = publicId; |
481 |
0 |
} |
482 |
|
|
483 |
|
|
484 |
|
|
485 |
|
public Resource getSpecificationLocation() |
486 |
|
{ |
487 |
202 |
return _specificationLocation; |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
|
|
492 |
|
public void setSpecificationLocation(Resource specificationLocation) |
493 |
|
{ |
494 |
39 |
_specificationLocation = specificationLocation; |
495 |
39 |
} |
496 |
|
|
497 |
|
|
498 |
|
|
499 |
|
public synchronized String toString() |
500 |
|
{ |
501 |
0 |
ToStringBuilder builder = new ToStringBuilder(this); |
502 |
|
|
503 |
0 |
builder.append("components", _components); |
504 |
0 |
builder.append("description", _description); |
505 |
0 |
builder.append("instantiatedExtensions", _instantiatedExtensions); |
506 |
0 |
builder.append("libraries", _libraries); |
507 |
0 |
builder.append("pages", _pages); |
508 |
0 |
builder.append("publicId", _publicId); |
509 |
0 |
builder.append("specificationLocation", _specificationLocation); |
510 |
|
|
511 |
0 |
extendDescription(builder); |
512 |
|
|
513 |
0 |
return builder.toString(); |
514 |
|
} |
515 |
|
|
516 |
|
|
517 |
|
|
518 |
|
|
519 |
|
|
520 |
|
|
521 |
|
|
522 |
|
|
523 |
|
protected void extendDescription(ToStringBuilder builder) |
524 |
|
{ |
525 |
0 |
} |
526 |
|
|
527 |
|
} |