1
2
3
4
5 package oshi.demo.jmx;
6
7 import java.io.IOException;
8 import java.io.ObjectInputStream;
9 import java.rmi.registry.LocateRegistry;
10 import java.util.Map;
11 import java.util.Set;
12
13 import javax.management.Attribute;
14 import javax.management.AttributeList;
15 import javax.management.AttributeNotFoundException;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.IntrospectionException;
19 import javax.management.InvalidAttributeValueException;
20 import javax.management.ListenerNotFoundException;
21 import javax.management.MBeanException;
22 import javax.management.MBeanInfo;
23 import javax.management.MBeanRegistrationException;
24 import javax.management.MBeanServer;
25 import javax.management.MBeanServerFactory;
26 import javax.management.MalformedObjectNameException;
27 import javax.management.NotCompliantMBeanException;
28 import javax.management.NotificationFilter;
29 import javax.management.NotificationListener;
30 import javax.management.ObjectInstance;
31 import javax.management.ObjectName;
32 import javax.management.OperationsException;
33 import javax.management.QueryExp;
34 import javax.management.ReflectionException;
35 import javax.management.loading.ClassLoaderRepository;
36 import javax.management.remote.JMXConnectorServer;
37 import javax.management.remote.JMXConnectorServerFactory;
38 import javax.management.remote.JMXServiceURL;
39
40 import oshi.SystemInfo;
41 import oshi.annotation.SuppressForbidden;
42 import oshi.demo.jmx.api.JMXOshiAgent;
43 import oshi.demo.jmx.api.StrategyRegistrationPlatformMBeans;
44 import oshi.demo.jmx.strategiesplatform.WindowsStrategyRegistrattionPlatform;
45
46 public class JMXOshiAgentServer implements JMXOshiAgent {
47 private String host;
48 private Integer port;
49 private Map<String, ?> properties;
50
51 private MBeanServer server;
52 private JMXServiceURL address;
53 private JMXConnectorServer cntorServer;
54 private ContextRegistrationPlatform contextRegistrationPlatform;
55 private static JMXOshiAgentServer jmxOshiAgentServer;
56
57 private JMXOshiAgentServer(Integer port, String host, Map<String, ?> properties,
58 ContextRegistrationPlatform platform) throws Exception {
59
60 this.port = port;
61 this.host = host;
62 this.properties = properties;
63 this.contextRegistrationPlatform = platform;
64
65 this.initilizeMbeanServer();
66 this.initilizeMBeans();
67 }
68
69
70 protected static JMXOshiAgentServer getInstance(String host, Integer port, Map<String, ?> properties,
71 ContextRegistrationPlatform platform) throws Exception {
72 if (jmxOshiAgentServer == null) {
73 jmxOshiAgentServer = new JMXOshiAgentServer(port, host, properties, platform);
74 }
75 return jmxOshiAgentServer;
76 }
77
78
79
80
81 private void initilizeMbeanServer() throws IOException, NotCompliantMBeanException, InstanceAlreadyExistsException,
82 MBeanRegistrationException, MalformedObjectNameException {
83
84 if (LocateRegistry.getRegistry(this.port) != null)
85 LocateRegistry.createRegistry(this.port);
86
87 address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + this.host + ":" + this.port + "/server");
88 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, this.properties, null);
89 server = MBeanServerFactory.createMBeanServer();
90 ObjectName cntorServerName = ObjectName.getInstance("connectors:protocol=rmi");
91 server.registerMBean(cntorServer, cntorServerName);
92 }
93
94
95
96
97
98 @SuppressForbidden(reason = "Using System.out in a demo class")
99 private void initilizeMBeans() throws Exception {
100
101 StrategyRegistrationPlatformMBeans platformMBeans = null;
102 SystemInfo si = new SystemInfo();
103
104 switch (SystemInfo.getCurrentPlatform()) {
105 case WINDOWS:
106 platformMBeans = new WindowsStrategyRegistrattionPlatform();
107 contextRegistrationPlatform.setStrategyRegistrationContext(platformMBeans);
108 break;
109 default:
110 System.out.println("Couldn't Initialize server ");
111 throw new Exception("Server could not be initialized");
112 }
113
114 platformMBeans.registerMBeans(si, this.server);
115 }
116
117 @SuppressForbidden(reason = "Using System.out in a demo class")
118 @Override
119 public void startAgent() throws IOException {
120 cntorServer.start();
121 System.out.println("Server Started");
122 }
123
124 @Override
125 public void stopAgent() throws IOException {
126 cntorServer.stop();
127 }
128
129 @Override
130 public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException,
131 InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException {
132 return null;
133 }
134
135 @Override
136 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName)
137 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
138 NotCompliantMBeanException, InstanceNotFoundException {
139 return null;
140 }
141
142 @Override
143 public ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature)
144 throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
145 NotCompliantMBeanException {
146 return null;
147 }
148
149 @Override
150 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params,
151 String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException,
152 MBeanException, NotCompliantMBeanException, InstanceNotFoundException {
153 return null;
154 }
155
156 @Override
157 public ObjectInstance registerMBean(Object object, ObjectName name)
158 throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
159 return server.registerMBean(object, name);
160 }
161
162 @Override
163 public void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException {
164
165 }
166
167 @Override
168 public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException {
169 return null;
170 }
171
172 @Override
173 public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
174 return null;
175 }
176
177 @Override
178 public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
179 return null;
180 }
181
182 @Override
183 public boolean isRegistered(ObjectName name) {
184 return false;
185 }
186
187 @Override
188 public Integer getMBeanCount() {
189 return null;
190 }
191
192 @Override
193 public Object getAttribute(ObjectName name, String attribute)
194 throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
195 return null;
196 }
197
198 @Override
199 public AttributeList getAttributes(ObjectName name, String[] attributes)
200 throws InstanceNotFoundException, ReflectionException {
201 return null;
202 }
203
204 @Override
205 public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException,
206 AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
207
208 }
209
210 @Override
211 public AttributeList setAttributes(ObjectName name, AttributeList attributes)
212 throws InstanceNotFoundException, ReflectionException {
213 return null;
214 }
215
216 @Override
217 public Object invoke(ObjectName name, String operationName, Object[] params, String[] signature)
218 throws InstanceNotFoundException, MBeanException, ReflectionException {
219 return null;
220 }
221
222 @Override
223 public String getDefaultDomain() {
224 return null;
225 }
226
227 @Override
228 public String[] getDomains() {
229 return new String[0];
230 }
231
232 @Override
233 public void addNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter,
234 Object handback) throws InstanceNotFoundException {
235
236 }
237
238 @Override
239 public void addNotificationListener(ObjectName name, ObjectName listener, NotificationFilter filter,
240 Object handback) throws InstanceNotFoundException {
241
242 }
243
244 @Override
245 public void removeNotificationListener(ObjectName name, ObjectName listener)
246 throws InstanceNotFoundException, ListenerNotFoundException {
247
248 }
249
250 @Override
251 public void removeNotificationListener(ObjectName name, ObjectName listener, NotificationFilter filter,
252 Object handback) throws InstanceNotFoundException, ListenerNotFoundException {
253
254 }
255
256 @Override
257 public void removeNotificationListener(ObjectName name, NotificationListener listener)
258 throws InstanceNotFoundException, ListenerNotFoundException {
259
260 }
261
262 @Override
263 public void removeNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter,
264 Object handback) throws InstanceNotFoundException, ListenerNotFoundException {
265
266 }
267
268 @Override
269 public MBeanInfo getMBeanInfo(ObjectName name)
270 throws InstanceNotFoundException, IntrospectionException, ReflectionException {
271 return null;
272 }
273
274 @Override
275 public boolean isInstanceOf(ObjectName name, String className) throws InstanceNotFoundException {
276 return false;
277 }
278
279 @Override
280 public Object instantiate(String className) throws ReflectionException, MBeanException {
281 return null;
282 }
283
284 @Override
285 public Object instantiate(String className, ObjectName loaderName)
286 throws ReflectionException, MBeanException, InstanceNotFoundException {
287 return null;
288 }
289
290 @Override
291 public Object instantiate(String className, Object[] params, String[] signature)
292 throws ReflectionException, MBeanException {
293 return null;
294 }
295
296 @Override
297 public Object instantiate(String className, ObjectName loaderName, Object[] params, String[] signature)
298 throws ReflectionException, MBeanException, InstanceNotFoundException {
299 return null;
300 }
301
302 @Override
303 public ObjectInputStream deserialize(ObjectName name, byte[] data)
304 throws InstanceNotFoundException, OperationsException {
305 return null;
306 }
307
308 @Override
309 public ObjectInputStream deserialize(String className, byte[] data)
310 throws OperationsException, ReflectionException {
311 return null;
312 }
313
314 @Override
315 public ObjectInputStream deserialize(String className, ObjectName loaderName, byte[] data)
316 throws InstanceNotFoundException, OperationsException, ReflectionException {
317 return null;
318 }
319
320 @Override
321 public ClassLoader getClassLoaderFor(ObjectName mbeanName) throws InstanceNotFoundException {
322 return null;
323 }
324
325 @Override
326 public ClassLoader getClassLoader(ObjectName loaderName) throws InstanceNotFoundException {
327 return null;
328 }
329
330 @Override
331 public ClassLoaderRepository getClassLoaderRepository() {
332 return null;
333 }
334 }