KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > driver > deploy > Deploy


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.pluto.driver.deploy;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.apache.pluto.descriptors.common.InitParamDD;
28 import org.apache.pluto.descriptors.common.SecurityRoleRefDD;
29 import org.apache.pluto.descriptors.portlet.PortletAppDD;
30 import org.apache.pluto.descriptors.portlet.PortletDD;
31 import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
32 import org.apache.pluto.descriptors.services.WebAppDescriptorService;
33 import org.apache.pluto.descriptors.servlet.SecurityRoleDD;
34 import org.apache.pluto.descriptors.servlet.ServletDD;
35 import org.apache.pluto.descriptors.servlet.ServletMappingDD;
36 import org.apache.pluto.descriptors.servlet.WebAppDD;
37
38 public class Deploy {
39
40     private static final String JavaDoc INVOKER_SERVLET =
41         "org.apache.pluto.core.PortletServlet";
42
43     // Services Which Must Be Initiated
44
private WebAppDescriptorService webAppDescriptorService;
45     private PortletAppDescriptorService portletAppDescriptorService;
46
47     // Private Services we use internally but don't
48
// want to expose to the rest of the work - yet.
49
private PortletApplicationExploder exploder;
50     private List JavaDoc registrars = new ArrayList JavaDoc();
51
52     private boolean debug = false;
53
54     public Deploy(WebAppDescriptorService webAppDescriptorService,
55                   PortletAppDescriptorService portletAppDescriptorService) {
56         this.webAppDescriptorService = webAppDescriptorService;
57         this.portletAppDescriptorService = portletAppDescriptorService;
58     }
59
60     /**
61      * Service Getter.
62      * @return
63      */

64     public WebAppDescriptorService getWebAppDDService() {
65         return webAppDescriptorService;
66     }
67
68     /**
69      * Service Setter.
70      */

71     public void setWebAppDDService(WebAppDescriptorService webAppDescriptorService) {
72         this.webAppDescriptorService = webAppDescriptorService;
73     }
74
75     /**
76      * Service Getter.
77      * @return
78      */

79     public PortletAppDescriptorService getPortletAppDDService() {
80         return portletAppDescriptorService;
81     }
82
83     /**
84      * Service Setter.
85      * @param portletAppDescriptorService
86      */

87     public void setPortletAppDDService(PortletAppDescriptorService portletAppDescriptorService) {
88         this.portletAppDescriptorService = portletAppDescriptorService;
89     }
90
91     PortletApplicationExploder getExploder() {
92         return exploder;
93     }
94
95     void setExploder(PortletApplicationExploder exploder) {
96         this.exploder = exploder;
97     }
98
99     List JavaDoc getRegistrars() {
100         return registrars;
101     }
102
103     void setRegistrars(List JavaDoc registrars) {
104         this.registrars = registrars;
105     }
106
107     void addRegistrar(PortalRegistrarService registrar) {
108         this.registrars.add(registrar);
109     }
110
111     public boolean isDebug() {
112         return debug;
113     }
114
115     public void setDebug(boolean debug) {
116         this.debug = debug;
117     }
118
119     /**
120      * Deploy the war file at the given location
121      * utilizing the preconfigured resources.
122      *
123      * @param warFile
124      * @throws IOException
125      */

126     public void deploy(File JavaDoc warFile) throws IOException JavaDoc {
127         if(exploder != null) {
128             exploder.explode(warFile);
129         }
130         updateDescriptors();
131         Iterator JavaDoc it = registrars.iterator();
132         PortalRegistrarService registrar = null;
133         while(it.hasNext()) {
134             registrar = (PortalRegistrarService)it.next();
135             registrar.register(portletAppDescriptorService);
136         }
137     }
138
139     public void updateDescriptors()
140         throws IOException JavaDoc {
141         PortletAppDD portletApp = portletAppDescriptorService.read();
142         WebAppDD webApp = webAppDescriptorService.read();
143
144         Iterator JavaDoc portlets = portletApp.getPortlets().iterator();
145         PortletDD portlet;
146         while (portlets.hasNext()) {
147             portlet = (PortletDD) portlets.next();
148             createServlet(webApp, portlet);
149             createServletMapping(webApp, portlet);
150         }
151
152         this.webAppDescriptorService.write(webApp);
153     }
154
155
156
157
158     private void createServlet(WebAppDD webApp, PortletDD portlet) {
159         if(debug) {
160             System.out.println("<VERBOSE> Creating Servlet Wrapper for Portlet: '"+portlet.getPortletName()+"'");
161         }
162         String JavaDoc pnm = portlet.getPortletName();
163         // check if already exists
164
ServletDD servlet = webApp.getServlet(pnm);
165         if (servlet != null) {
166             String JavaDoc cl = servlet.getServletClass();
167             if (!INVOKER_SERVLET.equals(cl)) {
168                 System.out.println(
169                     "Note: Replaced already existing the servlet with the name '"
170                     + pnm
171                     + "' with the wrapper servlet."
172                 );
173                 servlet.setServletClass(INVOKER_SERVLET);
174             }
175         } else {
176             servlet = new ServletDD();
177             servlet.setServletName(pnm);
178             servlet.setServletClass(INVOKER_SERVLET);
179             webApp.getServlets().add(servlet);
180         }
181
182         servlet.setDisplayName(pnm + "Wrapper (Pluto Invoker)");
183         servlet.setDescription("Auto Generated Portlet Invoker Servlet");
184
185         InitParamDD param = new InitParamDD();
186         param.setParamName("portlet-class");
187         param.setParamValue(portlet.getPortletClass());
188         servlet.getInitParams().add(param);
189
190         param = new InitParamDD();
191         param.setParamName("portlet-guid");
192         param.setParamValue(portletAppDescriptorService.getContextPath()+"."+portlet.getPortletName());
193         servlet.getInitParams().add(param);
194
195         createSecurityRoleRefs(webApp, servlet, portlet);
196     }
197
198     private void createSecurityRoleRefs(WebAppDD webApp,
199                                         ServletDD servlet,
200                                         PortletDD portlet) {
201
202         HashMap JavaDoc sRefs = new HashMap JavaDoc();
203         List JavaDoc sSecRoleRefs = servlet.getSecurityRoleRefs();
204         Iterator JavaDoc i = sSecRoleRefs.iterator();
205         SecurityRoleRefDD srr;
206         while(i.hasNext()) {
207             srr = (SecurityRoleRefDD)i.next();
208             sRefs.put(srr.getRoleName(), srr);
209         }
210
211         HashMap JavaDoc wRoles = new HashMap JavaDoc();
212         i = webApp.getSecurityRoles().iterator();
213         SecurityRoleDD sr;
214         while(i.hasNext()) {
215             sr = (SecurityRoleDD)i.next();
216             wRoles.put(sr.getRoleName(), sr);
217         }
218
219         Collection JavaDoc pRoles = portlet.getSecurityRoleRefs();
220         Iterator JavaDoc p = pRoles.iterator();
221         SecurityRoleRefDD pRole;
222         while(p.hasNext()) {
223             pRole = (SecurityRoleRefDD) p.next();
224             if (pRole.getRoleLink()==null
225                 && wRoles.get(pRole.getRoleName())==null) {
226                     System.out.println(
227                         "Note: The web application has no security role defined which matches the role name \""
228                         + pRole.getRoleName()
229                         + "\" of the security-role-ref element defined for the wrapper-servlet with the name '"
230                         + portlet.getPortletName()
231                         + "'.");
232                     break;
233             }
234             SecurityRoleRefDD sRR = (SecurityRoleRefDD)
235                 sRefs.get(pRole.getRoleName());
236
237             if (null != sRR) {
238                 System.out.println("Note: Replaced already existing element of type <security-role-ref> with value \""
239                         + pRole.getRoleName()
240                         + "\" for subelement of type <role-name> for the wrapper-servlet with the name '"
241                         + portlet.getPortletName()
242                         + "'.");
243                     sSecRoleRefs.remove(sRR);
244             }
245             SecurityRoleRefDD dd = new SecurityRoleRefDD();
246             dd.setDescription(pRole.getDescription());
247             dd.setRoleLink(pRole.getRoleLink());
248             dd.setRoleName(pRole.getRoleName());
249             sSecRoleRefs.add(dd);
250         }
251     }
252
253     private void createServletMapping(WebAppDD webApp,
254                                       PortletDD portlet) {
255         if(debug) {
256             System.out.println("<VERBOSE> Creating Servlet Mapping for Portlet: '"+portlet.getPortletName()+"'");
257         }
258         String JavaDoc url= "/"+portlet.getPortletName().replace(' ', '_') + "/*";
259         ServletMappingDD servletMapping = webApp.getServletMapping(url);
260         if (servletMapping != null) {
261             String JavaDoc nm = servletMapping.getServletName();
262             if (!nm.equals(portlet.getPortletName())) {
263                 System.out.println(
264                     "Note: Replaced Servlet Mapping with pattern: "+url
265                 );
266                 servletMapping.setServletName(portlet.getPortletName());
267             }
268         }
269         else {
270             servletMapping = new ServletMappingDD();
271             servletMapping.setUrlPattern(url);
272             servletMapping.setServletName(portlet.getPortletName());
273             webApp.getServletMappings().add(servletMapping);
274         }
275     }
276 }
277
Popular Tags