jdk/src/share/classes/javax/management/MBeanServerDelegate.java
changeset 1156 bbc2d15aaf7a
parent 2 90ce3da70b43
child 1225 6ef6227d36eb
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package javax.management;
    26 package javax.management;
    27 
    27 
       
    28 import com.sun.jmx.defaults.JmxProperties;
    28 import com.sun.jmx.defaults.ServiceName;
    29 import com.sun.jmx.defaults.ServiceName;
       
    30 import com.sun.jmx.mbeanserver.Util;
    29 
    31 
    30 /**
    32 /**
    31  * Represents  the MBean server from the management point of view.
    33  * Represents  the MBean server from the management point of view.
    32  * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
    34  * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
    33  * an MBean is registered/unregistered in the MBean server.
    35  * an MBean is registered/unregistered in the MBean server.
    37 public class MBeanServerDelegate implements MBeanServerDelegateMBean,
    39 public class MBeanServerDelegate implements MBeanServerDelegateMBean,
    38                                             NotificationEmitter   {
    40                                             NotificationEmitter   {
    39 
    41 
    40     /** The MBean server agent identification.*/
    42     /** The MBean server agent identification.*/
    41     private String mbeanServerId ;
    43     private String mbeanServerId ;
       
    44     private String mbeanServerName;
    42 
    45 
    43     /** The NotificationBroadcasterSupport object that sends the
    46     /** The NotificationBroadcasterSupport object that sends the
    44         notifications */
    47         notifications */
    45     private final NotificationBroadcasterSupport broadcaster;
    48     private final NotificationBroadcasterSupport broadcaster;
    46 
    49 
    66      * Create a MBeanServerDelegate object.
    69      * Create a MBeanServerDelegate object.
    67      */
    70      */
    68     public MBeanServerDelegate () {
    71     public MBeanServerDelegate () {
    69         stamp = getStamp();
    72         stamp = getStamp();
    70         broadcaster = new NotificationBroadcasterSupport() ;
    73         broadcaster = new NotificationBroadcasterSupport() ;
       
    74         mbeanServerName=null;
    71     }
    75     }
    72 
    76 
    73 
    77 
    74     /**
    78     /**
    75      * Returns the MBean server agent identity.
    79      * Returns the MBean server agent identity.
    80         if (mbeanServerId == null) {
    84         if (mbeanServerId == null) {
    81             String localHost;
    85             String localHost;
    82             try {
    86             try {
    83                 localHost = java.net.InetAddress.getLocalHost().getHostName();
    87                 localHost = java.net.InetAddress.getLocalHost().getHostName();
    84             } catch (java.net.UnknownHostException e) {
    88             } catch (java.net.UnknownHostException e) {
       
    89                 JmxProperties.MISC_LOGGER.finest("Can't get local host name, " +
       
    90                         "using \"localhost\" instead. Cause is: "+e);
    85                 localHost = "localhost";
    91                 localHost = "localhost";
    86             }
    92             }
    87             mbeanServerId = localHost + "_" + stamp;
    93             mbeanServerId =
       
    94                     Util.insertMBeanServerName(localHost + "_" + stamp,
       
    95                     mbeanServerName);
    88         }
    96         }
    89         return mbeanServerId;
    97         return mbeanServerId;
       
    98     }
       
    99 
       
   100     /**
       
   101      * The name of the MBeanServer.
       
   102      * @return The name of the MBeanServer, or {@value
       
   103      * javax.management.MBeanServerFactory#DEFAULT_MBEANSERVER_NAME} if no
       
   104      * name was specified.
       
   105      *
       
   106      * @since 1.7
       
   107      * @see #setMBeanServerName
       
   108      */
       
   109     public synchronized String getMBeanServerName() {
       
   110         if (Util.isMBeanServerNameUndefined(mbeanServerName))
       
   111             return MBeanServerFactory.DEFAULT_MBEANSERVER_NAME;
       
   112         return mbeanServerName;
       
   113     }
       
   114 
       
   115     /**
       
   116      * Sets the name of the MBeanServer. The name will be embedded into the
       
   117      * {@link #getMBeanServerId MBeanServerId} using the following format:<br>
       
   118      * {@code mbeanServerId: <mbeanServerId>;mbeanServerName=<mbeanServerName>}
       
   119      * <p>The characters {@code ':'} (colon), {@code ';'} (semicolon ),
       
   120      * {@code '*'} (star) and {@code '?'} (question mark) are not legal in an
       
   121      * MBean Server name.</p>
       
   122      * <p>For instance, if the {@code mbeanServerName} provided is
       
   123      * {@code "com.mycompany.myapp.server1"}, and the original
       
   124      * {@code MBeanServerId} was {@code "myhost_1213353064145"},
       
   125      * then {@code mbeanServerName} will be
       
   126      * embedded in the {@code MBeanServerId} - and the new value of the
       
   127      * {@code MBeanServerId} will be:
       
   128      * </p>
       
   129      * <pre>
       
   130      *       "myhost_1213353064145;mbeanServerName=com.mycompany.myapp.server1"
       
   131      * </pre>
       
   132      * <p>Note: The {@code mbeanServerName} is usually set by the
       
   133      *   {@code MBeanServerFactory}. It is set only once, before the
       
   134      *   MBean Server is returned by the factory. Once the MBean Server name is
       
   135      *   set, it is not possible to change it.
       
   136      * </p>
       
   137      * @param mbeanServerName The MBeanServer name.
       
   138      * @throws IllegalArgumentException if the MBeanServerName is already set
       
   139      *         to a different value, or if the provided name contains
       
   140      *         illegal characters, or if the provided name is {@code ""}
       
   141      *         (the empty string) or "-" (dash).
       
   142      * @throws UnsupportedOperationException if this object is of a legacy
       
   143      *         subclass of MBeanServerDelegate which overrides {@link
       
   144      *         #getMBeanServerId()}
       
   145      *         in a way that doesn't support setting an MBeanServer name.
       
   146      * @see MBeanServerFactory#getMBeanServerName
       
   147      * @since 1.7
       
   148      */
       
   149     public synchronized void setMBeanServerName(String mbeanServerName) {
       
   150         // Sets the name on the delegate. For complex backward
       
   151         // compatibility reasons it is not possible to give the
       
   152         // name to the MBeanServerDelegate constructor.
       
   153         //
       
   154         // The method setMBeanServerName() will call getMBeanServerId()
       
   155         // to check that the name is accurately set in the MBeanServerId.
       
   156         // If not (which could happen if a custom MBeanServerDelegate
       
   157         // implementation overrides getMBeanServerId() and was not updated
       
   158         // with respect to JMX 2.0 spec), this method will throw an
       
   159         // IllegalStateException...
       
   160 
       
   161         // will fail if mbeanServerName is illegal
       
   162         final String name = Util.checkServerName(mbeanServerName);
       
   163 
       
   164         // can only set mbeanServerDelegate once.
       
   165         if (this.mbeanServerName != null && !this.mbeanServerName.equals(name))
       
   166             throw new IllegalArgumentException(
       
   167                     "MBeanServerName already set to a different value");
       
   168 
       
   169         this.mbeanServerName = name;
       
   170 
       
   171         // will fail if mbeanServerId already has a different mbeanServerName
       
   172         mbeanServerId =
       
   173            Util.insertMBeanServerName(getMBeanServerId(),name);
       
   174 
       
   175         // check that we don't have a subclass which overrides
       
   176         // getMBeanServerId() without setting mbeanServerName
       
   177         if (!name.equals(
       
   178                 Util.extractMBeanServerName(getMBeanServerId())))
       
   179             throw new UnsupportedOperationException(
       
   180                     "Can't set MBeanServerName in MBeanServerId - " +
       
   181                     "unsupported by "+this.getClass().getName()+"?");
       
   182         // OK: at this point we know that we have correctly set mbeanServerName.
    90     }
   183     }
    91 
   184 
    92     /**
   185     /**
    93      * Returns the full name of the JMX specification implemented
   186      * Returns the full name of the JMX specification implemented
    94      * by this product.
   187      * by this product.
   208     /**
   301     /**
   209      * Defines the default ObjectName of the MBeanServerDelegate.
   302      * Defines the default ObjectName of the MBeanServerDelegate.
   210      *
   303      *
   211      * @since 1.6
   304      * @since 1.6
   212      */
   305      */
   213     public static final ObjectName DELEGATE_NAME;
   306     public static final ObjectName DELEGATE_NAME =
   214     static {
   307             Util.newObjectName("JMImplementation:type=MBeanServerDelegate");
   215         try {
       
   216             DELEGATE_NAME =
       
   217                 new ObjectName("JMImplementation:type=MBeanServerDelegate");
       
   218         } catch (MalformedObjectNameException e) {
       
   219             throw new Error("Can't initialize delegate name", e);
       
   220         }
       
   221     }
       
   222 
   308 
   223     /* Return a timestamp that is monotonically increasing even if
   309     /* Return a timestamp that is monotonically increasing even if
   224        System.currentTimeMillis() isn't (for example, if you call this
   310        System.currentTimeMillis() isn't (for example, if you call this
   225        constructor more than once in the same millisecond, or if the
   311        constructor more than once in the same millisecond, or if the
   226        clock always returns the same value).  This means that the ids
   312        clock always returns the same value).  This means that the ids