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         * specification as extension {@link Tapestry#SPECIFICATION_RESOLVER_DELEGATE_EXTENSION_NAME},
064         * or null if no such extension exists.
065         */
066    
067        public ISpecificationResolverDelegate getDelegate()
068        {
069            return _delegate;
070        }
071    
072        /**
073         * Returns the location of the servlet, within the servlet context.
074         */
075    
076        protected Resource getContextRoot()
077        {
078            return _contextRoot;
079        }
080    
081        public void setContextRoot(Resource contextRoot)
082        {
083            _contextRoot = contextRoot;
084        }
085    
086        /**
087         * Invoked in subclasses to identify the resolved namespace.
088         */
089    
090        protected void setNamespace(INamespace namespace)
091        {
092            _namespace = namespace;
093        }
094    
095        /**
096         * Returns the resolve namespace.
097         */
098    
099        public INamespace getNamespace()
100        {
101            return _namespace;
102        }
103    
104        /**
105         * Returns the specification source for the running application.
106         */
107    
108        protected ISpecificationSource getSpecificationSource()
109        {
110            return _specificationSource;
111        }
112    
113        /**
114         * Returns the location of /WEB-INF/, in the servlet context.
115         */
116    
117        protected Resource getWebInfLocation()
118        {
119            return _webInfLocation;
120        }
121    
122        /**
123         * Returns the location of the application-specific subdirectory, under /WEB-INF/, in the
124         * servlet context.
125         */
126    
127        protected Resource getWebInfAppLocation()
128        {
129            return _webInfAppLocation;
130        }
131    
132        /**
133         * Returns the resolved specification.
134         */
135    
136        public IComponentSpecification getSpecification()
137        {
138            return _specification;
139        }
140    
141        /**
142         * Invoked in subclass to set the final specification the initial inputs are resolved to.
143         */
144    
145        protected void setSpecification(IComponentSpecification specification)
146        {
147            _specification = specification;
148        }
149    
150        /**
151         * Clears the namespace and specification properties.
152         */
153    
154        protected void reset()
155        {
156            _namespace = null;
157            _specification = null;
158        }
159    
160        /** @since 4.0 */
161        public void setDelegate(ISpecificationResolverDelegate delegate)
162        {
163            _delegate = delegate;
164        }
165    
166        /** @since 4.0 */
167        public void setApplicationId(String applicationId)
168        {
169            _applicationId = applicationId;
170        }
171    
172        /** @since 4.0 */
173        public void setSpecificationSource(ISpecificationSource source)
174        {
175            _specificationSource = source;
176        }
177    
178        /** @since 4.0 */
179        protected INamespace getApplicationNamespace()
180        {
181            return _specificationSource.getApplicationNamespace();
182        }
183    
184        /** @since 4.0 */
185        protected INamespace getFrameworkNamespace()
186        {
187            return _specificationSource.getFrameworkNamespace();
188        }
189    
190        /**
191         * @since 4.0
192         */
193        protected INamespace findNamespaceForId(INamespace containerNamespace, String libraryId)
194        {
195            if (libraryId == null)
196                return containerNamespace;
197        
198            if (libraryId.equals(INamespace.APPLICATION_NAMESPACE))
199                return getApplicationNamespace();
200        
201            if (libraryId.equals(INamespace.FRAMEWORK_NAMESPACE))
202                return getFrameworkNamespace();
203        
204            return containerNamespace.getChildNamespace(libraryId);
205        }
206    }