KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > valve > SaveAccessLogValveAction


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.valve;
18
19 import java.util.Locale JavaDoc;
20 import java.io.IOException JavaDoc;
21 import javax.management.Attribute JavaDoc;
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.MBeanServerFactory JavaDoc;
24 import javax.management.QueryExp JavaDoc;
25 import javax.management.Query JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.JMException JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32 import org.apache.struts.action.Action;
33 import org.apache.struts.action.ActionError;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.util.MessageResources;
39 import org.apache.webapp.admin.ApplicationServlet;
40
41 /**
42  * The <code>Action</code> that completes <em>Add Valve</em> and
43  * <em>Edit Valve</em> transactions for AccessLog valve.
44  *
45  * @author Manveen Kaur
46  * @version $Revision: 1.8 $ $Date: 2004/10/18 06:37:55 $
47  */

48
49 public final class SaveAccessLogValveAction extends Action {
50
51
52     // ----------------------------------------------------- Instance Variables
53

54     /**
55      * The MBeanServer we will be interacting with.
56      */

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

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

77     public ActionForward execute(ActionMapping mapping,
78                                  ActionForm form,
79                                  HttpServletRequest JavaDoc request,
80                                  HttpServletResponse JavaDoc response)
81         throws IOException JavaDoc, ServletException JavaDoc {
82         
83         // Acquire the resources that we need
84
HttpSession JavaDoc session = request.getSession();
85         Locale JavaDoc locale = getLocale(request);
86         MessageResources resources = getResources(request);
87         
88         // Acquire a reference to the MBeanServer containing our MBeans
89
try {
90             mBServer = ((ApplicationServlet) getServlet()).getServer();
91         } catch (Throwable JavaDoc t) {
92             throw new ServletException JavaDoc
93             ("Cannot acquire MBeanServer reference", t);
94         }
95         
96         // Identify the requested action
97
AccessLogValveForm vform = (AccessLogValveForm) form;
98         String JavaDoc adminAction = vform.getAdminAction();
99         String JavaDoc vObjectName = vform.getObjectName();
100         String JavaDoc parent = vform.getParentObjectName();
101         String JavaDoc valveType = vform.getValveType();
102         
103         // Perform a "Create Valve" transaction (if requested)
104
if ("Create".equals(adminAction)) {
105         
106             vObjectName = ValveUtil.createValve(parent, valveType,
107                                 response, request, mapping,
108                                 (ApplicationServlet) getServlet());
109            
110         }
111
112         // Perform attribute updates as requested
113
String JavaDoc attribute = null;
114         try {
115         
116             ObjectName JavaDoc voname = new ObjectName JavaDoc(vObjectName);
117             
118             attribute = "directory";
119             mBServer.setAttribute(voname,
120                          new Attribute JavaDoc("directory", vform.getDirectory()));
121             attribute = "pattern";
122             mBServer.setAttribute(voname,
123                         new Attribute JavaDoc("pattern", vform.getPattern()));
124             attribute = "prefix";
125             mBServer.setAttribute(voname,
126                         new Attribute JavaDoc("prefix", vform.getPrefix()));
127             attribute = "suffix";
128             mBServer.setAttribute(voname,
129                         new Attribute JavaDoc("suffix", vform.getSuffix()));
130             attribute = "resolveHosts";
131             mBServer.setAttribute(voname,
132                         new Attribute JavaDoc("resolveHosts", new Boolean JavaDoc(vform.getResolveHosts())));
133             attribute = "rotatable";
134             mBServer.setAttribute(voname,
135                         new Attribute JavaDoc("rotatable", new Boolean JavaDoc(vform.getRotatable())));
136
137         } catch (Exception JavaDoc e) {
138
139             getServlet().log
140                 (resources.getMessage(locale, "users.error.attribute.set",
141                                       attribute), e);
142             response.sendError
143                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
144                  resources.getMessage(locale, "users.error.attribute.set",
145                                       attribute));
146             return (null);
147         }
148     
149         // Forward to the success reporting page
150
session.removeAttribute(mapping.getAttribute());
151         return (mapping.findForward("Save Successful"));
152     }
153 }
154
Popular Tags