001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.resolver;
016
017 import org.apache.hivemind.Resource;
018 import org.apache.tapestry.INamespace;
019 import org.apache.tapestry.engine.ISpecificationSource;
020 import org.apache.tapestry.spec.IComponentSpecification;
021
022 /**
023 * Base class for resolving a {@link org.apache.tapestry.spec.IComponentSpecification}for a
024 * particular page or component, within a specified {@link org.apache.tapestry.INamespace}. In some
025 * cases, a search is necessary.
026 *
027 * @author Howard Lewis Ship
028 * @since 3.0
029 */
030
031 public class AbstractSpecificationResolver
032 {
033 /** Set by resolve(). */
034 private INamespace _namespace;
035
036 /** Set by resolve(). */
037 private IComponentSpecification _specification;
038
039 /** Set by container. */
040 private ISpecificationSource _specificationSource;
041
042 private ISpecificationResolverDelegate _delegate;
043
044 private String _applicationId;
045
046 private Resource _contextRoot;
047
048 /** Initialized in initializeService(). */
049
050 private Resource _webInfLocation;
051
052 private Resource _webInfAppLocation;
053
054 public void initializeService()
055 {
056 _webInfLocation = _contextRoot.getRelativeResource("WEB-INF/");
057
058 _webInfAppLocation = _webInfLocation.getRelativeResource(_applicationId + "/");
059 }
060
061 /**
062 * Returns the {@link ISpecificationResolverDelegate}instance registered in the application
063 * or null if no such extension exists.
064 */
065
066 public ISpecificationResolverDelegate getDelegate()
067 {
068 return _delegate;
069 }
070
071 /**
072 * Returns the location of the servlet, within the servlet context.
073 */
074
075 protected Resource getContextRoot()
076 {
077 return _contextRoot;
078 }
079
080 public void setContextRoot(Resource contextRoot)
081 {
082 _contextRoot = contextRoot;
083 }
084
085 /**
086 * Invoked in subclasses to identify the resolved namespace.
087 */
088
089 protected void setNamespace(INamespace namespace)
090 {
091 _namespace = namespace;
092 }
093
094 /**
095 * Returns the resolve namespace.
096 */
097
098 public INamespace getNamespace()
099 {
100 return _namespace;
101 }
102
103 /**
104 * Returns the specification source for the running application.
105 */
106
107 protected ISpecificationSource getSpecificationSource()
108 {
109 return _specificationSource;
110 }
111
112 /**
113 * Returns the location of /WEB-INF/, in the servlet context.
114 */
115
116 protected Resource getWebInfLocation()
117 {
118 return _webInfLocation;
119 }
120
121 /**
122 * Returns the location of the application-specific subdirectory, under /WEB-INF/, in the
123 * servlet context.
124 */
125
126 protected Resource getWebInfAppLocation()
127 {
128 return _webInfAppLocation;
129 }
130
131 /**
132 * Returns the resolved specification.
133 */
134
135 public IComponentSpecification getSpecification()
136 {
137 return _specification;
138 }
139
140 /**
141 * Invoked in subclass to set the final specification the initial inputs are resolved to.
142 */
143
144 protected void setSpecification(IComponentSpecification specification)
145 {
146 _specification = specification;
147 }
148
149 /**
150 * Clears the namespace and specification properties.
151 */
152
153 protected void reset()
154 {
155 _namespace = null;
156 _specification = null;
157 }
158
159 /** @since 4.0 */
160 public void setDelegate(ISpecificationResolverDelegate delegate)
161 {
162 _delegate = delegate;
163 }
164
165 /** @since 4.0 */
166 public void setApplicationId(String applicationId)
167 {
168 _applicationId = applicationId;
169 }
170
171 /** @since 4.0 */
172 public void setSpecificationSource(ISpecificationSource source)
173 {
174 _specificationSource = source;
175 }
176
177 /** @since 4.0 */
178 protected INamespace getApplicationNamespace()
179 {
180 return _specificationSource.getApplicationNamespace();
181 }
182
183 /** @since 4.0 */
184 protected INamespace getFrameworkNamespace()
185 {
186 return _specificationSource.getFrameworkNamespace();
187 }
188
189 /**
190 * @since 4.0
191 */
192 protected INamespace findNamespaceForId(INamespace containerNamespace, String libraryId)
193 {
194 if (libraryId == null)
195 return containerNamespace;
196
197 if (libraryId.equals(INamespace.APPLICATION_NAMESPACE))
198 return getApplicationNamespace();
199
200 if (libraryId.equals(INamespace.FRAMEWORK_NAMESPACE))
201 return getFrameworkNamespace();
202
203 return containerNamespace.getChildNamespace(libraryId);
204 }
205 }