KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > resources > SaveEnvEntryAction


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.resources;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URLDecoder JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Locale 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.ActionError;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import javax.management.Attribute JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.MBeanServerFactory JavaDoc;
38 import javax.management.QueryExp JavaDoc;
39 import javax.management.Query JavaDoc;
40 import javax.management.ObjectInstance JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.management.JMException JavaDoc;
43 import javax.management.MBeanAttributeInfo JavaDoc;
44 import javax.management.MBeanOperationInfo JavaDoc;
45 import javax.management.MBeanInfo JavaDoc;
46 import org.apache.struts.util.MessageResources;
47 import org.apache.webapp.admin.ApplicationServlet;
48
49
50 /**
51  * <p>Implementation of <strong>Action</strong> that saves a new or
52  * updated Env Entry.</p>
53  *
54  * @author Manveen Kaur
55  * @version $Revision: 1.9 $ $Date: 2004/10/18 06:37:54 $
56  * @since 4.1
57  */

58
59 public final class SaveEnvEntryAction extends Action {
60
61
62     // ----------------------------------------------------- Instance Variables
63

64     /**
65      * The MBeanServer we will be interacting with.
66      */

67     private MBeanServer JavaDoc mserver = null;
68
69
70     // --------------------------------------------------------- Public Methods
71

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

88     public ActionForward execute(ActionMapping mapping,
89                                  ActionForm form,
90                                  HttpServletRequest JavaDoc request,
91                                  HttpServletResponse JavaDoc response)
92         throws IOException JavaDoc, ServletException JavaDoc {
93
94         // Look up the components we will be using as needed
95
if (mserver == null) {
96             mserver = ((ApplicationServlet) getServlet()).getServer();
97         }
98         MessageResources resources = getResources(request);
99         HttpSession JavaDoc session = request.getSession();
100         Locale JavaDoc locale = getLocale(request);
101
102         // Has this transaction been cancelled?
103
if (isCancelled(request)) {
104             return (mapping.findForward("List EnvEntries Setup"));
105         }
106
107         // Check the transaction token
108
if (!isTokenValid(request)) {
109             response.sendError
110                 (HttpServletResponse.SC_BAD_REQUEST,
111                  resources.getMessage(locale, "users.error.token"));
112             return (null);
113         }
114
115         // Perform any extra validation that is required
116
EnvEntryForm envEntryForm = (EnvEntryForm) form;
117         String JavaDoc objectName = envEntryForm.getObjectName();
118
119         // Perform an "Add Entry" transaction
120
if (objectName == null) {
121
122             String JavaDoc signature[] = new String JavaDoc[3];
123             signature[0] = "java.lang.String";
124             signature[1] = "java.lang.String";
125             signature[2] = "java.lang.String";
126
127             Object JavaDoc params[] = new Object JavaDoc[3];
128             params[0] = envEntryForm.getName();
129             params[1] = envEntryForm.getEntryType();
130             params[2] = envEntryForm.getValue();
131             
132             String JavaDoc resourcetype = envEntryForm.getResourcetype();
133             String JavaDoc path = envEntryForm.getPath();
134             String JavaDoc host = envEntryForm.getHost();
135             String JavaDoc domain = envEntryForm.getDomain();
136             
137             ObjectName JavaDoc oname = null;
138
139             try {
140             
141                 if (resourcetype.equals("Global")) {
142                     oname = new ObjectName JavaDoc( domain +
143                                             ResourceUtils.ENVIRONMENT_TYPE +
144                                             ResourceUtils.GLOBAL_TYPE +
145                                             ",name=" + params[0]);
146                 } else if (resourcetype.equals("Context")) {
147                     oname = new ObjectName JavaDoc( domain +
148                                             ResourceUtils.ENVIRONMENT_TYPE +
149                                             ResourceUtils.CONTEXT_TYPE +
150                                             ",path=" + path + ",host=" + host +
151                                             ",name=" + params[0]);
152                 }
153                             
154                 if (mserver.isRegistered(oname)) {
155                     ActionErrors errors = new ActionErrors();
156                     errors.add("name",
157                                new ActionError("resources.invalid.env"));
158                     saveErrors(request, errors);
159                     return (new ActionForward(mapping.getInput()));
160                 }
161                 
162                 oname = ResourceUtils.getNamingResourceObjectName(domain,
163                             resourcetype, path, host);
164                 
165                 // Create the new object and associated MBean
166
objectName = (String JavaDoc) mserver.invoke(oname, "addEnvironment",
167                                                      params, signature);
168                                                                      
169             } catch (Exception JavaDoc e) {
170
171                 getServlet().log
172                     (resources.getMessage(locale, "users.error.invoke",
173                                           "addEnvironment"), e);
174                 response.sendError
175                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
176                      resources.getMessage(locale, "users.error.invoke",
177                                           "addEnvironment"));
178                 return (null);
179             }
180
181         }
182
183         // Perform an "Update Environment Entry" transaction
184
String JavaDoc attribute = null;
185         try {
186             
187             // Construct an object name for this object
188
ObjectName JavaDoc oname = new ObjectName JavaDoc(objectName);
189
190             // Update the specified env entry
191
attribute = "override";
192             mserver.setAttribute
193                 (oname,
194                  new Attribute JavaDoc(attribute, new Boolean JavaDoc(envEntryForm.getOverride())));
195             attribute = "description";
196             mserver.setAttribute
197                 (oname,
198                  new Attribute JavaDoc(attribute, envEntryForm.getDescription()));
199             attribute = "type";
200             mserver.setAttribute
201                 (oname,
202                  new Attribute JavaDoc(attribute, envEntryForm.getEntryType()));
203             attribute = "value";
204             mserver.setAttribute
205                 (oname,
206                  new Attribute JavaDoc(attribute, envEntryForm.getValue()));
207
208         } catch (Exception JavaDoc e) {
209
210             getServlet().log
211                 (resources.getMessage(locale, "users.error.set.attribute",
212                                       attribute), e);
213             response.sendError
214                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
215                  resources.getMessage(locale, "users.error.set.attribute",
216                                       attribute));
217             return (null);
218
219         }
220         
221         // Proceed to the list entries screen
222
return (mapping.findForward("EnvEntries List Setup"));
223
224     }
225
226
227 }
228
Popular Tags