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