|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SpecificationSourceImpl.java | 75% | 81% | 90.9% | 81.5% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.services.impl;
|
|
16 |
|
|
17 |
import java.util.HashMap;
|
|
18 |
import java.util.Map;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.ClassResolver;
|
|
22 |
import org.apache.hivemind.Resource;
|
|
23 |
import org.apache.hivemind.util.ClasspathResource;
|
|
24 |
import org.apache.tapestry.INamespace;
|
|
25 |
import org.apache.tapestry.engine.ISpecificationSource;
|
|
26 |
import org.apache.tapestry.engine.Namespace;
|
|
27 |
import org.apache.tapestry.event.ResetEventListener;
|
|
28 |
import org.apache.tapestry.parse.ISpecificationParser;
|
|
29 |
import org.apache.tapestry.spec.IApplicationSpecification;
|
|
30 |
import org.apache.tapestry.spec.IComponentSpecification;
|
|
31 |
import org.apache.tapestry.spec.ILibrarySpecification;
|
|
32 |
import org.apache.tapestry.spec.LibrarySpecification;
|
|
33 |
import org.apache.tapestry.util.xml.DocumentParseException;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Default implementation of {@link ISpecificationSource} that
|
|
37 |
* expects to use the normal class loader to locate component
|
|
38 |
* specifications from within the classpath.
|
|
39 |
*
|
|
40 |
* <p>Caches specifications in memory forever, or until {@link #resetDidOccur()()} is invoked.
|
|
41 |
*
|
|
42 |
*
|
|
43 |
* @author Howard Lewis Ship
|
|
44 |
*
|
|
45 |
*/
|
|
46 |
|
|
47 |
public class SpecificationSourceImpl implements ISpecificationSource, ResetEventListener |
|
48 |
{ |
|
49 |
private ClassResolver _classResolver;
|
|
50 |
|
|
51 |
private IApplicationSpecification _specification;
|
|
52 |
private ISpecificationParser _parser;
|
|
53 |
|
|
54 |
private INamespace _applicationNamespace;
|
|
55 |
private INamespace _frameworkNamespace;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Contains previously parsed component specifications.
|
|
59 |
*
|
|
60 |
*/
|
|
61 |
|
|
62 |
private Map _componentCache = new HashMap(); |
|
63 |
|
|
64 |
/**
|
|
65 |
* Contains previously parsed page specifications.
|
|
66 |
*
|
|
67 |
* @since 2.2
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
|
|
71 |
private Map _pageCache = new HashMap(); |
|
72 |
|
|
73 |
/**
|
|
74 |
* Contains previously parsed library specifications, keyed
|
|
75 |
* on specification resource path.
|
|
76 |
*
|
|
77 |
* @since 2.2
|
|
78 |
*
|
|
79 |
*/
|
|
80 |
|
|
81 |
private Map _libraryCache = new HashMap(); |
|
82 |
|
|
83 |
/**
|
|
84 |
* Contains {@link INamespace} instances, keyed on id (which will
|
|
85 |
* be null for the application specification).
|
|
86 |
*
|
|
87 |
*/
|
|
88 |
|
|
89 |
private Map _namespaceCache = new HashMap(); |
|
90 |
|
|
91 |
/**
|
|
92 |
* Clears the specification cache. This is used during debugging.
|
|
93 |
*
|
|
94 |
*/
|
|
95 |
|
|
96 | 0 |
public synchronized void resetEventDidOccur() |
97 |
{ |
|
98 | 0 |
_componentCache.clear(); |
99 | 0 |
_pageCache.clear(); |
100 | 0 |
_libraryCache.clear(); |
101 | 0 |
_namespaceCache.clear(); |
102 |
|
|
103 | 0 |
_applicationNamespace = null;
|
104 | 0 |
_frameworkNamespace = null;
|
105 |
} |
|
106 |
|
|
107 | 446 |
protected IComponentSpecification parseSpecification(Resource resource, boolean asPage) |
108 |
{ |
|
109 | 446 |
IComponentSpecification result = null;
|
110 |
|
|
111 | 446 |
try
|
112 |
{ |
|
113 | 446 |
if (asPage)
|
114 | 99 |
result = _parser.parsePageSpecification(resource); |
115 |
else
|
|
116 | 347 |
result = _parser.parseComponentSpecification(resource); |
117 |
} |
|
118 |
catch (DocumentParseException ex)
|
|
119 |
{ |
|
120 | 0 |
throw new ApplicationRuntimeException( |
121 |
ImplMessages.unableToParseSpecification(resource), |
|
122 |
ex); |
|
123 |
} |
|
124 |
|
|
125 | 446 |
return result;
|
126 |
} |
|
127 |
|
|
128 | 63 |
protected ILibrarySpecification parseLibrarySpecification(Resource resource)
|
129 |
{ |
|
130 | 63 |
try
|
131 |
{ |
|
132 | 63 |
return _parser.parseLibrarySpecification(resource);
|
133 |
} |
|
134 |
catch (DocumentParseException ex)
|
|
135 |
{ |
|
136 | 0 |
throw new ApplicationRuntimeException( |
137 |
ImplMessages.unableToParseSpecification(resource), |
|
138 |
ex); |
|
139 |
} |
|
140 |
|
|
141 |
} |
|
142 |
|
|
143 |
/**
|
|
144 |
* Gets a component specification.
|
|
145 |
*
|
|
146 |
* @param resourcePath the complete resource path to the specification.
|
|
147 |
* @throws ApplicationRuntimeException if the specification cannot be obtained.
|
|
148 |
*
|
|
149 |
*/
|
|
150 |
|
|
151 | 347 |
public synchronized IComponentSpecification getComponentSpecification(Resource resourceLocation) |
152 |
{ |
|
153 | 347 |
IComponentSpecification result = |
154 |
(IComponentSpecification) _componentCache.get(resourceLocation); |
|
155 |
|
|
156 | 347 |
if (result == null) |
157 |
{ |
|
158 | 347 |
result = parseSpecification(resourceLocation, false);
|
159 |
|
|
160 | 347 |
_componentCache.put(resourceLocation, result); |
161 |
} |
|
162 |
|
|
163 | 347 |
return result;
|
164 |
} |
|
165 |
|
|
166 | 99 |
public synchronized IComponentSpecification getPageSpecification(Resource resourceLocation) |
167 |
{ |
|
168 | 99 |
IComponentSpecification result = (IComponentSpecification) _pageCache.get(resourceLocation); |
169 |
|
|
170 | 99 |
if (result == null) |
171 |
{ |
|
172 | 99 |
result = parseSpecification(resourceLocation, true);
|
173 |
|
|
174 | 99 |
_pageCache.put(resourceLocation, result); |
175 |
} |
|
176 |
|
|
177 | 99 |
return result;
|
178 |
} |
|
179 |
|
|
180 | 63 |
public synchronized ILibrarySpecification getLibrarySpecification(Resource resourceLocation) |
181 |
{ |
|
182 | 63 |
ILibrarySpecification result = (LibrarySpecification) _libraryCache.get(resourceLocation); |
183 |
|
|
184 | 63 |
if (result == null) |
185 |
{ |
|
186 | 63 |
result = parseLibrarySpecification(resourceLocation); |
187 | 63 |
_libraryCache.put(resourceLocation, result); |
188 |
} |
|
189 |
|
|
190 | 63 |
return result;
|
191 |
} |
|
192 |
|
|
193 | 110 |
public synchronized INamespace getApplicationNamespace() |
194 |
{ |
|
195 | 110 |
if (_applicationNamespace == null) |
196 | 45 |
_applicationNamespace = new Namespace(null, null, _specification, this, _classResolver); |
197 |
|
|
198 | 110 |
return _applicationNamespace;
|
199 |
} |
|
200 |
|
|
201 | 287 |
public synchronized INamespace getFrameworkNamespace() |
202 |
{ |
|
203 | 287 |
if (_frameworkNamespace == null) |
204 |
{ |
|
205 | 45 |
Resource frameworkLocation = |
206 |
new ClasspathResource(_classResolver, "/org/apache/tapestry/Framework.library"); |
|
207 |
|
|
208 | 45 |
ILibrarySpecification ls = getLibrarySpecification(frameworkLocation); |
209 |
|
|
210 | 45 |
_frameworkNamespace = new Namespace(INamespace.FRAMEWORK_NAMESPACE, null, ls, this, _classResolver); |
211 |
} |
|
212 |
|
|
213 | 287 |
return _frameworkNamespace;
|
214 |
} |
|
215 |
|
|
216 | 45 |
public void setParser(ISpecificationParser parser) |
217 |
{ |
|
218 | 45 |
_parser = parser; |
219 |
} |
|
220 |
|
|
221 | 45 |
public void setClassResolver(ClassResolver resolver) |
222 |
{ |
|
223 | 45 |
_classResolver = resolver; |
224 |
} |
|
225 |
|
|
226 | 45 |
public void setSpecification(IApplicationSpecification specification) |
227 |
{ |
|
228 | 45 |
_specification = specification; |
229 |
} |
|
230 |
} |
|