1 /*
2  * Copyright (c) 2003, 2006, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package sun.management.snmp.jvminstr;
26 
27 // java imports
28 //
29 import com.sun.jmx.mbeanserver.Util;
30 import java.io.Serializable;
31 import java.lang.management.RuntimeMXBean;
32 import java.lang.management.ManagementFactory;
33 import java.util.List;
34 import java.util.Map;
35 
36 // jmx imports
37 //
38 import javax.management.MBeanServer;
39 import com.sun.jmx.snmp.SnmpString;
40 import com.sun.jmx.snmp.SnmpStatusException;
41 
42 // jdmk imports
43 //
44 import com.sun.jmx.snmp.agent.SnmpMib;
45 
46 import sun.management.snmp.jvmmib.JvmRuntimeMBean;
47 import sun.management.snmp.jvmmib.EnumJvmRTBootClassPathSupport;
48 import sun.management.snmp.util.JvmContextFactory;
49 
50 /**
51  * The class is used for implementing the "JvmRuntime" group.
52  */
53 public class JvmRuntimeImpl implements JvmRuntimeMBean {
54 
55     /**
56      * Variable for storing the value of "JvmRTBootClassPathSupport".
57      *
58      * "Indicates whether the Java virtual machine supports the
59      * boot class path mechanism used by the system class loader
60      * to search for class files.
61      *
62      * See java.management.RuntimeMXBean.isBootClassPathSupported()
63      * "
64      *
65      */
66     static final EnumJvmRTBootClassPathSupport
67         JvmRTBootClassPathSupportSupported =
68         new EnumJvmRTBootClassPathSupport("supported");
69     static final EnumJvmRTBootClassPathSupport
70         JvmRTBootClassPathSupportUnSupported =
71         new EnumJvmRTBootClassPathSupport("unsupported");
72 
73     /**
74      * Constructor for the "JvmRuntime" group.
75      * If the group contains a table, the entries created through an SNMP SET
76      * will not be registered in Java DMK.
77      */
JvmRuntimeImpl(SnmpMib myMib)78     public JvmRuntimeImpl(SnmpMib myMib) {
79 
80     }
81 
82 
83     /**
84      * Constructor for the "JvmRuntime" group.
85      * If the group contains a table, the entries created through an SNMP SET
86      * will be AUTOMATICALLY REGISTERED in Java DMK.
87      */
JvmRuntimeImpl(SnmpMib myMib, MBeanServer server)88     public JvmRuntimeImpl(SnmpMib myMib, MBeanServer server) {
89 
90     }
91 
getRuntimeMXBean()92     static RuntimeMXBean getRuntimeMXBean() {
93         return ManagementFactory.getRuntimeMXBean();
94     }
95 
validDisplayStringTC(String str)96     private static String validDisplayStringTC(String str) {
97         return JVM_MANAGEMENT_MIB_IMPL.validDisplayStringTC(str);
98     }
99 
validPathElementTC(String str)100     private static String validPathElementTC(String str) {
101         return JVM_MANAGEMENT_MIB_IMPL.validPathElementTC(str);
102     }
103 
validJavaObjectNameTC(String str)104     private static String validJavaObjectNameTC(String str) {
105         return JVM_MANAGEMENT_MIB_IMPL.validJavaObjectNameTC(str);
106     }
107 
108 
splitPath(String path)109     static String[] splitPath(String path) {
110         final String[] items = path.split(java.io.File.pathSeparator);
111         // for (int i=0;i<items.length;i++) {
112         //    items[i]=validPathElementTC(items[i]);
113         // }
114         return items;
115     }
116 
getClassPath(Object userData)117     static String[] getClassPath(Object userData) {
118         final Map<Object, Object> m =
119                 Util.cast((userData instanceof Map)?userData:null);
120         final String tag = "JvmRuntime.getClassPath";
121 
122         // If the list is in the cache, simply return it.
123         //
124         if (m != null) {
125             final String[] cached = (String[])m.get(tag);
126             if (cached != null) return cached;
127         }
128 
129         final String[] args = splitPath(getRuntimeMXBean().getClassPath());
130 
131         if (m != null) m.put(tag,args);
132         return args;
133     }
134 
getBootClassPath(Object userData)135     static String[] getBootClassPath(Object userData) {
136         if (!getRuntimeMXBean().isBootClassPathSupported())
137         return new String[0];
138 
139         final Map<Object, Object> m =
140                 Util.cast((userData instanceof Map)?userData:null);
141         final String tag = "JvmRuntime.getBootClassPath";
142 
143         // If the list is in the cache, simply return it.
144         //
145         if (m != null) {
146             final String[] cached = (String[])m.get(tag);
147             if (cached != null) return cached;
148         }
149 
150         final String[] args = splitPath(getRuntimeMXBean().getBootClassPath());
151 
152         if (m != null) m.put(tag,args);
153         return args;
154     }
155 
getLibraryPath(Object userData)156     static String[] getLibraryPath(Object userData) {
157         final Map<Object, Object> m =
158                 Util.cast((userData instanceof Map)?userData:null);
159         final String tag = "JvmRuntime.getLibraryPath";
160 
161         // If the list is in the cache, simply return it.
162         //
163         if (m != null) {
164             final String[] cached = (String[])m.get(tag);
165             if (cached != null) return cached;
166         }
167 
168         final String[] args = splitPath(getRuntimeMXBean().getLibraryPath());
169 
170         if (m != null) m.put(tag,args);
171         return args;
172     }
173 
getInputArguments(Object userData)174     static String[] getInputArguments(Object userData) {
175         final Map<Object, Object> m =
176                 Util.cast((userData instanceof Map)?userData:null);
177         final String tag = "JvmRuntime.getInputArguments";
178 
179         // If the list is in the cache, simply return it.
180         //
181         if (m != null) {
182             final String[] cached = (String[])m.get(tag);
183             if (cached != null) return cached;
184         }
185 
186         final List<String> l = getRuntimeMXBean().getInputArguments();
187         final String[] args = l.toArray(new String[0]);
188 
189         if (m != null) m.put(tag,args);
190         return args;
191     }
192 
193     /**
194      * Getter for the "JvmRTSpecVendor" variable.
195      */
getJvmRTSpecVendor()196     public String getJvmRTSpecVendor() throws SnmpStatusException {
197         return validDisplayStringTC(getRuntimeMXBean().getSpecVendor());
198     }
199 
200     /**
201      * Getter for the "JvmRTSpecName" variable.
202      */
getJvmRTSpecName()203     public String getJvmRTSpecName() throws SnmpStatusException {
204         return validDisplayStringTC(getRuntimeMXBean().getSpecName());
205     }
206 
207     /**
208      * Getter for the "JvmRTVersion" variable.
209      */
getJvmRTVMVersion()210     public String getJvmRTVMVersion() throws SnmpStatusException {
211         return validDisplayStringTC(getRuntimeMXBean().getVmVersion());
212     }
213 
214     /**
215      * Getter for the "JvmRTVendor" variable.
216      */
getJvmRTVMVendor()217     public String getJvmRTVMVendor() throws SnmpStatusException {
218         return validDisplayStringTC(getRuntimeMXBean().getVmVendor());
219     }
220 
221     /**
222      * Getter for the "JvmRTManagementSpecVersion" variable.
223      */
getJvmRTManagementSpecVersion()224     public String getJvmRTManagementSpecVersion() throws SnmpStatusException {
225         return validDisplayStringTC(getRuntimeMXBean().
226                                     getManagementSpecVersion());
227     }
228 
229     /**
230      * Getter for the "JvmRTVMName" variable.
231      */
getJvmRTVMName()232     public String getJvmRTVMName() throws SnmpStatusException {
233         return validJavaObjectNameTC(getRuntimeMXBean().getVmName());
234     }
235 
236 
237     /**
238      * Getter for the "JvmRTInputArgsCount" variable.
239      */
getJvmRTInputArgsCount()240     public Integer getJvmRTInputArgsCount() throws SnmpStatusException {
241 
242         final String[] args = getInputArguments(JvmContextFactory.
243                                                 getUserData());
244         return new Integer(args.length);
245     }
246 
247     /**
248      * Getter for the "JvmRTBootClassPathSupport" variable.
249      */
getJvmRTBootClassPathSupport()250     public EnumJvmRTBootClassPathSupport getJvmRTBootClassPathSupport()
251         throws SnmpStatusException {
252         if(getRuntimeMXBean().isBootClassPathSupported())
253             return JvmRTBootClassPathSupportSupported;
254         else
255             return JvmRTBootClassPathSupportUnSupported;
256     }
257 
258     /**
259      * Getter for the "JvmRTUptimeMs" variable.
260      */
getJvmRTUptimeMs()261     public Long getJvmRTUptimeMs() throws SnmpStatusException {
262         return new Long(getRuntimeMXBean().getUptime());
263     }
264 
265     /**
266      * Getter for the "JvmRTStartTimeMs" variable.
267      */
getJvmRTStartTimeMs()268     public Long getJvmRTStartTimeMs() throws SnmpStatusException {
269         return new Long(getRuntimeMXBean().getStartTime());
270     }
271 
272     /**
273      * Getter for the "JvmRTSpecVersion" variable.
274      */
getJvmRTSpecVersion()275     public String getJvmRTSpecVersion() throws SnmpStatusException {
276         return validDisplayStringTC(getRuntimeMXBean().getSpecVersion());
277     }
278 
279     /**
280      * Getter for the "JvmRTName" variable.
281      */
getJvmRTName()282     public String getJvmRTName() throws SnmpStatusException {
283         return validDisplayStringTC(getRuntimeMXBean().getName());
284     }
285 
286 }
287