KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > users > SaveRoleAction


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.users;
19
20
21 import java.io.IOException JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import org.apache.struts.action.Action;
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 import org.apache.webapp.admin.TomcatTreeBuilder;
49
50 /**
51  * <p>Implementation of <strong>Action</strong> that saves a new or
52  * updated Role back to the underlying database.</p>
53  *
54  * @author Craig R. McClanahan
55  * @version $Revision: 1.4 $ $Date: 2004/10/18 06:37:55 $
56  * @since 4.1
57  */

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

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

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

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

89     public ActionForward execute(ActionMapping mapping,
90                                  ActionForm form,
91                                  HttpServletRequest JavaDoc request,
92                                  HttpServletResponse JavaDoc response)
93         throws IOException JavaDoc, ServletException JavaDoc {
94
95         // Look up the components we will be using as needed
96
if (mserver == null) {
97             mserver = ((ApplicationServlet) getServlet()).getServer();
98         }
99         MessageResources resources = getResources(request);
100         HttpSession JavaDoc session = request.getSession();
101         Locale JavaDoc locale = getLocale(request);
102
103         // Has this transaction been cancelled?
104
if (isCancelled(request)) {
105             return (mapping.findForward("List Roles Setup"));
106         }
107
108         // Check the transaction token
109
if (!isTokenValid(request)) {
110             response.sendError
111                 (HttpServletResponse.SC_BAD_REQUEST,
112                  resources.getMessage(locale, "users.error.token"));
113             return (null);
114         }
115
116         // Perform any extra validation that is required
117
RoleForm roleForm = (RoleForm) form;
118         String JavaDoc databaseName =
119             URLDecoder.decode(roleForm.getDatabaseName(),TomcatTreeBuilder.URL_ENCODING);
120         String JavaDoc objectName = roleForm.getObjectName();
121
122         // Perform an "Add Role" transaction
123
if (objectName == null) {
124
125             String JavaDoc signature[] = new String JavaDoc[2];
126             signature[0] = "java.lang.String";
127             signature[1] = "java.lang.String";
128
129             Object JavaDoc params[] = new Object JavaDoc[2];
130             params[0] = roleForm.getRolename();
131             params[1] = roleForm.getDescription();
132
133             ObjectName JavaDoc oname = null;
134
135             try {
136
137                 // Construct the MBean Name for our UserDatabase
138
oname = new ObjectName JavaDoc(databaseName);
139
140                 // Create the new object and associated MBean
141
mserver.invoke(oname, "createRole",
142                                params, signature);
143
144             } catch (Exception JavaDoc e) {
145
146                 getServlet().log
147                     (resources.getMessage(locale, "users.error.invoke",
148                                           "createRole"), e);
149                 response.sendError
150                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
151                      resources.getMessage(locale, "users.error.invoke",
152                                           "createRole"));
153                 return (null);
154             }
155
156         }
157
158         // Perform an "Update Role" transaction
159
else {
160
161             ObjectName JavaDoc oname = null;
162             String JavaDoc attribute = null;
163
164             try {
165
166                 // Construct an object name for this object
167
oname = new ObjectName JavaDoc(objectName);
168
169                 // Update the specified role
170
attribute = "description";
171                 mserver.setAttribute
172                     (oname,
173                      new Attribute JavaDoc(attribute, roleForm.getDescription()));
174
175             } catch (Exception JavaDoc e) {
176
177                 getServlet().log
178                     (resources.getMessage(locale, "users.error.set.attribute",
179                                           attribute), e);
180                 response.sendError
181                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
182                      resources.getMessage(locale, "users.error.set.attribute",
183                                           attribute));
184                 return (null);
185
186             }
187
188         }
189
190         // Save the updated database information
191
try {
192
193             ObjectName JavaDoc dname = new ObjectName JavaDoc(databaseName);
194             mserver.invoke(dname, "save",
195                            new Object JavaDoc[0], new String JavaDoc[0]);
196
197         } catch (Exception JavaDoc e) {
198
199             getServlet().log
200                 (resources.getMessage(locale, "users.error.invoke",
201                                       "save"), e);
202             response.sendError
203                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
204                  resources.getMessage(locale, "users.error.invoke",
205                                       "save"));
206             return (null);
207
208         }
209
210         // Proceed to the list roles screen
211
return (mapping.findForward("Roles List Setup"));
212
213     }
214
215
216 }
217
Popular Tags