1 /*
2  * Copyright (c) 2003, 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;
25 
26 import java.lang.management.*;
27 import javax.management.*;
28 import nsk.share.*;
29 
30 /**
31  * <code>RuntimeMonitor</code> class is a wrapper of <code>RuntimeMXBean</code>.
32  * Depending on command line arguments, an instance of this class redirects
33  * invocations to the <code>RuntimeMXBean</code> interface. If
34  * <code>-testMode="directly"</code> option is set, this instance directly
35  * invokes corresponding method of the <code>RuntimeMXBean</code> interface. If
36  * <code>-testMode="server"</code> option is set it will make invocations via
37  * MBeanServer. If <code>-testMode="proxy"</code> option is set it will make
38  * invocations via MBeanServer proxy.
39  *
40  * @see ArgumentHandler
41  */
42 public class RuntimeMonitor extends Monitor {
43     // An instance of ClassLoadingMBean
44     private final static RuntimeMXBean mbean
45         = ManagementFactory.getRuntimeMXBean();
46 
47     // Name of an attribute of RuntimeMonitor
48     private static final String IS_BOOT = "BootClassPathSupported";
49 
50     private RuntimeMXBean proxyInstance;
51 
52     static {
53         Monitor.logPrefix = "RuntimeMonitor> ";
54     }
55 
56     /**
57      * Creates a new <code>RuntimeMonitor</code> object.
58      *
59      * @param log <code>Log</code> object to print info to.
60      * @param argumentHandler <code>ArgumentHandler</code> object that saves
61      *        all info about test's arguments.
62      *
63      */
RuntimeMonitor(Log log, ArgumentHandler argumentHandler)64     public RuntimeMonitor(Log log, ArgumentHandler argumentHandler) {
65         super(log, argumentHandler);
66     }
67 
68     /**
69      *
70      * Return a proxy instance for a platform
71      * {@link java.lang.management.RuntimeMXBean
72      * <code>RuntimeMXBean</code>} interface.
73      *
74      */
getProxy()75     RuntimeMXBean getProxy() {
76         if (proxyInstance == null) {
77             // create proxy instance
78             try {
79                 proxyInstance = (RuntimeMXBean)
80                 ManagementFactory.newPlatformMXBeanProxy(
81                     getMBeanServer(),
82                     ManagementFactory.RUNTIME_MXBEAN_NAME,
83                     RuntimeMXBean.class
84                 );
85             } catch (java.io.IOException e) {
86                 throw new Failure(e);
87             }
88         }
89         return proxyInstance;
90     }
91 
92     /**
93      * Redirects the invocation to
94      * {@link java.lang.management.RuntimeMXBean#isBootClassPathSupported()
95      * <code>RuntimeMXBean.isBootClassPathSupported()</code>}.
96      *
97      * @return <code>true</code>, if the JVM supports the class path mechanism;
98      *         <code>flase</code> otherwise.
99      */
isBootClassPathSupported()100     public boolean isBootClassPathSupported() {
101         int mode = getTestMode();
102 
103         switch (mode) {
104         case DIRECTLY_MODE:
105             return mbean.isBootClassPathSupported();
106 
107         case SERVER_MODE:
108             return getBooleanAttribute(mbeanObjectName, IS_BOOT);
109 
110         case PROXY_MODE:
111             return getProxy().isBootClassPathSupported();
112         }
113 
114         throw new TestBug("Unknown testMode " + mode);
115     }
116 } // RuntimeMonitor
117