KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > server > SaveServerAction


1 /*
2  * Copyright 2001,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
18 package org.apache.webapp.admin.server;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.io.IOException JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import org.apache.struts.action.Action;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import javax.management.Attribute JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MBeanServerFactory JavaDoc;
35 import javax.management.QueryExp JavaDoc;
36 import javax.management.Query JavaDoc;
37 import javax.management.ObjectInstance JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import org.apache.webapp.admin.ApplicationServlet;
40 import org.apache.webapp.admin.TomcatTreeBuilder;
41 import org.apache.struts.util.MessageResources;
42
43 /**
44  * Implementation of <strong>Action</strong> that saves server properties.
45  *
46  * @author Jazmin Jonson
47  * @author Manveen Kaur
48  * @version $Revision: 1.6 $ $Date: 2004/10/18 06:37:54 $
49  */

50
51 public final class SaveServerAction extends Action {
52     
53     /**
54      * The MBeanServer we will be interacting with.
55      */

56     private MBeanServer JavaDoc mBServer = null;
57     
58     // --------------------------------------------------------- Public Methods
59

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

76     public ActionForward execute(ActionMapping mapping,
77     ActionForm form,
78     HttpServletRequest JavaDoc request,
79     HttpServletResponse JavaDoc response)
80     throws IOException JavaDoc, ServletException JavaDoc {
81         
82        // Acquire the resources that we need
83
HttpSession JavaDoc session = request.getSession();
84         Locale JavaDoc locale = getLocale(request);
85         MessageResources resources = getResources(request);
86         
87         // Acquire a reference to the MBeanServer containing our MBeans
88
try {
89             mBServer = ((ApplicationServlet) getServlet()).getServer();
90         } catch (Throwable JavaDoc t) {
91             throw new ServletException JavaDoc
92             ("Cannot acquire MBeanServer reference", t);
93         }
94         
95         ActionErrors errors = new ActionErrors();
96         
97         // Report any errors we have discovered back to the original form
98
if (!errors.isEmpty()) {
99             saveErrors(request, errors);
100             return (new ActionForward(mapping.getInput()));
101         }
102         
103         ServerForm sform = (ServerForm) form;
104         String JavaDoc sObjectName = sform.getObjectName();
105         // Acquire a reference to the Server MBean
106
ObjectName JavaDoc soname = null;
107         try {
108             soname = new ObjectName JavaDoc(sObjectName);
109         } catch (Throwable JavaDoc t) {
110             throw new ServletException JavaDoc
111             ("Cannot acquire Server MBean reference ", t);
112         }
113         
114
115         // Perform attribute updates as requested
116
String JavaDoc attribute = null;
117         try{
118             attribute = "port";
119             int port = 0;
120             try {
121                 port = Integer.parseInt(sform.getPortNumberText());
122             } catch (Throwable JavaDoc t) {
123                 port = 0;
124             }
125             mBServer.setAttribute(soname,
126                                   new Attribute JavaDoc("port", new Integer JavaDoc(port)));
127             // set port warning as port < 1024 requires
128
// special software capabilities
129
if (port < 1024) {
130                 request.setAttribute("warning", "server.port.warning");
131             }
132             
133             attribute = "shutdown";
134             mBServer.setAttribute(soname,
135                                   new Attribute JavaDoc("shutdown", sform.getShutdownText()));
136             
137         } catch(Exception JavaDoc e){
138             getServlet().log
139                 (resources.getMessage(locale, "users.error.attribute.set",
140                                       attribute), e);
141             response.sendError
142                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
143                  resources.getMessage(locale, "users.error.attribute.set",
144                                       attribute));
145             return (null);
146        }
147         
148         // Forward to the success reporting page
149
session.removeAttribute(mapping.getAttribute());
150         return (mapping.findForward("Save Successful"));
151     }
152 }
153
Popular Tags