KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > service > container > BaseWebAppAction


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2003-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BaseWebAppAction.java,v 1.14 2005/07/22 13:57:15 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.service.container;
27
28 import java.net.URL JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import org.apache.struts.action.ActionMapping;
35 import org.objectweb.jonas.jmx.J2eeObjectName;
36 import org.objectweb.jonas.jmx.JonasManagementRepr;
37 import org.objectweb.jonas.jmx.JonasObjectName;
38 import org.objectweb.jonas.webapp.jonasadmin.JettyObjectName;
39 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
40 import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
41
42 /**
43  * @author Michel-Ange ANTON
44  */

45
46 abstract public class BaseWebAppAction extends BaseDeployAction {
47
48 // --------------------------------------------------------- Protected variables
49

50     protected WebAppForm mWebAppForm;
51     protected WarForm mWarForm;
52
53 // --------------------------------------------------------- Public Methods
54

55 // --------------------------------------------------------- Protected Methods
56

57     /**
58      * Initialize the forms and parameters.
59      * @param p_Mapping Mapping to reset form
60      * @param p_Request Http request used
61      * @return true if the forms must be populated
62      */

63     protected boolean initialize(ActionMapping p_Mapping, HttpServletRequest JavaDoc p_Request) {
64
65         boolean bPopulate = false;
66
67         // Global form used
68
mWebAppForm = null;
69         mWarForm = null;
70
71         // Action
72
String JavaDoc sParamAction = p_Request.getParameter("action");
73         // Selected WebApp (ObjectName)
74
String JavaDoc sParamWebAppObjectName = p_Request.getParameter("on");
75         // Selected War
76
String JavaDoc sParamPathWar = p_Request.getParameter("war");
77
78         // Detect create action
79
if (sParamAction != null && "create".equals(sParamAction)) {
80             // Create a new WebApp
81
mWebAppForm = createWebAppForm(p_Mapping, p_Request);
82             mWebAppForm.setAction("create");
83             // Remove WarForm
84
m_Session.removeAttribute("warForm");
85         } else if (sParamWebAppObjectName != null) {
86             // Build forms with the object name
87
mWebAppForm = createWebAppForm(p_Mapping, p_Request);
88             mWebAppForm.setObjectName(sParamWebAppObjectName);
89             bPopulate = true;
90             // Remove WarForm
91
m_Session.removeAttribute("warForm");
92         } else if (sParamPathWar != null) {
93             // Create a new War form
94
mWarForm = createWarForm(p_Mapping, p_Request);
95             mWarForm.setPath(sParamPathWar);
96             mWarForm.setFilename(JonasAdminJmx.extractFilename(sParamPathWar));
97             bPopulate = true;
98         } else {
99             // Edit last form in session
100
mWebAppForm = (WebAppForm) m_Session.getAttribute("webAppForm");
101             if (mWebAppForm != null) {
102                 mWebAppForm.setAction("edit");
103             }
104             mWarForm = (WarForm) m_Session.getAttribute("warForm");
105             // Force populate
106
if (("reload".equals(sParamAction) == true) || ("start".equals(sParamAction) == true)
107                 || ("stop".equals(sParamAction) == true)) {
108                 bPopulate = true;
109             }
110         }
111         return bPopulate;
112     }
113
114     /**
115      * Create a new WebApp form in function of Web Server.
116      * @param p_Mapping Mapping to reset form
117      * @param p_Request Http request used
118      * @return The new form or null if web server unknown
119      */

120     protected WebAppForm createWebAppForm(ActionMapping p_Mapping, HttpServletRequest JavaDoc p_Request) {
121         WebAppForm oForm = null;
122         if (m_WhereAreYou.isCatalinaServer() == true) {
123             oForm = new WebAppCatalinaForm();
124             oForm.reset(p_Mapping, p_Request);
125             m_Session.setAttribute("webAppForm", oForm);
126             m_Session.setAttribute("webAppCatalinaForm", oForm);
127         }
128         else if (m_WhereAreYou.isJettyServer() == true) {
129             oForm = new WebAppJettyForm();
130             oForm.reset(p_Mapping, p_Request);
131             m_Session.setAttribute("webAppForm", oForm);
132             m_Session.setAttribute("webAppJettyForm", oForm);
133         }
134         return oForm;
135     }
136
137     /**
138      * Create a new War form
139      * @param p_Mapping Mapping to reset form
140      * @param p_Request Http request used
141      * @return The new form or null if web server unknown
142      */

143     protected WarForm createWarForm(ActionMapping p_Mapping, HttpServletRequest JavaDoc p_Request) {
144         WarForm oForm = new WarForm();
145         oForm.reset(p_Mapping, p_Request);
146         m_Session.setAttribute("warForm", oForm);
147         return oForm;
148     }
149
150     /**
151      * Populate a WebApp form in function of Web Server.
152      * @param p_ObjectName The MBean name
153      * @param p_Form The form to populate
154      * @throws Exception
155      */

156     protected void populateWebApp(String JavaDoc p_ObjectName, WebAppForm p_Form)
157         throws Exception JavaDoc {
158         if (m_WhereAreYou.isCatalinaServer() == true) {
159             populateWebAppCatalina(p_ObjectName, (WebAppCatalinaForm) p_Form);
160         }
161         else if (m_WhereAreYou.isJettyServer() == true) {
162             populateWebAppJetty(p_ObjectName, (WebAppJettyForm) p_Form);
163         }
164     }
165
166     /**
167      * Populate a form with the Catalina Context MBean
168      * and with the JOnAS War MBean if it's found.
169      * @param p_ObjectName The MBean name
170      * @param p_Form The form to populate
171      * @throws Exception
172      */

173     protected void populateWebAppCatalina(String JavaDoc p_ObjectName, WebAppCatalinaForm p_Form)
174         throws Exception JavaDoc {
175         // Context
176
ObjectName JavaDoc on = new ObjectName JavaDoc(p_ObjectName);
177         p_Form.setObjectName(on.toString());
178         p_Form.setName(on.getKeyProperty("name"));
179         p_Form.setJ2eeApplication(on.getKeyProperty("J2EEApplication"));
180         p_Form.setJ2eeServer(on.getKeyProperty("J2EEServer"));
181         p_Form.setPathContext(WebAppItem.extractPathContext(p_Form.getName()));
182         p_Form.setHost(getStringAttribute(on, "hostname"));
183         p_Form.setCookies(getBooleanAttribute(on, "cookies"));
184         p_Form.setReloadable(getBooleanAttribute(on, "reloadable"));
185         p_Form.setSwallowOutput(getBooleanAttribute(on, "swallowOutput"));
186         p_Form.setCrossContext(getBooleanAttribute(on, "crossContext"));
187         p_Form.setOverride(getBooleanAttribute(on, "override"));
188         p_Form.setState(getIntegerAttribute(on, "state"));
189     }
190
191     /**
192      * Populate a form with the Jetty Context MBean
193      * @param p_ObjectName Jetty WebContainer+WebApplicationContext MBean
194      * @param p_Form The form to populate
195      * @throws Exception
196      */

197     protected void populateWebAppJetty(String JavaDoc p_ObjectName, WebAppJettyForm p_Form)
198         throws Exception JavaDoc {
199             ObjectName JavaDoc on = new ObjectName JavaDoc(p_ObjectName);
200             // Context
201
p_Form.setStarted(getBooleanAttribute(on, "started"));
202             p_Form.setResourceBase(getStringAttribute(on, "resourceBase"));
203             p_Form.setDisplayName(getStringAttribute(on, "displayName"));
204             p_Form.setPathContext(getStringAttribute(on, "contextPath"));
205     }
206
207     /**
208      * Return the JOnAS MBean War.
209      * @param p_PathContext The path context to find
210      * @return The JOnAS MBean War or null if not found
211      * @throws Exception
212      */

213     protected ObjectName JavaDoc findJonasMbeanWar(String JavaDoc p_PathContext)
214         throws Exception JavaDoc {
215         ObjectName JavaDoc onRet = null;
216         ObjectName JavaDoc on;
217         String JavaDoc sPathContext;
218         StringBuffer JavaDoc sb;
219
220         Iterator JavaDoc itOnWars = JonasAdminJmx.getListMbean(JonasObjectName.allWars()).iterator();
221         while (itOnWars.hasNext()) {
222             on = (ObjectName JavaDoc) itOnWars.next();
223             sPathContext = getStringAttribute(on, "ContextRoot");
224             if (sPathContext.charAt(0) != '/') {
225                 sb = new StringBuffer JavaDoc("/");
226                 sb.append(sPathContext);
227                 sPathContext = sb.toString();
228             }
229             // Detect the good War
230
if (p_PathContext.equals(sPathContext) == true) {
231                 onRet = on;
232                 break;
233             }
234         }
235         return onRet;
236     }
237
238     /**
239      * Return the WebApp MBean.
240      * @param p_PathContext The path context to find
241      * @return The WebApplication (WebModule) MBean or null if not found
242      * @throws Exception
243      */

244     protected ObjectName JavaDoc findWebAppMbean(String JavaDoc p_PathContext)
245         throws Exception JavaDoc {
246         ObjectName JavaDoc onRet = null;
247         if (m_WhereAreYou.isCatalinaServer() == true) {
248             onRet = findJ2eeWebModuleMbean(p_PathContext);
249         }
250         else if (m_WhereAreYou.isJettyServer() == true) {
251
252         }
253         return onRet;
254     }
255
256     /**
257      * Return the J2ee WebModule MBean.
258      * @param p_PathContext The path context to find
259      * @return The WebApplication (WebModule) MBean or null if not found
260      * @throws Exception
261      */

262     protected ObjectName JavaDoc findJ2eeWebModuleMbean(String JavaDoc p_PathContext)
263         throws Exception JavaDoc {
264         ObjectName JavaDoc onRet = null;
265         ObjectName JavaDoc on;
266         String JavaDoc sPathContext;
267         String JavaDoc sSearchPathContext = p_PathContext;
268         if (sSearchPathContext.charAt(0) != '/') {
269             sSearchPathContext = "/" + p_PathContext;
270         }
271
272         Iterator JavaDoc itOns = JonasAdminJmx.getListMbean(J2eeObjectName.getWebModules(m_WhereAreYou.
273             getCurrentCatalinaDomainName())).iterator();
274         while (itOns.hasNext()) {
275             on = (ObjectName JavaDoc) itOns.next();
276             //ori: sPathContext = getStringAttribute(on, "name");
277
sPathContext = on.getKeyProperty("name");
278             // Detect the good War
279
if (sSearchPathContext.equals(sPathContext) == true) {
280                 onRet = on;
281                 break;
282             }
283         }
284         return onRet;
285     }
286
287     /**
288      * Populate a form with the JOnAS WebModule MBean given.
289      * @param p_ObjectName The JOnAS WebModue MBean
290      * @param p_Form The form to populate
291      * @throws Exception
292      */

293     protected void populateWar(ObjectName JavaDoc p_ObjectName, WarForm p_Form)
294         throws Exception JavaDoc {
295         if (p_ObjectName != null) {
296             //p_Form.setPath(p_ObjectName.getKeyProperty("fname"));
297
String JavaDoc webModulePath = ((URL JavaDoc) JonasManagementRepr.getAttribute(p_ObjectName, "warURL")).toString();
298             p_Form.setPath(webModulePath);
299             p_Form.setFilename(JonasAdminJmx.extractFilename(p_Form.getPath()));
300             // here the hostName is set to null as the War MBean has HostName=null
301
// hope Jetty MBean will provide in the future the correct host name
302
p_Form.setHostName(getStringAttribute(p_ObjectName, "hostname"));
303             p_Form.setInEarCase(getBooleanAttribute(p_ObjectName, "inEarCase"));
304             //p_Form.setContextRoot(getStringAttribute(p_ObjectName, "ContextRoot"));
305
p_Form.setContextRoot(getStringAttribute(p_ObjectName, "path"));
306             p_Form.setJava2DelegationModel(getBooleanAttribute(p_ObjectName, "java2DelegationModel"));
307             p_Form.setXmlContent(getStringAttribute(p_ObjectName, "deploymentDescriptor"));
308             p_Form.setJonasXmlContent(getStringAttribute(p_ObjectName, "jonasDeploymentDescriptor"));
309             p_Form.setServletsName((String JavaDoc[]) JonasManagementRepr.getAttribute(p_ObjectName
310                 , "servlets"));
311             p_Form.setWarPath((URL JavaDoc) JonasManagementRepr.getAttribute(p_ObjectName, "warURL"));
312             p_Form.setEarPath((URL JavaDoc) JonasManagementRepr.getAttribute(p_ObjectName, "earURL"));
313         }
314     }
315     /**
316      * Populate a form with the JOnAS WebModule MBean given.
317      * @param p_ObjectName The JOnAS WebModue MBean
318      * @param p_Form The form to populate
319      * @throws Exception
320      */

321     protected void populateJettyWar(ObjectName JavaDoc p_ObjectName, WarForm p_Form)
322         throws Exception JavaDoc {
323         if (p_ObjectName != null) {
324             p_Form.setPath(p_ObjectName.getKeyProperty("fname"));
325             p_Form.setFilename(JonasAdminJmx.extractFilename(p_Form.getPath()));
326
327             p_Form.setHostName(getStringAttribute(p_ObjectName, "HostName"));
328             p_Form.setInEarCase(getBooleanAttribute(p_ObjectName, "InEarCase"));
329             p_Form.setContextRoot(getStringAttribute(p_ObjectName, "ContextRoot"));
330             p_Form.setJava2DelegationModel(getBooleanAttribute(p_ObjectName, "Java2DelegationModel"));
331             p_Form.setXmlContent(getStringAttribute(p_ObjectName, "XmlContent"));
332             p_Form.setJonasXmlContent(getStringAttribute(p_ObjectName, "JOnASXmlContent"));
333             p_Form.setServletsName((String JavaDoc[]) JonasManagementRepr.getAttribute(p_ObjectName
334                 , "ServletsName"));
335             p_Form.setWarPath((URL JavaDoc) JonasManagementRepr.getAttribute(p_ObjectName, "WarURL"));
336             p_Form.setEarPath((URL JavaDoc) JonasManagementRepr.getAttribute(p_ObjectName, "EarURL"));
337         }
338     }
339
340     /**
341      * Populate a form with the JOnAS War path given.
342      * @param p_Path The JOnAS War MBean path
343      * @param p_Form The form to populate
344      * @throws Exception
345      */

346     protected void populateWar(String JavaDoc p_Path, WarForm p_Form)
347         throws Exception JavaDoc {
348         populateWar(JonasObjectName.war(p_Path), p_Form);
349     }
350 }
351
Popular Tags