KickJava   Java API By Example, From Geeks To Geeks.

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


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.host;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.management.MBeanServer 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 import org.apache.webapp.admin.ApplicationServlet;
35 import org.apache.webapp.admin.LabelValueBean;
36 import org.apache.webapp.admin.Lists;
37 import javax.management.ObjectName JavaDoc;
38
39 /**
40  * The <code>Action</code> that sets up <em>Add Alias</em> transactions.
41  *
42  * @author Manveen Kaur
43  * @version $Revision: 1.4 $ $Date: 2004/10/18 06:37:53 $
44  */

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

51     private MBeanServer JavaDoc mBServer = null;
52     
53     // --------------------------------------------------------- Public Methods
54

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

70     public ActionForward execute(ActionMapping mapping,
71     ActionForm form,
72     HttpServletRequest JavaDoc request,
73     HttpServletResponse JavaDoc response)
74     throws IOException JavaDoc, ServletException JavaDoc {
75         
76         // Acquire the resources that we need
77
HttpSession JavaDoc session = request.getSession();
78         Locale JavaDoc locale = getLocale(request);
79         MessageResources resources = getResources(request);
80         
81         // Acquire a reference to the MBeanServer containing our MBeans
82
try {
83             mBServer = ((ApplicationServlet) getServlet()).getServer();
84         } catch (Throwable JavaDoc t) {
85             throw new ServletException JavaDoc
86             ("Cannot acquire MBeanServer reference", t);
87         }
88         
89
90         // the host Name is needed to retrieve the existing aliases
91
// and add new aliases to
92
String JavaDoc hostName = request.getParameter("hostName");
93         // Fill in the form values for display and editing
94
AliasForm aliasFm = new AliasForm();
95         session.setAttribute("aliasForm", aliasFm);
96         
97         // retrieve all aliases
98
String JavaDoc operation = null;
99         try {
100             ObjectName JavaDoc hname = new ObjectName JavaDoc(hostName);
101
102             operation = "findAliases";
103             String JavaDoc aliases[] =
104                 (String JavaDoc[]) mBServer.invoke(hname, operation, null, null);
105             
106             aliasFm.setAliasVals(new ArrayList JavaDoc(Arrays.asList(aliases)));
107
108         } catch (Throwable JavaDoc t) {
109             getServlet().log
110             (resources.getMessage(locale, "users.error.invoke",
111                                   operation), t);
112             response.sendError
113                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
114                 resources.getMessage(locale, "users.error.invoke",
115                                      operation));
116             return (null);
117         }
118         
119         aliasFm.setAliasName("");
120         aliasFm.setHostName(hostName);
121
122         // Forward to the host display page
123
return (mapping.findForward("Alias"));
124         
125     }
126         
127 }
128
Popular Tags