1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.monitor;
20
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import javax.management.MBeanServerConnection;
26
27 import org.eclipse.jetty.monitor.jmx.MonitorAction;
28 import org.eclipse.jetty.monitor.jmx.MonitorTask;
29 import org.eclipse.jetty.monitor.jmx.ServiceConnection;
30 import org.eclipse.jetty.xml.XmlConfiguration;
31
32
33
34
35
36
37
38
39 public class JMXMonitor
40 {
41 private static JMXMonitor __monitor = new JMXMonitor();
42
43 private String _serverUrl;
44 private ServiceConnection _serviceConnection;
45
46 private Set<MonitorAction> _actions = new HashSet<MonitorAction>();
47
48
49
50
51
52
53
54 public JMXMonitor() {}
55
56
57
58
59
60
61
62
63 public boolean addActions(MonitorAction... actions)
64 {
65 return getInstance().add(actions);
66 }
67
68
69
70
71
72
73
74
75 public boolean removeActions(MonitorAction... actions)
76 {
77 return getInstance().remove(actions);
78 }
79
80
81
82
83
84
85
86 public void setUrl(String url)
87 {
88 getInstance().set(url);
89 }
90
91 public MBeanServerConnection getConnection()
92 throws IOException
93 {
94 return getInstance().get();
95 }
96
97 public static JMXMonitor getInstance()
98 {
99 return __monitor;
100 }
101
102 public static boolean addMonitorActions(MonitorAction... actions)
103 {
104 return getInstance().add(actions);
105 }
106
107 public static boolean removeMonitorActions(MonitorAction... actions)
108 {
109 return getInstance().remove(actions);
110 }
111
112 public static void setServiceUrl(String url)
113 {
114 getInstance().set(url);
115 }
116
117
118
119
120
121
122
123
124 public static MBeanServerConnection getServiceConnection()
125 throws IOException
126 {
127 return getInstance().getConnection();
128 }
129
130 public static void main(final String args[]) throws Exception
131 {
132 XmlConfiguration.main(args);
133 }
134
135 private synchronized boolean add(MonitorAction... actions)
136 {
137 boolean result = true;
138
139 for (MonitorAction action : actions)
140 {
141 if (!_actions.add(action))
142 {
143 result = false;
144 }
145 else
146 {
147 MonitorTask.schedule(action);
148 }
149 }
150
151 return result;
152 }
153
154 private synchronized boolean remove(MonitorAction... actions)
155 {
156 boolean result = true;
157
158 for (MonitorAction action : actions)
159 {
160 if (!_actions.remove(action))
161 {
162 result = false;
163 }
164
165 MonitorTask.cancel(action);
166 }
167
168 return result;
169 }
170
171 private synchronized void set(String url)
172 {
173 _serverUrl = url;
174
175 if (_serviceConnection != null)
176 {
177 _serviceConnection.disconnect();
178 _serviceConnection = null;
179 }
180 }
181
182 private synchronized MBeanServerConnection get()
183 throws IOException
184 {
185 if (_serviceConnection == null)
186 {
187 _serviceConnection = new ServiceConnection(_serverUrl);
188 _serviceConnection.connect();
189 }
190
191 return _serviceConnection.getConnection();
192 }
193 }