KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.webapp.admin.host;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.ArrayList 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.ActionErrors;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.MBeanServerFactory JavaDoc;
40 import javax.management.QueryExp JavaDoc;
41 import javax.management.Query JavaDoc;
42 import javax.management.ObjectInstance JavaDoc;
43 import javax.management.ObjectName JavaDoc;
44 import javax.management.JMException JavaDoc;
45 import org.apache.struts.util.MessageResources;
46
47 import org.apache.webapp.admin.ApplicationServlet;
48 import org.apache.webapp.admin.Lists;
49 import org.apache.webapp.admin.TomcatTreeBuilder;
50
51 /**
52  * The <code>Action</code> that sets up <em>Delete Hosts</em> transactions.
53  *
54  * @author Manveen Kaur
55  * @version $Revision: 1.9 $ $Date: 2004/10/18 06:37:53 $
56  */

57
58 public class DeleteHostAction extends Action {
59     
60
61     /**
62      * The MBeanServer we will be interacting with.
63      */

64     private MBeanServer JavaDoc mBServer = null;
65     
66
67     // --------------------------------------------------------- Public Methods
68

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

84     public ActionForward execute(ActionMapping mapping,
85                                  ActionForm form,
86                                  HttpServletRequest JavaDoc request,
87                                  HttpServletResponse JavaDoc response)
88         throws IOException JavaDoc, ServletException JavaDoc {
89         
90
91         // Acquire the resources that we need
92
HttpSession JavaDoc session = request.getSession();
93         Locale JavaDoc locale = getLocale(request);
94         MessageResources resources = getResources(request);
95         
96         // Acquire a reference to the MBeanServer containing our MBeans
97
try {
98             mBServer = ((ApplicationServlet) getServlet()).getServer();
99         } catch (Throwable JavaDoc t) {
100             throw new ServletException JavaDoc
101             ("Cannot acquire MBeanServer reference", t);
102         }
103         
104         // Set up a form bean containing the currently selected
105
// objects to be deleted
106
HostsForm hostsForm = new HostsForm();
107         String JavaDoc select = request.getParameter("select");
108         String JavaDoc domain = null;
109         if (select != null) {
110             String JavaDoc hosts[] = new String JavaDoc[1];
111             hosts[0] = select;
112             hostsForm.setHosts(hosts);
113                         
114             try {
115                 domain = (new ObjectName JavaDoc(select)).getDomain();
116             } catch (Exception JavaDoc e) {
117                 throw new ServletException JavaDoc
118                 ("Error extracting service name from the host to be deleted", e);
119             }
120         }
121         String JavaDoc adminHost = null;
122         // Get the host name the admin app runs on
123
// this host cannot be deleted from the admin tool
124
try {
125             adminHost = Lists.getAdminAppHost(
126                                   mBServer, "domain" ,request);
127         } catch (Exception JavaDoc e) {
128             String JavaDoc message =
129                 resources.getMessage(locale, "error.hostName.bad",
130                                         adminHost);
131             getServlet().log(message);
132             response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
133             return (null);
134         }
135         request.setAttribute("adminAppHost", adminHost);
136         request.setAttribute("hostsForm", hostsForm);
137         
138         // Accumulate a list of all available hosts
139
ArrayList JavaDoc list = new ArrayList JavaDoc();
140         try {
141             String JavaDoc pattern = domain + TomcatTreeBuilder.HOST_TYPE +
142                 TomcatTreeBuilder.WILDCARD;
143             Iterator JavaDoc items =
144                 mBServer.queryNames(new ObjectName JavaDoc(pattern), null).iterator();
145             while (items.hasNext()) {
146                 list.add(items.next().toString());
147             }
148         } catch (Exception JavaDoc e) {
149             getServlet().log
150                 (resources.getMessage(locale, "users.error.select"));
151             response.sendError
152                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
153                  resources.getMessage(locale, "users.error.select"));
154             return (null);
155         }
156         Collections.sort(list);
157         request.setAttribute("hostsList", list);
158         
159         // Forward to the list display page
160
return (mapping.findForward("Hosts"));
161
162     }
163
164 }
165
Popular Tags