KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

65     private String JavaDoc createDataSourceRealmTypes[] =
66     { "java.lang.String", // parent
67
"java.lang.String", // dataSourceName
68
"java.lang.String", // roleNameCol
69
"java.lang.String", // userCredCol
70
"java.lang.String", // userNameCol
71
"java.lang.String", // userRoleTable
72
"java.lang.String", // userTable
73
};
74
75
76     /**
77      * The MBeanServer we will be interacting with.
78      */

79     private MBeanServer JavaDoc mBServer = null;
80     
81
82     // --------------------------------------------------------- Public Methods
83

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

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