KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > context > DeleteContextsAction


1 /*
2  * Copyright 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
18 package org.apache.webapp.admin.context;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Locale JavaDoc;
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.util.MessageResources;
33
34 import org.apache.webapp.admin.ApplicationServlet;
35 import org.apache.webapp.admin.TomcatTreeBuilder;
36 import org.apache.webapp.admin.TreeControl;
37 import org.apache.webapp.admin.TreeControlNode;
38
39
40 /**
41  * The <code>Action</code> that completes <em>Delete Contexts</em>
42  * transactions.
43  *
44  * @author Manveen Kaur
45  * @version $Revision: 1.7 $ $Date: 2005/01/14 23:55:41 $
46  */

47
48 public class DeleteContextsAction extends Action {
49
50
51     /**
52      * Signature for the <code>removeContext</code> operation.
53      */

54     private String JavaDoc removeContextTypes[] =
55     { "java.lang.String", // Object name
56
};
57
58
59     /**
60      * The MBeanServer we will be interacting with.
61      */

62     private MBeanServer JavaDoc mBServer = null;
63     
64
65     // --------------------------------------------------------- Public Methods
66

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

83     public ActionForward execute(ActionMapping mapping,
84                                  ActionForm form,
85                                  HttpServletRequest JavaDoc request,
86                                  HttpServletResponse JavaDoc response)
87         throws IOException JavaDoc, ServletException JavaDoc {
88         
89         
90         // Look up the components we will be using as needed
91
HttpSession JavaDoc session = request.getSession();
92         Locale JavaDoc locale = getLocale(request);
93         MessageResources resources = getResources(request);
94
95         // Acquire a reference to the MBeanServer containing our MBeans
96
try {
97             mBServer = ((ApplicationServlet) getServlet()).getServer();
98         } catch (Throwable JavaDoc t) {
99             throw new ServletException JavaDoc
100             ("Cannot acquire MBeanServer reference", t);
101         }
102         
103         // Delete the specified Contexts
104
String JavaDoc contexts[] = ((ContextsForm) form).getContexts();
105         String JavaDoc values[] = new String JavaDoc[1];
106         String JavaDoc operation = "removeContext";
107
108         try {
109
110             // Look up our tree control data structure
111
TreeControl control = (TreeControl)
112                 session.getAttribute("treeControlTest");
113
114             // Remove the specified contexts
115
for (int i = 0; i < contexts.length; i++) {
116                 values[0] = contexts[i];
117                 if (control != null) {
118                     control.selectNode(null);
119                     TreeControlNode node = control.findNode(contexts[i]);
120                     String JavaDoc domain = node.getDomain();
121                     ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
122                     mBServer.invoke(fname, operation,
123                                 values, removeContextTypes);
124                     if (node != null) {
125                         node.remove();
126                     } else {
127                         getServlet().log("Missing TreeControlNode for " +
128                                          contexts[i]);
129                     }
130                 } else {
131                     getServlet().log("Missing TreeControl attribute");
132                 }
133             }
134
135         } catch (Exception JavaDoc e) {
136             getServlet().log
137                 (resources.getMessage(locale, "users.error.invoke",
138                                       operation), e);
139             response.sendError
140                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
141                  resources.getMessage(locale, "users.error.invoke",
142                                       operation));
143             return (null);
144
145         }
146
147         // Report successful completion of this transaction
148
return (mapping.findForward("Save Successful"));
149
150     }
151     
152 }
153
Popular Tags