KickJava   Java API By Example, From Geeks To Geeks.

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


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

46
47 public class DeleteContextAction extends Action {
48     
49
50     /**
51      * The MBeanServer we will be interacting with.
52      */

53     private MBeanServer JavaDoc mBServer = null;
54     
55
56     // --------------------------------------------------------- Public Methods
57

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

73     public ActionForward execute(ActionMapping mapping,
74                                  ActionForm form,
75                                  HttpServletRequest JavaDoc request,
76                                  HttpServletResponse JavaDoc response)
77         throws IOException JavaDoc, ServletException JavaDoc {
78         
79
80         // Acquire the resources that we need
81
HttpSession JavaDoc session = request.getSession();
82         Locale JavaDoc locale = getLocale(request);
83         MessageResources resources = getResources(request);
84         
85         // Acquire a reference to the MBeanServer containing our MBeans
86
try {
87             mBServer = ((ApplicationServlet) getServlet()).getServer();
88         } catch (Throwable JavaDoc t) {
89             throw new ServletException JavaDoc
90             ("Cannot acquire MBeanServer reference", t);
91         }
92         
93         // object name that forms the basis of the search pattern
94
// to get list of all available contexts
95
String JavaDoc patternObject = null;
96         
97         // Set up a form bean containing the currently selected
98
// objects to be deleted
99
ContextsForm contextsForm = new ContextsForm();
100         String JavaDoc select = request.getParameter("select");
101         if (select != null) {
102             String JavaDoc contexts[] = new String JavaDoc[1];
103             contexts[0] = select;
104             contextsForm.setContexts(contexts);
105             patternObject = select;
106         }
107         request.setAttribute("contextsForm", contextsForm);
108                 
109         // get the parent host object name
110
String JavaDoc parent = request.getParameter("parent");
111         if (parent != null) {
112             patternObject = parent;
113         }
114         
115         // Accumulate a list of all available contexts
116
ArrayList JavaDoc list = new ArrayList JavaDoc();
117         try {
118             ObjectName JavaDoc poname = new ObjectName JavaDoc(patternObject);
119             String JavaDoc domain = poname.getDomain();
120             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(domain);
121             sb.append(":j2eeType=WebModule,*");
122             ObjectName JavaDoc search = new ObjectName JavaDoc(sb.toString());
123             // get all available contexts only for this host
124
Iterator JavaDoc items =
125                 mBServer.queryNames(search, null).iterator();
126             String JavaDoc item = null;
127             String JavaDoc host = poname.getKeyProperty("host");
128             if (host==null) {
129                 String JavaDoc name = poname.getKeyProperty("name");
130                 if ((name != null) && (name.length() > 0)) {
131                     name = name.substring(2);
132                     int i = name.indexOf("/");
133                     host = name.substring(0,i);
134                 }
135             }
136             String JavaDoc hostPrefix = "//"+host;
137             String JavaDoc hostAttr = null;
138             while (items.hasNext()) {
139                 item = items.next().toString();
140                 ObjectName JavaDoc oname = new ObjectName JavaDoc(item);
141                 hostAttr = oname.getKeyProperty("name");
142                 if (hostAttr.startsWith(hostPrefix)) {
143                     list.add(item);
144                 }
145             }
146         } catch (Exception JavaDoc e) {
147             getServlet().log
148                 (resources.getMessage(locale, "users.error.select"));
149             response.sendError
150                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
151                  resources.getMessage(locale, "users.error.select"));
152             return (null);
153         }
154         Collections.sort(list);
155         request.setAttribute("contextsList", list);
156         
157         // Forward to the list display page
158
return (mapping.findForward("Contexts"));
159
160     }
161
162 }
163
Popular Tags