KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > TomcatTreeBuilder


1 /*
2  * Copyright 2001,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;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.net.URLEncoder 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.ManagedBean;
31 import org.apache.commons.modeler.Registry;
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.Action;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.util.MessageResources;
39 import javax.management.AttributeNotFoundException JavaDoc;
40 import javax.management.MalformedObjectNameException JavaDoc;
41 import javax.management.MBeanServer JavaDoc;
42 import javax.management.MBeanServerFactory JavaDoc;
43 import javax.management.QueryExp JavaDoc;
44 import javax.management.Query JavaDoc;
45 import javax.management.ObjectInstance JavaDoc;
46 import javax.management.ObjectName JavaDoc;
47 import javax.management.JMException JavaDoc;
48 import javax.management.MBeanAttributeInfo JavaDoc;
49 import javax.management.MBeanInfo JavaDoc;
50
51 /**
52  * <p> Implementation of TreeBuilder interface for Tomcat Tree Controller
53  * to build plugin components into the tree
54  *
55  * @author Jazmin Jonson
56  * @author Manveen Kaur
57  * @author Amy Roh
58  * @version $Revision: 1.14 $ $Date: 2004/10/18 06:37:53 $
59  */

60
61
62 public class TomcatTreeBuilder implements TreeBuilder{
63     
64     // This SERVER_LABEL needs to be localized
65
private final static String JavaDoc SERVER_LABEL = "Tomcat Server";
66     
67     public final static String JavaDoc DEFAULT_DOMAIN = "Catalina";
68     public final static String JavaDoc SERVER_TYPE = ":type=Server";
69     public final static String JavaDoc FACTORY_TYPE =
70                         DEFAULT_DOMAIN + ":type=MBeanFactory";
71     public final static String JavaDoc SERVICE_TYPE = ":type=Service";
72     public final static String JavaDoc ENGINE_TYPE = ":type=Engine";
73     public final static String JavaDoc CONNECTOR_TYPE = ":type=Connector";
74     public final static String JavaDoc HOST_TYPE = ":type=Host";
75     public final static String JavaDoc CONTEXT_TYPE = ":type=Context";
76     public final static String JavaDoc LOADER_TYPE = ":type=Loader";
77     public final static String JavaDoc MANAGER_TYPE = ":type=Manager";
78     public final static String JavaDoc LOGGER_TYPE = ":type=Logger";
79     public final static String JavaDoc REALM_TYPE = ":type=Realm";
80     public final static String JavaDoc VALVE_TYPE = ":type=Valve";
81
82     public final static String JavaDoc WILDCARD = ",*";
83
84     public final static String JavaDoc URL_ENCODING="UTF-8";
85     
86     private static MBeanServer JavaDoc mBServer = null;
87     private MessageResources resources = null;
88     private Locale JavaDoc locale = null;
89
90     public void buildTree(TreeControl treeControl,
91                           ApplicationServlet servlet,
92                           HttpServletRequest JavaDoc request) {
93
94         try {
95             HttpSession JavaDoc session = request.getSession();
96             locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
97             mBServer = servlet.getServer();
98             TreeControlNode root = treeControl.getRoot();
99             resources = (MessageResources)
100                 servlet.getServletContext().getAttribute(Globals.MESSAGES_KEY);
101             getServers(root);
102         } catch(Throwable JavaDoc t){
103             t.printStackTrace(System.out);
104         }
105
106     }
107     
108     public static ObjectName JavaDoc getMBeanFactory()
109             throws MalformedObjectNameException JavaDoc {
110         
111         return new ObjectName JavaDoc(FACTORY_TYPE);
112     }
113     
114
115     /**
116      * Append nodes for all defined servers.
117      *
118      * @param rootNode Root node for the tree control
119      * @param resources The MessageResources for our localized messages
120      * messages
121      *
122      * @exception Exception if an exception occurs building the tree
123      */

124     public void getServers(TreeControlNode rootNode) throws Exception JavaDoc {
125         
126         String JavaDoc domain = rootNode.getDomain();
127         Iterator JavaDoc serverNames =
128             Lists.getServers(mBServer,domain).iterator();
129         while (serverNames.hasNext()) {
130             String JavaDoc serverName = (String JavaDoc) serverNames.next();
131             ObjectName JavaDoc objectName = new ObjectName JavaDoc(serverName);
132             String JavaDoc nodeLabel = SERVER_LABEL;
133             TreeControlNode serverNode =
134                 new TreeControlNode(serverName,
135                                     "Server.gif",
136                                     nodeLabel,
137                                     "EditServer.do?select=" +
138                                     URLEncoder.encode(serverName,URL_ENCODING) +
139                                     "&nodeLabel=" +
140                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
141                                     "content",
142                                     true, domain);
143             rootNode.addChild(serverNode);
144             getServices(serverNode, serverName);
145         }
146         
147     }
148     
149
150     /**
151      * Append nodes for all defined services for the specified server.
152      *
153      * @param serverNode Server node for the tree control
154      * @param serverName Object name of the parent server
155      * @param resources The MessageResources for our localized messages
156      * messages
157      *
158      * @exception Exception if an exception occurs building the tree
159      */

160     public void getServices(TreeControlNode serverNode, String JavaDoc serverName)
161         throws Exception JavaDoc {
162
163         String JavaDoc domain = serverNode.getDomain();
164         Iterator JavaDoc serviceNames =
165             Lists.getServices(mBServer, serverName).iterator();
166         while (serviceNames.hasNext()) {
167             String JavaDoc serviceName = (String JavaDoc) serviceNames.next();
168             ObjectName JavaDoc objectName = new ObjectName JavaDoc(serviceName);
169             String JavaDoc nodeLabel =
170                 resources.getMessage(locale,
171                     "server.service.treeBuilder.subtreeNode") + " (" +
172                     objectName.getKeyProperty("serviceName") + ")";
173             TreeControlNode serviceNode =
174                 new TreeControlNode(serviceName,
175                                     "Service.gif",
176                                     nodeLabel,
177                                     "EditService.do?select=" +
178                                     URLEncoder.encode(serviceName,URL_ENCODING) +
179                                     "&nodeLabel=" +
180                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
181                                     "content",
182                                     false, domain);
183             serverNode.addChild(serviceNode);
184             getConnectors(serviceNode, serviceName);
185             getHosts(serviceNode, serviceName);
186             getRealms(serviceNode, serviceName);
187             getValves(serviceNode, serviceName);
188         }
189
190     }
191     
192
193     /**
194      * Append nodes for all defined connectors for the specified service.
195      *
196      * @param serviceNode Service node for the tree control
197      * @param serviceName Object name of the parent service
198      *
199      * @exception Exception if an exception occurs building the tree
200      */

201     public void getConnectors(TreeControlNode serviceNode, String JavaDoc serviceName)
202                         throws Exception JavaDoc{
203         
204         String JavaDoc domain = serviceNode.getDomain();
205         Iterator JavaDoc connectorNames =
206             Lists.getConnectors(mBServer, serviceName).iterator();
207         while (connectorNames.hasNext()) {
208             String JavaDoc connectorName = (String JavaDoc) connectorNames.next();
209             ObjectName JavaDoc objectName = new ObjectName JavaDoc(connectorName);
210             String JavaDoc nodeLabel =
211                 resources.getMessage(locale,
212                     "server.service.treeBuilder.connector") + " (" +
213                     objectName.getKeyProperty("port") + ")";
214             TreeControlNode connectorNode =
215                 new TreeControlNode(connectorName,
216                                     "Connector.gif",
217                                     nodeLabel,
218                                     "EditConnector.do?select=" +
219                                     URLEncoder.encode(connectorName,URL_ENCODING) +
220                                     "&nodeLabel=" +
221                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
222                                     "content",
223                                     false, domain);
224             serviceNode.addChild(connectorNode);
225         }
226     }
227     
228
229     /**
230      * Append nodes for all defined hosts for the specified service.
231      *
232      * @param serviceNode Service node for the tree control
233      * @param serviceName Object name of the parent service
234      * @param resources The MessageResources for our localized messages
235      * messages
236      *
237      * @exception Exception if an exception occurs building the tree
238      */

239     public void getHosts(TreeControlNode serviceNode, String JavaDoc serviceName)
240         throws Exception JavaDoc {
241         
242         String JavaDoc domain = serviceNode.getDomain();
243         Iterator JavaDoc hostNames =
244             Lists.getHosts(mBServer, serviceName).iterator();
245         while (hostNames.hasNext()) {
246             String JavaDoc hostName = (String JavaDoc) hostNames.next();
247             ObjectName JavaDoc objectName = new ObjectName JavaDoc(hostName);
248             String JavaDoc nodeLabel =
249                 resources.getMessage(locale,
250                     "server.service.treeBuilder.host") + " (" +
251                     objectName.getKeyProperty("host") + ")";
252             TreeControlNode hostNode =
253                 new TreeControlNode(hostName,
254                                     "Host.gif",
255                                     nodeLabel,
256                                     "EditHost.do?select=" +
257                                     URLEncoder.encode(hostName,URL_ENCODING) +
258                                     "&nodeLabel=" +
259                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
260                                     "content",
261                                     false, domain);
262             serviceNode.addChild(hostNode);
263             getContexts(hostNode, hostName);
264             getRealms(hostNode, hostName);
265             getValves(hostNode, hostName);
266         }
267
268     }
269
270     
271     /**
272      * Append nodes for all defined contexts for the specified host.
273      *
274      * @param hostNode Host node for the tree control
275      * @param hostName Object name of the parent host
276      * @param resources The MessageResources for our localized messages
277      * messages
278      *
279      * @exception Exception if an exception occurs building the tree
280      */

281     public void getContexts(TreeControlNode hostNode, String JavaDoc hostName)
282         throws Exception JavaDoc {
283         
284         String JavaDoc domain = hostNode.getDomain();
285         Iterator JavaDoc contextNames =
286             Lists.getContexts(mBServer, hostName).iterator();
287         while (contextNames.hasNext()) {
288             String JavaDoc contextName = (String JavaDoc) contextNames.next();
289             ObjectName JavaDoc objectName = new ObjectName JavaDoc(contextName);
290             String JavaDoc name = objectName.getKeyProperty("name");
291             name = name.substring(2);
292             int i = name.indexOf("/");
293             String JavaDoc path = name.substring(i);
294             String JavaDoc nodeLabel =
295                 resources.getMessage(locale,
296                     "server.service.treeBuilder.context") + " (" + path + ")";
297             TreeControlNode contextNode =
298                 new TreeControlNode(contextName,
299                                     "Context.gif",
300                                     nodeLabel,
301                                     "EditContext.do?select=" +
302                                     URLEncoder.encode(contextName,URL_ENCODING) +
303                                     "&nodeLabel=" +
304                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
305                                     "content",
306                                     false, domain);
307             hostNode.addChild(contextNode);
308             getResources(contextNode, contextName);
309             getRealms(contextNode, contextName);
310             getValves(contextNode, contextName);
311         }
312     }
313     
314     /**
315      * Append nodes for any defined realms for the specified container.
316      *
317      * @param containerNode Container node for the tree control
318      * @param containerName Object name of the parent container
319      *
320      * @exception Exception if an exception occurs building the tree
321      */

322     public void getRealms(TreeControlNode containerNode,
323                           String JavaDoc containerName) throws Exception JavaDoc {
324
325         String JavaDoc domain = containerNode.getDomain();
326         Iterator JavaDoc realmNames =
327             Lists.getRealms(mBServer, containerName).iterator();
328         while (realmNames.hasNext()) {
329             String JavaDoc realmName = (String JavaDoc) realmNames.next();
330         ObjectName JavaDoc objectName = new ObjectName JavaDoc(realmName);
331             // Create tree nodes for non JAASRealm only
332
try {
333                 mBServer.getAttribute(objectName, "validate");
334             } catch (AttributeNotFoundException JavaDoc e) {
335                 String JavaDoc nodeLabel = resources.getMessage(locale,
336                     "server.service.treeBuilder.realmFor",
337                     containerNode.getLabel());
338             TreeControlNode realmNode =
339             new TreeControlNode(realmName,
340                                     "Realm.gif",
341                                     nodeLabel,
342                                     "EditRealm.do?select=" +
343                                     URLEncoder.encode(realmName,URL_ENCODING) +
344                                     "&nodeLabel=" +
345                                     URLEncoder.encode(nodeLabel,URL_ENCODING),
346                                     "content",
347                                     false, domain);
348                 containerNode.addChild(realmNode);
349             }
350         }
351         
352     }
353         
354     
355     /**
356      * Append nodes for any define resources for the specified Context.
357      *
358      * @param containerNode Container node for the tree control
359      * @param containerName Object name of the parent container
360      * @param resources The MessageResources for our localized messages
361      * messages
362      */

363     public void getResources(TreeControlNode containerNode, String JavaDoc containerName)
364         throws Exception JavaDoc {
365
366         String JavaDoc domain = containerNode.getDomain();
367         ObjectName JavaDoc oname = new ObjectName JavaDoc(containerName);
368         String JavaDoc type = oname.getKeyProperty("type");
369         if (type == null) {
370             type = oname.getKeyProperty("j2eeType");
371             if (type.equals("WebModule")) {
372                 type = "Context";
373             } else {
374                 type = "";
375             }
376         }
377         String JavaDoc path = "";
378         String JavaDoc host = "";
379         String JavaDoc name = oname.getKeyProperty("name");
380         if ((name != null) && (name.length() > 0)) {
381             // context resource
382
name = name.substring(2);
383             int i = name.indexOf("/");
384             host = name.substring(0,i);
385             path = name.substring(i);
386         }
387         TreeControlNode subtree = new TreeControlNode
388             ("Context Resource Administration " + containerName,
389              "folder_16_pad.gif",
390              resources.getMessage(locale, "resources.treeBuilder.subtreeNode"),
391              null,
392              "content",
393              true, domain);
394         containerNode.addChild(subtree);
395         TreeControlNode datasources = new TreeControlNode
396             ("Context Data Sources " + containerName,
397             "Datasource.gif",
398             resources.getMessage(locale, "resources.treeBuilder.datasources"),
399             "resources/listDataSources.do?resourcetype=" +
400                 URLEncoder.encode(type,URL_ENCODING) + "&path=" +
401                 URLEncoder.encode(path,URL_ENCODING) + "&host=" +
402                 URLEncoder.encode(host,URL_ENCODING) + "&domain=" +
403                 URLEncoder.encode(domain,URL_ENCODING) + "&forward=" +
404                 URLEncoder.encode("DataSources List Setup",URL_ENCODING),
405             "content",
406             false, domain);
407         TreeControlNode mailsessions = new TreeControlNode
408             ("Context Mail Sessions " + containerName,
409             "Mailsession.gif",
410             resources.getMessage(locale, "resources.treeBuilder.mailsessions"),
411             "resources/listMailSessions.do?resourcetype=" +
412                 URLEncoder.encode(type,URL_ENCODING) + "&path=" +
413                 URLEncoder.encode(path,URL_ENCODING) + "&host=" +
414                 URLEncoder.encode(host,URL_ENCODING) + "&domain=" +
415                 URLEncoder.encode(domain,URL_ENCODING) + "&forward=" +
416                 URLEncoder.encode("MailSessions List Setup",URL_ENCODING),
417             "content",
418             false, domain);
419         TreeControlNode resourcelinks = new TreeControlNode
420             ("Resource Links " + containerName,
421             "ResourceLink.gif",
422             resources.getMessage(locale, "resources.treeBuilder.resourcelinks"),
423             "resources/listResourceLinks.do?resourcetype=" +
424                 URLEncoder.encode(type,URL_ENCODING) + "&path=" +
425                 URLEncoder.encode(path,URL_ENCODING) + "&host=" +
426                 URLEncoder.encode(host,URL_ENCODING) + "&domain=" +
427                 URLEncoder.encode(domain,URL_ENCODING) + "&forward=" +
428                 URLEncoder.encode("ResourceLinks List Setup",URL_ENCODING),
429             "content",
430             false, domain);
431         TreeControlNode envs = new TreeControlNode
432             ("Context Environment Entries "+ containerName,
433             "EnvironmentEntries.gif",
434             resources.getMessage(locale, "resources.env.entries"),
435             "resources/listEnvEntries.do?resourcetype=" +
436                 URLEncoder.encode(type,URL_ENCODING) + "&path=" +
437                 URLEncoder.encode(path,URL_ENCODING) + "&host=" +
438                 URLEncoder.encode(host,URL_ENCODING) + "&domain=" +
439                 URLEncoder.encode(domain,URL_ENCODING) + "&forward=" +
440                 URLEncoder.encode("EnvEntries List Setup",URL_ENCODING),
441             "content",
442             false, domain);
443         subtree.addChild(datasources);
444         subtree.addChild(mailsessions);
445         subtree.addChild(resourcelinks);
446         subtree.addChild(envs);
447     }
448     
449     
450    /**
451      * Append nodes for any defined valves for the specified container.
452      *
453      * @param containerNode Container node for the tree control
454      * @param containerName Object name of the parent container
455      *
456      * @exception Exception if an exception occurs building the tree
457      */

458     public void getValves(TreeControlNode containerNode,
459                           String JavaDoc containerName) throws Exception JavaDoc {
460
461         String JavaDoc domain = containerNode.getDomain();
462         Iterator JavaDoc valveNames =
463                 Lists.getValves(mBServer, containerName).iterator();
464         while (valveNames.hasNext()) {
465             String JavaDoc valveName = (String JavaDoc) valveNames.next();
466             ObjectName JavaDoc objectName = new ObjectName JavaDoc(valveName);
467             String JavaDoc nodeLabel = "Valve for " + containerNode.getLabel();
468             TreeControlNode valveNode =
469                 new TreeControlNode(valveName,
470                                     "Valve.gif",
471                                     nodeLabel,
472                                     "EditValve.do?select=" +
473                                     URLEncoder.encode(valveName,URL_ENCODING) +
474                                     "&nodeLabel=" +
475                                     URLEncoder.encode(nodeLabel,URL_ENCODING) +
476                                     "&parent=" +
477                                     URLEncoder.encode(containerName,URL_ENCODING),
478                                     "content",
479                                     false, domain);
480             containerNode.addChild(valveNode);
481         }
482     }
483 }
484
Popular Tags