KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > SpecificationSourceImpl


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 JavaDoc;
18 import java.util.Map JavaDoc;
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 JavaDoc _componentCache = new HashMap JavaDoc();
63
64     /**
65      * Contains previously parsed page specifications.
66      *
67      * @since 2.2
68      *
69      */

70
71     private Map JavaDoc _pageCache = new HashMap JavaDoc();
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 JavaDoc _libraryCache = new HashMap JavaDoc();
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 JavaDoc _namespaceCache = new HashMap JavaDoc();
90
91     /**
92      * Clears the specification cache. This is used during debugging.
93      *
94      */

95
96     public synchronized void resetEventDidOccur()
97     {
98         _componentCache.clear();
99         _pageCache.clear();
100         _libraryCache.clear();
101         _namespaceCache.clear();
102
103         _applicationNamespace = null;
104         _frameworkNamespace = null;
105     }
106
107     protected IComponentSpecification parseSpecification(Resource resource, boolean asPage)
108     {
109         IComponentSpecification result = null;
110
111         try
112         {
113             if (asPage)
114                 result = _parser.parsePageSpecification(resource);
115             else
116                 result = _parser.parseComponentSpecification(resource);
117         }
118         catch (DocumentParseException ex)
119         {
120             throw new ApplicationRuntimeException(
121                 ImplMessages.unableToParseSpecification(resource),
122                 ex);
123         }
124
125         return result;
126     }
127
128     protected ILibrarySpecification parseLibrarySpecification(Resource resource)
129     {
130         try
131         {
132             return _parser.parseLibrarySpecification(resource);
133         }
134         catch (DocumentParseException ex)
135         {
136             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     public synchronized IComponentSpecification getComponentSpecification(Resource resourceLocation)
152     {
153         IComponentSpecification result =
154             (IComponentSpecification) _componentCache.get(resourceLocation);
155
156         if (result == null)
157         {
158             result = parseSpecification(resourceLocation, false);
159
160             _componentCache.put(resourceLocation, result);
161         }
162
163         return result;
164     }
165
166     public synchronized IComponentSpecification getPageSpecification(Resource resourceLocation)
167     {
168         IComponentSpecification result = (IComponentSpecification) _pageCache.get(resourceLocation);
169
170         if (result == null)
171         {
172             result = parseSpecification(resourceLocation, true);
173
174             _pageCache.put(resourceLocation, result);
175         }
176
177         return result;
178     }
179
180     public synchronized ILibrarySpecification getLibrarySpecification(Resource resourceLocation)
181     {
182         ILibrarySpecification result = (LibrarySpecification) _libraryCache.get(resourceLocation);
183
184         if (result == null)
185         {
186             result = parseLibrarySpecification(resourceLocation);
187             _libraryCache.put(resourceLocation, result);
188         }
189
190         return result;
191     }
192
193     public synchronized INamespace getApplicationNamespace()
194     {
195         if (_applicationNamespace == null)
196             _applicationNamespace = new Namespace(null, null, _specification, this, _classResolver);
197
198         return _applicationNamespace;
199     }
200
201     public synchronized INamespace getFrameworkNamespace()
202     {
203         if (_frameworkNamespace == null)
204         {
205             Resource frameworkLocation =
206                 new ClasspathResource(_classResolver, "/org/apache/tapestry/Framework.library");
207
208             ILibrarySpecification ls = getLibrarySpecification(frameworkLocation);
209
210             _frameworkNamespace = new Namespace(INamespace.FRAMEWORK_NAMESPACE, null, ls, this, _classResolver);
211         }
212
213         return _frameworkNamespace;
214     }
215
216     public void setParser(ISpecificationParser parser)
217     {
218         _parser = parser;
219     }
220
221     public void setClassResolver(ClassResolver resolver)
222     {
223         _classResolver = resolver;
224     }
225
226     public void setSpecification(IApplicationSpecification specification)
227     {
228         _specification = specification;
229     }
230 }
Popular Tags