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  import org.eclipse.jetty.util.log.Logger;
28  
29  public class AbstractHandlerMBean extends ObjectMBean
30  {
31      private static final Logger LOG = Log.getLogger(AbstractHandlerMBean.class);
32  
33      /* ------------------------------------------------------------ */
34      public AbstractHandlerMBean(Object managedObject)
35      {
36          super(managedObject);
37      }
38  
39      /* ------------------------------------------------------------ */
40      @Override
41      public String getObjectContextBasis()
42      {
43          if (_managed != null )
44          {
45              String basis = null;
46              if (_managed instanceof ContextHandler)
47              {
48                  return null;
49              }
50              else if (_managed instanceof AbstractHandler)
51              {
52                  AbstractHandler handler = (AbstractHandler)_managed;
53                  Server server = handler.getServer();
54                  if (server != null)
55                  {
56                      ContextHandler context = 
57                          AbstractHandlerContainer.findContainerOf(server,
58                                  ContextHandler.class, handler);
59                      
60                      if (context != null)
61                          basis = getContextName(context);
62                  }
63              }
64              if (basis != null)
65                  return basis;
66          }
67          return super.getObjectContextBasis();
68      }
69      
70      /* ------------------------------------------------------------ */
71      @Override
72      public String getObjectNameBasis()
73      {
74          if (_managed != null )
75          {
76              String name = null;
77              if (_managed instanceof ContextHandler)
78              {
79                  ContextHandler context = (ContextHandler)_managed;
80                  name = getContextName(context);
81              }
82              
83              if (name != null)
84                  return name;
85          }
86          
87          return super.getObjectNameBasis();
88      }
89  
90      /* ------------------------------------------------------------ */
91      protected String getContextName(ContextHandler context)
92      {
93          String name = null;
94          
95          if (context.getContextPath()!=null && context.getContextPath().length()>0)
96          {
97              int idx = context.getContextPath().lastIndexOf('/');
98              name = idx < 0 ? context.getContextPath() : context.getContextPath().substring(++idx);
99              if (name==null || name.length()==0)
100                 name= "ROOT";
101         }
102         
103         if (name==null && context.getBaseResource()!=null)
104         {
105             try
106             {
107                 if (context.getBaseResource().getFile()!=null)
108                     name = context.getBaseResource().getFile().getName();
109             }
110             catch(IOException e)
111             {
112                 LOG.ignore(e);
113                 name=context.getBaseResource().getName();
114             }
115         }
116         
117         return name;
118     }
119 }