KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > CommitChangesAction


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 package org.apache.webapp.admin;
18
19
20 import java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33
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 /**
42  * Implementation of <strong>Action</strong> that saves the current settings
43  * and writes them out to server.xml
44  *
45  * @author Manveen Kaur
46  * @version $Revision: 1.6 $ $Date: 2004/10/18 06:37:53 $
47  */

48
49 public final class CommitChangesAction extends Action {
50
51     /**
52      * The MBeanServer we will be interacting with.
53      */

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

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

75     public ActionForward execute(ActionMapping mapping,
76                                  ActionForm form,
77                                  HttpServletRequest JavaDoc request,
78                                  HttpServletResponse JavaDoc response)
79         throws IOException JavaDoc, ServletException JavaDoc {
80
81         // Acquire a reference to the MBeanServer containing our MBeans
82
try {
83             mBServer = ((ApplicationServlet) getServlet()).getServer();
84         } catch (Throwable JavaDoc t) {
85             throw new ServletException JavaDoc
86             ("Cannot acquire MBeanServer reference", t);
87         }
88
89        // Acquire the resources that we need
90
HttpSession JavaDoc session = request.getSession();
91         Locale JavaDoc locale = getLocale(request);
92         MessageResources resources = getResources(request);
93         
94        ObjectName JavaDoc sname = null;
95         try {
96            sname = new ObjectName JavaDoc(TomcatTreeBuilder.DEFAULT_DOMAIN +
97                                     TomcatTreeBuilder.SERVER_TYPE);
98         } catch (Exception JavaDoc e) {
99             String JavaDoc message = "Could not get Server Object";
100             getServlet().log(message);
101             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
102             return (null);
103         }
104         
105        String JavaDoc operation = "storeConfig";
106        try {
107             mBServer.invoke(sname, operation, null, null);
108         } catch (Throwable JavaDoc t) {
109             getServlet().log
110             (resources.getMessage(locale, "users.error.invoke",
111                                   operation), t);
112             response.sendError
113                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
114                 resources.getMessage(locale, "users.error.invoke",
115                                      operation));
116             return (null);
117         }
118  
119
120         getServlet().log("Debugging -- changes saved to conf/server.xml");
121         // Forward control back to the banner
122
return (mapping.findForward("Banner"));
123
124     }
125
126
127 }
128
Popular Tags