KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > host > SaveHostAction


1 /*
2  * Copyright 2001-2002,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.webapp.admin.host;
18
19
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.management.Attribute JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.QueryExp JavaDoc;
28 import javax.management.Query JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.JMException JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import org.apache.struts.action.Action;
37 import org.apache.struts.action.ActionError;
38 import org.apache.struts.action.ActionErrors;
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42 import org.apache.struts.util.MessageResources;
43 import org.apache.webapp.admin.ApplicationServlet;
44 import org.apache.webapp.admin.TomcatTreeBuilder;
45 import org.apache.webapp.admin.TreeControl;
46 import org.apache.webapp.admin.TreeControlNode;
47
48
49
50 /**
51  * The <code>Action</code> that completes <em>Add Host</em> and
52  * <em>Edit Host</em> transactions.
53  *
54  * @author Manveen Kaur
55  * @version $Revision: 1.13 $ $Date: 2005/01/29 19:36:06 $
56  */

57
58 public final class SaveHostAction extends Action {
59
60
61     // ----------------------------------------------------- Instance Variables
62

63     /**
64      * Signature for the <code>createStandardHost</code> operation.
65      */

66     private String JavaDoc createStandardHostTypes[] =
67     { "java.lang.String", // parent
68
"java.lang.String", // name
69
"java.lang.String", // appBase
70
"boolean", // autoDeploy
71
"boolean", // deployOnStartup
72
"boolean", // deployXML
73
"boolean", // unpackWARs
74
"boolean", // xmlNamespaceAware
75
"boolean", // xmlValidation
76
};
77
78     /**
79      * The MBeanServer we will be interacting with.
80      */

81     private MBeanServer JavaDoc mBServer = null;
82
83
84     // --------------------------------------------------------- Public Methods
85

86
87     /**
88      * Process the specified HTTP request, and create the corresponding HTTP
89      * response (or forward to another web component that will create it).
90      * Return an <code>ActionForward</code> instance describing where and how
91      * control should be forwarded, or <code>null</code> if the response has
92      * already been completed.
93      *
94      * @param mapping The ActionMapping used to select this instance
95      * @param actionForm The optional ActionForm bean for this request (if any)
96      * @param request The HTTP request we are processing
97      * @param response The HTTP response we are creating
98      *
99      * @exception IOException if an input/output error occurs
100      * @exception ServletException if a servlet exception occurs
101      */

102     public ActionForward execute(ActionMapping mapping,
103                                  ActionForm form,
104                                  HttpServletRequest JavaDoc request,
105                                  HttpServletResponse JavaDoc response)
106         throws IOException JavaDoc, ServletException JavaDoc {
107
108         // Acquire the resources that we need
109
HttpSession JavaDoc session = request.getSession();
110         Locale JavaDoc locale = getLocale(request);
111         MessageResources resources = getResources(request);
112
113         // Acquire a reference to the MBeanServer containing our MBeans
114
try {
115             mBServer = ((ApplicationServlet) getServlet()).getServer();
116         } catch (Throwable JavaDoc t) {
117             throw new ServletException JavaDoc
118             ("Cannot acquire MBeanServer reference", t);
119         }
120
121         // Identify the requested action
122
HostForm hform = (HostForm) form;
123         String JavaDoc adminAction = hform.getAdminAction();
124         String JavaDoc hObjectName = hform.getObjectName();
125         ObjectName JavaDoc honame = null;
126
127         // Perform a "Create Host" transaction (if requested)
128
if ("Create".equals(adminAction)) {
129
130             String JavaDoc operation = null;
131             Object JavaDoc values[] = null;
132
133             try {
134                 String JavaDoc serviceName = hform.getServiceName();
135                 ObjectName JavaDoc soname = new ObjectName JavaDoc(serviceName);
136                 String JavaDoc domain = soname.getDomain();
137                 // Ensure that the requested host name is unique
138
ObjectName JavaDoc oname =
139                     new ObjectName JavaDoc(domain +
140                                    TomcatTreeBuilder.HOST_TYPE +
141                                    ",host=" + hform.getHostName());
142                 if (mBServer.isRegistered(oname)) {
143                     ActionErrors errors = new ActionErrors();
144                     errors.add("hostName",
145                                new ActionError("error.hostName.exists"));
146                     saveErrors(request, errors);
147                     return (new ActionForward(mapping.getInput()));
148                 }
149
150                 // Look up our MBeanFactory MBean
151
ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
152
153                 // Create a new StandardHost object
154
values = new Object JavaDoc[9];
155                 values[0] = domain + TomcatTreeBuilder.ENGINE_TYPE;
156                 values[1] = hform.getHostName();
157                 values[2] = hform.getAppBase();
158                 values[3] = new Boolean JavaDoc(hform.getAutoDeploy());
159                 values[4] = new Boolean JavaDoc(hform.getDeployOnStartup());
160                 values[5] = new Boolean JavaDoc(hform.getDeployXML());
161                 values[6] = new Boolean JavaDoc(hform.getUnpackWARs());
162                 values[7] = new Boolean JavaDoc(hform.getXmlNamespaceAware());
163                 values[8] = new Boolean JavaDoc(hform.getXmlValidation());
164
165
166                 operation = "createStandardHost";
167                 hObjectName = (String JavaDoc)
168                     mBServer.invoke(fname, operation,
169                                     values, createStandardHostTypes);
170
171                 // Add the new Host to our tree control node
172
TreeControl control = (TreeControl)
173                     session.getAttribute("treeControlTest");
174                 if (control != null) {
175                     String JavaDoc parentName = serviceName;
176                     TreeControlNode parentNode = control.findNode(parentName);
177                     if (parentNode != null) {
178                         String JavaDoc nodeLabel =
179                             resources.getMessage(locale, "server.service.treeBuilder.host") +
180                             " (" + hform.getHostName() + ")";
181                         String JavaDoc encodedName =
182                             URLEncoder.encode(hObjectName,TomcatTreeBuilder.URL_ENCODING);
183                         TreeControlNode childNode =
184                             new TreeControlNode(hObjectName,
185                                                 "Host.gif",
186                                                 nodeLabel,
187                                                 "EditHost.do?select=" +
188                                                 encodedName,
189                                                 "content",
190                                                 true, domain);
191                         parentNode.addChild(childNode);
192                         // FIXME - force a redisplay
193
} else {
194                         getServlet().log
195                             ("Cannot find parent node '" + parentName + "'");
196                     }
197                 } else {
198                     getServlet().log
199                         ("Cannot find TreeControlNode!");
200                 }
201
202             } catch (Exception JavaDoc e) {
203                 getServlet().log
204                     (resources.getMessage(locale, "users.error.invoke",
205                                           operation), e);
206                 response.sendError
207                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
208                      resources.getMessage(locale, "users.error.invoke",
209                                           operation));
210                 return (null);
211
212             }
213
214         }
215
216         // Perform attribute updates as requested
217
String JavaDoc attribute = null;
218         try {
219
220             honame = new ObjectName JavaDoc(hObjectName);
221
222             attribute = "appBase";
223             String JavaDoc appBase = "";
224             try {
225                 appBase = hform.getAppBase();
226             } catch (Throwable JavaDoc t) {
227                 appBase = "";
228             }
229             mBServer.setAttribute(honame,
230                                   new Attribute JavaDoc("appBase", appBase));
231
232             attribute = "autoDeploy";
233             String JavaDoc autoDeploy = "true";
234             try {
235                 autoDeploy = hform.getAutoDeploy();
236             } catch (Throwable JavaDoc t) {
237                 autoDeploy = "true";
238             }
239             mBServer.setAttribute(honame,
240                                   new Attribute JavaDoc("autoDeploy", new Boolean JavaDoc(autoDeploy)));
241
242             attribute = "deployXML";
243             String JavaDoc deployXML = "true";
244             try {
245                 deployXML = hform.getDeployXML();
246             } catch (Throwable JavaDoc t) {
247                 deployXML = "true";
248             }
249             mBServer.setAttribute(honame,
250                                   new Attribute JavaDoc("deployXML", new Boolean JavaDoc(deployXML)));
251
252             attribute = "deployOnStartup";
253             String JavaDoc deployOnStartup = "true";
254             try {
255                 deployOnStartup = hform.getDeployOnStartup();
256             } catch (Throwable JavaDoc t) {
257                 deployOnStartup = "true";
258             }
259             mBServer.setAttribute(honame,
260                                   new Attribute JavaDoc("deployOnStartup", new Boolean JavaDoc(deployOnStartup)));
261                                   
262             attribute = "unpackWARs";
263             String JavaDoc unpackWARs = "false";
264             try {
265                 unpackWARs = hform.getUnpackWARs();
266             } catch (Throwable JavaDoc t) {
267                 unpackWARs = "false";
268             }
269             mBServer.setAttribute(honame,
270                                   new Attribute JavaDoc("unpackWARs", new Boolean JavaDoc(unpackWARs)));
271
272             attribute = "xmlNamespaceAware";
273             String JavaDoc xmlNamespaceAware = "false";
274             try {
275                 xmlNamespaceAware = hform.getXmlNamespaceAware();
276             } catch (Throwable JavaDoc t) {
277                 xmlNamespaceAware = "false";
278             }
279             mBServer.setAttribute(honame,
280                                   new Attribute JavaDoc("xmlNamespaceAware", new Boolean JavaDoc(xmlNamespaceAware)));
281
282             attribute = "xmlValidation";
283             String JavaDoc xmlValidation = "false";
284             try {
285                 xmlValidation = hform.getXmlValidation();
286             } catch (Throwable JavaDoc t) {
287                 xmlValidation = "false";
288             }
289             mBServer.setAttribute(honame,
290                                   new Attribute JavaDoc("xmlValidation", new Boolean JavaDoc(xmlValidation)));
291
292         } catch (Exception JavaDoc e) {
293
294             getServlet().log
295                 (resources.getMessage(locale, "users.error.attribute.set",
296                                       attribute), e);
297             response.sendError
298                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
299                  resources.getMessage(locale, "users.error.attribute.set",
300                                       attribute));
301             return (null);
302         }
303
304         // Forward to the success reporting page
305
session.removeAttribute(mapping.getAttribute());
306         return (mapping.findForward("Save Successful"));
307
308     }
309
310 }
311
Popular Tags