KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

65     private String JavaDoc createStandardRealmTypes[] =
66     { "java.lang.String", // parent
67
"java.lang.String", // driverName
68
"java.lang.String", // connectionName
69
"java.lang.String", // connectionPassword
70
"java.lang.String", // connectionURL
71
};
72
73
74     /**
75      * The MBeanServer we will be interacting with.
76      */

77     private MBeanServer JavaDoc mBServer = null;
78     
79
80     // --------------------------------------------------------- Public Methods
81

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

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