KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URLEncoder JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.List JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.management.Attribute JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.QueryExp JavaDoc;
28 import javax.management.Query JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.JMException JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import org.apache.struts.action.Action;
37 import org.apache.struts.action.ActionError;
38 import org.apache.struts.action.ActionErrors;
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42 import org.apache.struts.util.MessageResources;
43 import org.apache.webapp.admin.ApplicationServlet;
44 import org.apache.webapp.admin.TomcatTreeBuilder;
45 import org.apache.webapp.admin.TreeControl;
46 import org.apache.webapp.admin.TreeControlNode;
47
48
49
50 /**
51  * The <code>Action</code> that completes <em>Add Alias</em> and
52  * <em>Edit Alias</em> transactions.
53  *
54  * @author Manveen Kaur
55  * @version $Revision: 1.4 $ $Date: 2004/10/18 06:37:53 $
56  */

57
58 public final class SaveAliasAction extends Action {
59
60
61     // ----------------------------------------------------- Instance Variables
62

63     /**
64      * Signature for the <code>createStandardAlias</code> operation.
65      */

66     private String JavaDoc createStandardAliasTypes[] =
67     { "java.lang.String", // host
68
};
69
70     /**
71      * The MBeanServer we will be interacting with.
72      */

73     private MBeanServer JavaDoc mBServer = null;
74     
75
76     // --------------------------------------------------------- Public Methods
77

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

94     public ActionForward execute(ActionMapping mapping,
95                                  ActionForm form,
96                                  HttpServletRequest JavaDoc request,
97                                  HttpServletResponse JavaDoc response)
98         throws IOException JavaDoc, ServletException JavaDoc {
99         
100         // Acquire the resources that we need
101
HttpSession JavaDoc session = request.getSession();
102         Locale JavaDoc locale = getLocale(request);
103         MessageResources resources = getResources(request);
104         
105         // Acquire a reference to the MBeanServer containing our MBeans
106
try {
107             mBServer = ((ApplicationServlet) getServlet()).getServer();
108         } catch (Throwable JavaDoc t) {
109             throw new ServletException JavaDoc
110             ("Cannot acquire MBeanServer reference", t);
111         }
112         
113         // Identify the requested action
114
AliasForm aform = (AliasForm) form;
115
116         // Perform a "Create Alias" transaction (if requested)
117
String JavaDoc operation = "addAlias";
118         
119         //alias to be added
120
Object JavaDoc values[] = new String JavaDoc[1];
121         values[0] = aform.getAliasName();
122
123         String JavaDoc hostName = aform.getHostName();
124
125         // validate if this alias already exists.
126
List JavaDoc aliasVals = aform.getAliasVals();
127         if (aliasVals.contains(values[0])) {
128            ActionErrors errors = new ActionErrors();
129             errors.add("aliasName",
130                        new ActionError("error.aliasName.exists"));
131             saveErrors(request, errors);
132             return (new ActionForward(mapping.getInput()));
133         }
134         
135         try {
136             
137             ObjectName JavaDoc hname = new ObjectName JavaDoc(hostName);
138             mBServer.invoke(hname, operation, values, createStandardAliasTypes);
139
140         } catch (Throwable JavaDoc t) {
141             getServlet().log
142             (resources.getMessage(locale, "users.error.invoke",
143                                   operation), t);
144             response.sendError
145                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
146                 resources.getMessage(locale, "users.error.invoke",
147                                      operation));
148             return (null);
149         }
150                         
151         // Forward to the success reporting page
152
session.removeAttribute(mapping.getAttribute());
153         return (mapping.findForward("Save Successful"));
154         
155     }
156     
157 }
158
Popular Tags