Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/webapp/admin/TomcatTreeBuilder.java


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;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Locale;
25  import java.net.URLEncoder;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
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;
40  import javax.management.MalformedObjectNameException;
41  import javax.management.MBeanServer;
42  import javax.management.MBeanServerFactory;
43  import javax.management.QueryExp;
44  import javax.management.Query;
45  import javax.management.ObjectInstance;
46  import javax.management.ObjectName;
47  import javax.management.JMException;
48  import javax.management.MBeanAttributeInfo;
49  import javax.management.MBeanInfo;
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: 303390 $ $Date: 2004-10-18 02:37:56 -0400 (Mon, 18 Oct 2004) $
59   */
60  
61  
62  public class TomcatTreeBuilder implements TreeBuilder{
63      
64      // This SERVER_LABEL needs to be localized
65      private final static String SERVER_LABEL = "Tomcat Server";
66      
67      public final static String DEFAULT_DOMAIN = "Catalina";
68      public final static String SERVER_TYPE = ":type=Server";
69      public final static String FACTORY_TYPE = 
70                          DEFAULT_DOMAIN + ":type=MBeanFactory";
71      public final static String SERVICE_TYPE = ":type=Service";
72      public final static String ENGINE_TYPE = ":type=Engine";
73      public final static String CONNECTOR_TYPE = ":type=Connector";
74      public final static String HOST_TYPE = ":type=Host";
75      public final static String CONTEXT_TYPE = ":type=Context";
76      public final static String LOADER_TYPE = ":type=Loader";
77      public final static String MANAGER_TYPE = ":type=Manager";
78      public final static String LOGGER_TYPE = ":type=Logger";
79      public final static String REALM_TYPE = ":type=Realm";
80      public final static String VALVE_TYPE = ":type=Valve";
81  
82      public final static String WILDCARD = ",*";
83  
84      public final static String URL_ENCODING="UTF-8";
85      
86      private static MBeanServer mBServer = null;
87      private MessageResources resources = null;
88      private Locale locale = null;
89  
90      public void buildTree(TreeControl treeControl,
91                            ApplicationServlet servlet,
92                            HttpServletRequest request) {
93  
94          try {
95              HttpSession session = request.getSession();
96              locale = (Locale) 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 t){
103             t.printStackTrace(System.out);
104         }
105 
106     }
107     
108     public static ObjectName getMBeanFactory() 
109             throws MalformedObjectNameException {
110         
111         return new ObjectName(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 {
125         
126         String domain = rootNode.getDomain();
127         Iterator serverNames =
128             Lists.getServers(mBServer,domain).iterator();
129         while (serverNames.hasNext()) {
130             String serverName = (String) serverNames.next();
131             ObjectName objectName = new ObjectName(serverName);
132             String 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 serverName) 
161         throws Exception {
162 
163         String domain = serverNode.getDomain();
164         Iterator serviceNames =
165             Lists.getServices(mBServer, serverName).iterator();
166         while (serviceNames.hasNext()) {
167             String serviceName = (String) serviceNames.next();
168             ObjectName objectName = new ObjectName(serviceName);
169             String 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 serviceName)
202                         throws Exception{
203         
204         String domain = serviceNode.getDomain();
205         Iterator connectorNames =
206             Lists.getConnectors(mBServer, serviceName).iterator();
207         while (connectorNames.hasNext()) {
208             String connectorName = (String) connectorNames.next();
209             ObjectName objectName = new ObjectName(connectorName);
210             String 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 serviceName) 
240         throws Exception {
241         
242         String domain = serviceNode.getDomain();
243         Iterator hostNames =
244             Lists.getHosts(mBServer, serviceName).iterator();
245         while (hostNames.hasNext()) {
246             String hostName = (String) hostNames.next();
247             ObjectName objectName = new ObjectName(hostName);
248             String 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 hostName) 
282         throws Exception {
283         
284         String domain = hostNode.getDomain();
285         Iterator contextNames =
286             Lists.getContexts(mBServer, hostName).iterator();
287         while (contextNames.hasNext()) {
288             String contextName = (String) contextNames.next();
289             ObjectName objectName = new ObjectName(contextName);
290             String name = objectName.getKeyProperty("name");
291             name = name.substring(2);
292             int i = name.indexOf("/");
293             String path = name.substring(i);
294             String 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 containerName) throws Exception {
324 
325         String domain = containerNode.getDomain();
326         Iterator realmNames =
327             Lists.getRealms(mBServer, containerName).iterator();
328         while (realmNames.hasNext()) {
329             String realmName = (String) realmNames.next();
330       ObjectName objectName = new ObjectName(realmName);
331             // Create tree nodes for non JAASRealm only
332             try {
333                 mBServer.getAttribute(objectName, "validate");
334             } catch (AttributeNotFoundException e) {
335                 String 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 containerName) 
364         throws Exception {
365 
366         String domain = containerNode.getDomain();
367         ObjectName oname = new ObjectName(containerName);
368         String 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 path = "";
378         String host = "";
379         String 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 containerName) throws Exception {
460 
461         String domain = containerNode.getDomain();
462         Iterator valveNames =
463                 Lists.getValves(mBServer, containerName).iterator();        
464         while (valveNames.hasNext()) {
465             String valveName = (String) valveNames.next();
466             ObjectName objectName = new ObjectName(valveName);
467             String 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 }