View Javadoc

1   //========================================================================
2   //Copyright (c) Webtide LLC
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //
8   //The Eclipse Public License is available at
9   //http://www.eclipse.org/legal/epl-v10.html
10  //
11  //The Apache License v2.0 is available at
12  //http://www.apache.org/licenses/LICENSE-2.0.txt
13  //
14  //You may elect to redistribute this code under either of these licenses.
15  //========================================================================
16  
17  package org.eclipse.jetty.server.handler.jmx;
18  
19  import java.io.IOException;
20  
21  import org.eclipse.jetty.jmx.ObjectMBean;
22  import org.eclipse.jetty.server.Server;
23  import org.eclipse.jetty.server.handler.AbstractHandler;
24  import org.eclipse.jetty.server.handler.AbstractHandlerContainer;
25  import org.eclipse.jetty.server.handler.ContextHandler;
26  import org.eclipse.jetty.util.log.Log;
27  
28  public class AbstractHandlerMBean extends ObjectMBean
29  {
30      /* ------------------------------------------------------------ */
31      public AbstractHandlerMBean(Object managedObject)
32      {
33          super(managedObject);
34      }
35  
36      /* ------------------------------------------------------------ */
37      @Override
38      public String getObjectContextBasis()
39      {
40          if (_managed != null )
41          {
42              String basis = null;
43              if (_managed instanceof ContextHandler)
44              {
45                  return null;
46              }
47              else if (_managed instanceof AbstractHandler)
48              {
49                  AbstractHandler handler = (AbstractHandler)_managed;
50                  Server server = handler.getServer();
51                  if (server != null)
52                  {
53                      ContextHandler context = 
54                          AbstractHandlerContainer.findContainerOf(server,
55                                  ContextHandler.class, handler);
56                      
57                      if (context != null)
58                          basis = getContextName(context);
59                  }
60              }
61              if (basis != null)
62                  return basis;
63          }
64          return super.getObjectContextBasis();
65      }
66      
67      /* ------------------------------------------------------------ */
68      @Override
69      public String getObjectNameBasis()
70      {
71          if (_managed != null )
72          {
73              String name = null;
74              if (_managed instanceof ContextHandler)
75              {
76                  ContextHandler context = (ContextHandler)_managed;
77                  name = getContextName(context);
78              }
79              
80              if (name != null)
81                  return name;
82          }
83          
84          return super.getObjectNameBasis();
85      }
86  
87      /* ------------------------------------------------------------ */
88      protected String getContextName(ContextHandler context)
89      {
90          String name = null;
91          
92          if (context.getContextPath()!=null && context.getContextPath().length()>0)
93          {
94              int idx = context.getContextPath().lastIndexOf('/');
95              name = idx < 0 ? context.getContextPath() : context.getContextPath().substring(++idx);
96              if (name==null || name.length()==0)
97                  name= "ROOT";
98          }
99          
100         if (name==null && context.getBaseResource()!=null)
101         {
102             try
103             {
104                 if (context.getBaseResource().getFile()!=null)
105                     name = context.getBaseResource().getFile().getName();
106             }
107             catch(IOException e)
108             {
109                 Log.ignore(e);
110                 name=context.getBaseResource().getName();
111             }
112         }
113         
114         return name;
115     }
116 }