KickJava   Java API By Example, From Geeks To Geeks.

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


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 Group 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 SaveGroupAction 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
GroupForm groupForm = (GroupForm) form;
118         String JavaDoc databaseName =
119             URLDecoder.decode(groupForm.getDatabaseName(),TomcatTreeBuilder.URL_ENCODING);
120         String JavaDoc objectName = groupForm.getObjectName();
121
122         // Perform an "Add Group" 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] = groupForm.getGroupname();
131             params[1] = groupForm.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
objectName = (String JavaDoc) mserver.invoke(oname, "createGroup",
142                                                      params, signature);
143
144             } catch (Exception JavaDoc e) {
145
146                 getServlet().log
147                     (resources.getMessage(locale, "users.error.invoke",
148                                           "createGroup"), e);
149                 response.sendError
150                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
151                      resources.getMessage(locale, "users.error.invoke",
152                                           "createGroup"));
153                 return (null);
154             }
155
156         }
157
158         // Perform an "Update Group" 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, groupForm.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
191         // Reset the roles associated with this group
192
try {
193
194             ObjectName JavaDoc oname = new ObjectName JavaDoc(objectName);
195             mserver.invoke(oname, "removeRoles",
196                            new Object JavaDoc[0], new String JavaDoc[0]);
197             String JavaDoc roles[] = groupForm.getRoles();
198             if (roles == null) {
199                 roles = new String JavaDoc[0];
200             }
201             String JavaDoc addsig[] = new String JavaDoc[1];
202             addsig[0] = "java.lang.String";
203             Object JavaDoc addpar[] = new Object JavaDoc[1];
204             for (int i = 0; i < roles.length; i++) {
205                 addpar[0] =
206                     (new ObjectName JavaDoc(roles[i])).getKeyProperty("rolename");
207                 mserver.invoke(oname, "addRole",
208                                addpar, addsig);
209             }
210
211         } catch (Exception JavaDoc e) {
212
213             getServlet().log
214                 (resources.getMessage(locale, "users.error.invoke",
215                                       "addRole"), e);
216             response.sendError
217                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
218                  resources.getMessage(locale, "users.error.invoke",
219                                       "addRole"));
220             return (null);
221
222         }
223
224         // Save the updated database information
225
try {
226
227             ObjectName JavaDoc dname = new ObjectName JavaDoc(databaseName);
228             mserver.invoke(dname, "save",
229                            new Object JavaDoc[0], new String JavaDoc[0]);
230
231         } catch (Exception JavaDoc e) {
232
233             getServlet().log
234                 (resources.getMessage(locale, "users.error.invoke",
235                                       "save"), e);
236             response.sendError
237                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
238                  resources.getMessage(locale, "users.error.invoke",
239                                       "save"));
240             return (null);
241
242         }
243
244         // Proceed to the list roles screen
245
return (mapping.findForward("Groups List Setup"));
246
247     }
248
249
250 }
251
Popular Tags