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