jdk/src/share/classes/sun/management/ManagementFactoryHelper.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 401 ef01e0dccd63
child 1226 eba24d3b9c0c
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 401
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.logging.LogManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.ObjectInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.InstanceAlreadyExistsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanRegistrationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.StandardEmitterMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.StandardMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.action.LoadLibraryAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import com.sun.management.OSMBeanFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import com.sun.management.HotSpotDiagnosticMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import static java.lang.management.ManagementFactory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    63
 * ManagementFactoryHelper provides static factory methods to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * instances of the management interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    66
public class ManagementFactoryHelper {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    67
    private ManagementFactoryHelper() {};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static VMManagement jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static boolean mbeansCreated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static ClassLoadingImpl    classMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static MemoryImpl          memoryMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static ThreadImpl          threadMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static RuntimeImpl         runtimeMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static CompilationImpl     compileMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static OperatingSystemImpl osMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (classMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            classMBean = new ClassLoadingImpl(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return classMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static synchronized MemoryMXBean getMemoryMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (memoryMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            memoryMBean = new MemoryImpl(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return memoryMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static synchronized ThreadMXBean getThreadMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (threadMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            threadMBean = new ThreadImpl(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return threadMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static synchronized RuntimeMXBean getRuntimeMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (runtimeMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            runtimeMBean = new RuntimeImpl(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return runtimeMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static synchronized CompilationMXBean getCompilationMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (compileMBean == null && jvm.getCompilerName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            compileMBean = new CompilationImpl(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return compileMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (osMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            osMBean = (OperatingSystemImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                          OSMBeanFactory.getOperatingSystemMXBean(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return osMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        List<MemoryPoolMXBean> list = new ArrayList<MemoryPoolMXBean>(pools.length);
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   125
        for (MemoryPoolMXBean p : pools) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            list.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length);
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   134
        for (MemoryManagerMXBean m : mgrs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            result.add(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length);
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   143
        for (MemoryManagerMXBean m : mgrs) {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   144
            if (GarbageCollectorMXBean.class.isInstance(m)) {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   145
                 result.add(GarbageCollectorMXBean.class.cast(m));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static HotSpotDiagnostic hsDiagMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static HotspotRuntime hsRuntimeMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static HotspotClassLoading hsClassMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static HotspotThread hsThreadMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static HotspotCompilation hsCompileMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static HotspotMemory hsMemoryMBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (hsDiagMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            hsDiagMBean = new HotSpotDiagnostic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return hsDiagMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * This method is for testing only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (hsRuntimeMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            hsRuntimeMBean = new HotspotRuntime(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return hsRuntimeMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * This method is for testing only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (hsClassMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            hsClassMBean = new HotspotClassLoading(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return hsClassMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This method is for testing only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (hsThreadMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            hsThreadMBean = new HotspotThread(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return hsThreadMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * This method is for testing only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (hsMemoryMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            hsMemoryMBean = new HotspotMemory(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return hsMemoryMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This method is for testing only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (hsCompileMBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            hsCompileMBean = new HotspotCompilation(jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return hsCompileMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   218
     * Registers a given MBean if not registered in the MBeanServer;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   219
     * otherwise, just return.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   223
            final ObjectName objName = Util.newObjectName(mbeanName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // inner class requires these fields to be final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            final MBeanServer mbs0 = mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            final Object mbean0 = mbean;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   228
            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   229
                public Void run() throws MBeanRegistrationException,
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   230
                                         NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    try {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   232
                        mbs0.registerMBean(mbean0, objName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    } catch (InstanceAlreadyExistsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        // if an instance with the object name exists in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        // the MBeanServer ignore the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw Util.newException(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        "sun.management:type=HotspotClassLoading";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        "sun.management:type=HotspotCompilation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private final static String HOTSPOT_MEMORY_MBEAN_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        "sun.management:type=HotspotMemory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        "sun.management:type=HotspotRuntime";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private final static String HOTSPOT_THREAD_MBEAN_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        "sun.management:type=HotspotThreading";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static void registerInternalMBeans(MBeanServer mbs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // register all internal MBeans if not registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // No exception is thrown if a MBean with that object name
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   264
        // already registered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        addMBean(mbs, getHotspotClassLoadingMBean(),
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   266
            HOTSPOT_CLASS_LOADING_MBEAN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        addMBean(mbs, getHotspotMemoryMBean(),
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   268
            HOTSPOT_MEMORY_MBEAN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        addMBean(mbs, getHotspotRuntimeMBean(),
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   270
            HOTSPOT_RUNTIME_MBEAN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        addMBean(mbs, getHotspotThreadMBean(),
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   272
            HOTSPOT_THREAD_MBEAN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // CompilationMBean may not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (getCompilationMXBean() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            addMBean(mbs, getHotspotCompilationMBean(),
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   277
                HOTSPOT_COMPILATION_MBEAN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private static void unregisterMBean(MBeanServer mbs, String mbeanName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   283
            final ObjectName objName = Util.newObjectName(mbeanName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // inner class requires these fields to be final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            final MBeanServer mbs0 = mbs;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   287
            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   288
                public Void run() throws MBeanRegistrationException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                           RuntimeOperationsException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        mbs0.unregisterMBean(objName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    } catch (InstanceNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        // ignore exception if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw Util.newException(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    static void unregisterInternalMBeans(MBeanServer mbs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // unregister all internal MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // CompilationMBean may not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (getCompilationMXBean() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        AccessController.doPrivileged(new LoadLibraryAction("management"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        jvm = new VMManagementImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static boolean isThreadSuspended(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public static boolean isThreadRunningNative(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public static Thread.State toThreadState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // suspended and native bits may be set in state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return sun.misc.VM.toThreadState(threadStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // These values are defined in jmm.h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}