1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 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
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
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 }