KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionErrors;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.util.MessageResources;
35
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.ObjectInstance JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.JMException JavaDoc;
40
41 import org.apache.webapp.admin.ApplicationServlet;
42 import org.apache.webapp.admin.LabelValueBean;
43 import org.apache.webapp.admin.Lists;
44 import org.apache.webapp.admin.TomcatTreeBuilder;
45
46 /**
47  * The <code>Action</code> that sets up <em>Edit Host</em> transactions.
48  *
49  * @author Manveen Kaur
50  * @version $Revision: 1.11 $ $Date: 2004/10/18 06:37:53 $
51  */

52
53 public class EditHostAction extends Action {
54
55     /**
56      * The MBeanServer we will be interacting with.
57      */

58     private MBeanServer JavaDoc mBServer = null;
59
60
61     // --------------------------------------------------------- Public Methods
62

63     /**
64      * Process the specified HTTP request, and create the corresponding HTTP
65      * response (or forward to another web component that will create it).
66      * Return an <code>ActionForward</code> instance describing where and how
67      * control should be forwarded, or <code>null</code> if the response has
68      * already been completed.
69      *
70      * @param mapping The ActionMapping used to select this instance
71      * @param actionForm The optional ActionForm bean for this request (if any)
72      * @param request The HTTP request we are processing
73      * @param response The HTTP response we are creating
74      *
75      * @exception IOException if an input/output error occurs
76      * @exception ServletException if a servlet exception occurs
77      */

78     public ActionForward execute(ActionMapping mapping,
79                                  ActionForm form,
80                                  HttpServletRequest JavaDoc request,
81                                  HttpServletResponse JavaDoc response)
82         throws IOException JavaDoc, ServletException JavaDoc {
83
84         // Acquire the resources that we need
85
HttpSession JavaDoc session = request.getSession();
86         Locale JavaDoc locale = getLocale(request);
87         MessageResources resources = getResources(request);
88
89         // Acquire a reference to the MBeanServer containing our MBeans
90
try {
91             mBServer = ((ApplicationServlet) getServlet()).getServer();
92         } catch (Throwable JavaDoc t) {
93             throw new ServletException JavaDoc
94             ("Cannot acquire MBeanServer reference", t);
95         }
96
97         // Set up the object names of the MBeans we are manipulating
98
ObjectName JavaDoc hname = null;
99         StringBuffer JavaDoc sb = null;
100         try {
101             hname = new ObjectName JavaDoc(request.getParameter("select"));
102         } catch (Exception JavaDoc e) {
103             String JavaDoc message =
104                 resources.getMessage(locale, "error.hostName.bad",
105                                      request.getParameter("select"));
106             getServlet().log(message);
107             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
108             return (null);
109         }
110         
111         String JavaDoc adminHost = null;
112         String JavaDoc domain = hname.getDomain();
113         // Get the host name the admin app runs on
114
// this host cannot be deleted from the admin tool
115
try {
116             adminHost = Lists.getAdminAppHost(
117                                   mBServer, domain ,request);
118         } catch (Exception JavaDoc e) {
119             String JavaDoc message =
120                 resources.getMessage(locale, "error.hostName.bad",
121                                         adminHost);
122             getServlet().log(message);
123             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
124             return (null);
125         }
126         request.setAttribute("adminAppHost", adminHost);
127
128         // Fill in the form values for display and editing
129
HostForm hostFm = new HostForm();
130         session.setAttribute("hostForm", hostFm);
131         hostFm.setAdminAction("Edit");
132         hostFm.setObjectName(hname.toString());
133         sb = new StringBuffer JavaDoc();
134         sb.append(resources.getMessage(locale, "server.service.treeBuilder.host"));
135         sb.append(" (");
136         sb.append(hname.getKeyProperty("host"));
137         sb.append(")");
138         hostFm.setNodeLabel(sb.toString());
139         hostFm.setBooleanVals(Lists.getBooleanValues());
140
141         String JavaDoc attribute = null;
142         try {
143
144             // Copy scalar properties
145
attribute = "name";
146             hostFm.setHostName
147                 ((String JavaDoc) mBServer.getAttribute(hname, attribute));
148
149             attribute = "appBase";
150             hostFm.setAppBase
151                 ((String JavaDoc) mBServer.getAttribute(hname, attribute));
152             attribute = "autoDeploy";
153             hostFm.setAutoDeploy
154                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
155             attribute = "deployXML";
156             hostFm.setDeployXML
157                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
158             attribute = "deployOnStartup";
159             hostFm.setDeployOnStartup
160                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
161             attribute = "unpackWARs";
162             hostFm.setUnpackWARs
163                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
164             attribute = "xmlNamespaceAware";
165             hostFm.setXmlNamespaceAware
166                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
167             attribute = "xmlValidation";
168             hostFm.setXmlValidation
169                 (((Boolean JavaDoc) mBServer.getAttribute(hname, attribute)).toString());
170
171         } catch (Throwable JavaDoc t) {
172             getServlet().log
173                 (resources.getMessage(locale, "users.error.attribute.get",
174                                       attribute), t);
175             response.sendError
176                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
177                  resources.getMessage(locale, "users.error.attribute.get",
178                                       attribute));
179             return (null);
180         }
181
182         // retrieve all aliases
183
String JavaDoc operation = null;
184         try {
185             operation = "findAliases";
186             String JavaDoc aliases[] =
187                 (String JavaDoc[]) mBServer.invoke(hname, operation, null, null);
188
189             hostFm.setAliasVals(new ArrayList JavaDoc(Arrays.asList(aliases)));
190
191         } catch (Throwable JavaDoc t) {
192             getServlet().log
193             (resources.getMessage(locale, "users.error.invoke",
194                                   operation), t);
195             response.sendError
196                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
197                 resources.getMessage(locale, "users.error.invoke",
198                                      operation));
199             return (null);
200         }
201
202         // Forward to the host display page
203
return (mapping.findForward("Host"));
204
205     }
206 }
207
Popular Tags