KickJava   Java API By Example, From Geeks To Geeks.

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


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 JNDI realm.
52  *
53  * @author Manveen Kaur
54  * @author Amy Roh
55  * @version $Revision: 1.13 $ $Date: 2004/10/18 06:37:54 $
56  */

57
58 public final class SaveJNDIRealmAction extends Action {
59
60
61     // ----------------------------------------------------- Instance Variables
62

63     /**
64      * Signature for the <code>createStandardRealm</code> operation.
65      */

66     private String JavaDoc createStandardRealmTypes[] =
67     { "java.lang.String", // parent
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
JNDIRealmForm rform = (JNDIRealmForm) form;
116         String JavaDoc adminAction = rform.getAdminAction();
117         String JavaDoc rObjectName = rform.getObjectName();
118
119         // Perform a "Create JNDI 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("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 =
164                     TomcatTreeBuilder.getMBeanFactory();
165
166                 // Create a new StandardRealm object
167
values = new String JavaDoc[1];
168                 values[0] = parent;
169                 operation = "createJNDIRealm";
170                 rObjectName = (String JavaDoc)
171                     mBServer.invoke(fname, operation,
172                                     values, createStandardRealmTypes);
173
174                 if (rObjectName==null) {
175                     request.setAttribute("warning", "error.jndirealm");
176                     return (mapping.findForward("Save Unsuccessful"));
177                 }
178                 
179                 // Add the new Realm to our tree control node
180
TreeControl control = (TreeControl)
181                     session.getAttribute("treeControlTest");
182                 if (control != null) {
183                     TreeControlNode parentNode = control.findNode(rform.getParentObjectName());
184                     if (parentNode != null) {
185                         String JavaDoc nodeLabel = rform.getNodeLabel();
186                         String JavaDoc encodedName =
187                             URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING);
188                         TreeControlNode childNode =
189                             new TreeControlNode(rObjectName,
190                                                 "Realm.gif",
191                                                 nodeLabel,
192                                                 "EditRealm.do?select=" +
193                                                 encodedName,
194                                                 "content",
195                                                 true, domain);
196                         parentNode.addChild(childNode);
197                         // FIXME - force a redisplay
198
} else {
199                         getServlet().log
200                             ("Cannot find parent node '" + parent + "'");
201                     }
202                 } else {
203                     getServlet().log
204                         ("Cannot find TreeControlNode!");
205                 }
206
207             } catch (Exception JavaDoc e) {
208
209                 getServlet().log
210                     (resources.getMessage(locale, "users.error.invoke",
211                                           operation), e);
212                 response.sendError
213                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
214                      resources.getMessage(locale, "users.error.invoke",
215                                           operation));
216                 return (null);
217
218             }
219
220         }
221
222         // Perform attribute updates as requested
223
String JavaDoc attribute = null;
224         try {
225
226             ObjectName JavaDoc roname = new ObjectName JavaDoc(rObjectName);
227
228             attribute = "connectionName";
229             String JavaDoc connectionName = rform.getConnectionName();
230             if ((connectionName != null) && (connectionName.length()>0)) {
231                 mBServer.setAttribute(roname,
232                         new Attribute JavaDoc("connectionName", connectionName));
233             }
234
235             attribute = "connectionPassword";
236             String JavaDoc connectionPassword = rform.getConnectionPassword();
237             if ((connectionPassword != null) && (connectionPassword.length()>0)) {
238                 mBServer.setAttribute(roname,
239                         new Attribute JavaDoc("connectionPassword", connectionPassword));
240             }
241
242             attribute = "connectionURL";
243             String JavaDoc connectionURL = rform.getConnectionURL();
244             if ((connectionURL != null) && (connectionURL.length()>0)) {
245                 mBServer.setAttribute(roname,
246                         new Attribute JavaDoc("connectionURL", connectionURL));
247             }
248
249             attribute = "contextFactory";
250             String JavaDoc contextFactory = rform.getContextFactory();
251             if ((contextFactory != null) && (contextFactory.length()>0)) {
252                 mBServer.setAttribute(roname,
253                         new Attribute JavaDoc("contextFactory", contextFactory));
254             }
255
256             attribute = "digest";
257             String JavaDoc digest = rform.getDigest();
258             if ((digest != null) && (digest.length()>0)) {
259                 mBServer.setAttribute(roname,
260                                         new Attribute JavaDoc("digest", digest));
261             }
262
263             attribute = "roleBase";
264             String JavaDoc roleBase = rform.getRoleBase();
265             if ((roleBase != null) && (roleBase.length()>0)) {
266                 mBServer.setAttribute(roname,
267                         new Attribute JavaDoc("roleBase", roleBase));
268             }
269
270             attribute = "roleName";
271             String JavaDoc roleName = rform.getRoleName();
272             if ((roleName != null) && (roleName.length()>0)) {
273                 mBServer.setAttribute(roname,
274                         new Attribute JavaDoc("roleName", roleName));
275             }
276
277             attribute = "roleSearch";
278             String JavaDoc rolePattern = rform.getRolePattern();
279             if ((rolePattern != null) && (rolePattern.length()>0)) {
280                 mBServer.setAttribute(roname,
281                         new Attribute JavaDoc("roleSearch", rolePattern));
282             }
283
284             attribute = "roleSubtree";
285             String JavaDoc roleSubtree = rform.getRoleSubtree();
286             if ((roleSubtree != null) && (roleSubtree.length()>0)) {
287                 mBServer.setAttribute(roname,
288                     new Attribute JavaDoc("roleSubtree", new Boolean JavaDoc(roleSubtree)));
289             }
290
291             attribute = "userBase";
292             String JavaDoc userBase = rform.getUserBase();
293             if ((userBase != null) && (userBase.length()>0)) {
294                 mBServer.setAttribute(roname,
295                         new Attribute JavaDoc("userBase", userBase));
296             }
297
298             attribute = "userPassword";
299             String JavaDoc userPassword = rform.getUserPassword();
300             if ((userPassword != null) && (userPassword.length()>0)) {
301                 mBServer.setAttribute(roname,
302                         new Attribute JavaDoc("userPassword", userPassword));
303             }
304
305             attribute = "userPattern";
306             String JavaDoc userPattern = rform.getUserPattern();
307             if ((userPattern != null) && (userPattern.length()>0)) {
308                 mBServer.setAttribute(roname,
309                         new Attribute JavaDoc("userPattern", userPattern));
310             }
311
312             attribute = "userRoleName";
313             String JavaDoc userRoleName = rform.getUserRoleName();
314             if ((userRoleName != null) && (userRoleName.length()>0)) {
315                 mBServer.setAttribute(roname,
316                         new Attribute JavaDoc("userRoleName", userRoleName));
317             }
318
319             attribute = "userSearch";
320             String JavaDoc userSearch = rform.getUserSearch();
321             if ((userSearch != null) && (userSearch.length()>0)) {
322                 mBServer.setAttribute(roname,
323                         new Attribute JavaDoc("userSearch", userSearch));
324             }
325
326             attribute = "userSubtree";
327             String JavaDoc userSubtree = rform.getUserSubtree();
328             if ((userSubtree != null) && (userSubtree.length()>0)) {
329                 mBServer.setAttribute(roname,
330                     new Attribute JavaDoc("userSubtree", new Boolean JavaDoc(userSubtree)));
331             }
332
333         } catch (Exception JavaDoc e) {
334
335             getServlet().log
336                 (resources.getMessage(locale, "users.error.attribute.set",
337                                       attribute), e);
338             response.sendError
339                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
340                  resources.getMessage(locale, "users.error.attribute.set",
341                                       attribute));
342             return (null);
343         }
344
345         // Forward to the success reporting page
346
session.removeAttribute(mapping.getAttribute());
347         return (mapping.findForward("Save Successful"));
348
349     }
350
351 }
352
Popular Tags