View Javadoc

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