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