jdk/src/share/classes/sun/management/Util.java
changeset 401 ef01e0dccd63
parent 2 90ce3da70b43
child 715 f16baef3a20e
equal deleted inserted replaced
399:bcc2354430ff 401:ef01e0dccd63
    24  */
    24  */
    25 
    25 
    26 package sun.management;
    26 package sun.management;
    27 
    27 
    28 import java.lang.management.*;
    28 import java.lang.management.*;
       
    29 import java.util.List;
       
    30 import java.security.Permission;
       
    31 import javax.management.ObjectName;
       
    32 import javax.management.MalformedObjectNameException;
       
    33 
    29 import static java.lang.management.ManagementFactory.*;
    34 import static java.lang.management.ManagementFactory.*;
    30 import java.util.List;
       
    31 
    35 
    32 class Util {
    36 class Util {
    33     static String getMBeanObjectName(MemoryPoolMXBean pool) {
    37     static RuntimeException newException(Exception e) {
    34         return MEMORY_POOL_MXBEAN_DOMAIN_TYPE +
    38         throw new RuntimeException(e);
    35             ",name=" + pool.getName();
       
    36     }
    39     }
    37 
    40 
    38     static String getMBeanObjectName(MemoryManagerMXBean mgr) {
    41     private static final String[] EMPTY_STRING_ARRAY = new String[0];
    39         if (mgr instanceof GarbageCollectorMXBean) {
    42     static String[] toStringArray(List<String> list) {
    40             return getMBeanObjectName((GarbageCollectorMXBean) mgr);
    43         return (String[]) list.toArray(EMPTY_STRING_ARRAY);
    41         } else {
    44     }
    42             return MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE +
    45 
    43                 ",name=" + mgr.getName();
    46     static ObjectName newObjectName(String name) {
       
    47         return com.sun.jmx.mbeanserver.Util.newObjectName(name);
       
    48     }
       
    49 
       
    50     public static ObjectName newObjectName(String domainAndType, String name) {
       
    51         return newObjectName(domainAndType + ",name=" + name);
       
    52     }
       
    53 
       
    54     private static ManagementPermission monitorPermission =
       
    55         new ManagementPermission("monitor");
       
    56     private static ManagementPermission controlPermission =
       
    57         new ManagementPermission("control");
       
    58 
       
    59     /**
       
    60      * Check that the current context is trusted to perform monitoring
       
    61      * or management.
       
    62      * <p>
       
    63      * If the check fails we throw a SecurityException, otherwise
       
    64      * we return normally.
       
    65      *
       
    66      * @exception  SecurityException  if a security manager exists and if
       
    67      *             the caller does not have ManagementPermission("control").
       
    68      */
       
    69     static void checkAccess(ManagementPermission p)
       
    70          throws SecurityException {
       
    71         SecurityManager sm = System.getSecurityManager();
       
    72         if (sm != null) {
       
    73             sm.checkPermission(p);
    44         }
    74         }
    45     }
    75     }
    46 
    76 
    47     static String getMBeanObjectName(GarbageCollectorMXBean gc) {
    77     static void checkMonitorAccess() throws SecurityException {
    48         return GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
    78         checkAccess(monitorPermission);
    49             ",name=" + gc.getName();
       
    50     }
    79     }
    51 
    80     static void checkControlAccess() throws SecurityException {
    52     static RuntimeException newException(Exception e) {
    81         checkAccess(controlPermission);
    53         RuntimeException e1 = new RuntimeException(e.getMessage());
       
    54         e1.initCause(e);
       
    55         return e1;
       
    56     }
       
    57 
       
    58     static InternalError newInternalError(Exception e) {
       
    59         InternalError e1 = new InternalError(e.getMessage());
       
    60         e1.initCause(e);
       
    61         return e1;
       
    62     }
       
    63     static AssertionError newAssertionError(Exception e) {
       
    64         AssertionError e1 = new AssertionError(e.getMessage());
       
    65         e1.initCause(e);
       
    66         return e1;
       
    67     }
       
    68 
       
    69     private static String[] EMPTY_STRING_ARRAY = new String[0];
       
    70     static String[] toStringArray(List<String> list) {
       
    71         return (String[]) list.toArray(EMPTY_STRING_ARRAY);
       
    72     }
    82     }
    73 }
    83 }