jdk/src/share/classes/com/sun/jmx/mbeanserver/JmxMBeanServer.java
changeset 1156 bbc2d15aaf7a
parent 2 90ce3da70b43
child 1510 e747d3193ef2
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
     1 /*
     1 /*
     2  * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.jmx.mbeanserver;
    26 package com.sun.jmx.mbeanserver;
    27 
    27 
    28 import java.util.Iterator;
    28 import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
    29 import java.util.logging.Level;
    29 import com.sun.jmx.interceptor.NamespaceDispatchInterceptor;
    30 import java.util.Set;
    30 
    31 import java.io.ObjectInputStream;
    31 import java.io.ObjectInputStream;
    32 import java.security.AccessController;
    32 import java.security.AccessController;
    33 import java.security.Permission;
    33 import java.security.Permission;
    34 import java.security.PrivilegedExceptionAction;
    34 import java.security.PrivilegedExceptionAction;
    35 
    35 import java.util.Iterator;
    36 // RI import
    36 import java.util.Set;
    37 import javax.management.MBeanPermission;
    37 import java.util.logging.Level;
    38 import javax.management.AttributeNotFoundException;
    38 
    39 import javax.management.MBeanException;
       
    40 import javax.management.ReflectionException;
       
    41 import javax.management.MBeanInfo;
       
    42 import javax.management.QueryExp;
       
    43 import javax.management.NotificationListener;
       
    44 import javax.management.NotificationFilter;
       
    45 import javax.management.ListenerNotFoundException;
       
    46 import javax.management.IntrospectionException;
       
    47 import javax.management.OperationsException;
       
    48 import javax.management.InstanceNotFoundException;
       
    49 import javax.management.NotCompliantMBeanException;
       
    50 import javax.management.MBeanRegistrationException;
       
    51 import javax.management.InstanceAlreadyExistsException;
       
    52 import javax.management.InvalidAttributeValueException;
       
    53 import javax.management.ObjectName;
       
    54 import javax.management.ObjectInstance;
       
    55 import javax.management.Attribute;
    39 import javax.management.Attribute;
    56 import javax.management.AttributeList;
    40 import javax.management.AttributeList;
    57 import javax.management.RuntimeOperationsException;
    41 import javax.management.AttributeNotFoundException;
       
    42 import javax.management.InstanceAlreadyExistsException;
       
    43 import javax.management.InstanceNotFoundException;
       
    44 import javax.management.IntrospectionException;
       
    45 import javax.management.InvalidAttributeValueException;
       
    46 import javax.management.ListenerNotFoundException;
       
    47 import javax.management.MBeanException;
       
    48 import javax.management.MBeanInfo;
       
    49 import javax.management.MBeanPermission;
       
    50 import javax.management.MBeanRegistrationException;
    58 import javax.management.MBeanServer;
    51 import javax.management.MBeanServer;
    59 import javax.management.MBeanServerDelegate;
    52 import javax.management.MBeanServerDelegate;
       
    53 import javax.management.NotCompliantMBeanException;
       
    54 import javax.management.NotificationFilter;
       
    55 import javax.management.NotificationListener;
       
    56 import javax.management.ObjectInstance;
       
    57 import javax.management.ObjectName;
       
    58 import javax.management.OperationsException;
       
    59 import javax.management.QueryExp;
       
    60 import javax.management.ReflectionException;
       
    61 import javax.management.RuntimeOperationsException;
    60 import javax.management.loading.ClassLoaderRepository;
    62 import javax.management.loading.ClassLoaderRepository;
    61 
       
    62 import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
       
    63 import com.sun.jmx.interceptor.DefaultMBeanServerInterceptor;
       
    64 import com.sun.jmx.interceptor.MBeanServerInterceptor;
       
    65 
    63 
    66 /**
    64 /**
    67  * This is the base class for MBean manipulation on the agent side. It
    65  * This is the base class for MBean manipulation on the agent side. It
    68  * contains the methods necessary for the creation, registration, and
    66  * contains the methods necessary for the creation, registration, and
    69  * deletion of MBeans as well as the access methods for registered MBeans.
    67  * deletion of MBeans as well as the access methods for registered MBeans.
   100     private final SecureClassLoaderRepository secureClr;
    98     private final SecureClassLoaderRepository secureClr;
   101 
    99 
   102     /** true if interceptors are enabled **/
   100     /** true if interceptors are enabled **/
   103     private final boolean interceptorsEnabled;
   101     private final boolean interceptorsEnabled;
   104 
   102 
   105     /** Revisit: transient ??? **/
   103     private final MBeanServer outerShell;
   106     private final transient MBeanServer outerShell;
   104 
   107 
   105     private volatile MBeanServer mbsInterceptor = null;
   108     /** Revisit: transient ??? **/
   106 
   109     private transient MBeanServerInterceptor mbsInterceptor = null;
       
   110 
       
   111     /** Revisit: transient ??? **/
       
   112     /** The MBeanServerDelegate object representing the MBean Server */
   107     /** The MBeanServerDelegate object representing the MBean Server */
   113     private final transient MBeanServerDelegate mBeanServerDelegateObject;
   108     private final MBeanServerDelegate mBeanServerDelegateObject;
       
   109 
       
   110     private final String mbeanServerName;
   114 
   111 
   115     /**
   112     /**
   116      * <b>Package:</b> Creates an MBeanServer with the
   113      * <b>Package:</b> Creates an MBeanServer with the
   117      * specified default domain name, outer interface, and delegate.
   114      * specified default domain name, outer interface, and delegate.
   118      * <p>The default domain name is used as the domain part in the ObjectName
   115      * <p>The default domain name is used as the domain part in the ObjectName
   241         this.mBeanServerDelegateObject = delegate;
   238         this.mBeanServerDelegateObject = delegate;
   242         this.outerShell   = outer;
   239         this.outerShell   = outer;
   243 
   240 
   244         final Repository repository = new Repository(domain,fairLock);
   241         final Repository repository = new Repository(domain,fairLock);
   245         this.mbsInterceptor =
   242         this.mbsInterceptor =
   246             new DefaultMBeanServerInterceptor(outer, delegate, instantiator,
   243             new NamespaceDispatchInterceptor(outer, delegate, instantiator,
   247                                               repository);
   244                                               repository);
   248         this.interceptorsEnabled = interceptors;
   245         this.interceptorsEnabled = interceptors;
       
   246         this.mbeanServerName = Util.getMBeanServerSecurityName(delegate);
   249         initialize();
   247         initialize();
   250     }
   248     }
   251 
   249 
   252     /**
   250     /**
   253      * Tell whether {@link MBeanServerInterceptor}s are enabled on this
   251      * Tell whether {@link MBeanServerInterceptor}s are enabled on this
   939      */
   937      */
   940     public Object instantiate(String className)
   938     public Object instantiate(String className)
   941         throws ReflectionException, MBeanException {
   939         throws ReflectionException, MBeanException {
   942 
   940 
   943         /* Permission check */
   941         /* Permission check */
   944         checkMBeanPermission(className, null, null, "instantiate");
   942         checkMBeanPermission(mbeanServerName, className, null, null,
       
   943                 "instantiate");
   945 
   944 
   946         return instantiator.instantiate(className);
   945         return instantiator.instantiate(className);
   947     }
   946     }
   948 
   947 
   949     /**
   948     /**
   976     public Object instantiate(String className, ObjectName loaderName)
   975     public Object instantiate(String className, ObjectName loaderName)
   977         throws ReflectionException, MBeanException,
   976         throws ReflectionException, MBeanException,
   978                InstanceNotFoundException {
   977                InstanceNotFoundException {
   979 
   978 
   980         /* Permission check */
   979         /* Permission check */
   981         checkMBeanPermission(className, null, null, "instantiate");
   980         checkMBeanPermission(mbeanServerName, className, null,
       
   981                 null, "instantiate");
   982 
   982 
   983         ClassLoader myLoader = outerShell.getClass().getClassLoader();
   983         ClassLoader myLoader = outerShell.getClass().getClassLoader();
   984         return instantiator.instantiate(className, loaderName, myLoader);
   984         return instantiator.instantiate(className, loaderName, myLoader);
   985     }
   985     }
   986 
   986 
  1014     public Object instantiate(String className, Object params[],
  1014     public Object instantiate(String className, Object params[],
  1015                               String signature[])
  1015                               String signature[])
  1016         throws ReflectionException, MBeanException {
  1016         throws ReflectionException, MBeanException {
  1017 
  1017 
  1018         /* Permission check */
  1018         /* Permission check */
  1019         checkMBeanPermission(className, null, null, "instantiate");
  1019         checkMBeanPermission(mbeanServerName, className, null, null,
       
  1020                 "instantiate");
  1020 
  1021 
  1021         ClassLoader myLoader = outerShell.getClass().getClassLoader();
  1022         ClassLoader myLoader = outerShell.getClass().getClassLoader();
  1022         return instantiator.instantiate(className, params, signature,
  1023         return instantiator.instantiate(className, params, signature,
  1023                                         myLoader);
  1024                                         myLoader);
  1024     }
  1025     }
  1057                               Object params[], String signature[])
  1058                               Object params[], String signature[])
  1058         throws ReflectionException, MBeanException,
  1059         throws ReflectionException, MBeanException,
  1059                InstanceNotFoundException {
  1060                InstanceNotFoundException {
  1060 
  1061 
  1061         /* Permission check */
  1062         /* Permission check */
  1062         checkMBeanPermission(className, null, null, "instantiate");
  1063         checkMBeanPermission(mbeanServerName, className, null,
       
  1064                 null, "instantiate");
  1063 
  1065 
  1064         ClassLoader myLoader = outerShell.getClass().getClassLoader();
  1066         ClassLoader myLoader = outerShell.getClass().getClassLoader();
  1065         return instantiator.instantiate(className,loaderName,params,signature,
  1067         return instantiator.instantiate(className,loaderName,params,signature,
  1066                                         myLoader);
  1068                                         myLoader);
  1067     }
  1069     }
  1234                 MBEANSERVER_LOGGER.logp(Level.FINEST,
  1236                 MBEANSERVER_LOGGER.logp(Level.FINEST,
  1235                         JmxMBeanServer.class.getName(), "initialize",
  1237                         JmxMBeanServer.class.getName(), "initialize",
  1236                         "Unexpected exception occurred", e);
  1238                         "Unexpected exception occurred", e);
  1237             }
  1239             }
  1238             throw new
  1240             throw new
  1239                 IllegalStateException("Can't register delegate.");
  1241                 IllegalStateException("Can't register delegate.",e);
  1240         }
  1242         }
  1241 
  1243 
  1242 
  1244 
  1243         /* Add my class loader to the repository
  1245         /* Add my class loader to the repository
  1244            This can be null if my class loader is the bootstrap
  1246            This can be null if my class loader is the bootstrap
  1276      * @exception UnsupportedOperationException if
  1278      * @exception UnsupportedOperationException if
  1277      *            {@link MBeanServerInterceptor}s
  1279      *            {@link MBeanServerInterceptor}s
  1278      *            are not enabled on this object.
  1280      *            are not enabled on this object.
  1279      * @see #interceptorsEnabled
  1281      * @see #interceptorsEnabled
  1280      **/
  1282      **/
  1281     public synchronized MBeanServerInterceptor getMBeanServerInterceptor() {
  1283     public synchronized MBeanServer getMBeanServerInterceptor() {
  1282         if (interceptorsEnabled) return mbsInterceptor;
  1284         if (interceptorsEnabled) return mbsInterceptor;
  1283         else throw new UnsupportedOperationException(
  1285         else throw new UnsupportedOperationException(
  1284                        "MBeanServerInterceptors are disabled.");
  1286                        "MBeanServerInterceptors are disabled.");
  1285     }
  1287     }
  1286 
  1288 
  1290      *            {@link MBeanServerInterceptor}s
  1292      *            {@link MBeanServerInterceptor}s
  1291      *            are not enabled on this object.
  1293      *            are not enabled on this object.
  1292      * @see #interceptorsEnabled
  1294      * @see #interceptorsEnabled
  1293      **/
  1295      **/
  1294     public synchronized void
  1296     public synchronized void
  1295         setMBeanServerInterceptor(MBeanServerInterceptor interceptor) {
  1297         setMBeanServerInterceptor(MBeanServer interceptor) {
  1296         if (!interceptorsEnabled) throw new UnsupportedOperationException(
  1298         if (!interceptorsEnabled) throw new UnsupportedOperationException(
  1297                        "MBeanServerInterceptors are disabled.");
  1299                        "MBeanServerInterceptors are disabled.");
  1298         if (interceptor == null) throw new
  1300         if (interceptor == null) throw new
  1299             IllegalArgumentException("MBeanServerInterceptor is null");
  1301             IllegalArgumentException("MBeanServerInterceptor is null");
  1300         mbsInterceptor = interceptor;
  1302         mbsInterceptor = interceptor;
  1328      * <p>Return the ClassLoaderRepository for that MBeanServer.
  1330      * <p>Return the ClassLoaderRepository for that MBeanServer.
  1329      * @return The ClassLoaderRepository for that MBeanServer.
  1331      * @return The ClassLoaderRepository for that MBeanServer.
  1330      **/
  1332      **/
  1331     public ClassLoaderRepository getClassLoaderRepository() {
  1333     public ClassLoaderRepository getClassLoaderRepository() {
  1332         /* Permission check */
  1334         /* Permission check */
  1333         checkMBeanPermission(null, null, null, "getClassLoaderRepository");
  1335         checkMBeanPermission(mbeanServerName, null, null,
       
  1336                 null, "getClassLoaderRepository");
  1334         return secureClr;
  1337         return secureClr;
  1335     }
  1338     }
  1336 
  1339 
  1337     public MBeanServerDelegate getMBeanServerDelegate() {
  1340     public MBeanServerDelegate getMBeanServerDelegate() {
  1338         if (!interceptorsEnabled) throw new UnsupportedOperationException(
  1341         if (!interceptorsEnabled) throw new UnsupportedOperationException(
  1482     }
  1485     }
  1483 
  1486 
  1484     // SECURITY CHECKS
  1487     // SECURITY CHECKS
  1485     //----------------
  1488     //----------------
  1486 
  1489 
  1487     private static void checkMBeanPermission(String classname,
  1490     private static void checkMBeanPermission(String serverName,
       
  1491                                              String classname,
  1488                                              String member,
  1492                                              String member,
  1489                                              ObjectName objectName,
  1493                                              ObjectName objectName,
  1490                                              String actions)
  1494                                              String actions)
  1491         throws SecurityException {
  1495         throws SecurityException {
  1492         SecurityManager sm = System.getSecurityManager();
  1496         SecurityManager sm = System.getSecurityManager();
  1493         if (sm != null) {
  1497         if (sm != null) {
  1494             Permission perm = new MBeanPermission(classname,
  1498             Permission perm = new MBeanPermission(serverName,
       
  1499                                                   classname,
  1495                                                   member,
  1500                                                   member,
  1496                                                   objectName,
  1501                                                   objectName,
  1497                                                   actions);
  1502                                                   actions);
  1498             sm.checkPermission(perm);
  1503             sm.checkPermission(perm);
  1499         }
  1504         }