KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > context > SaveContextAction


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.context;
18
19
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.io.IOException JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import org.apache.commons.modeler.Registry;
31 import org.apache.struts.action.Action;
32 import org.apache.struts.action.ActionError;
33 import org.apache.struts.action.ActionErrors;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37 import org.apache.struts.util.MessageResources;
38 import org.apache.webapp.admin.ApplicationServlet;
39 import org.apache.webapp.admin.TomcatTreeBuilder;
40 import org.apache.webapp.admin.TreeControl;
41 import org.apache.webapp.admin.TreeControlNode;
42
43
44
45 /**
46  * The <code>Action</code> that completes <em>Add Context</em> and
47  * <em>Edit Context</em> transactions.
48  *
49  * @author Manveen Kaur
50  * @version $Revision: 1.18 $ $Date: 2005/01/14 23:55:41 $
51  */

52
53 public final class SaveContextAction extends Action {
54
55
56     // ----------------------------------------------------- Instance Variables
57

58     /**
59      * Signature for the <code>createStandardContext</code> operation.
60      */

61     private String JavaDoc createStandardContextTypes[] =
62     { "java.lang.String", // parent
63
"java.lang.String", // path
64
"java.lang.String", // docBase
65
};
66
67    /**
68      * Signature for the <code>createStandardLoader</code> operation.
69      */

70     private String JavaDoc createStandardLoaderTypes[] =
71     { "java.lang.String", // parent
72
};
73
74    /**
75      * Signature for the <code>createStandardManager</code> operation.
76      */

77     private String JavaDoc createStandardManagerTypes[] =
78     { "java.lang.String", // parent
79
};
80
81     /**
82      * Signature for the <code>removeContext</code> operation.
83      */

84     private String JavaDoc removeContextTypes[] =
85     { "java.lang.String", // Object name
86
};
87         
88     /**
89      * The MBeanServer we will be interacting with.
90      */

91     private MBeanServer JavaDoc mBServer = null;
92     
93
94     // --------------------------------------------------------- Public Methods
95

96     
97     /**
98      * Process the specified HTTP request, and create the corresponding HTTP
99      * response (or forward to another web component that will create it).
100      * Return an <code>ActionForward</code> instance describing where and how
101      * control should be forwarded, or <code>null</code> if the response has
102      * already been completed.
103      *
104      * @param mapping The ActionMapping used to select this instance
105      * @param actionForm The optional ActionForm bean for this request (if any)
106      * @param request The HTTP request we are processing
107      * @param response The HTTP response we are creating
108      *
109      * @exception IOException if an input/output error occurs
110      * @exception ServletException if a servlet exception occurs
111      */

112     public ActionForward execute(ActionMapping mapping,
113                                  ActionForm form,
114                                  HttpServletRequest JavaDoc request,
115                                  HttpServletResponse JavaDoc response)
116         throws IOException JavaDoc, ServletException JavaDoc {
117         
118         // Acquire the resources that we need
119
HttpSession JavaDoc session = request.getSession();
120         Locale JavaDoc locale = getLocale(request);
121         MessageResources resources = getResources(request);
122         
123         // Acquire a reference to the MBeanServer containing our MBeans
124
try {
125             mBServer = ((ApplicationServlet) getServlet()).getServer();
126         } catch (Throwable JavaDoc t) {
127             throw new ServletException JavaDoc
128             ("Cannot acquire MBeanServer reference", t);
129         }
130         
131         // Identify the requested action
132
ContextForm cform = (ContextForm) form;
133         String JavaDoc adminAction = cform.getAdminAction();
134         String JavaDoc cObjectName = cform.getObjectName();
135         String JavaDoc lObjectName = cform.getLoaderObjectName();
136         String JavaDoc mObjectName = cform.getManagerObjectName();
137         if ((cform.getPath() == null) || (cform.getPath().length()<1)) {
138             cform.setPath("/");
139         }
140        
141         // Perform a "Create Context" transaction (if requested)
142
if ("Create".equals(adminAction)) {
143
144             String JavaDoc operation = null;
145             Object JavaDoc values[] = null;
146             
147             try {
148                 // get the parent host name
149
String JavaDoc parentName = cform.getParentObjectName();
150                 ObjectName JavaDoc honame = new ObjectName JavaDoc(parentName);
151                 
152                 // Ensure that the requested context name is unique
153
ObjectName JavaDoc oname =
154                         new ObjectName JavaDoc(honame.getDomain() +
155                                     ":j2eeType=WebModule,name=//" +
156                                     honame.getKeyProperty("host") +
157                                     cform.getPath() +
158                                     // FIXME set J2EEApplication and J2EEServer
159
",J2EEApplication=none,J2EEServer=none");
160                 
161                 if (mBServer.isRegistered(oname)) {
162                     ActionErrors errors = new ActionErrors();
163                     errors.add("contextName",
164                                new ActionError("error.contextName.exists"));
165                     saveErrors(request, errors);
166                     return (new ActionForward(mapping.getInput()));
167                 }
168                 
169                 // Look up our MBeanFactory MBean
170
ObjectName JavaDoc fname =
171                     TomcatTreeBuilder.getMBeanFactory();
172
173                 // Create a new StandardContext object
174
values = new Object JavaDoc[3];
175                 values[0] = parentName;
176                 values[1] = cform.getPath();
177                 values[2] = cform.getDocBase();
178                 
179                 operation = "createStandardContext";
180                 cObjectName = (String JavaDoc)
181                     mBServer.invoke(fname, operation,
182                                     values, createStandardContextTypes);
183                 // Create a new Loader object
184
values = new String JavaDoc[1];
185                 // parent of loader is the newly created context
186
values[0] = cObjectName.toString();
187                 operation = "createWebappLoader";
188                 lObjectName = (String JavaDoc)
189                     mBServer.invoke(fname, operation,
190                                     values, createStandardLoaderTypes);
191                 
192                 // Create a new StandardManager object
193
values = new String JavaDoc[1];
194                 // parent of manager is the newly created Context
195
values[0] = cObjectName.toString();
196                 operation = "createStandardManager";
197                 mObjectName = (String JavaDoc)
198                     mBServer.invoke(fname, operation,
199                                     values, createStandardManagerTypes);
200                                                                        
201                 if (mObjectName==null) {
202                     operation = "removeLoader";
203                     values[0] = lObjectName;
204                     mBServer.invoke(fname, operation, values,
205                         removeContextTypes);
206                     operation = "removeContext";
207                     values[0] = cObjectName;
208                     mBServer.invoke(fname, operation, values,
209                         removeContextTypes);
210                     Registry.getRegistry().unregisterComponent(new ObjectName JavaDoc(cObjectName));
211                     request.setAttribute("warning", "error.context.directory");
212                     return (mapping.findForward("Save Unsuccessful"));
213                 }
214                 
215                 // Add the new Context to our tree control node
216
addToTreeControlNode(oname, cObjectName, parentName,
217                                     resources, session, locale);
218
219             } catch (Exception JavaDoc e) {
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 coname = new ObjectName JavaDoc(cObjectName);
238             ObjectName JavaDoc loname = new ObjectName JavaDoc(lObjectName);
239             ObjectName JavaDoc moname = new ObjectName JavaDoc(mObjectName);
240
241             attribute = "path";
242             String JavaDoc path = "";
243             try {
244                 path = cform.getPath();
245             } catch (Throwable JavaDoc t) {
246                 path = "";
247             }
248             mBServer.setAttribute(coname,
249                                   new Attribute JavaDoc("path", path));
250             
251             attribute = "workDir";
252             String JavaDoc workDir = "";
253             workDir = cform.getWorkDir();
254             if ((workDir!=null) && (workDir.length()>=1)) {
255                 mBServer.setAttribute(coname,
256                                   new Attribute JavaDoc("workDir", workDir));
257             }
258  
259             attribute = "cookies";
260             String JavaDoc cookies = "false";
261             try {
262                 cookies = cform.getCookies();
263             } catch (Throwable JavaDoc t) {
264                 cookies = "false";
265             }
266             mBServer.setAttribute(coname,
267                                   new Attribute JavaDoc("cookies", new Boolean JavaDoc(cookies)));
268
269             attribute = "crossContext";
270             String JavaDoc crossContext = "false";
271             try {
272                 crossContext = cform.getCrossContext();
273             } catch (Throwable JavaDoc t) {
274                 crossContext = "false";
275             }
276             mBServer.setAttribute(coname,
277                                   new Attribute JavaDoc("crossContext", new Boolean JavaDoc(crossContext)));
278
279             attribute = "override";
280             String JavaDoc override = "false";
281             try {
282                 override = cform.getOverride();
283             } catch (Throwable JavaDoc t) {
284                 override = "false";
285             }
286             mBServer.setAttribute(coname,
287                                   new Attribute JavaDoc("override", new Boolean JavaDoc(override)));
288
289             attribute = "privileged";
290             String JavaDoc privileged = "false";
291             try {
292                 privileged = cform.getPrivileged();
293             } catch (Throwable JavaDoc t) {
294                 privileged = "false";
295             }
296             mBServer.setAttribute(coname,
297                                   new Attribute JavaDoc("privileged", new Boolean JavaDoc(privileged)));
298
299             attribute = "reloadable";
300             String JavaDoc reloadable = "false";
301             try {
302                 reloadable = cform.getReloadable();
303             } catch (Throwable JavaDoc t) {
304                 reloadable = "false";
305             }
306             mBServer.setAttribute(coname,
307                                   new Attribute JavaDoc("reloadable", new Boolean JavaDoc(reloadable)));
308
309             attribute = "swallowOutput";
310             String JavaDoc swallowOutput = "false";
311             try {
312                 swallowOutput = cform.getSwallowOutput();
313             } catch (Throwable JavaDoc t) {
314                 swallowOutput = "false";
315             }
316             mBServer.setAttribute(coname,
317                                   new Attribute JavaDoc("swallowOutput", new Boolean JavaDoc(swallowOutput)));
318
319             attribute = "useNaming";
320             String JavaDoc useNaming = "false";
321             try {
322                 useNaming = cform.getUseNaming();
323             } catch (Throwable JavaDoc t) {
324                 useNaming = "false";
325             }
326             mBServer.setAttribute(coname,
327                                   new Attribute JavaDoc("useNaming", new Boolean JavaDoc(useNaming)));
328
329             attribute = "antiJARLocking";
330             String JavaDoc antiJarLocking = cform.getAntiJarLocking();
331             mBServer.setAttribute(coname,
332                                   new Attribute JavaDoc("antiJARLocking", new Boolean JavaDoc(antiJarLocking)));
333
334             attribute = "antiResourceLocking";
335             String JavaDoc antiResourceLocking = cform.getAntiResourceLocking();
336             mBServer.setAttribute(coname,
337                                   new Attribute JavaDoc("antiResourceLocking", new Boolean JavaDoc(antiResourceLocking)));
338
339         
340             // Loader properties
341
attribute = "reloadable";
342             try {
343                 reloadable = cform.getLdrReloadable();
344             } catch (Throwable JavaDoc t) {
345                 reloadable = "false";
346             }
347             mBServer.setAttribute(loname,
348                                   new Attribute JavaDoc("reloadable", new Boolean JavaDoc(reloadable)));
349             
350             //attribute = "checkInterval";
351
//int checkInterval = 15;
352
//try {
353
// checkInterval = Integer.parseInt(cform.getLdrCheckInterval());
354
//} catch (Throwable t) {
355
// checkInterval = 15;
356
//}
357
//mBServer.setAttribute(loname,
358
// new Attribute("checkInterval", new Integer(checkInterval)));
359

360             // Manager properties
361
attribute = "entropy";
362             String JavaDoc entropy = cform.getMgrSessionIDInit();
363             if ((entropy!=null) && (entropy.length()>=1)) {
364                 mBServer.setAttribute(moname,
365                                   new Attribute JavaDoc("entropy",entropy));
366             }
367             
368             //attribute = "checkInterval";
369
//try {
370
// checkInterval = Integer.parseInt(cform.getMgrCheckInterval());
371
//} catch (Throwable t) {
372
// checkInterval = 60;
373
//}
374
//mBServer.setAttribute(moname,
375
// new Attribute("checkInterval", new Integer(checkInterval)));
376

377             attribute = "maxActiveSessions";
378             int maxActiveSessions = -1;
379             try {
380                 maxActiveSessions = Integer.parseInt(cform.getMgrMaxSessions());
381             } catch (Throwable JavaDoc t) {
382                 maxActiveSessions = -1;
383             }
384             mBServer.setAttribute(moname,
385                                   new Attribute JavaDoc("maxActiveSessions", new Integer JavaDoc(maxActiveSessions)));
386
387         } catch (Exception JavaDoc e) {
388
389             getServlet().log
390                 (resources.getMessage(locale, "users.error.attribute.set",
391                                       attribute), e);
392             response.sendError
393                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
394                  resources.getMessage(locale, "users.error.attribute.set",
395                                       attribute));
396             return (null);
397         }
398         
399         // Forward to the success reporting page
400
session.removeAttribute(mapping.getAttribute());
401         return (mapping.findForward("Save Successful"));
402         
403     }
404     
405     
406     /**
407      * Append nodes for any define resources for the specified Context.
408      *
409      * @param containerNode Container node for the tree control
410      * @param containerName Object name of the parent container
411      * @param resources The MessageResources for our localized messages
412      * messages
413      */

414     public void addToTreeControlNode(ObjectName JavaDoc oname, String JavaDoc containerName,
415                                     String JavaDoc parentName, MessageResources resources,
416                                     HttpSession JavaDoc session, Locale JavaDoc locale)
417         throws Exception JavaDoc {
418                               
419         String JavaDoc domain = oname.getDomain();
420         TreeControl control = (TreeControl) session.getAttribute("treeControlTest");
421         if (control != null) {
422             TreeControlNode parentNode = control.findNode(parentName);
423             if (parentNode != null) {
424                 String JavaDoc type = "Context";
425                 String JavaDoc path = "";
426                 String JavaDoc host = "";
427                 String JavaDoc name = oname.getKeyProperty("name");
428                 if ((name != null) && (name.length() > 0)) {
429                     name = name.substring(2);
430                     int i = name.indexOf("/");
431                     host = name.substring(0,i);
432                     path = name.substring(i);
433                 }
434                 String JavaDoc nodeLabel =
435                     resources.getMessage(locale, "server.service.treeBuilder.context") +
436                     " (" + path + ")";
437                 String JavaDoc encodedName = URLEncoder.encode(oname.toString(),TomcatTreeBuilder.URL_ENCODING);
438                 TreeControlNode childNode =
439                     new TreeControlNode(oname.toString(),
440                                         "Context.gif",
441                                         nodeLabel,
442                                         "EditContext.do?select=" +
443                                         encodedName,
444                                         "content",
445                                         true, domain);
446                 parentNode.addChild(childNode);
447         
448                 // FIXME - force a redisplay
449
TreeControlNode subtree = new TreeControlNode
450                     ("Context Resource Administration " + containerName,
451                     "folder_16_pad.gif",
452                     resources.getMessage(locale, "resources.treeBuilder.subtreeNode"),
453                     null,
454                     "content",
455                     true, domain);
456                 childNode.addChild(subtree);
457                 TreeControlNode datasources = new TreeControlNode
458                     ("Context Data Sources " + containerName,
459                     "Datasource.gif",
460                     resources.getMessage(locale, "resources.treeBuilder.datasources"),
461                     "resources/listDataSources.do?resourcetype=" +
462                     URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" +
463                     URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" +
464                     URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" +
465                     URLEncoder.encode("DataSources List Setup",TomcatTreeBuilder.URL_ENCODING),
466                     "content",
467                     false, domain);
468                 TreeControlNode mailsessions = new TreeControlNode
469                     ("Context Mail Sessions " + containerName,
470                     "Mailsession.gif",
471                     resources.getMessage(locale, "resources.treeBuilder.mailsessions"),
472                     "resources/listMailSessions.do?resourcetype=" +
473                     URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" +
474                     URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" +
475                     URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" +
476                     URLEncoder.encode("MailSessions List Setup",TomcatTreeBuilder.URL_ENCODING),
477                     "content",
478                     false, domain);
479                 TreeControlNode resourcelinks = new TreeControlNode
480                     ("Resource Links " + containerName,
481                     "ResourceLink.gif",
482                     resources.getMessage(locale, "resources.treeBuilder.resourcelinks"),
483                     "resources/listResourceLinks.do?resourcetype=" +
484                     URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" +
485                     URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" +
486                     URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" +
487                     URLEncoder.encode("ResourceLinks List Setup",TomcatTreeBuilder.URL_ENCODING),
488                     "content",
489                     false, domain);
490                 TreeControlNode envs = new TreeControlNode
491                     ("Context Environment Entries "+ containerName,
492                     "EnvironmentEntries.gif",
493                     resources.getMessage(locale ,"resources.env.entries"),
494                     "resources/listEnvEntries.do?resourcetype=" +
495                     URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" +
496                     URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" +
497                     URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" +
498                     URLEncoder.encode("EnvEntries List Setup",TomcatTreeBuilder.URL_ENCODING),
499                     "content",
500                     false, domain);
501                 subtree.addChild(datasources);
502                 subtree.addChild(mailsessions);
503                 subtree.addChild(resourcelinks);
504                 subtree.addChild(envs);
505             } else {
506                     getServlet().log
507                         ("Cannot find parent node '" + parentName + "'");
508             }
509         }else {
510             getServlet().log("Cannot find TreeControlNode!");
511         }
512     }
513         
514 }
515
Popular Tags