001    // Copyright 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.portlet;
016    
017    import javax.portlet.PortletConfig;
018    
019    import org.apache.hivemind.Resource;
020    import org.apache.tapestry.parse.ISpecificationParser;
021    import org.apache.tapestry.services.ApplicationGlobals;
022    import org.apache.tapestry.spec.ApplicationSpecification;
023    import org.apache.tapestry.spec.IApplicationSpecification;
024    import org.apache.tapestry.web.WebContext;
025    import org.apache.tapestry.web.WebContextResource;
026    
027    /**
028     * Locates and reads the application specification for the portlet and stores it
029     * into {@link org.apache.tapestry.services.ApplicationGlobals}.
030     * <p>
031     * TODO: Merge this code with
032     * {@link org.apache.tapestry.services.impl.ApplicationSpecificationInitializer},
033     * they're very similar. This would probably be an additional service that would
034     * do the lookup based on the {@link org.apache.tapestry.web.WebActivator}&nbsp;and
035     * the {@link org.apache.tapestry.web.WebContext}.
036     * 
037     * @author Howard M. Lewis Ship
038     * @since 4.0
039     * @see org.apache.tapestry.services.impl.ApplicationSpecificationInitializer
040     */
041    public class PortletApplicationSpecificationInitializer implements
042            PortletApplicationInitializer
043    {
044    
045        private WebContext _context;
046    
047        private ApplicationGlobals _globals;
048    
049        private ISpecificationParser _parser;
050    
051        public void initialize(PortletConfig portletConfig)
052        {
053            String name = portletConfig.getPortletName();
054    
055            Resource resource = findApplicationSpecification(name);
056    
057            IApplicationSpecification specification = resource == null ? constructStandinSpecification(name)
058                    : _parser.parseApplicationSpecification(resource);
059    
060            _globals.storeSpecification(specification);
061        }
062    
063        private Resource findApplicationSpecification(String name)
064        {
065            String expectedName = name + ".application";
066    
067            Resource webInfLocation = new WebContextResource(_context, "/WEB-INF/");
068            Resource webInfAppLocation = webInfLocation.getRelativeResource(name
069                    + "/");
070    
071            Resource result = check(webInfAppLocation, expectedName);
072            if (result != null) return result;
073    
074            return check(webInfLocation, expectedName);
075        }
076    
077        private Resource check(Resource resource, String name)
078        {
079            Resource result = resource.getRelativeResource(name);
080    
081            if (result.getResourceURL() != null) return result;
082    
083            return null;
084        }
085    
086        private IApplicationSpecification constructStandinSpecification(String name)
087        {
088            ApplicationSpecification result = new ApplicationSpecification();
089    
090            // Pretend the file exists in the most common expected location.
091    
092            Resource virtualLocation = new WebContextResource(_context, "/WEB-INF/"
093                    + name + ".application");
094    
095            result.setSpecificationLocation(virtualLocation);
096    
097            result.setName(name);
098    
099            return result;
100        }
101    
102        public void setContext(WebContext context)
103        {
104            _context = context;
105        }
106    
107        public void setGlobals(ApplicationGlobals globals)
108        {
109            _globals = globals;
110        }
111    
112        public void setParser(ISpecificationParser parser)
113        {
114            _parser = parser;
115        }
116    }