KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > host > DeleteHostsAction


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.host;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.ObjectInstance JavaDoc;
29 import javax.management.modelmbean.ModelMBean JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import org.apache.struts.action.Action;
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
40 import org.apache.webapp.admin.ApplicationServlet;
41 import org.apache.webapp.admin.TomcatTreeBuilder;
42 import org.apache.webapp.admin.TreeControl;
43 import org.apache.webapp.admin.TreeControlNode;
44
45
46 /**
47  * The <code>Action</code> that completes <em>Delete Hosts</em>
48  * transactions.
49  *
50  * @author Manveen Kaur
51  * @version $Revision: 1.7 $ $Date: 2005/01/29 19:36:06 $
52  */

53
54 public class DeleteHostsAction extends Action {
55
56
57     /**
58      * Signature for the <code>removeHost</code> operation.
59      */

60     private String JavaDoc removeHostTypes[] =
61     { "java.lang.String", // Object name
62
};
63
64
65     /**
66      * The MBeanServer we will be interacting with.
67      */

68     private MBeanServer JavaDoc mBServer = null;
69     
70
71     // --------------------------------------------------------- Public Methods
72

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

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