001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2003-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: BaseWebAppAction.java 9967 2007-01-29 14:57:49Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.service.container;
025:
026: import java.net.URL;
027: import java.util.Iterator;
028:
029: import javax.management.ObjectName;
030: import javax.servlet.http.HttpServletRequest;
031:
032: import org.apache.struts.action.ActionMapping;
033: import org.objectweb.jonas.jmx.J2eeObjectName;
034: import org.objectweb.jonas.jmx.JonasManagementRepr;
035: import org.objectweb.jonas.jmx.JonasObjectName;
036: import org.objectweb.jonas.webapp.jonasadmin.JettyObjectName;
037: import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
038: import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
039:
040: /**
041: * @author Michel-Ange ANTON
042: */
043:
044: abstract public class BaseWebAppAction extends BaseDeployAction {
045:
046: // --------------------------------------------------------- Protected variables
047:
048: protected WebAppForm mWebAppForm;
049: protected WarForm mWarForm;
050:
051: // --------------------------------------------------------- Public Methods
052:
053: // --------------------------------------------------------- Protected Methods
054:
055: /**
056: * Initialize the forms and parameters.
057: * @param p_Mapping Mapping to reset form
058: * @param p_Request Http request used
059: * @return true if the forms must be populated
060: */
061: protected boolean initialize(ActionMapping p_Mapping,
062: HttpServletRequest p_Request) {
063:
064: boolean bPopulate = false;
065:
066: // Global form used
067: mWebAppForm = null;
068: mWarForm = null;
069:
070: // Action
071: String sParamAction = p_Request.getParameter("action");
072: // Selected WebApp (ObjectName)
073: String sParamWebAppObjectName = p_Request.getParameter("on");
074: // Selected War
075: String sParamPathWar = p_Request.getParameter("war");
076:
077: // Detect create action
078: if (sParamAction != null && "create".equals(sParamAction)) {
079: // Create a new WebApp
080: mWebAppForm = createWebAppForm(p_Mapping, p_Request);
081: mWebAppForm.setAction("create");
082: // Remove WarForm
083: m_Session.removeAttribute("warForm");
084: } else if (sParamWebAppObjectName != null) {
085: // Build forms with the object name
086: mWebAppForm = createWebAppForm(p_Mapping, p_Request);
087: mWebAppForm.setObjectName(sParamWebAppObjectName);
088: bPopulate = true;
089: // Remove WarForm
090: m_Session.removeAttribute("warForm");
091: } else if (sParamPathWar != null) {
092: // Create a new War form
093: mWarForm = createWarForm(p_Mapping, p_Request);
094: mWarForm.setPath(sParamPathWar);
095: mWarForm.setFilename(JonasAdminJmx
096: .extractFilename(sParamPathWar));
097: bPopulate = true;
098: } else {
099: // Edit last form in session
100: mWebAppForm = (WebAppForm) m_Session
101: .getAttribute("webAppForm");
102: if (mWebAppForm != null) {
103: mWebAppForm.setAction("edit");
104: }
105: mWarForm = (WarForm) m_Session.getAttribute("warForm");
106: // Force populate
107: if (("reload".equals(sParamAction) == true)
108: || ("start".equals(sParamAction) == true)
109: || ("stop".equals(sParamAction) == true)) {
110: bPopulate = true;
111: }
112: }
113: return bPopulate;
114: }
115:
116: /**
117: * Create a new WebApp form in function of Web Server.
118: * @param p_Mapping Mapping to reset form
119: * @param p_Request Http request used
120: * @return The new form or null if web server unknown
121: */
122: protected WebAppForm createWebAppForm(ActionMapping p_Mapping,
123: HttpServletRequest p_Request) {
124: WebAppForm oForm = null;
125: if (m_WhereAreYou.isCatalinaServer() == true) {
126: oForm = new WebAppCatalinaForm();
127: oForm.reset(p_Mapping, p_Request);
128: m_Session.setAttribute("webAppForm", oForm);
129: m_Session.setAttribute("webAppCatalinaForm", oForm);
130: } else if (m_WhereAreYou.isJettyServer() == true) {
131: oForm = new WebAppJettyForm();
132: oForm.reset(p_Mapping, p_Request);
133: m_Session.setAttribute("webAppForm", oForm);
134: m_Session.setAttribute("webAppJettyForm", oForm);
135: }
136: return oForm;
137: }
138:
139: /**
140: * Create a new War form
141: * @param p_Mapping Mapping to reset form
142: * @param p_Request Http request used
143: * @return The new form or null if web server unknown
144: */
145: protected WarForm createWarForm(ActionMapping p_Mapping,
146: HttpServletRequest p_Request) {
147: WarForm oForm = new WarForm();
148: oForm.reset(p_Mapping, p_Request);
149: m_Session.setAttribute("warForm", oForm);
150: return oForm;
151: }
152:
153: /**
154: * Populate a WebApp form in function of Web Server.
155: * @param p_ObjectName The MBean name
156: * @param p_Form The form to populate
157: * @throws Exception
158: */
159: protected void populateWebApp(String p_ObjectName, WebAppForm p_Form)
160: throws Exception {
161: if (m_WhereAreYou.isCatalinaServer() == true) {
162: populateWebAppCatalina(p_ObjectName,
163: (WebAppCatalinaForm) p_Form);
164: } else if (m_WhereAreYou.isJettyServer() == true) {
165: populateWebAppJetty(p_ObjectName, (WebAppJettyForm) p_Form);
166: }
167: }
168:
169: /**
170: * Populate a form with the Catalina Context MBean
171: * and with the JOnAS War MBean if it's found.
172: * @param p_ObjectName The MBean name
173: * @param p_Form The form to populate
174: * @throws Exception
175: */
176: protected void populateWebAppCatalina(String p_ObjectName,
177: WebAppCatalinaForm p_Form) throws Exception {
178: // Context
179: ObjectName on = new ObjectName(p_ObjectName);
180: p_Form.setObjectName(on.toString());
181: p_Form.setName(on.getKeyProperty("name"));
182: p_Form.setJ2eeApplication(on.getKeyProperty("J2EEApplication"));
183: p_Form.setJ2eeServer(on.getKeyProperty("J2EEServer"));
184: p_Form.setPathContext(WebAppItem.extractPathContext(p_Form
185: .getName()));
186: p_Form.setLabelPathContext(WebAppItem.extractLabelPathContext(
187: p_Form.getPathContext(), m_WhereAreYou
188: .getCurrentCatalinaDefaultHostName()));
189: p_Form.setHost(getStringAttribute(on, "hostname"));
190: p_Form.setCookies(getBooleanAttribute(on, "cookies"));
191: p_Form.setReloadable(getBooleanAttribute(on, "reloadable"));
192: p_Form
193: .setSwallowOutput(getBooleanAttribute(on,
194: "swallowOutput"));
195: p_Form.setCrossContext(getBooleanAttribute(on, "crossContext"));
196: p_Form.setOverride(getBooleanAttribute(on, "override"));
197: p_Form.setState(getIntegerAttribute(on, "state"));
198: }
199:
200: /**
201: * Populate a form with the Jetty Context MBean
202: * @param p_ObjectName Jetty WebContainer+WebApplicationContext MBean
203: * @param p_Form The form to populate
204: * @throws Exception
205: */
206: protected void populateWebAppJetty(String p_ObjectName,
207: WebAppJettyForm p_Form) throws Exception {
208: ObjectName on = new ObjectName(p_ObjectName);
209: // Context
210: p_Form.setStarted(getBooleanAttribute(on, "started"));
211: p_Form.setResourceBase(getStringAttribute(on, "resourceBase"));
212: p_Form.setDisplayName(getStringAttribute(on, "displayName"));
213: p_Form.setPathContext(getStringAttribute(on, "contextPath"));
214: p_Form.setLabelPathContext(WebAppItem.extractLabelPathContext(
215: p_Form.getPathContext(), null));
216: }
217:
218: /**
219: * Return the JOnAS MBean War.
220: * @param p_PathContext The path context to find
221: * @return The JOnAS MBean War or null if not found
222: * @throws Exception
223: */
224: protected ObjectName findJonasMbeanWar(String p_PathContext,
225: String jonasServerName) throws Exception {
226: ObjectName onRet = null;
227: ObjectName on;
228: String sPathContext;
229: StringBuffer sb;
230:
231: Iterator itOnWars = JonasAdminJmx.getListMbean(
232: JonasObjectName.allWars(), jonasServerName).iterator();
233: while (itOnWars.hasNext()) {
234: on = (ObjectName) itOnWars.next();
235: sPathContext = getStringAttribute(on, "ContextRoot");
236: if (sPathContext.charAt(0) != '/') {
237: sb = new StringBuffer("/");
238: sb.append(sPathContext);
239: sPathContext = sb.toString();
240: }
241: // Detect the good War
242: if (p_PathContext.equals(sPathContext) == true) {
243: onRet = on;
244: break;
245: }
246: }
247: return onRet;
248: }
249:
250: /**
251: * Return the WebApp MBean.
252: * @param p_PathContext The path context to find
253: * @return The WebApplication (WebModule) MBean or null if not found
254: * @throws Exception
255: */
256: protected ObjectName findWebAppMbean(String p_PathContext,
257: String jonasServerName) throws Exception {
258: ObjectName onRet = null;
259: if (m_WhereAreYou.isCatalinaServer() == true) {
260: onRet = findJ2eeWebModuleMbean(p_PathContext,
261: jonasServerName);
262: } else if (m_WhereAreYou.isJettyServer() == true) {
263:
264: }
265: return onRet;
266: }
267:
268: /**
269: * Return the J2ee WebModule MBean.
270: * @param p_PathContext The path context to find
271: * @return The WebApplication (WebModule) MBean or null if not found
272: * @throws Exception
273: */
274: protected ObjectName findJ2eeWebModuleMbean(String p_PathContext,
275: String jonasServerName) throws Exception {
276: ObjectName onRet = null;
277: ObjectName on;
278: String sPathContext;
279: String sSearchPathContext = p_PathContext;
280: if (sSearchPathContext.charAt(0) != '/') {
281: sSearchPathContext = "/" + p_PathContext;
282: }
283:
284: Iterator itOns = JonasAdminJmx.getListMbean(
285: J2eeObjectName.getWebModules(m_WhereAreYou
286: .getCurrentCatalinaDomainName()),
287: jonasServerName).iterator();
288: while (itOns.hasNext()) {
289: on = (ObjectName) itOns.next();
290: //ori: sPathContext = getStringAttribute(on, "name");
291: sPathContext = on.getKeyProperty("name");
292: // Detect the good War
293: if (sSearchPathContext.equals(sPathContext) == true) {
294: onRet = on;
295: break;
296: }
297: }
298: return onRet;
299: }
300:
301: /**
302: * Populate a form with the JOnAS WebModule MBean given.
303: * @param p_ObjectName The JOnAS WebModue MBean
304: * @param p_Form The form to populate
305: * @throws Exception
306: */
307: protected void populateWar(ObjectName p_ObjectName, WarForm p_Form,
308: String jonasServerName) throws Exception {
309: if (p_ObjectName != null) {
310: //p_Form.setPath(p_ObjectName.getKeyProperty("fname"));
311: String webModulePath = ((URL) JonasManagementRepr
312: .getAttribute(p_ObjectName, "warURL",
313: jonasServerName)).toString();
314: p_Form.setPath(webModulePath);
315: p_Form.setFilename(JonasAdminJmx.extractFilename(p_Form
316: .getPath()));
317: // here the hostName is set to null as the War MBean has HostName=null
318: // hope Jetty MBean will provide in the future the correct host name
319: p_Form.setHostName(getStringAttribute(p_ObjectName,
320: "hostname"));
321: p_Form.setInEar(getBooleanAttribute(p_ObjectName,
322: "inEarCase"));
323: //p_Form.setContextRoot(getStringAttribute(p_ObjectName, "ContextRoot"));
324: p_Form.setContextRoot(getStringAttribute(p_ObjectName,
325: "path"));
326: p_Form.setJava2DelegationModel(getBooleanAttribute(
327: p_ObjectName, "java2DelegationModel"));
328: p_Form.setXmlContent(getStringAttribute(p_ObjectName,
329: "deploymentDescriptor"));
330: p_Form.setJonasXmlContent(getStringAttribute(p_ObjectName,
331: "jonasDeploymentDescriptor"));
332: p_Form.setServletsName((String[]) JonasManagementRepr
333: .getAttribute(p_ObjectName, "servlets",
334: jonasServerName));
335: p_Form.setWarPath((URL) JonasManagementRepr.getAttribute(
336: p_ObjectName, "warURL", jonasServerName));
337: p_Form.setEarPath((URL) JonasManagementRepr.getAttribute(
338: p_ObjectName, "earURL", jonasServerName));
339: p_Form.setEarON((String) JonasManagementRepr.getAttribute(
340: p_ObjectName, "earON", jonasServerName));
341: }
342: }
343:
344: /**
345: * Populate a form with the JOnAS WebModule MBean given.
346: * @param p_ObjectName The JOnAS WebModue MBean
347: * @param p_Form The form to populate
348: * @throws Exception
349: */
350: protected void populateJettyWar(ObjectName p_ObjectName,
351: WarForm p_Form, String jonasServerName) throws Exception {
352: if (p_ObjectName != null) {
353: p_Form.setPath(p_ObjectName.getKeyProperty("fname"));
354: p_Form.setFilename(JonasAdminJmx.extractFilename(p_Form
355: .getPath()));
356:
357: p_Form.setHostName(getStringAttribute(p_ObjectName,
358: "HostName"));
359: p_Form.setInEar(getBooleanAttribute(p_ObjectName,
360: "InEarCase"));
361: p_Form.setContextRoot(getStringAttribute(p_ObjectName,
362: "ContextRoot"));
363: p_Form.setJava2DelegationModel(getBooleanAttribute(
364: p_ObjectName, "Java2DelegationModel"));
365: p_Form.setXmlContent(getStringAttribute(p_ObjectName,
366: "XmlContent"));
367: p_Form.setJonasXmlContent(getStringAttribute(p_ObjectName,
368: "JOnASXmlContent"));
369: p_Form.setServletsName((String[]) JonasManagementRepr
370: .getAttribute(p_ObjectName, "ServletsName",
371: jonasServerName));
372: p_Form.setWarPath((URL) JonasManagementRepr.getAttribute(
373: p_ObjectName, "WarURL", jonasServerName));
374: p_Form.setEarPath((URL) JonasManagementRepr.getAttribute(
375: p_ObjectName, "EarURL", jonasServerName));
376: }
377: }
378:
379: /**
380: * Populate a form with the JOnAS War path given.
381: * @param p_Path The JOnAS War MBean path
382: * @param p_Form The form to populate
383: * @throws Exception
384: */
385: protected void populateWar(String p_Path, WarForm p_Form,
386: String jonasServerName) throws Exception {
387: populateWar(JonasObjectName.war(p_Path), p_Form,
388: jonasServerName);
389: }
390: }
|