KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.ArrayList 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 import org.apache.struts.util.MessageResources;
34
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.ObjectInstance JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.JMException JavaDoc;
39
40 import org.apache.webapp.admin.ApplicationServlet;
41 import org.apache.webapp.admin.LabelValueBean;
42 import org.apache.webapp.admin.Lists;
43 import org.apache.webapp.admin.TomcatTreeBuilder;
44
45 /**
46  * A generic <code>Action</code> that sets up <em>Edit
47  * Valve </em> transactions, based on the type of Valve.
48  *
49  * @author Manveen Kaur
50  * @version $Revision: 1.8 $ $Date: 2004/10/18 06:37:55 $
51  */

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

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

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

79     public ActionForward execute(ActionMapping mapping,
80                                  ActionForm form,
81                                  HttpServletRequest JavaDoc request,
82                                  HttpServletResponse JavaDoc response)
83         throws IOException JavaDoc, ServletException JavaDoc {
84         
85         // Acquire the resources that we need
86
HttpSession JavaDoc session = request.getSession();
87         Locale JavaDoc locale = getLocale(request);
88         MessageResources resources = getResources(request);
89         
90         // Acquire a reference to the MBeanServer containing our MBeans
91
try {
92             mBServer = ((ApplicationServlet) getServlet()).getServer();
93         } catch (Throwable JavaDoc t) {
94             throw new ServletException JavaDoc
95             ("Cannot acquire MBeanServer reference", t);
96         }
97         
98         // Set up the object names of the MBeans we are manipulating
99
ObjectName JavaDoc vname = null;
100         StringBuffer JavaDoc sb = null;
101         try {
102             vname = new ObjectName JavaDoc(request.getParameter("select"));
103         } catch (Exception JavaDoc e) {
104             String JavaDoc message =
105                 resources.getMessage(locale, "error.valveName.bad",
106                                      request.getParameter("select"));
107             getServlet().log(message);
108             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
109             return (null);
110         }
111         
112        String JavaDoc parent = request.getParameter("parent");
113        String JavaDoc valveType = null;
114        String JavaDoc attribute = null;
115        
116        // Find what type of Valve this is
117
try {
118             attribute = "className";
119             String JavaDoc className = (String JavaDoc)
120                 mBServer.getAttribute(vname, attribute);
121             int period = className.lastIndexOf(".");
122             if (period >= 0)
123                 valveType = className.substring(period + 1);
124         } catch (Throwable JavaDoc t) {
125           getServlet().log
126                 (resources.getMessage(locale, "users.error.attribute.get",
127                                       attribute), t);
128             response.sendError
129                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
130                  resources.getMessage(locale, "users.error.attribute.get",
131                                       attribute));
132             return (null);
133         }
134
135         // Forward to the appropriate valve display page
136
if ("AccessLogValve".equalsIgnoreCase(valveType)) {
137                setUpAccessLogValve(vname, request, response);
138         } else if ("RemoteAddrValve".equalsIgnoreCase(valveType)) {
139                setUpRemoteAddrValve(vname, request, response);
140         } else if ("RemoteHostValve".equalsIgnoreCase(valveType)) {
141                 setUpRemoteHostValve(vname, request, response);
142         } else if ("RequestDumperValve".equalsIgnoreCase(valveType)) {
143                setUpRequestDumperValve(vname, request, response);
144         } else if ("SingleSignOn".equalsIgnoreCase(valveType)) {
145                setUpSingleSignOnValve(vname, request, response);
146         }
147        
148         
149         return (mapping.findForward(valveType));
150                 
151     }
152
153     private void setUpAccessLogValve(ObjectName JavaDoc vname, HttpServletRequest JavaDoc request,
154                                         HttpServletResponse JavaDoc response)
155     throws IOException JavaDoc {
156         // Fill in the form values for display and editing
157
HttpSession JavaDoc session = request.getSession();
158         Locale JavaDoc locale = getLocale(request);
159         MessageResources resources = getResources(request);
160         String JavaDoc parent = request.getParameter("parent");
161         AccessLogValveForm valveFm = new AccessLogValveForm();
162         session.setAttribute("accessLogValveForm", valveFm);
163         valveFm.setAdminAction("Edit");
164         valveFm.setObjectName(vname.toString());
165         valveFm.setParentObjectName(parent);
166         String JavaDoc valveType = "AccessLogValve";
167         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
168         String JavaDoc host = vname.getKeyProperty("host");
169         String JavaDoc context = vname.getKeyProperty("path");
170         if (host!=null) {
171             sb.append("Host (" + host + ") > ");
172         }
173         if (context!=null) {
174             sb.append("Context (" + context + ") > ");
175         }
176         sb.append("Valve");
177         valveFm.setNodeLabel(sb.toString());
178         valveFm.setValveType(valveType);
179         valveFm.setBooleanVals(Lists.getBooleanValues());
180         String JavaDoc attribute = null;
181         try {
182             
183             // Copy scalar properties
184
attribute = "directory";
185             valveFm.setDirectory
186                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
187             attribute = "pattern";
188             valveFm.setPattern
189                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
190             attribute = "prefix";
191             valveFm.setPrefix
192                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
193             attribute = "suffix";
194             valveFm.setSuffix
195                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
196             attribute = "resolveHosts";
197             valveFm.setResolveHosts
198                 (((Boolean JavaDoc) mBServer.getAttribute(vname, attribute)).toString());
199             attribute = "rotatable";
200             valveFm.setRotatable
201                 (((Boolean JavaDoc) mBServer.getAttribute(vname, attribute)).toString());
202
203         } catch (Throwable JavaDoc t) {
204             getServlet().log
205                 (resources.getMessage(locale, "users.error.attribute.get",
206                                       attribute), t);
207             response.sendError
208                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
209                  resources.getMessage(locale, "users.error.attribute.get",
210                                       attribute));
211         }
212     }
213
214     private void setUpRequestDumperValve(ObjectName JavaDoc vname, HttpServletRequest JavaDoc request,
215                                         HttpServletResponse JavaDoc response)
216     throws IOException JavaDoc {
217         // Fill in the form values for display and editing
218
HttpSession JavaDoc session = request.getSession();
219         Locale JavaDoc locale = getLocale(request);
220         MessageResources resources = getResources(request);
221         String JavaDoc parent = request.getParameter("parent");
222         RequestDumperValveForm valveFm = new RequestDumperValveForm();
223         session.setAttribute("requestDumperValveForm", valveFm);
224         valveFm.setAdminAction("Edit");
225         valveFm.setObjectName(vname.toString());
226         valveFm.setParentObjectName(parent);
227         String JavaDoc valveType = "RequestDumperValve";
228         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Valve (");
229         sb.append(valveType);
230         sb.append(")");
231         valveFm.setNodeLabel(sb.toString());
232         valveFm.setValveType(valveType);
233         String JavaDoc attribute = null;
234     }
235
236     private void setUpSingleSignOnValve(ObjectName JavaDoc vname, HttpServletRequest JavaDoc request,
237                                         HttpServletResponse JavaDoc response)
238     throws IOException JavaDoc {
239         // Fill in the form values for display and editing
240
HttpSession JavaDoc session = request.getSession();
241         Locale JavaDoc locale = getLocale(request);
242         MessageResources resources = getResources(request);
243         String JavaDoc parent = request.getParameter("parent");
244         SingleSignOnValveForm valveFm = new SingleSignOnValveForm();
245         session.setAttribute("singleSignOnValveForm", valveFm);
246         valveFm.setAdminAction("Edit");
247         valveFm.setObjectName(vname.toString());
248         valveFm.setParentObjectName(parent);
249         String JavaDoc valveType = "SingleSignOn";
250         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Valve (");
251         sb.append(valveType);
252         sb.append(")");
253         valveFm.setNodeLabel(sb.toString());
254         valveFm.setValveType(valveType);
255         String JavaDoc attribute = null;
256     }
257
258
259     private void setUpRemoteAddrValve(ObjectName JavaDoc vname, HttpServletRequest JavaDoc request,
260                                         HttpServletResponse JavaDoc response)
261     throws IOException JavaDoc {
262         // Fill in the form values for display and editing
263
HttpSession JavaDoc session = request.getSession();
264         Locale JavaDoc locale = getLocale(request);
265         MessageResources resources = getResources(request);
266         String JavaDoc parent = request.getParameter("parent");
267         RemoteAddrValveForm valveFm = new RemoteAddrValveForm();
268         session.setAttribute("remoteAddrValveForm", valveFm);
269         valveFm.setAdminAction("Edit");
270         valveFm.setObjectName(vname.toString());
271         valveFm.setParentObjectName(parent);
272         String JavaDoc valveType = "RemoteAddrValve";
273         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Valve (");
274         sb.append(valveType);
275         sb.append(")");
276         valveFm.setNodeLabel(sb.toString());
277         valveFm.setValveType(valveType);
278         String JavaDoc attribute = null;
279         try {
280             
281             // Copy scalar properties
282
attribute = "allow";
283             valveFm.setAllow
284                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
285             attribute = "deny";
286             valveFm.setDeny
287                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
288                         
289         } catch (Throwable JavaDoc t) {
290             getServlet().log
291                 (resources.getMessage(locale, "users.error.attribute.get",
292                                       attribute), t);
293             response.sendError
294                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
295                  resources.getMessage(locale, "users.error.attribute.get",
296                                       attribute));
297         }
298     }
299
300     private void setUpRemoteHostValve(ObjectName JavaDoc vname, HttpServletRequest JavaDoc request,
301                                         HttpServletResponse JavaDoc response)
302     throws IOException JavaDoc {
303         // Fill in the form values for display and editing
304
HttpSession JavaDoc session = request.getSession();
305         Locale JavaDoc locale = getLocale(request);
306         MessageResources resources = getResources(request);
307         String JavaDoc parent = request.getParameter("parent");
308         RemoteHostValveForm valveFm = new RemoteHostValveForm();
309         session.setAttribute("remoteHostValveForm", valveFm);
310         valveFm.setAdminAction("Edit");
311         valveFm.setObjectName(vname.toString());
312         valveFm.setParentObjectName(parent);
313         String JavaDoc valveType = "RemoteHostValve";
314         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Valve (");
315         sb.append(valveType);
316         sb.append(")");
317         valveFm.setNodeLabel(sb.toString());
318         valveFm.setValveType(valveType);
319         String JavaDoc attribute = null;
320         try {
321             
322             // Copy scalar properties
323
attribute = "allow";
324             valveFm.setAllow
325                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
326             attribute = "deny";
327             valveFm.setDeny
328                 ((String JavaDoc) mBServer.getAttribute(vname, attribute));
329                         
330         } catch (Throwable JavaDoc t) {
331             getServlet().log
332                 (resources.getMessage(locale, "users.error.attribute.get",
333                                       attribute), t);
334             response.sendError
335                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
336                  resources.getMessage(locale, "users.error.attribute.get",
337                                       attribute));
338         }
339     }
340     
341 }
342
Popular Tags