KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.Locale JavaDoc;
25 import javax.management.Attribute JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.QueryExp JavaDoc;
29 import javax.management.Query JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.JMException JavaDoc;
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.MBeanInfo JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import org.apache.struts.action.Action;
41 import org.apache.struts.action.ActionErrors;
42 import org.apache.struts.action.ActionForm;
43 import org.apache.struts.action.ActionForward;
44 import org.apache.struts.action.ActionMapping;
45 import org.apache.struts.util.MessageResources;
46 import org.apache.webapp.admin.ApplicationServlet;
47 import org.apache.webapp.admin.TomcatTreeBuilder;
48
49
50 /**
51  * <p>Implementation of <strong>Action</strong> that sets up and stashes
52  * a <code>GroupForm</code> bean in request scope. The form bean will have
53  * a null <code>objectName</code> property if this form represents a group
54  * being added, or a non-null value for an existing group.</p>
55  *
56  * @author Craig R. McClanahan
57  * @version $Revision: 1.4 $ $Date: 2004/10/18 06:37:55 $
58  * @since 4.1
59  */

60
61 public final class SetUpGroupAction extends Action {
62
63
64     // ----------------------------------------------------- Instance Variables
65

66
67     /**
68      * The MBeanServer we will be interacting with.
69      */

70     private MBeanServer JavaDoc mserver = null;
71
72
73     // --------------------------------------------------------- Public Methods
74

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

91     public ActionForward execute(ActionMapping mapping,
92                                  ActionForm form,
93                                  HttpServletRequest JavaDoc request,
94                                  HttpServletResponse JavaDoc response)
95         throws IOException JavaDoc, ServletException JavaDoc {
96
97         // Look up the components we will be using as needed
98
if (mserver == null) {
99             mserver = ((ApplicationServlet) getServlet()).getServer();
100         }
101         MessageResources resources = getResources(request);
102         HttpSession JavaDoc session = request.getSession();
103         Locale JavaDoc locale = getLocale(request);
104
105         // Set up a bean containing all possible roles
106
String JavaDoc databaseName =
107             URLDecoder.decode(request.getParameter("databaseName"),TomcatTreeBuilder.URL_ENCODING);
108         try {
109             request.setAttribute("rolesForm",
110                                  UserUtils.getRolesForm(mserver,
111                                                         databaseName));
112         } catch (Exception JavaDoc e) {
113             getServlet().log
114                 (resources.getMessage(locale,
115                                       "users.error.attribute.get",
116                                       "roles"), e);
117             response.sendError
118                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
119                  resources.getMessage
120                  (locale, "users.error.attribute.get", "roles"));
121             return (null);
122         }
123
124         // Set up the form bean based on the creating or editing state
125
String JavaDoc objectName = request.getParameter("objectName");
126         GroupForm groupForm = new GroupForm();
127         if (objectName == null) {
128             groupForm.setNodeLabel
129                 (resources.getMessage(locale, "users.group.newGroup"));
130             groupForm.setObjectName(null);
131         } else {
132             groupForm.setNodeLabel
133                 (resources.getMessage(locale, "users.group.oldGroup"));
134             groupForm.setObjectName(objectName);
135             String JavaDoc attribute = null;
136             try {
137                 ObjectName JavaDoc oname = new ObjectName JavaDoc(objectName);
138                 attribute = "groupname";
139                 groupForm.setGroupname
140                     ((String JavaDoc) mserver.getAttribute(oname, attribute));
141                 attribute = "description";
142                 groupForm.setDescription
143                     ((String JavaDoc) mserver.getAttribute(oname, attribute));
144                 attribute = "roles";
145                 groupForm.setRoles
146                     ((String JavaDoc[]) mserver.getAttribute(oname, attribute));
147             } catch (Exception JavaDoc e) {
148                 getServlet().log
149                     (resources.getMessage(locale,
150                         "users.error.attribute.get", attribute), e);
151                 response.sendError
152                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
153                      resources.getMessage
154                          (locale, "users.error.attribute.get", attribute));
155                 return (null);
156             }
157         }
158         groupForm.setDatabaseName(databaseName);
159
160         // Stash the form bean and forward to the display page
161
saveToken(request);
162         request.setAttribute("groupForm", groupForm);
163         return (mapping.findForward("Group"));
164
165     }
166
167 }
168
Popular Tags