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 | 0 | 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 | 0 | if (_libraries == null) _libraries = new HashMap(); |
98 | |
|
99 | 0 | if (_libraries.containsKey(id)) |
100 | 0 | throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-child-namespace-id", id)); |
101 | |
|
102 | 0 | _libraries.put(id, path); |
103 | 0 | } |
104 | |
|
105 | |
public List getLibraryIds() |
106 | |
{ |
107 | 0 | 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 | 0 | if (_pages == null) |
118 | 0 | _pages = new HashMap(); |
119 | |
|
120 | 0 | if (_pages.containsKey(name)) |
121 | 0 | throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-page-name", name)); |
122 | |
|
123 | 0 | _pages.put(name, path); |
124 | 0 | } |
125 | |
|
126 | |
public List getPageNames() |
127 | |
{ |
128 | 0 | return sortedKeys(_pages); |
129 | |
} |
130 | |
|
131 | |
public void setComponentSpecificationPath(String alias, String path) |
132 | |
{ |
133 | 0 | if (_components == null) |
134 | 0 | _components = new HashMap(); |
135 | |
|
136 | 0 | if (_components.containsKey(alias)) |
137 | 0 | throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-component-alias", alias)); |
138 | |
|
139 | 0 | _components.put(alias, path); |
140 | 0 | } |
141 | |
|
142 | |
public String getComponentSpecificationPath(String alias) |
143 | |
{ |
144 | 0 | return (String) get(_components, alias); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public List getComponentTypes() |
152 | |
{ |
153 | 0 | return sortedKeys(_components); |
154 | |
} |
155 | |
|
156 | |
private List sortedKeys(Map map) |
157 | |
{ |
158 | 0 | if (map == null) |
159 | 0 | return Collections.EMPTY_LIST; |
160 | |
|
161 | 0 | List result = new ArrayList(map.keySet()); |
162 | |
|
163 | 0 | Collections.sort(result); |
164 | |
|
165 | 0 | return result; |
166 | |
} |
167 | |
|
168 | |
private Object get(Map map, Object key) |
169 | |
{ |
170 | 0 | if (map == null) |
171 | 0 | return null; |
172 | |
|
173 | 0 | return map.get(key); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public String getDescription() |
181 | |
{ |
182 | 0 | return _description; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public void setDescription(String description) |
190 | |
{ |
191 | 0 | _description = description; |
192 | 0 | } |
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 | 0 | if (_extensions == null) |
217 | 0 | _extensions = new HashMap(); |
218 | |
|
219 | 0 | if (_extensions.containsKey(name)) |
220 | 0 | throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.duplicate-extension-name", |
221 | |
this, name)); |
222 | |
|
223 | 0 | _extensions.put(name, extension); |
224 | 0 | } |
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 | 0 | if (_extensions == null) |
243 | 0 | return null; |
244 | |
|
245 | 0 | 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 | 0 | return getExtension(name, null); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
public synchronized Object getExtension(String name, Class typeConstraint) |
277 | |
{ |
278 | 0 | if (_instantiatedExtensions == null) |
279 | 0 | _instantiatedExtensions = new HashMap(); |
280 | |
|
281 | 0 | Object result = _instantiatedExtensions.get(name); |
282 | 0 | IExtensionSpecification spec = getExtensionSpecification(name); |
283 | |
|
284 | 0 | if (spec == null) |
285 | 0 | throw new IllegalArgumentException(Tapestry.format("LibrarySpecification.no-such-extension", name)); |
286 | |
|
287 | 0 | if (result == null) |
288 | |
{ |
289 | |
|
290 | 0 | result = spec.instantiateExtension(); |
291 | |
|
292 | 0 | _instantiatedExtensions.put(name, result); |
293 | |
} |
294 | |
|
295 | 0 | if (typeConstraint != null) |
296 | 0 | applyTypeConstraint(name, result, typeConstraint, spec.getLocation()); |
297 | |
|
298 | 0 | 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 | 0 | Class extensionClass = extension.getClass(); |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | 0 | if (typeConstraint.isAssignableFrom(extensionClass)) |
327 | 0 | return; |
328 | |
|
329 | 0 | String key = typeConstraint.isInterface() |
330 | |
? "LibrarySpecification.extension-does-not-implement-interface" |
331 | |
: "LibrarySpecification.extension-not-a-subclass"; |
332 | |
|
333 | 0 | 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 | 0 | if (_extensions == null) |
345 | 0 | return; |
346 | |
|
347 | 0 | Iterator i = _extensions.entrySet().iterator(); |
348 | |
|
349 | 0 | while(i.hasNext()) |
350 | |
{ |
351 | 0 | Map.Entry entry = (Map.Entry) i.next(); |
352 | |
|
353 | 0 | IExtensionSpecification spec = (IExtensionSpecification) entry.getValue(); |
354 | |
|
355 | 0 | if (!spec.isImmediate()) |
356 | 0 | continue; |
357 | |
|
358 | 0 | String name = (String) entry.getKey(); |
359 | |
|
360 | 0 | getExtension(name); |
361 | 0 | } |
362 | |
|
363 | 0 | } |
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 | 0 | return _specificationLocation; |
488 | |
} |
489 | |
|
490 | |
|
491 | |
|
492 | |
public void setSpecificationLocation(Resource specificationLocation) |
493 | |
{ |
494 | 0 | _specificationLocation = specificationLocation; |
495 | 0 | } |
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 | |
} |