|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.engine; |
|
16 |
| |
|
17 |
| import java.util.ArrayList; |
|
18 |
| import java.util.Collections; |
|
19 |
| import java.util.HashMap; |
|
20 |
| import java.util.HashSet; |
|
21 |
| import java.util.List; |
|
22 |
| import java.util.Map; |
|
23 |
| import java.util.Set; |
|
24 |
| |
|
25 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
26 |
| import org.apache.hivemind.Location; |
|
27 |
| import org.apache.hivemind.Resource; |
|
28 |
| import org.apache.tapestry.INamespace; |
|
29 |
| import org.apache.tapestry.Tapestry; |
|
30 |
| import org.apache.tapestry.services.NamespaceResources; |
|
31 |
| import org.apache.tapestry.spec.IComponentSpecification; |
|
32 |
| import org.apache.tapestry.spec.ILibrarySpecification; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| public class Namespace implements INamespace |
|
44 |
| { |
|
45 |
| private final ILibrarySpecification _specification; |
|
46 |
| |
|
47 |
| private final String _id; |
|
48 |
| |
|
49 |
| private String _extendedId; |
|
50 |
| |
|
51 |
| private final INamespace _parent; |
|
52 |
| |
|
53 |
| private final boolean _frameworkNamespace; |
|
54 |
| |
|
55 |
| private final boolean _applicationNamespace; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| private final NamespaceResources _resources; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| private final Map _pages = Collections.synchronizedMap(new HashMap()); |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| private final Map _components = Collections.synchronizedMap(new HashMap()); |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| private final Map _children = Collections.synchronizedMap(new HashMap()); |
|
80 |
| |
|
81 |
336
| public Namespace(String id, INamespace parent, ILibrarySpecification specification,
|
|
82 |
| NamespaceResources resources) |
|
83 |
| { |
|
84 |
336
| _id = id;
|
|
85 |
336
| _parent = parent;
|
|
86 |
336
| _specification = specification;
|
|
87 |
336
| _resources = resources;
|
|
88 |
| |
|
89 |
336
| _applicationNamespace = (_id == null);
|
|
90 |
336
| _frameworkNamespace = FRAMEWORK_NAMESPACE.equals(_id);
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public String toString()
|
|
94 |
| { |
|
95 |
0
| StringBuffer buffer = new StringBuffer("Namespace@");
|
|
96 |
0
| buffer.append(Integer.toHexString(hashCode()));
|
|
97 |
0
| buffer.append('[');
|
|
98 |
| |
|
99 |
0
| if (_applicationNamespace)
|
|
100 |
0
| buffer.append("<application>");
|
|
101 |
| else |
|
102 |
0
| buffer.append(getExtendedId());
|
|
103 |
| |
|
104 |
0
| buffer.append(']');
|
|
105 |
| |
|
106 |
0
| return buffer.toString();
|
|
107 |
| } |
|
108 |
| |
|
109 |
318
| public String getId()
|
|
110 |
| { |
|
111 |
318
| return _id;
|
|
112 |
| } |
|
113 |
| |
|
114 |
633
| public String getExtendedId()
|
|
115 |
| { |
|
116 |
633
| if (_applicationNamespace)
|
|
117 |
450
| return null;
|
|
118 |
| |
|
119 |
183
| if (_extendedId == null)
|
|
120 |
99
| _extendedId = buildExtendedId();
|
|
121 |
| |
|
122 |
183
| return _extendedId;
|
|
123 |
| } |
|
124 |
| |
|
125 |
0
| public INamespace getParentNamespace()
|
|
126 |
| { |
|
127 |
0
| return _parent;
|
|
128 |
| } |
|
129 |
| |
|
130 |
483
| public INamespace getChildNamespace(String id)
|
|
131 |
| { |
|
132 |
483
| String firstId = id;
|
|
133 |
483
| String nextIds = null;
|
|
134 |
| |
|
135 |
| |
|
136 |
483
| int index = id.indexOf('.');
|
|
137 |
483
| if (index >= 0)
|
|
138 |
| { |
|
139 |
0
| firstId = id.substring(0, index);
|
|
140 |
0
| nextIds = id.substring(index + 1);
|
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
483
| INamespace result = (INamespace) _children.get(firstId);
|
|
145 |
| |
|
146 |
483
| if (result == null)
|
|
147 |
| { |
|
148 |
51
| result = createNamespace(firstId);
|
|
149 |
| |
|
150 |
51
| _children.put(firstId, result);
|
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
483
| if (result != null && nextIds != null)
|
|
156 |
0
| result = result.getChildNamespace(nextIds);
|
|
157 |
| |
|
158 |
483
| return result;
|
|
159 |
| } |
|
160 |
| |
|
161 |
0
| public List getChildIds()
|
|
162 |
| { |
|
163 |
0
| return _specification.getLibraryIds();
|
|
164 |
| } |
|
165 |
| |
|
166 |
189
| public IComponentSpecification getPageSpecification(String name)
|
|
167 |
| { |
|
168 |
189
| IComponentSpecification result = (IComponentSpecification) _pages.get(name);
|
|
169 |
| |
|
170 |
189
| if (result == null)
|
|
171 |
| { |
|
172 |
129
| result = locatePageSpecification(name);
|
|
173 |
| |
|
174 |
129
| _pages.put(name, result);
|
|
175 |
| } |
|
176 |
| |
|
177 |
189
| return result;
|
|
178 |
| } |
|
179 |
| |
|
180 |
0
| public List getPageNames()
|
|
181 |
| { |
|
182 |
0
| Set names = new HashSet();
|
|
183 |
| |
|
184 |
0
| names.addAll(_pages.keySet());
|
|
185 |
0
| names.addAll(_specification.getPageNames());
|
|
186 |
| |
|
187 |
0
| List result = new ArrayList(names);
|
|
188 |
| |
|
189 |
0
| Collections.sort(result);
|
|
190 |
| |
|
191 |
0
| return result;
|
|
192 |
| } |
|
193 |
| |
|
194 |
4047
| public IComponentSpecification getComponentSpecification(String alias)
|
|
195 |
| { |
|
196 |
4047
| IComponentSpecification result = (IComponentSpecification) _components.get(alias);
|
|
197 |
| |
|
198 |
4047
| if (result == null)
|
|
199 |
| { |
|
200 |
846
| result = locateComponentSpecification(alias);
|
|
201 |
846
| _components.put(alias, result);
|
|
202 |
| } |
|
203 |
| |
|
204 |
4047
| return result;
|
|
205 |
| } |
|
206 |
| |
|
207 |
216
| public ILibrarySpecification getSpecification()
|
|
208 |
| { |
|
209 |
216
| return _specification;
|
|
210 |
| } |
|
211 |
| |
|
212 |
99
| private String buildExtendedId()
|
|
213 |
| { |
|
214 |
99
| if (_parent == null)
|
|
215 |
48
| return _id;
|
|
216 |
| |
|
217 |
51
| String parentId = _parent.getExtendedId();
|
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
51
| if (parentId == null)
|
|
222 |
51
| return _id;
|
|
223 |
| |
|
224 |
0
| return parentId + "." + _id;
|
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
3
| public String getNamespaceId()
|
|
233 |
| { |
|
234 |
3
| if (_frameworkNamespace)
|
|
235 |
0
| return Tapestry.getMessage("Namespace.framework-namespace");
|
|
236 |
| |
|
237 |
3
| if (_applicationNamespace)
|
|
238 |
3
| return Tapestry.getMessage("Namespace.application-namespace");
|
|
239 |
| |
|
240 |
0
| return Tapestry.format("Namespace.nested-namespace", getExtendedId());
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
129
| private IComponentSpecification locatePageSpecification(String name)
|
|
251 |
| { |
|
252 |
129
| String path = _specification.getPageSpecificationPath(name);
|
|
253 |
| |
|
254 |
129
| if (path == null)
|
|
255 |
0
| throw new ApplicationRuntimeException(Tapestry.format(
|
|
256 |
| "Namespace.no-such-page", |
|
257 |
| name, |
|
258 |
| getNamespaceId())); |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
129
| return _resources.getPageSpecification(getSpecificationLocation(), path, getLocation());
|
|
265 |
| } |
|
266 |
| |
|
267 |
846
| private IComponentSpecification locateComponentSpecification(String type)
|
|
268 |
| { |
|
269 |
846
| String path = _specification.getComponentSpecificationPath(type);
|
|
270 |
| |
|
271 |
846
| if (path == null)
|
|
272 |
0
| throw new ApplicationRuntimeException(Tapestry.format(
|
|
273 |
| "Namespace.no-such-alias", |
|
274 |
| type, |
|
275 |
| getNamespaceId())); |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
846
| return _resources
|
|
282 |
| .getComponentSpecification(getSpecificationLocation(), path, getLocation()); |
|
283 |
| } |
|
284 |
| |
|
285 |
51
| private INamespace createNamespace(String id)
|
|
286 |
| { |
|
287 |
51
| String path = _specification.getLibrarySpecificationPath(id);
|
|
288 |
| |
|
289 |
51
| if (path == null)
|
|
290 |
0
| throw new ApplicationRuntimeException(Tapestry.format(
|
|
291 |
| "Namespace.library-id-not-found", |
|
292 |
| id, |
|
293 |
| getNamespaceId())); |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
51
| ILibrarySpecification ls = _resources.findChildLibrarySpecification(
|
|
300 |
| getSpecificationLocation(), |
|
301 |
| path, |
|
302 |
| getLocation()); |
|
303 |
| |
|
304 |
51
| return new Namespace(id, this, ls, _resources);
|
|
305 |
| } |
|
306 |
| |
|
307 |
444
| public synchronized boolean containsPage(String name)
|
|
308 |
| { |
|
309 |
444
| return _pages.containsKey(name) || (_specification.getPageSpecificationPath(name) != null);
|
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
366
| public String constructQualifiedName(String pageName)
|
|
315 |
| { |
|
316 |
366
| String prefix = getExtendedId();
|
|
317 |
| |
|
318 |
366
| if (prefix == null)
|
|
319 |
282
| return pageName;
|
|
320 |
| |
|
321 |
84
| return prefix + SEPARATOR + pageName;
|
|
322 |
| } |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
3543
| public Resource getSpecificationLocation()
|
|
327 |
| { |
|
328 |
3543
| return _specification.getSpecificationLocation();
|
|
329 |
| } |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
723
| public boolean isApplicationNamespace()
|
|
334 |
| { |
|
335 |
723
| return _applicationNamespace;
|
|
336 |
| } |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
177
| public synchronized void installPageSpecification(String pageName,
|
|
341 |
| IComponentSpecification specification) |
|
342 |
| { |
|
343 |
177
| _pages.put(pageName, specification);
|
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
450
| public synchronized void installComponentSpecification(String type,
|
|
349 |
| IComponentSpecification specification) |
|
350 |
| { |
|
351 |
450
| _components.put(type, specification);
|
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
4497
| public synchronized boolean containsComponentType(String type)
|
|
357 |
| { |
|
358 |
4497
| return _components.containsKey(type)
|
|
359 |
| || (_specification.getComponentSpecificationPath(type) != null); |
|
360 |
| } |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
1026
| public Location getLocation()
|
|
365 |
| { |
|
366 |
1026
| if (_specification == null)
|
|
367 |
0
| return null;
|
|
368 |
| |
|
369 |
1026
| return _specification.getLocation();
|
|
370 |
| } |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
921
| public String getPropertyValue(String propertyName)
|
|
380 |
| { |
|
381 |
921
| return _specification.getProperty(propertyName);
|
|
382 |
| } |
|
383 |
| } |