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
40 public class JMXMonitor
41 {
42 private static JMXMonitor __monitor = new JMXMonitor();
43
44 private String _serverUrl;
45 private ServiceConnection _serviceConnection;
46
47 private Set<MonitorAction> _actions = new HashSet<MonitorAction>();
48
49
50
51
52
53
54
55 public JMXMonitor() {}
56
57
58
59
60
61
62
63
64 public boolean addActions(MonitorAction... actions)
65 {
66 return getInstance().add(actions);
67 }
68
69
70
71
72
73
74
75
76 public boolean removeActions(MonitorAction... actions)
77 {
78 return getInstance().remove(actions);
79 }
80
81
82
83
84
85
86
87 public void setUrl(String url)
88 {
89 getInstance().set(url);
90 }
91
92 public MBeanServerConnection getConnection()
93 throws IOException
94 {
95 return getInstance().get();
96 }
97
98 public static JMXMonitor getInstance()
99 {
100 return __monitor;
101 }
102
103 public static boolean addMonitorActions(MonitorAction... actions)
104 {
105 return getInstance().add(actions);
106 }
107
108 public static boolean removeMonitorActions(MonitorAction... actions)
109 {
110 return getInstance().remove(actions);
111 }
112
113 public static void setServiceUrl(String url)
114 {
115 getInstance().set(url);
116 }
117
118
119
120
121
122
123
124
125 public static MBeanServerConnection getServiceConnection()
126 throws IOException
127 {
128 return getInstance().getConnection();
129 }
130
131 public static void main(final String args[]) throws Exception
132 {
133 XmlConfiguration.main(args);
134 }
135
136 private synchronized boolean add(MonitorAction... actions)
137 {
138 boolean result = true;
139
140 for (MonitorAction action : actions)
141 {
142 if (!_actions.add(action))
143 {
144 result = false;
145 }
146 else
147 {
148 MonitorTask.schedule(action);
149 }
150 }
151
152 return result;
153 }
154
155 private synchronized boolean remove(MonitorAction... actions)
156 {
157 boolean result = true;
158
159 for (MonitorAction action : actions)
160 {
161 if (!_actions.remove(action))
162 {
163 result = false;
164 }
165
166 MonitorTask.cancel(action);
167 }
168
169 return result;
170 }
171
172 private synchronized void set(String url)
173 {
174 _serverUrl = url;
175
176 if (_serviceConnection != null)
177 {
178 _serviceConnection.disconnect();
179 _serviceConnection = null;
180 }
181 }
182
183 private synchronized MBeanServerConnection get()
184 throws IOException
185 {
186 if (_serviceConnection == null)
187 {
188 _serviceConnection = new ServiceConnection(_serverUrl);
189 _serviceConnection.connect();
190 }
191
192 return _serviceConnection.getConnection();
193 }
194 }