KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > PortletApplicationSpecificationInitializer


1 // Copyright 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.portlet;
16
17 import javax.portlet.PortletConfig;
18
19 import org.apache.hivemind.Resource;
20 import org.apache.tapestry.parse.ISpecificationParser;
21 import org.apache.tapestry.services.ApplicationGlobals;
22 import org.apache.tapestry.spec.ApplicationSpecification;
23 import org.apache.tapestry.spec.IApplicationSpecification;
24 import org.apache.tapestry.web.WebContext;
25 import org.apache.tapestry.web.WebContextResource;
26
27 /**
28  * Locates and reads the application specification for the portlet and stores it into
29  * {@link org.apache.tapestry.services.ApplicationGlobals}.
30  * <p>
31  * TODO: Merge this code with
32  * {@link org.apache.tapestry.services.impl.ApplicationSpecificationInitializer}, they're very
33  * similar. This would probably be an additional service that would do the lookup based on the
34  * {@link org.apache.tapestry.web.WebActivator}&nbsp;and the {@link org.apache.tapestry.web.WebContext}.
35  *
36  * @author Howard M. Lewis Ship
37  * @since 4.0
38  * @see org.apache.tapestry.services.impl.ApplicationSpecificationInitializer
39  */

40 public class PortletApplicationSpecificationInitializer implements PortletApplicationInitializer
41 {
42     private WebContext _context;
43
44     private ApplicationGlobals _globals;
45
46     private ISpecificationParser _parser;
47
48     public void initialize(PortletConfig portletConfig)
49     {
50         String JavaDoc name = portletConfig.getPortletName();
51
52         Resource resource = findApplicationSpecification(name);
53
54         IApplicationSpecification specification = resource == null ? constructStandinSpecification(name)
55                 : _parser.parseApplicationSpecification(resource);
56
57         _globals.storeSpecification(specification);
58     }
59
60     private Resource findApplicationSpecification(String JavaDoc name)
61     {
62         String JavaDoc expectedName = name + ".application";
63
64         Resource webInfLocation = new WebContextResource(_context, "/WEB-INF/");
65         Resource webInfAppLocation = webInfLocation.getRelativeResource(name + "/");
66
67         Resource result = check(webInfAppLocation, expectedName);
68         if (result != null)
69             return result;
70
71         return check(webInfLocation, expectedName);
72     }
73
74     private Resource check(Resource resource, String JavaDoc name)
75     {
76         Resource result = resource.getRelativeResource(name);
77
78         if (result.getResourceURL() != null)
79             return result;
80
81         return null;
82     }
83
84     private IApplicationSpecification constructStandinSpecification(String JavaDoc name)
85     {
86         ApplicationSpecification result = new ApplicationSpecification();
87
88         // Pretend the file exists in the most common expected location.
89

90         Resource virtualLocation = new WebContextResource(_context, "/WEB-INF/" + name
91                 + ".application");
92
93         result.setSpecificationLocation(virtualLocation);
94
95         result.setName(name);
96
97         return result;
98     }
99
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 }
Popular Tags