KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > realm > SaveUserDatabaseRealmAction


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.realm;
18
19
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale 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 import org.apache.webapp.admin.valve.ValveUtil;
48
49 /**
50  * The <code>Action</code> that completes <em>Add Realm</em> and
51  * <em>Edit Realm</em> transactions for UserDatabase realm.
52  *
53  * @author Manveen Kaur
54  * @version $Revision: 1.11 $ $Date: 2004/10/18 06:37:54 $
55  */

56
57 public final class SaveUserDatabaseRealmAction extends Action {
58
59
60     // ----------------------------------------------------- Instance Variables
61

62     /**
63      * Signature for the <code>createUserDatabaseRealm</code> operation.
64      */

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

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

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

95     public ActionForward execute(ActionMapping mapping,
96                                  ActionForm form,
97                                  HttpServletRequest JavaDoc request,
98                                  HttpServletResponse JavaDoc response)
99         throws IOException JavaDoc, ServletException JavaDoc {
100         
101         // Acquire the resources that we need
102
HttpSession JavaDoc session = request.getSession();
103         Locale JavaDoc locale = getLocale(request);
104         MessageResources resources = getResources(request);
105         
106         // Acquire a reference to the MBeanServer containing our MBeans
107
try {
108             mBServer = ((ApplicationServlet) getServlet()).getServer();
109         } catch (Throwable JavaDoc t) {
110             throw new ServletException JavaDoc
111             ("Cannot acquire MBeanServer reference", t);
112         }
113         
114         // Identify the requested action
115
UserDatabaseRealmForm rform = (UserDatabaseRealmForm) form;
116         String JavaDoc adminAction = rform.getAdminAction();
117         String JavaDoc rObjectName = rform.getObjectName();
118
119         // Perform a "Create UserDatabase Realm" transaction (if requested)
120
if ("Create".equals(adminAction)) {
121
122             String JavaDoc operation = null;
123             String JavaDoc values[] = null;
124
125             try {
126
127                 String JavaDoc parent = rform.getParentObjectName();
128                 String JavaDoc objectName = ValveUtil.getObjectName(parent,
129                                     TomcatTreeBuilder.REALM_TYPE);
130                 
131                 ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
132                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(pname.getDomain());
133                 
134                 // For service, create the corresponding Engine mBean
135
// Parent in this case needs to be the container mBean for the service
136
try {
137                     if ("Service".equalsIgnoreCase(pname.getKeyProperty("type"))) {
138                         sb.append(":type=Engine");
139                         parent = sb.toString();
140                     }
141                 } catch (Exception JavaDoc e) {
142                     String JavaDoc message =
143                         resources.getMessage(locale, "error.engineName.bad",
144                                          sb.toString());
145                     getServlet().log(message);
146                     response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
147                     return (null);
148                 }
149                                                 
150                 // Ensure that the requested user database name is unique
151
ObjectName JavaDoc oname =
152                     new ObjectName JavaDoc(objectName);
153                 if (mBServer.isRegistered(oname)) {
154                     ActionErrors errors = new ActionErrors();
155                     errors.add("realmName",
156                                new ActionError("error.realmName.exists"));
157                     saveErrors(request, errors);
158                     return (new ActionForward(mapping.getInput()));
159                 }
160                 
161                 String JavaDoc domain = oname.getDomain();
162                 // Look up our MBeanFactory MBean
163
ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
164
165                 // Create a new StandardRealm object
166
values = new String JavaDoc[2];
167                 values[0] = parent;
168                 values[1] = rform.getResource();
169                 operation = "createUserDatabaseRealm";
170                 rObjectName = (String JavaDoc)
171                     mBServer.invoke(fname, operation,
172                                     values, createUserDatabaseRealmTypes);
173                 if (rObjectName==null) {
174                     request.setAttribute("warning", "error.userdbrealm");
175                     return (mapping.findForward("Save Unsuccessful"));
176                 }
177
178                 // Add the new Realm to our tree control node
179
TreeControl control = (TreeControl)
180                     session.getAttribute("treeControlTest");
181                 if (control != null) {
182                     TreeControlNode parentNode = control.findNode(rform.getParentObjectName());
183                     if (parentNode != null) {
184                         String JavaDoc nodeLabel = rform.getNodeLabel();
185                         String JavaDoc encodedName =
186                             URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING);
187                         TreeControlNode childNode =
188                             new TreeControlNode(rObjectName,
189                                                 "Realm.gif",
190                                                 nodeLabel,
191                                                 "EditRealm.do?select=" +
192                                                 encodedName,
193                                                 "content",
194                                                 true, domain);
195                         parentNode.addChild(childNode);
196                         // FIXME - force a redisplay
197
} else {
198                         getServlet().log
199                             ("Cannot find parent node '" + parent + "'");
200                     }
201                 } else {
202                     getServlet().log
203                         ("Cannot find TreeControlNode!");
204                 }
205
206             } catch (Exception JavaDoc e) {
207
208                 getServlet().log
209                     (resources.getMessage(locale, "users.error.invoke",
210                                           operation), e);
211                 response.sendError
212                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
213                      resources.getMessage(locale, "users.error.invoke",
214                                           operation));
215                 return (null);
216
217             }
218
219         }
220
221         // Perform attribute updates as requested
222
String JavaDoc attribute = null;
223         try {
224
225             ObjectName JavaDoc roname = new ObjectName JavaDoc(rObjectName);
226
227             attribute = "resourceName";
228             mBServer.setAttribute(roname,
229                                   new Attribute JavaDoc("resourceName", rform.getResource()));
230
231         } catch (Exception JavaDoc e) {
232
233             getServlet().log
234                 (resources.getMessage(locale, "users.error.attribute.set",
235                                       attribute), e);
236             response.sendError
237                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
238                  resources.getMessage(locale, "users.error.attribute.set",
239                                       attribute));
240             return (null);
241         }
242         
243         // Forward to the success reporting page
244
session.removeAttribute(mapping.getAttribute());
245         return (mapping.findForward("Save Successful"));
246         
247     }
248     
249 }
250
Popular Tags