KickJava   Java API By Example, From Geeks To Geeks.

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


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: ApplyWebAppCatalinaAction.java,v 1.12 2005/04/21 08:59:54 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.service.container;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.struts.action.ActionMessage;
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.objectweb.jonas.jmx.CatalinaObjectName;
40 import org.objectweb.jonas.jmx.JonasManagementRepr;
41 import org.objectweb.jonas.jmx.JonasObjectName;
42 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminException;
43 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
44
45 /**
46  * @author Michel-Ange ANTON
47  */

48 public class ApplyWebAppCatalinaAction extends BaseWebAppAction {
49
50     /**
51      * Signature for the <code>createStandardConnector</code> operation.
52      */

53     private String JavaDoc sa_CreateJOnASStandardContext[] = {
54         "java.lang.String", "java.lang.String", "java.lang.String"
55     };
56
57     private static String JavaDoc sDefaultForward = "ActionEditWebAppCatalina";
58
59 // --------------------------------------------------------- Protected Variables
60

61 // --------------------------------------------------------- Public Methods
62

63     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
64         , HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response)
65         throws IOException JavaDoc, ServletException JavaDoc {
66
67         // Default forward
68
ActionForward oForward = null;
69
70         // Identify the requested action
71
WebAppCatalinaForm oForm = (WebAppCatalinaForm) p_Form;
72
73         // Populate
74
try {
75             // Perform a "Create WebApp" transaction (if requested)
76
if ("create".equals(oForm.getAction())) {
77                 oForward = createWebApplication(oForm, p_Mapping, p_Request);
78             }
79             // Save in memory the new datas
80
else if ("edit".equals(oForm.getAction())) {
81                 oForward = populateMbean(oForm, p_Mapping.findForward(sDefaultForward), p_Mapping
82                     , p_Request);
83             }
84             // Reload the web app
85
else if ("reload".equals(oForm.getAction())) {
86                 oForward = reloadWebApplication(oForm, p_Mapping);
87             }
88             // Stop the web app
89
else if ("stop".equals(oForm.getAction())) {
90                 oForward = stopWebApplication(oForm, p_Mapping);
91             }
92             // Start the web app
93
else if ("start".equals(oForm.getAction()) == true) {
94                 oForward = startWebApplication(oForm, p_Mapping);
95             }
96         } catch (JonasAdminException e) {
97             // Build error
98
m_Errors.add("webapp", new ActionMessage(e.getId()));
99             saveErrors(p_Request, m_Errors);
100             // Return to the current page
101
oForward = new ActionForward(p_Mapping.getInput());
102         } catch (Throwable JavaDoc t) {
103             addGlobalError(t);
104             saveErrors(p_Request, m_Errors);
105             oForward = p_Mapping.findForward("Global Error");
106         }
107
108         // Next Forward
109
return oForward;
110     }
111
112     /**
113      * Create a new web application (Context, Manager, Loader).
114      * @param p_Form The current form
115      * @param p_Mapping The current mapping
116      * @param p_Request The current request
117      * @return The forward to go to the next page
118      * @throws Exception
119      */

120     protected ActionForward createWebApplication(WebAppCatalinaForm p_Form, ActionMapping p_Mapping
121         , HttpServletRequest JavaDoc p_Request)
122         throws Exception JavaDoc {
123         Object JavaDoc values[] = null;
124
125         // Ensure that the requested context is unique
126
ObjectName JavaDoc onContext = findWebAppMbean(p_Form.getPathContext());
127         if (onContext != null) {
128             throw new JonasAdminException("error.webapp.context.exists");
129         }
130
131         // Look up the JOnAS Web service
132
ObjectName JavaDoc onWebService = JonasObjectName.webContainerService();
133         // Create a new JOnASStandardContext object
134
values = new Object JavaDoc[3];
135         // parent
136
values[0] = CatalinaObjectName.catalinaHost(m_WhereAreYou.getCurrentCatalinaServiceName()
137             , m_WhereAreYou.getCurrentCatalinaDefaultHostName()).toString();
138         values[1] = p_Form.getPathContext();
139         values[2] = "";
140         p_Form.setObjectName((String JavaDoc) JonasManagementRepr.invoke(onWebService
141             , "createJOnASStandardContext", values, sa_CreateJOnASStandardContext));
142
143         // Populate
144
ActionForward oForward = populateMbean(p_Form
145             , p_Mapping.findForward("ActionListWebContainers"), p_Mapping, p_Request);
146
147         // Stop the WebApp (Tomcat start it at creating)
148
try {
149             stopWebApplication(p_Form, p_Mapping);
150         } catch (Exception JavaDoc e) {
151             // none
152
}
153
154         // refresh tree
155
refreshTree(p_Request);
156         // Force the node selected in tree
157
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER) + WhereAreYou.NODE_SEPARATOR
158             + "services" + WhereAreYou.NODE_SEPARATOR + "web" + WhereAreYou.NODE_SEPARATOR
159             + p_Form.getName(), true);
160
161         return oForward;
162     }
163
164     /**
165      * Populate the Mbean web application (Context, Manager, Loader).
166      * @param p_Form The current form
167      * @param p_Forward The current forward
168      * @param p_Mapping The current mapping
169      * @param p_Request The current request
170      * @return If 'save' is requested then return the new forward to save
171      * else return the current forward
172      * @throws Exception
173      */

174     protected ActionForward populateMbean(WebAppCatalinaForm p_Form, ActionForward p_Forward
175         , ActionMapping p_Mapping, HttpServletRequest JavaDoc p_Request)
176         throws Exception JavaDoc {
177         ActionForward oForward = p_Forward;
178         // Context
179
ObjectName JavaDoc on = ObjectName.getInstance(p_Form.getObjectName());
180         //setBooleanAttribute(on, "available", p_Form.isAvailable());
181
setBooleanAttribute(on, "cookies", p_Form.isCookies());
182         setBooleanAttribute(on, "crossContext", p_Form.isCrossContext());
183         setBooleanAttribute(on, "reloadable", p_Form.isReloadable());
184         setBooleanAttribute(on, "swallowOutput", p_Form.isSwallowOutput());
185         setBooleanAttribute(on, "override", p_Form.isOverride());
186         // Save in configuration file
187
if (p_Form.isSave()) {
188             //ObjectName onServer = CatalinaObjectName.catalinaServer();
189
//JonasManagementRepr.invoke(onServer, "store", null, null);
190
p_Form.setSave(false);
191             p_Request.setAttribute("forward", p_Forward.getName());
192             oForward = p_Mapping.findForward("ActionEditServletServer");
193         }
194         return oForward;
195     }
196
197     /**
198      * Reload the current web application.
199      * @param p_Form The current form
200      * @param p_Mapping The current mapping
201      * @return The forward to go to the next page
202      * @throws Exception
203      */

204     protected ActionForward reloadWebApplication(WebAppCatalinaForm p_Form, ActionMapping p_Mapping)
205         throws Exception JavaDoc {
206         // Reload
207
try {
208             String JavaDoc sWebModuleObjectName = p_Form.getObjectName();
209             ObjectName JavaDoc proxyObjectName = JonasObjectName.webModuleProxy(m_WhereAreYou.getCurrentCatalinaDomainName());
210             Object JavaDoc[] params = { sWebModuleObjectName };
211             String JavaDoc[] signs = { "java.lang.String" };
212             JonasManagementRepr.invoke(proxyObjectName, "reload", params, signs);
213             /*
214             ObjectName onContext = new ObjectName(p_Form.getObjectName());
215             JonasManagementRepr.invoke(onContext, "reload", null, null);
216             */

217         }
218         catch (Throwable JavaDoc t) {
219             throw new JonasAdminException("error.webapp.reload");
220         }
221         // Forward
222
ActionForward oForward = null;
223         if (p_Form.getName().equals(m_WhereAreYou.getApplicationContextPath())) {
224             oForward = p_Mapping.findForward("Main Index");
225         } else {
226             oForward = p_Mapping.findForward(sDefaultForward);
227         }
228         return oForward;
229     }
230
231     /**
232      * Start the current web application.
233      * @param p_Form The current form
234      * @param p_Mapping The current mapping
235      * @return The forward to go to the next page
236      * @throws Exception
237      */

238     protected ActionForward startWebApplication(WebAppCatalinaForm p_Form, ActionMapping p_Mapping)
239         throws Exception JavaDoc {
240         try {
241             String JavaDoc sWebModuleObjectName = p_Form.getObjectName();
242             ObjectName JavaDoc proxyObjectName = JonasObjectName.webModuleProxy(m_WhereAreYou.getCurrentCatalinaDomainName());
243             Object JavaDoc[] params = {sWebModuleObjectName};
244             String JavaDoc[] signs = {"java.lang.String"};
245             JonasManagementRepr.invoke(proxyObjectName, "start", params, signs);
246             /*
247             ObjectName onContext = new ObjectName(p_Form.getObjectName());
248             JonasManagementRepr.invoke(onContext, "start", null, null);
249             */

250         } catch (Throwable JavaDoc t) {
251             throw new JonasAdminException("error.webapp.start");
252         }
253         return p_Mapping.findForward(sDefaultForward);
254     }
255
256     /**
257      * Stop the current web application.
258      * @param p_Form The current form
259      * @param p_Mapping The current mapping
260      * @return The forward to go to the next page
261      * @throws Exception
262      */

263     protected ActionForward stopWebApplication(WebAppCatalinaForm p_Form, ActionMapping p_Mapping)
264         throws Exception JavaDoc {
265         try {
266             String JavaDoc sWebModuleObjectName = p_Form.getObjectName();
267             ObjectName JavaDoc proxyObjectName = JonasObjectName.webModuleProxy(m_WhereAreYou.getCurrentCatalinaDomainName());
268             Object JavaDoc[] params = {sWebModuleObjectName};
269             String JavaDoc[] signs = {"java.lang.String"};
270             JonasManagementRepr.invoke(proxyObjectName, "stop", params, signs);
271             /*
272             ObjectName onContext = new ObjectName(p_Form.getObjectName());
273             JonasManagementRepr.invoke(onContext, "stop", null, null);
274             */

275         } catch (Throwable JavaDoc t) {
276             throw new JonasAdminException("error.webapp.stop");
277         }
278         return p_Mapping.findForward(sDefaultForward);
279     }
280 }
281
Popular Tags