1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.monitoring.share.server;
25 
26 import javax.management.*;
27 import javax.management.openmbean.CompositeData;
28 import java.lang.management.*;
29 import nsk.share.*;
30 import nsk.monitoring.share.*;
31 import java.lang.reflect.Method;
32 import java.lang.reflect.Array;
33 import java.lang.reflect.InvocationTargetException;
34 
35 public class ServerMXBean {
36         protected MBeanServer mbeanServer;
37         protected ObjectName objectName;
38 
ServerMXBean(MBeanServer mbeanServer, String name)39         public ServerMXBean(MBeanServer mbeanServer, String name) {
40                 this.mbeanServer = mbeanServer;
41                 try {
42                         this.objectName = new ObjectName(name);
43                 } catch (Exception e) {
44                         throw Monitoring.convertException(e);
45                 }
46         }
47 
ServerMXBean(MBeanServer mbeanServer, ObjectName objectName)48         public ServerMXBean(MBeanServer mbeanServer, ObjectName objectName) {
49                 this.mbeanServer = mbeanServer;
50                 this.objectName = objectName;
51         }
52 
convertException(Exception e)53         protected RuntimeException convertException(Exception e) {
54                 //e.printStackTrace(logger.getOutStream());
55                 return new Failure(e);
56         }
57 
58         /**
59          * Retrieves the <code>int</code> value of the specified attribute
60          * from MBeanServer.
61          *
62          * @param object MBean's <code>ObjectName</code>
63          * @param name name of the attribute.
64          *
65          * @return value of the attribute.
66          */
getIntAttribute(String name)67         protected int getIntAttribute(String name) {
68                 try {
69                         Integer i = (Integer) mbeanServer.getAttribute(objectName, name);
70                         return i.intValue();
71                 } catch (Exception e) {
72                         throw convertException(e);
73                 }
74         }
75 
76         /**
77          * Retrieves the <code>long</code> value of the specified attribute
78          * from MBeanServer.
79          *
80          * @param object MBean's <code>ObjectName</code>
81          * @param name name of the attribute.
82          *
83          * @return value of the attribute.
84          */
getLongAttribute(String name)85         protected long getLongAttribute(String name) {
86                 try {
87                         Long l = (Long) mbeanServer.getAttribute(objectName, name);
88                         return l.longValue();
89                 } catch (Exception e) {
90                         throw convertException(e);
91                 }
92         }
93 
94         /**
95          * Sets the value of the specified <code>long</code> attribute
96          * from MBeanServer.
97          *
98          * @param object MBean's <code>ObjectName</code>
99          * @param name name of the attribute.
100          *
101          * @return value of the attribute.
102          */
setLongAttribute(String name, long value)103         protected void setLongAttribute(String name, long value) {
104                 Attribute attribute = new Attribute(name, new Long(value));
105                 try {
106                         mbeanServer.setAttribute(objectName, attribute);
107                 } catch (Exception e) {
108                         throw convertException(e);
109                 }
110         }
111 
112 
113         /**
114          * Sets the <code>boolean</code> value to the specified attribute from
115          * MBeanServer.
116          *
117          * @param object MBean's <code>ObjectName</code>
118          * @param name name of the attribute.
119          * @param value value of the attribute.
120          */
setBooleanAttribute(String name, boolean value)121         protected void setBooleanAttribute(String name, boolean value) {
122                 Attribute attribute = new Attribute(name, new Boolean(value));
123                 try {
124                         mbeanServer.setAttribute(objectName, attribute);
125                 } catch (Exception e) {
126                         throw convertException(e);
127                 }
128         }
129 
130         /**
131          * Retrieves the <code>boolean</code> value of the specified attribute
132          * from MBeanServer.
133          *
134          * @param object MBean's <code>ObjectName</code>
135          * @param name name of the attribute.
136          *
137          * @return value of the attribute.
138          */
getBooleanAttribute(String name)139         protected boolean getBooleanAttribute(String name) {
140                 try {
141                         Boolean b = (Boolean) mbeanServer.getAttribute(objectName, name);
142                         return b.booleanValue();
143                 } catch (Exception e) {
144                         throw convertException(e);
145                 }
146         }
147 
148         /**
149          * Retrieves the <code>String</code> value of the specified attribute
150          * from MBeanServer.
151          *
152          * @param object MBean's <code>ObjectName</code>
153          * @param name name of the attribute.
154          *
155          * @return value of the attribute.
156          */
getStringAttribute(String name)157         protected String getStringAttribute(String name) {
158                 try {
159                         String s = (String) mbeanServer.getAttribute(objectName, name);
160                         return s;
161                 } catch (Exception e) {
162                         throw convertException(e);
163                 }
164         }
165 
166         /**
167          * Retrieves the <code>String</code> value of the specified attribute
168          * from MBeanServer.
169          *
170          * @param object MBean's <code>ObjectName</code>
171          * @param name name of the attribute.
172          *
173          * @return value of the attribute.
174          */
getStringArrayAttribute(String name)175         protected String[] getStringArrayAttribute(String name) {
176                 try {
177                         String[] s = (String[]) mbeanServer.getAttribute(objectName, name);
178                         return s;
179                 } catch (Exception e) {
180                         throw convertException(e);
181                 }
182         }
183 
184         /**
185          * Retrieves the <code>MemoryUsage</code> value of the specified attribute
186          * from MBeanServer.
187          *
188          * @param object MBean's <code>ObjectName</code>
189          * @param name name of the attribute.
190          *
191          * @return value of the attribute.
192          */
getMemoryUsageAttribute(String name)193         protected MemoryUsage getMemoryUsageAttribute(String name) {
194                 try {
195                         Object data = mbeanServer.getAttribute(objectName, name);
196                         if (data instanceof MemoryUsage)
197                                 return (MemoryUsage) data;
198                         return MemoryUsage.from((CompositeData) data);
199                 } catch (Exception e) {
200                         throw convertException(e);
201                 }
202         }
203 
204         /**
205          * Retrieves the <code>MemoryType</code> value of the specified attribute
206          * from MBeanServer.
207          *
208          * @param object MBean's <code>ObjectName</code>
209          * @param name name of the attribute.
210          *
211          * @return value of the attribute.
212          */
getMemoryTypeAttribute(String name)213         protected MemoryType getMemoryTypeAttribute(String name) {
214                 try {
215                         Object data = mbeanServer.getAttribute(objectName, name);
216                         return (MemoryType) data;
217                 } catch (Exception e) {
218                         throw convertException(e);
219                 }
220         }
221 
convertArray(Object o, Class<T[]> cl)222         protected<T> T[] convertArray(Object o, Class<T[]> cl) {
223                 if (cl.isInstance(o))
224                         return (T[]) o;
225                 else {
226                         CompositeData[] data = (CompositeData[]) o;
227                         Class<?> ccl = cl.getComponentType();
228                         T[] t = (T[]) Array.newInstance(ccl, data.length);
229                         for (int i = 0; i < t.length; ++i)
230                                 t[i] = (T) convertObject(data[i], ccl);
231                         return t;
232                 }
233         }
234 
convertObject(Object o, Class<T> cl)235         protected<T> T convertObject(Object o, Class<T> cl) {
236                 if (cl.isInstance(o))
237                         return (T) o;
238                 else {
239                         try {
240                                 Method method = cl.getMethod("from", CompositeData.class);
241                                 return (T) method.invoke(null, o);
242                         } catch (NoSuchMethodException e) {
243                                 throw Monitoring.convertException(e);
244                         } catch (IllegalAccessException e) {
245                                 throw Monitoring.convertException(e);
246                         } catch (InvocationTargetException e) {
247                                 throw Monitoring.convertException(e);
248                         }
249                 }
250         }
251 
invokeVoidMethod(String name)252         protected void invokeVoidMethod(String name) {
253                 invokeMethod(name, new Object[0], null);
254         }
255 
invokeMethod(String name, Object[] params, String[] signature)256         protected Object invokeMethod(String name, Object[] params, String[] signature) {
257                 try {
258                         return mbeanServer.invoke(objectName, name, params, signature);
259                 } catch (InstanceNotFoundException e) {
260                         throw Monitoring.convertException(e);
261                 } catch (MBeanException e) {
262                         throw Monitoring.convertException(e);
263                 } catch (ReflectionException e) {
264                         throw Monitoring.convertException(e);
265                 }
266         }
267 
getObjectName()268         public ObjectName getObjectName() {
269                 return null;
270         }
271 }
272