KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > service > SaveServiceAction


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.service;
18
19
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.io.IOException 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.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import javax.servlet.http.HttpSession JavaDoc;
37 import org.apache.struts.action.Action;
38 import org.apache.struts.action.ActionError;
39 import org.apache.struts.action.ActionErrors;
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.action.ActionForward;
42 import org.apache.struts.action.ActionMapping;
43 import org.apache.struts.util.MessageResources;
44 import org.apache.webapp.admin.ApplicationServlet;
45 import org.apache.webapp.admin.Lists;
46 import org.apache.webapp.admin.TomcatTreeBuilder;
47 import org.apache.webapp.admin.TreeControl;
48 import org.apache.webapp.admin.TreeControlNode;
49 import org.apache.webapp.admin.valve.ValveUtil;
50
51
52
53 /**
54  * The <code>Action</code> that completes <em>Add Service</em> and
55  * <em>Edit Service</em> transactions.
56  *
57  * @author Manveen Kaur
58  * @author Amy Roh
59  * @version $Revision: 1.17 $ $Date: 2004/10/18 06:37:55 $
60  */

61
62 public final class SaveServiceAction extends Action {
63
64
65     // ----------------------------------------------------- Instance Variables
66

67
68     /**
69      * Signature for the <code>createStandardEngine</code> operation.
70      */

71     private String JavaDoc createStandardEngineTypes[] =
72     { "java.lang.String", // parent
73
"java.lang.String", // name
74
"java.lang.String", // defaultHost
75
};
76
77
78     /**
79      * Signature for the <code>createStandardService</code> operation.
80      */

81     private String JavaDoc createStandardServiceTypes[] =
82     { "java.lang.String", // parent
83
"java.lang.String", // name
84
"java.lang.String" // domain
85
};
86
87
88     /**
89      * Signature for the <code>createStandardEngineService</code> operation.
90      */

91     private String JavaDoc createStandardEngineServiceTypes[] =
92     { "java.lang.String", // parent
93
"java.lang.String", // engineName
94
"java.lang.String", // defaultHost
95
"java.lang.String" // serviceName
96
};
97     
98     
99     /**
100      * Signature for the <code>createUserDatabaseRealm</code> operation.
101      */

102     private String JavaDoc createUserDatabaseRealmTypes[] =
103     { "java.lang.String", // parent
104
"java.lang.String", // name
105
};
106
107
108     /**
109      * The MBeanServer we will be interacting with.
110      */

111     private MBeanServer JavaDoc mBServer = null;
112     
113
114     // --------------------------------------------------------- Public Methods
115

116     
117     /**
118      * Process the specified HTTP request, and create the corresponding HTTP
119      * response (or forward to another web component that will create it).
120      * Return an <code>ActionForward</code> instance describing where and how
121      * control should be forwarded, or <code>null</code> if the response has
122      * already been completed.
123      *
124      * @param mapping The ActionMapping used to select this instance
125      * @param actionForm The optional ActionForm bean for this request (if any)
126      * @param request The HTTP request we are processing
127      * @param response The HTTP response we are creating
128      *
129      * @exception IOException if an input/output error occurs
130      * @exception ServletException if a servlet exception occurs
131      */

132     public ActionForward execute(ActionMapping mapping,
133                                  ActionForm form,
134                                  HttpServletRequest JavaDoc request,
135                                  HttpServletResponse JavaDoc response)
136         throws IOException JavaDoc, ServletException JavaDoc {
137         
138         // Acquire the resources that we need
139
HttpSession JavaDoc session = request.getSession();
140         Locale JavaDoc locale = getLocale(request);
141         MessageResources resources = getResources(request);
142         
143         // Acquire a reference to the MBeanServer containing our MBeans
144
try {
145             mBServer = ((ApplicationServlet) getServlet()).getServer();
146         } catch (Throwable JavaDoc t) {
147             throw new ServletException JavaDoc
148             ("Cannot acquire MBeanServer reference", t);
149         }
150         
151         // Identify the requested action
152
ServiceForm sform = (ServiceForm) form;
153         String JavaDoc adminAction = sform.getAdminAction();
154         String JavaDoc sObjectName = sform.getObjectName();
155         String JavaDoc eObjectName = sform.getEngineObjectName();
156         String JavaDoc serverObjectName = sform.getServerObjectName();
157         ObjectName JavaDoc eoname = null;
158         ObjectName JavaDoc soname = null;
159         // Perform a "Create Service" transaction (if requested)
160
if ("Create".equals(adminAction)) {
161
162             String JavaDoc operation = null;
163             String JavaDoc values[] = null;
164
165             try {
166                 // engine name is domain
167
String JavaDoc engineName = sform.getEngineName();
168                 //String domain = (new ObjectName(serverObjectName)).getDomain();
169
// Ensure that the requested service name is unique
170
ObjectName JavaDoc oname =
171                     new ObjectName JavaDoc("*" + TomcatTreeBuilder.SERVICE_TYPE +
172                                 ",serviceName="+sform.getServiceName());
173                 Iterator JavaDoc names = mBServer.queryNames(oname, null).iterator();
174                 while (names.hasNext()) {
175                     if (mBServer.isRegistered((ObjectName JavaDoc)names.next())) {
176                         ActionErrors errors = new ActionErrors();
177                         errors.add("serviceName",
178                                new ActionError("error.serviceName.exists"));
179                         saveErrors(request, errors);
180                         return (new ActionForward(mapping.getInput()));
181                     }
182                 }
183                 
184                 oname = new ObjectName JavaDoc(engineName + TomcatTreeBuilder.ENGINE_TYPE);
185                 if (mBServer.isRegistered(oname)) {
186                     ActionErrors errors = new ActionErrors();
187                     errors.add("serviceName",
188                                new ActionError("error.engineName.exists"));
189                     saveErrors(request, errors);
190                     return (new ActionForward(mapping.getInput()));
191                 }
192                 
193                 // Look up our MBeanFactory MBean
194
ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
195
196                 // Create a new StandardService and StandardEngine object
197
values = new String JavaDoc[4];
198                 values[0] = TomcatTreeBuilder.SERVER_TYPE;
199                 values[1] = engineName;
200                 values[2] = sform.getDefaultHost();
201                 values[3] = sform.getServiceName();
202                 operation = "createStandardEngineService";
203                 Vector JavaDoc onames = (Vector JavaDoc)
204                     mBServer.invoke(fname, operation,
205                                     values, createStandardEngineServiceTypes);
206                 eoname = (ObjectName JavaDoc)onames.get(0);
207                 soname = (ObjectName JavaDoc)onames.get(1);
208                 sObjectName = soname.toString();
209                 eObjectName = eoname.toString();
210                 
211                 String JavaDoc realmOName = ValveUtil.getObjectName(
212                                     eObjectName, TomcatTreeBuilder.REALM_TYPE);
213             
214                 ObjectName JavaDoc roname = new ObjectName JavaDoc(realmOName);
215                 if (mBServer.isRegistered(roname)) {
216                     mBServer.unregisterMBean(roname);
217                 }
218                 
219                 // Create a new UserDatabaseRealm object
220
values = new String JavaDoc[2];
221                 values[0] = eObjectName;
222                 values[1] = "UserDatabase";
223                 operation = "createUserDatabaseRealm";
224                 //realmOName = (String)
225
// mBServer.invoke(fname, operation,
226
// values, createUserDatabaseRealmTypes);
227

228                 //Enumeration enum = onames.elements();
229
//while (enum.hasMoreElements()) {
230
// getServlet().log("save service "+enum.nextElement());
231
//}
232
sObjectName = soname.toString();
233                 eObjectName = eoname.toString();
234                 
235                 // Create a new StandardService object
236
//values = new String[3];
237
//values[0] = TomcatTreeBuilder.SERVER_TYPE;
238
//values[1] = sform.getServiceName();
239
//values[2] = engineName;
240
//operation = "createStandardService";
241
//sObjectName = (String)
242
// mBServer.invoke(fname, operation,
243
// values, createStandardServiceTypes);
244

245                 // Create a new StandardEngine object
246
//values = new String[3];
247
//values[0] = sObjectName;
248
//values[1] = sform.getEngineName();
249
//values[2] = sform.getDefaultHost();
250
//if ("".equals(values[2])) {
251
// values[2] = null;
252
//}
253
//operation = "createStandardEngine";
254
//eObjectName = (String)
255
// mBServer.invoke(fname, operation,
256
// values, createStandardEngineTypes);
257

258                 // Add the new Service to our tree control node
259
TreeControl control = (TreeControl)
260                     session.getAttribute("treeControlTest");
261                 if (control != null) {
262                     String JavaDoc parentName = TomcatTreeBuilder.DEFAULT_DOMAIN +
263                                             TomcatTreeBuilder.SERVER_TYPE;
264                     TreeControlNode parentNode = control.findNode(parentName);
265                     if (parentNode != null) {
266                         String JavaDoc nodeLabel = resources.getMessage(locale,
267                             "server.service.treeBuilder.subtreeNode") +" (" +
268                             soname.getKeyProperty("serviceName") + ")";
269                         String JavaDoc encodedName =
270                             URLEncoder.encode(sObjectName,TomcatTreeBuilder.URL_ENCODING);
271                         TreeControlNode childNode =
272                             new TreeControlNode(sObjectName,
273                                                 "Service.gif",
274                                                 nodeLabel,
275                                                 "EditService.do?select=" +
276                                                 encodedName,
277                                                 "content",
278                                                 true, engineName);
279                         parentNode.addChild(childNode);
280                         // update tree to display the newly added realm
281
//Iterator realmNames =
282
// Lists.getRealms(mBServer, sObjectName).iterator();
283
//while (realmNames.hasNext()) {
284
// String realmName = (String) realmNames.next();
285
// ObjectName objectName = new ObjectName(realmName);
286
// nodeLabel = "Realm for service (" +
287
// sform.getServiceName() + ")";
288
// TreeControlNode realmNode =
289
// new TreeControlNode(realmName,
290
// "Realm.gif",
291
// nodeLabel,
292
// "EditRealm.do?select=" +
293
// URLEncoder.encode(realmName) +
294
// "&nodeLabel=" +
295
// URLEncoder.encode(nodeLabel),
296
// "content",
297
// false, engineName);
298
// childNode.addChild(realmNode);
299
//}
300
// FIXME - force a redisplay
301
} else {
302                         getServlet().log
303                             ("Cannot find parent node '" + parentName + "'");
304                     }
305                 } else {
306                     getServlet().log
307                         ("Cannot find TreeControlNode!");
308                 }
309
310             } catch (Exception JavaDoc e) {
311
312                 getServlet().log
313                     (resources.getMessage(locale, "users.error.invoke",
314                                           operation), e);
315                 response.sendError
316                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
317                      resources.getMessage(locale, "users.error.invoke",
318                                           operation));
319                 return (null);
320
321             }
322
323         }
324
325         // Perform attribute updates as requested
326
String JavaDoc attribute = null;
327         try {
328         
329             eoname = new ObjectName JavaDoc(eObjectName);
330             soname = new ObjectName JavaDoc(sObjectName);
331
332             attribute = "defaultHost";
333             String JavaDoc defaultHost = sform.getDefaultHost();
334             if ("".equals(defaultHost)) {
335                 defaultHost = null;
336             }
337             mBServer.setAttribute(eoname,
338                                   new Attribute JavaDoc("defaultHost", defaultHost));
339
340         } catch (Exception JavaDoc e) {
341
342             getServlet().log
343                 (resources.getMessage(locale, "users.error.attribute.set",
344                                       attribute), e);
345             response.sendError
346                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
347                  resources.getMessage(locale, "users.error.attribute.set",
348                                       attribute));
349             return (null);
350         }
351         
352         // Forward to the success reporting page
353
session.removeAttribute(mapping.getAttribute());
354         return (mapping.findForward("Save Successful"));
355         
356     }
357     
358 }
359
Popular Tags