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 |
116
| public Namespace(String id, INamespace parent, ILibrarySpecification specification,
|
82 |
| NamespaceResources resources) |
83 |
| { |
84 |
116
| _id = id;
|
85 |
116
| _parent = parent;
|
86 |
116
| _specification = specification;
|
87 |
116
| _resources = resources;
|
88 |
| |
89 |
116
| _applicationNamespace = (_id == null);
|
90 |
116
| _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 |
118
| public String getId()
|
110 |
| { |
111 |
118
| return _id;
|
112 |
| } |
113 |
| |
114 |
212
| public String getExtendedId()
|
115 |
| { |
116 |
212
| if (_applicationNamespace)
|
117 |
151
| return null;
|
118 |
| |
119 |
61
| if (_extendedId == null)
|
120 |
29
| _extendedId = buildExtendedId();
|
121 |
| |
122 |
61
| return _extendedId;
|
123 |
| } |
124 |
| |
125 |
0
| public INamespace getParentNamespace()
|
126 |
| { |
127 |
0
| return _parent;
|
128 |
| } |
129 |
| |
130 |
161
| public INamespace getChildNamespace(String id)
|
131 |
| { |
132 |
161
| String firstId = id;
|
133 |
161
| String nextIds = null;
|
134 |
| |
135 |
| |
136 |
161
| int index = id.indexOf('.');
|
137 |
161
| if (index >= 0)
|
138 |
| { |
139 |
0
| firstId = id.substring(0, index);
|
140 |
0
| nextIds = id.substring(index + 1);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
161
| INamespace result = (INamespace) _children.get(firstId);
|
145 |
| |
146 |
161
| if (result == null)
|
147 |
| { |
148 |
17
| result = createNamespace(firstId);
|
149 |
| |
150 |
17
| _children.put(firstId, result);
|
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
161
| if (result != null && nextIds != null)
|
156 |
0
| result = result.getChildNamespace(nextIds);
|
157 |
| |
158 |
161
| return result;
|
159 |
| } |
160 |
| |
161 |
0
| public List getChildIds()
|
162 |
| { |
163 |
0
| return _specification.getLibraryIds();
|
164 |
| } |
165 |
| |
166 |
67
| public IComponentSpecification getPageSpecification(String name)
|
167 |
| { |
168 |
67
| IComponentSpecification result = (IComponentSpecification) _pages.get(name);
|
169 |
| |
170 |
67
| if (result == null)
|
171 |
| { |
172 |
45
| result = locatePageSpecification(name);
|
173 |
| |
174 |
45
| _pages.put(name, result);
|
175 |
| } |
176 |
| |
177 |
67
| 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 |
1473
| public IComponentSpecification getComponentSpecification(String alias)
|
195 |
| { |
196 |
1473
| IComponentSpecification result = (IComponentSpecification) _components.get(alias);
|
197 |
| |
198 |
1473
| if (result == null)
|
199 |
| { |
200 |
304
| result = locateComponentSpecification(alias);
|
201 |
304
| _components.put(alias, result);
|
202 |
| } |
203 |
| |
204 |
1473
| return result;
|
205 |
| } |
206 |
| |
207 |
70
| public ILibrarySpecification getSpecification()
|
208 |
| { |
209 |
70
| return _specification;
|
210 |
| } |
211 |
| |
212 |
29
| private String buildExtendedId()
|
213 |
| { |
214 |
29
| if (_parent == null)
|
215 |
18
| return _id;
|
216 |
| |
217 |
11
| String parentId = _parent.getExtendedId();
|
218 |
| |
219 |
| |
220 |
| |
221 |
11
| if (parentId == null)
|
222 |
11
| return _id;
|
223 |
| |
224 |
0
| return parentId + "." + _id;
|
225 |
| } |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
1
| public String getNamespaceId()
|
233 |
| { |
234 |
1
| if (_frameworkNamespace)
|
235 |
0
| return Tapestry.getMessage("Namespace.framework-namespace");
|
236 |
| |
237 |
1
| if (_applicationNamespace)
|
238 |
1
| 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 |
45
| private IComponentSpecification locatePageSpecification(String name)
|
251 |
| { |
252 |
45
| String path = _specification.getPageSpecificationPath(name);
|
253 |
| |
254 |
45
| 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 |
45
| return _resources.getPageSpecification(getSpecificationLocation(), path, getLocation());
|
265 |
| } |
266 |
| |
267 |
304
| private IComponentSpecification locateComponentSpecification(String type)
|
268 |
| { |
269 |
304
| String path = _specification.getComponentSpecificationPath(type);
|
270 |
| |
271 |
304
| 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 |
304
| return _resources
|
282 |
| .getComponentSpecification(getSpecificationLocation(), path, getLocation()); |
283 |
| } |
284 |
| |
285 |
17
| private INamespace createNamespace(String id)
|
286 |
| { |
287 |
17
| String path = _specification.getLibrarySpecificationPath(id);
|
288 |
| |
289 |
17
| 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 |
17
| ILibrarySpecification ls = _resources.findChildLibrarySpecification(
|
300 |
| getSpecificationLocation(), |
301 |
| path, |
302 |
| getLocation()); |
303 |
| |
304 |
17
| return new Namespace(id, this, ls, _resources);
|
305 |
| } |
306 |
| |
307 |
161
| public synchronized boolean containsPage(String name)
|
308 |
| { |
309 |
161
| return _pages.containsKey(name) || (_specification.getPageSpecificationPath(name) != null);
|
310 |
| } |
311 |
| |
312 |
| |
313 |
| |
314 |
131
| public String constructQualifiedName(String pageName)
|
315 |
| { |
316 |
131
| String prefix = getExtendedId();
|
317 |
| |
318 |
131
| if (prefix == null)
|
319 |
99
| return pageName;
|
320 |
| |
321 |
32
| return prefix + SEPARATOR + pageName;
|
322 |
| } |
323 |
| |
324 |
| |
325 |
| |
326 |
992
| public Resource getSpecificationLocation()
|
327 |
| { |
328 |
992
| return _specification.getSpecificationLocation();
|
329 |
| } |
330 |
| |
331 |
| |
332 |
| |
333 |
260
| public boolean isApplicationNamespace()
|
334 |
| { |
335 |
260
| return _applicationNamespace;
|
336 |
| } |
337 |
| |
338 |
| |
339 |
| |
340 |
64
| public synchronized void installPageSpecification(String pageName,
|
341 |
| IComponentSpecification specification) |
342 |
| { |
343 |
64
| _pages.put(pageName, specification);
|
344 |
| } |
345 |
| |
346 |
| |
347 |
| |
348 |
159
| public synchronized void installComponentSpecification(String type,
|
349 |
| IComponentSpecification specification) |
350 |
| { |
351 |
159
| _components.put(type, specification);
|
352 |
| } |
353 |
| |
354 |
| |
355 |
| |
356 |
1632
| public synchronized boolean containsComponentType(String type)
|
357 |
| { |
358 |
1632
| return _components.containsKey(type)
|
359 |
| || (_specification.getComponentSpecificationPath(type) != null); |
360 |
| } |
361 |
| |
362 |
| |
363 |
| |
364 |
366
| public Location getLocation()
|
365 |
| { |
366 |
366
| if (_specification == null)
|
367 |
0
| return null;
|
368 |
| |
369 |
366
| return _specification.getLocation();
|
370 |
| } |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
323
| public String getPropertyValue(String propertyName)
|
380 |
| { |
381 |
323
| return _specification.getProperty(propertyName);
|
382 |
| } |
383 |
| } |