jdk/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java
author dfuchs
Thu, 04 Sep 2008 14:46:36 +0200
changeset 1156 bbc2d15aaf7a
parent 1002 1e6a1b77f22a
child 1222 78e3d021d528
permissions -rw-r--r--
5072476: RFE: support cascaded (federated) MBean Servers 6299231: Add support for named MBean Servers Summary: New javax.management.namespace package. Reviewed-by: emcmanus
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: 526
diff changeset
     2
 * Copyright 2000-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 com.sun.jmx.interceptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    28
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    29
// JMX RI
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    30
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    31
import com.sun.jmx.mbeanserver.DynamicMBean2;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    32
import com.sun.jmx.mbeanserver.Introspector;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    33
import com.sun.jmx.mbeanserver.MBeanInjector;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    34
import com.sun.jmx.mbeanserver.MBeanInstantiator;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    35
import com.sun.jmx.mbeanserver.ModifiableClassLoaderRepository;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    36
import com.sun.jmx.mbeanserver.NamedObject;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    37
import com.sun.jmx.mbeanserver.NotifySupport;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    38
import com.sun.jmx.mbeanserver.Repository;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    39
import com.sun.jmx.mbeanserver.Repository.RegistrationContext;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    40
import com.sun.jmx.mbeanserver.Util;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    41
import com.sun.jmx.remote.util.EnvHelp;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessControlContext;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    45
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.Permission;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    47
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.ProtectionDomain;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    49
import java.util.ArrayList;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    50
import java.util.HashSet;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    51
import java.util.Iterator;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    52
import java.util.LinkedList;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    53
import java.util.List;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    54
import java.util.Queue;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    55
import java.util.Set;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    56
import java.util.WeakHashMap;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    57
import java.util.logging.Level;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// JMX import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.management.AttributeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.DynamicMBean;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    64
import javax.management.DynamicWrapperMBean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.InstanceAlreadyExistsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.management.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.management.JMRuntimeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.management.MBeanPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.management.MBeanRegistrationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.management.MBeanServerDelegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.management.MBeanServerNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.management.MBeanTrustPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import javax.management.NotificationBroadcaster;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    83
import javax.management.NotificationBroadcasterSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import javax.management.ObjectInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
import javax.management.QueryEval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
import javax.management.QueryExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import javax.management.RuntimeErrorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
import javax.management.RuntimeMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import javax.management.RuntimeOperationsException;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
    95
import javax.management.namespace.JMXNamespace;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * This is the default class for MBean manipulation on the agent side. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * contains the methods necessary for the creation, registration, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * deletion of MBeans as well as the access methods for registered MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * This is the core component of the JMX infrastructure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Every MBean which is added to the MBean server becomes manageable: its attributes and operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * become remotely accessible through the connectors/adaptors connected to that MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * A Java object cannot be registered in the MBean server unless it is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * When an MBean is registered or unregistered in the MBean server an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * {@link javax.management.MBeanServerNotification MBeanServerNotification}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Notification is emitted. To register an object as listener to MBeanServerNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * you should call the MBean server method {@link #addNotificationListener addNotificationListener} with <CODE>ObjectName</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * the <CODE>ObjectName</CODE> of the {@link javax.management.MBeanServerDelegate MBeanServerDelegate}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * This <CODE>ObjectName</CODE> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <CODE>JMImplementation:type=MBeanServerDelegate</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   118
public class DefaultMBeanServerInterceptor
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   119
        extends MBeanServerInterceptorSupport {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** The MBeanInstantiator object used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final transient MBeanInstantiator instantiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** The MBean server object that is associated to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private transient MBeanServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   129
    /** The MBean server delegate object that is associated to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private final transient MBeanServerDelegate delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /** The Repository object used by the DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private final transient Repository repository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** Wrappers for client listeners.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /* See the comment before addNotificationListener below.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final transient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        WeakHashMap<ListenerWrapper, WeakReference<ListenerWrapper>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            listenerWrappers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                new WeakHashMap<ListenerWrapper,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                WeakReference<ListenerWrapper>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   144
    private final NamespaceDispatchInterceptor dispatcher;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   145
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /** The default domain of the object names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private final String domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   149
    /** The mbeanServerName  */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   150
    private final String mbeanServerName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   152
    /** The sequence number identifying the notifications sent */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // Now sequence number is handled by MBeanServerDelegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // private int sequenceNumber=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Creates a DefaultMBeanServerInterceptor with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * repository instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>Do not forget to call <code>initialize(outer,delegate)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * before using this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param outer A pointer to the MBeanServer object that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *        passed to the MBeans when invoking their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *        {@link javax.management.MBeanRegistration} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param delegate A pointer to the MBeanServerDelegate associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *        with the new MBeanServer. The new MBeanServer must register
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *        this MBean in its MBean repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param instantiator The MBeanInstantiator that will be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *        instantiate MBeans and take care of class loading issues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param repository The repository to use for this MBeanServer.
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   170
     * @param dispatcher The dispatcher used by this MBeanServer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public DefaultMBeanServerInterceptor(MBeanServer         outer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                         MBeanServerDelegate delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                         MBeanInstantiator   instantiator,
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   175
                                         Repository          repository,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   176
                                         NamespaceDispatchInterceptor dispatcher)  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (outer == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            IllegalArgumentException("outer MBeanServer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (delegate == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            IllegalArgumentException("MBeanServerDelegate cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (instantiator == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            IllegalArgumentException("MBeanInstantiator cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (repository == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            IllegalArgumentException("Repository cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.server   = outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.delegate = delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.instantiator = instantiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.repository   = repository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.domain       = repository.getDefaultDomain();
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   191
        this.dispatcher   = dispatcher;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   192
        this.mbeanServerName = Util.getMBeanServerSecurityName(delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public ObjectInstance createMBean(String className, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return createMBean(className, name, (Object[]) null, (String[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                      ObjectName loaderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
               NotCompliantMBeanException, InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return createMBean(className, name, loaderName, (Object[]) null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                           (String[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                      Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
               NotCompliantMBeanException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return createMBean(className, name, null, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                               params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } catch (InstanceNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            /* Can only happen if loaderName doesn't exist, but we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
               passed null, so we shouldn't get this exception.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                new IllegalArgumentException("Unexpected exception: " + e), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                      ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                      Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
               NotCompliantMBeanException, InstanceNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return createMBean(className, name, loaderName, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                           params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                       ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                       boolean withDefaultLoaderRepository,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                       Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
               NotCompliantMBeanException, InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Class theClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                new IllegalArgumentException("The class name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                      "Exception occurred during MBean creation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (name.isPattern()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    new IllegalArgumentException("Invalid name->" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                                 name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                final String msg = "Exception occurred during MBean creation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new RuntimeOperationsException(wrapped, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   271
        checkMBeanPermission(mbeanServerName,className, null, null, "instantiate");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   272
        checkMBeanPermission(mbeanServerName,className, null, name, "registerMBean");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        /* Load the appropriate class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (withDefaultLoaderRepository) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        "createMBean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        "ClassName = " + className + ", ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            theClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                instantiator.findClassWithDefaultLoaderRepository(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        "createMBean", "ClassName = " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        ", ObjectName = " + name + ", Loader name = null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            theClass = instantiator.findClass(className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                  server.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            loaderName = nonDefaultDomain(loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        "createMBean", "ClassName = " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        ", ObjectName = " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        ", Loader name = " + loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            theClass = instantiator.findClass(className, loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        checkMBeanTrustPermission(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // Check that the MBean can be instantiated by the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        Introspector.testCreation(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // Check the JMX MBean compliance of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        Introspector.checkCompliance(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Object moi= instantiator.instantiate(theClass, params,  signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                             server.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        final String infoClassName = getNewMBeanClassName(moi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return registerObject(infoClassName, moi, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public ObjectInstance registerMBean(Object object, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throws InstanceAlreadyExistsException, MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        NotCompliantMBeanException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Class theClass = object.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Introspector.checkCompliance(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        final String infoClassName = getNewMBeanClassName(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   336
        checkMBeanPermission(mbeanServerName,infoClassName, null, name, "registerMBean");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        checkMBeanTrustPermission(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return registerObject(infoClassName, object, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static String getNewMBeanClassName(Object mbeanToRegister)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (mbeanToRegister instanceof DynamicMBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            DynamicMBean mbean = (DynamicMBean) mbeanToRegister;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                name = mbean.getMBeanInfo().getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                // Includes case where getMBeanInfo() returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                NotCompliantMBeanException ncmbe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    new NotCompliantMBeanException("Bad getMBeanInfo()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                ncmbe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                throw ncmbe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                final String msg = "MBeanInfo has null class name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return mbeanToRegister.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private final Set<ObjectName> beingUnregistered =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        new HashSet<ObjectName>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void unregisterMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throws InstanceNotFoundException, MBeanRegistrationException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                new IllegalArgumentException("Object name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                      "Exception occurred trying to unregister the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /* The semantics of preDeregister are tricky.  If it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
           exception, then the unregisterMBean fails.  This allows an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
           MBean to refuse to be unregistered.  If it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
           successfully, then the unregisterMBean can proceed.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
           this case the preDeregister may have cleaned up some state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
           and will not expect to be called a second time.  So if two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
           threads try to unregister the same MBean at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
           then one of them must wait for the other one to either (a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
           call preDeregister and get an exception or (b) call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
           preDeregister successfully and unregister the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
           Suppose thread T1 is unregistering an MBean and thread T2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
           is trying to unregister the same MBean, so waiting for T1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
           Then a deadlock is possible if the preDeregister for T1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
           ends up needing a lock held by T2.  Given the semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
           just described, there does not seem to be any way to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
           this.  This will not happen to code where it is clear for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
           any given MBean what thread may unregister that MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
           On the other hand we clearly do not want a thread that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
           unregistering MBean A to have to wait for another thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
           that is unregistering another MBean B (see bug 6318664).  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
           deadlock in this situation could reasonably be considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
           gratuitous.  So holding a global lock across the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
           preDeregister call would be bad.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
           So we have a set of ObjectNames that some thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
           currently unregistering.  When a thread wants to unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
           a name, it must first check if the name is in the set, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
           if so it must wait.  When a thread successfully unregisters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
           a name it removes the name from the set and notifies any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
           waiting threads that the set has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
           This implies that we must be very careful to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
           the name is removed from the set and waiters notified, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
           matter what code path is taken.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        synchronized (beingUnregistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            while (beingUnregistered.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    beingUnregistered.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    throw new MBeanRegistrationException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    // pretend the exception came from preDeregister;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    // in another execution sequence it could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            beingUnregistered.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            exclusiveUnregisterMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            synchronized (beingUnregistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                beingUnregistered.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                beingUnregistered.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private void exclusiveUnregisterMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throws InstanceNotFoundException, MBeanRegistrationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // may throw InstanceNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   445
        checkMBeanPermission(mbeanServerName, instance, null, name,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   446
                "unregisterMBean");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (instance instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            preDeregisterInvoke((MBeanRegistration) instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   451
        final Object resource = getResource(instance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   453
        // Unregisters the MBean from the repository.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   454
        // Returns the resource context that was used.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   455
        // The returned context does nothing for regular MBeans.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   456
        // For ClassLoader MBeans and JMXNamespace (and JMXDomain)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   457
        // MBeans - the context makes it possible to unregister these
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   458
        // objects from the appropriate framework artifacts, such as
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   459
        // the CLR or the dispatcher, from within the repository lock.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   460
        // In case of success, we also need to call context.done() at the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   461
        // end of this method.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   462
        //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   463
        final ResourceContext context =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   464
                unregisterFromRepository(resource, instance, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   466
        try {
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   467
            if (instance instanceof MBeanRegistration)
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   468
                postDeregisterInvoke(name,(MBeanRegistration) instance);
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   469
        } finally {
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   470
            context.done();
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
   471
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public ObjectInstance getObjectInstance(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   480
        checkMBeanPermission(mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   481
                instance, null, name, "getObjectInstance");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        final String className = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return new ObjectInstance(name, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // Check if the caller has the right to invoke 'queryMBeans'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            //
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   493
            checkMBeanPermission(mbeanServerName,(String) null, null, null, "queryMBeans");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            // Perform query without "query".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            Set<ObjectInstance> list = queryMBeansImpl(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            // Check if the caller has the right to invoke 'queryMBeans'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // on each specific classname/objectname in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            Set<ObjectInstance> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                new HashSet<ObjectInstance>(list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                try {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   506
                    checkMBeanPermission(mbeanServerName,oi.getClassName(), null,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                         oi.getObjectName(), "queryMBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    allowedList.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    // OK: Do not add this ObjectInstance to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            // Apply query to allowed MBeans only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return filterListOfObjectInstances(allowedList, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // Perform query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return queryMBeansImpl(name, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    private Set<ObjectInstance> queryMBeansImpl(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                                QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // Query the MBeans on the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   528
        Set<NamedObject> list = repository.query(name, query);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return (objectInstancesFromFilteredNamedObjects(list, query));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        Set<ObjectName> queryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            // Check if the caller has the right to invoke 'queryNames'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            //
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   539
            checkMBeanPermission(mbeanServerName,(String) null, null, null, "queryNames");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            // Perform query without "query".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            Set<ObjectInstance> list = queryMBeansImpl(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            // Check if the caller has the right to invoke 'queryNames'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // on each specific classname/objectname in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            Set<ObjectInstance> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                new HashSet<ObjectInstance>(list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                try {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   552
                    checkMBeanPermission(mbeanServerName, oi.getClassName(), null,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                         oi.getObjectName(), "queryNames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    allowedList.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    // OK: Do not add this ObjectInstance to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            // Apply query to allowed MBeans only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            Set<ObjectInstance> queryObjectInstanceList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                filterListOfObjectInstances(allowedList, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            queryList = new HashSet<ObjectName>(queryObjectInstanceList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            for (ObjectInstance oi : queryObjectInstanceList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                queryList.add(oi.getObjectName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // Perform query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            queryList = queryNamesImpl(name, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return queryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private Set<ObjectName> queryNamesImpl(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // Query the MBeans on the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   579
        Set<NamedObject> list = repository.query(name, query);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return (objectNamesFromFilteredNamedObjects(list, query));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public boolean isRegistered(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            throw new RuntimeOperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                     new IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                     "Object name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   593
        /* No Permission check */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   594
        // isRegistered is always unchecked as per JMX spec.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return (repository.contains(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public String[] getDomains()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // Check if the caller has the right to invoke 'getDomains'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            //
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   604
            checkMBeanPermission(mbeanServerName, (String) null, null, null, "getDomains");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            // Return domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            String[] domains = repository.getDomains();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            // Check if the caller has the right to invoke 'getDomains'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // on each specific domain in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            List<String> result = new ArrayList<String>(domains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            for (int i = 0; i < domains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                try {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   616
                    ObjectName dom =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   617
                            Util.newObjectName(domains[i] + ":x=x");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   618
                    checkMBeanPermission(mbeanServerName, (String) null, null, dom, "getDomains");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    result.add(domains[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    // OK: Do not add this domain to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // Make an array from result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            return repository.getDomains();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public Integer getMBeanCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return (repository.getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public Object getAttribute(ObjectName name, String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        throws MBeanException, AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
               InstanceNotFoundException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                IllegalArgumentException("Attribute cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    "getAttribute",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    "Attribute = " + attribute + ", ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        final DynamicMBean instance = getMBean(name);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   662
        checkMBeanPermission(mbeanServerName, instance, attribute,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   663
                name, "getAttribute");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return instance.getAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } catch (AttributeNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            rethrowMaybeMBeanException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            throw new AssertionError(); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public AttributeList getAttributes(ObjectName name, String[] attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        throws InstanceNotFoundException, ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                IllegalArgumentException("ObjectName name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                IllegalArgumentException("Attributes cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    "getAttributes", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        final DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        final String[] allowedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (sm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            allowedAttributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            final String classname = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            // Check if the caller has the right to invoke 'getAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            //
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   708
            checkMBeanPermission(mbeanServerName, classname, null, name, "getAttribute");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            // Check if the caller has the right to invoke 'getAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // on each specific attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            List<String> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                new ArrayList<String>(attributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            for (String attr : attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                try {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   717
                    checkMBeanPermission(mbeanServerName, classname, attr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                         name, "getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    allowedList.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    // OK: Do not add this attribute to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   724
            allowedAttributes =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   725
                    allowedList.toArray(new String[allowedList.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return instance.getAttributes(allowedAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            rethrow(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public void setAttribute(ObjectName name, Attribute attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        throws InstanceNotFoundException, AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
               InvalidAttributeValueException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
               ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                IllegalArgumentException("ObjectName name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                "Exception occurred trying to invoke the setter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                IllegalArgumentException("Attribute cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                "Exception occurred trying to invoke the setter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    "setAttribute", "ObjectName = " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    ", Attribute = " + attribute.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        DynamicMBean instance = getMBean(name);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   763
        checkMBeanPermission(mbeanServerName, instance, attribute.getName(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                             name, "setAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            instance.setAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        } catch (AttributeNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        } catch (InvalidAttributeValueException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            rethrowMaybeMBeanException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public AttributeList setAttributes(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                       AttributeList attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            throws InstanceNotFoundException, ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                IllegalArgumentException("ObjectName name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                "Exception occurred trying to invoke the setter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            IllegalArgumentException("AttributeList  cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            "Exception occurred trying to invoke the setter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        final DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        final AttributeList allowedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (sm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            allowedAttributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            String classname = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            // Check if the caller has the right to invoke 'setAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            //
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   806
            checkMBeanPermission(mbeanServerName, classname, null, name, "setAttribute");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            // Check if the caller has the right to invoke 'setAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            // on each specific attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            allowedAttributes = new AttributeList(attributes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            for (Iterator i = attributes.iterator(); i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    Attribute attribute = (Attribute) i.next();
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   815
                    checkMBeanPermission(mbeanServerName, classname, attribute.getName(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                         name, "setAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    allowedAttributes.add(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    // OK: Do not add this attribute to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return instance.setAttributes(allowedAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            rethrow(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public Object invoke(ObjectName name, String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                         Object params[], String signature[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            throws InstanceNotFoundException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                   ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        DynamicMBean instance = getMBean(name);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   839
        checkMBeanPermission(mbeanServerName, instance, operationName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   840
                name, "invoke");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return instance.invoke(operationName, params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            rethrowMaybeMBeanException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /* Centralize some of the tedious exception wrapping demanded by the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
       spec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private static void rethrow(Throwable t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            throws ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            throw t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        } catch (ReflectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        } catch (RuntimeOperationsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        } catch (RuntimeErrorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            throw new RuntimeMBeanException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            throw new RuntimeErrorException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        } catch (Throwable t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            throw new RuntimeException("Unexpected exception", t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    private static void rethrowMaybeMBeanException(Throwable t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            throws ReflectionException, MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (t instanceof MBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            throw (MBeanException) t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        rethrow(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Register <code>object</code> in the repository, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * given <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * This method is called by the various createMBean() flavours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * and by registerMBean() after all MBean compliance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * have been performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * This method does not performed any kind of test compliance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * and the caller should make sure that the given <code>object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * is MBean compliant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * This methods performed all the basic steps needed for object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * registration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * <li>If the <code>object</code> implements the MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *     interface, it invokes preRegister() on the object.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <li>Then the object is added to the repository with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *     <code>name</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <li>Finally, if the <code>object</code> implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *     MBeanRegistration interface, it invokes postRegister()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *     on the object.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @param object A reference to a MBean compliant object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @param name   The ObjectName of the <code>object</code> MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @return the actual ObjectName with which the object was registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @exception InstanceAlreadyExistsException if an object is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *            registered with that name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @exception MBeanRegistrationException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *            registration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    private ObjectInstance registerObject(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                                          Object object, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        throws InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
               MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (object == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                new IllegalArgumentException("Cannot add null object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                        "Exception occurred trying to register the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        DynamicMBean mbean = Introspector.makeDynamicMBean(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return registerDynamicMBean(classname, mbean, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    private ObjectInstance registerDynamicMBean(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                                DynamicMBean mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                                                ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        throws InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
               MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    "registerMBean", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   942
        ObjectName logicalName = preRegister(mbean, server, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   944
        // preRegister returned successfully, so from this point on we
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   945
        // must call postRegister(false) if there is any problem.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   946
        boolean registered = false;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   947
        boolean registerFailed = false;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   948
        ResourceContext context = null;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   949
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   950
        try {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   951
            mbean = injectResources(mbean, server, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   952
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            if (mbean instanceof DynamicMBean2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    ((DynamicMBean2) mbean).preRegister2(server, logicalName);
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   956
                    registerFailed = true;  // until we succeed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    if (e instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                        throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    if (e instanceof InstanceAlreadyExistsException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        throw (InstanceAlreadyExistsException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            if (logicalName != name && logicalName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                logicalName =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   968
                        ObjectName.getInstance(nonDefaultDomain(logicalName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   970
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   971
            checkMBeanPermission(mbeanServerName, classname, null, logicalName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   972
                    "registerMBean");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   974
            if (logicalName == null) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   975
                final RuntimeException wrapped =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   976
                    new IllegalArgumentException("No object name specified");
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   977
                throw new RuntimeOperationsException(wrapped,
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   978
                            "Exception occurred trying to register the MBean");
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   979
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   980
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   981
            final Object resource = getResource(mbean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   983
            // Register the MBean with the repository.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   984
            // Returns the resource context that was used.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   985
            // The returned context does nothing for regular MBeans.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   986
            // For ClassLoader MBeans and JMXNamespace (and JMXDomain)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   987
            // MBeans - the context makes it possible to register these
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   988
            // objects with the appropriate framework artifacts, such as
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   989
            // the CLR or the dispatcher, from within the repository lock.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   990
            // In case of success, we also need to call context.done() at the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   991
            // end of this method.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   992
            //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   993
            context = registerWithRepository(resource, mbean, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   994
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   995
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   996
            registerFailed = false;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   997
            registered = true;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
   998
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
   999
        } finally {
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1000
            try {
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1001
                postRegister(logicalName, mbean, registered, registerFailed);
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1002
            } finally {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1003
                if (registered && context!=null) context.done();
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1004
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1006
        return new ObjectInstance(logicalName, classname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1009
    private static void throwMBeanRegistrationException(Throwable t, String where)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1010
    throws MBeanRegistrationException {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1011
        if (t instanceof RuntimeException) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1012
            throw new RuntimeMBeanException((RuntimeException)t,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1013
                    "RuntimeException thrown " + where);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1014
        } else if (t instanceof Error) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1015
            throw new RuntimeErrorException((Error)t,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1016
                    "Error thrown " + where);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1017
        } else if (t instanceof MBeanRegistrationException) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1018
            throw (MBeanRegistrationException)t;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1019
        } else if (t instanceof Exception) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1020
            throw new MBeanRegistrationException((Exception)t,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1021
                    "Exception thrown " + where);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1022
        } else // neither Error nor Exception??
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1023
            throw new RuntimeException(t);
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1024
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1025
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1026
    private static ObjectName preRegister(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1027
            DynamicMBean mbean, MBeanServer mbs, ObjectName name)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1028
            throws InstanceAlreadyExistsException, MBeanRegistrationException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1029
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1030
        ObjectName newName = null;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1031
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1032
        try {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1033
            if (mbean instanceof MBeanRegistration)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1034
                newName = ((MBeanRegistration) mbean).preRegister(mbs, name);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1035
        } catch (Throwable t) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1036
            throwMBeanRegistrationException(t, "in preRegister method");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (newName != null) return newName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        else return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1043
    private static DynamicMBean injectResources(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1044
            DynamicMBean mbean, MBeanServer mbs, ObjectName name)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1045
    throws MBeanRegistrationException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1046
        try {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1047
            Object resource = getResource(mbean);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1048
            MBeanInjector.inject(resource, mbs, name);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1049
            if (MBeanInjector.injectsSendNotification(resource)) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1050
                NotificationBroadcasterSupport nbs =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1051
                        new NotificationBroadcasterSupport();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1052
                MBeanInjector.injectSendNotification(resource, nbs);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1053
                mbean = NotifySupport.wrap(mbean, nbs);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1054
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1055
            return mbean;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1056
        } catch (Throwable t) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1057
            throwMBeanRegistrationException(t, "injecting @Resources");
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1058
            return null;  // not reached
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1059
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1060
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1062
    private static void postRegister(
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1063
            ObjectName logicalName, DynamicMBean mbean,
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1064
            boolean registrationDone, boolean registerFailed) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1065
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1066
        if (registerFailed && mbean instanceof DynamicMBean2)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1067
            ((DynamicMBean2) mbean).registerFailed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        try {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1069
            if (mbean instanceof MBeanRegistration)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1070
                ((MBeanRegistration) mbean).postRegister(registrationDone);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        } catch (RuntimeException e) {
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1072
            MBEANSERVER_LOGGER.fine("While registering MBean ["+logicalName+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1073
                    "]: " + "Exception thrown by postRegister: " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1074
                    "rethrowing <"+e+">, but keeping the MBean registered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            throw new RuntimeMBeanException(e,
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1076
                      "RuntimeException thrown in postRegister method: "+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1077
                      "rethrowing <"+e+">, but keeping the MBean registered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        } catch (Error er) {
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1079
            MBEANSERVER_LOGGER.fine("While registering MBean ["+logicalName+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1080
                    "]: " + "Error thrown by postRegister: " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1081
                    "rethrowing <"+er+">, but keeping the MBean registered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            throw new RuntimeErrorException(er,
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1083
                      "Error thrown in postRegister method: "+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1084
                      "rethrowing <"+er+">, but keeping the MBean registered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    private static void preDeregisterInvoke(MBeanRegistration moi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            throws MBeanRegistrationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            moi.preDeregister();
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1092
        } catch (Throwable t) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1093
            throwMBeanRegistrationException(t, "in preDeregister method");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1097
    private static void postDeregisterInvoke(ObjectName mbean,
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1098
            MBeanRegistration moi) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            moi.postDeregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        } catch (RuntimeException e) {
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1102
            MBEANSERVER_LOGGER.fine("While unregistering MBean ["+mbean+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1103
                    "]: " + "Exception thrown by postDeregister: " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1104
                    "rethrowing <"+e+">, although the MBean is succesfully " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1105
                    "unregistered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            throw new RuntimeMBeanException(e,
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1107
                      "RuntimeException thrown in postDeregister method: "+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1108
                      "rethrowing <"+e+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1109
                      ">, although the MBean is sucessfully unregistered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        } catch (Error er) {
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1111
            MBEANSERVER_LOGGER.fine("While unregistering MBean ["+mbean+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1112
                    "]: " + "Error thrown by postDeregister: " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1113
                    "rethrowing <"+er+">, although the MBean is succesfully " +
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1114
                    "unregistered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            throw new RuntimeErrorException(er,
1002
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1116
                      "Error thrown in postDeregister method: "+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1117
                      "rethrowing <"+er+
1e6a1b77f22a 6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents: 834
diff changeset
  1118
                      ">, although the MBean is sucessfully unregistered");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Gets a specific MBean controlled by the DefaultMBeanServerInterceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * The name must have a non-default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    private DynamicMBean getMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                               "Exception occurred trying to get an MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
  1134
        DynamicMBean obj = repository.retrieve(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                        "getMBean", name + " : Found no object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            throw new InstanceNotFoundException(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private static Object getResource(DynamicMBean mbean) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1147
        if (mbean instanceof DynamicWrapperMBean)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1148
            return ((DynamicWrapperMBean) mbean).getWrappedObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            return mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1153
    private static ClassLoader getResourceLoader(DynamicMBean mbean) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1154
        if (mbean instanceof DynamicWrapperMBean)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1155
            return ((DynamicWrapperMBean) mbean).getWrappedClassLoader();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1156
        else
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1157
            return mbean.getClass().getClassLoader();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1158
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1159
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    private ObjectName nonDefaultDomain(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (name == null || name.getDomain().length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        /* The ObjectName looks like ":a=b", and that's what its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
           toString() will return in this implementation.  So
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
           we can just stick the default domain in front of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
           to get a non-default-domain name.  We depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
           fact that toString() works like that and that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
           leaves wildcards in place (so we can detect an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
           if one is supplied where it shouldn't be).  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        final String completeName = domain + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1173
        return Util.newObjectName(completeName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    public String getDefaultDomain()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * Notification handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * This is not trivial, because the MBeanServer translates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * source of a received notification from a reference to an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * into the ObjectName of that MBean.  While that does make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * notification sending easier for MBean writers, it comes at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * considerable cost.  We need to replace the source of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * notification, which is basically wrong if there are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * listeners registered directly with the MBean (without going
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * through the MBean server).  We also need to wrap the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * supplied by the client of the MBeanServer with a listener that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * performs the substitution before forwarding.  This is why we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * strongly discourage people from putting MBean references in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * source of their notifications.  Instead they should arrange to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * put the ObjectName there themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * However, existing code relies on the substitution, so we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * stuck with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Here's how we handle it.  When you add a listener, we make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * ListenerWrapper around it.  We look that up in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * listenerWrappers map, and if there was already a wrapper for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * that listener with the given ObjectName, we reuse it.  This map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * is a WeakHashMap, so a listener that is no longer registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * with any MBean can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * We cannot use simpler solutions such as always creating a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * wrapper or always registering the same listener with the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * and using the handback to find the client's original listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * The reason is that we need to support the removeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * variant that removes all (listener,filter,handback) triples on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * a broadcaster that have a given listener.  And we do not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * any way to inspect a broadcaster's internal list of triples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * So the same client listener must always map to the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * listener registered with the broadcaster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * Another possible solution would be to map from ObjectName to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * list of listener wrappers (or IdentityHashMap of listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * wrappers), making this list the first time a listener is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * on a given MBean, and removing it when the MBean is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * This is probably more costly in memory, but could be useful if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * some day we don't want to rely on weak references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    public void addNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                                        NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    "addNotificationListener", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        DynamicMBean instance = getMBean(name);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1239
        checkMBeanPermission(mbeanServerName, instance, null,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1240
                name, "addNotificationListener");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        NotificationBroadcaster broadcaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                getNotificationBroadcaster(name, instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                                           NotificationBroadcaster.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        // ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        // Check listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                IllegalArgumentException("Null listener"),"Null listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        NotificationListener listenerWrapper =
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1255
            getListenerWrapper(listener, name, instance, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        broadcaster.addNotificationListener(listenerWrapper, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    public void addNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                                        ObjectName listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        // Get listener object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        DynamicMBean instance = getMBean(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        if (!(resource instanceof NotificationListener)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                IllegalArgumentException(listener.getCanonicalName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                "The MBean " + listener.getCanonicalName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                "does not implement the NotificationListener interface") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        // Add a listener on an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    "addNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        server.addNotificationListener(name,(NotificationListener) resource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                       filter, handback) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                           NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        removeNotificationListener(name, listener, null, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                                           NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        removeNotificationListener(name, listener, filter, handback, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                                           ObjectName listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        NotificationListener instance = getListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    "removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        server.removeNotificationListener(name, instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                                           ObjectName listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        NotificationListener instance = getListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    "removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        server.removeNotificationListener(name, instance, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    private NotificationListener getListener(ObjectName listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        // Get listener object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        DynamicMBean instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            instance = getMBean(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        } catch (InstanceNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            throw EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                          new ListenerNotFoundException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if (!(resource instanceof NotificationListener)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            final RuntimeException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                new IllegalArgumentException(listener.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                "MBean " + listener.getCanonicalName() + " does not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                "implement " + NotificationListener.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            throw new RuntimeOperationsException(exc, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        return (NotificationListener) resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    private void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                                            NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                                            NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                                            Object handback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                            boolean removeAll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    "removeNotificationListener", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        DynamicMBean instance = getMBean(name);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1377
        checkMBeanPermission(mbeanServerName, instance, null, name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                             "removeNotificationListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        /* We could simplify the code by assigning broadcaster after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
           assigning listenerWrapper, but that would change the error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
           behavior when both the broadcaster and the listener are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
           erroneous.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        Class<? extends NotificationBroadcaster> reqClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            removeAll ? NotificationBroadcaster.class : NotificationEmitter.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        NotificationBroadcaster broadcaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            getNotificationBroadcaster(name, instance, reqClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        NotificationListener listenerWrapper =
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1391
            getListenerWrapper(listener, name, instance, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        if (listenerWrapper == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            throw new ListenerNotFoundException("Unknown listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        if (removeAll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            broadcaster.removeNotificationListener(listenerWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            NotificationEmitter emitter = (NotificationEmitter) broadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            emitter.removeNotificationListener(listenerWrapper,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                                               filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                               handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    private static <T extends NotificationBroadcaster>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            T getNotificationBroadcaster(ObjectName name, Object instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                         Class<T> reqClass) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1409
        if (reqClass.isInstance(instance))
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1410
            return reqClass.cast(instance);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1411
        if (instance instanceof DynamicWrapperMBean)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1412
            instance = ((DynamicWrapperMBean) instance).getWrappedObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        if (reqClass.isInstance(instance))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            return reqClass.cast(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        final RuntimeException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            new IllegalArgumentException(name.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            "MBean " + name.getCanonicalName() + " does not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            "implement " + reqClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        throw new RuntimeOperationsException(exc, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    public MBeanInfo getMBeanInfo(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        throws InstanceNotFoundException, IntrospectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
               ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        DynamicMBean moi = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        final MBeanInfo mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            mbi = moi.getMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        } catch (RuntimeMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        } catch (RuntimeErrorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                    "getMBeanInfo threw RuntimeException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            throw new RuntimeErrorException(e, "getMBeanInfo threw Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        if (mbi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            throw new JMRuntimeException("MBean " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                                         "has no MBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1448
        checkMBeanPermission(mbeanServerName, mbi.getClassName(), null, name, "getMBeanInfo");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        return mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    public boolean isInstanceOf(ObjectName name, String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1456
        final DynamicMBean instance = getMBean(name);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1457
        checkMBeanPermission(mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1458
                instance, null, name, "isInstanceOf");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        try {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1461
            Object resource = getResource(instance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1463
            final String resourceClassName =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1464
                    (resource instanceof DynamicMBean) ?
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1465
                        getClassName((DynamicMBean) resource) :
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1466
                        resource.getClass().getName();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1467
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1468
            if (resourceClassName.equals(className))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                return true;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1470
            final ClassLoader cl = getResourceLoader(instance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            final Class<?> classNameClass = Class.forName(className, false, cl);
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1473
            if (classNameClass.isInstance(resource))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1476
            // Ensure that isInstanceOf(NotificationEmitter) is true when
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1477
            // the MBean is a NotificationEmitter by virtue of a @Resource
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1478
            // annotation specifying a SendNotification resource.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1479
            // This is a hack.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1480
            if (instance instanceof NotificationBroadcaster &&
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1481
                    classNameClass.isAssignableFrom(NotificationEmitter.class))
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1482
                return true;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1483
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1484
            final Class<?> resourceClass = Class.forName(resourceClassName, false, cl);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1485
            return classNameClass.isAssignableFrom(resourceClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            /* Could be SecurityException or ClassNotFoundException */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                        "isInstanceOf", "Exception calling isInstanceOf", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * <p>Return the {@link java.lang.ClassLoader} that was used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * loading the class of the named MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @param mbeanName The ObjectName of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * @return The ClassLoader used for that MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * @exception InstanceNotFoundException if the named MBean is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    public ClassLoader getClassLoaderFor(ObjectName mbeanName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        DynamicMBean instance = getMBean(mbeanName);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1509
        checkMBeanPermission(mbeanServerName, instance, null, mbeanName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1510
                "getClassLoaderFor");
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1511
        return getResourceLoader(instance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * <p>Return the named {@link java.lang.ClassLoader}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * @param loaderName The ObjectName of the ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @return The named ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * @exception InstanceNotFoundException if the named ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    public ClassLoader getClassLoader(ObjectName loaderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        if (loaderName == null) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1525
            checkMBeanPermission(mbeanServerName, (String) null, null, null, "getClassLoader");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            return server.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        DynamicMBean instance = getMBean(loaderName);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1530
        checkMBeanPermission(mbeanServerName, instance, null, loaderName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1531
                "getClassLoader");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        /* Check if the given MBean is a ClassLoader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        if (!(resource instanceof ClassLoader))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            throw new InstanceNotFoundException(loaderName.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                                " is not a classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        return (ClassLoader) resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * Sends an MBeanServerNotifications with the specified type for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * MBean with the specified ObjectName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    private void sendNotification(String NotifType, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // Create notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        MBeanServerNotification notif = new MBeanServerNotification(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            NotifType,MBeanServerDelegate.DELEGATE_NAME,0,name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                    "sendNotification", NotifType + " " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        delegate.sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * Applies the specified queries to the set of NamedObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    private Set<ObjectName>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        objectNamesFromFilteredNamedObjects(Set<NamedObject> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                                            QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        Set<ObjectName> result = new HashSet<ObjectName>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        // No query ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                result.add(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            // Access the filter
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1581
            final MBeanServer oldServer = QueryEval.getMBeanServer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    boolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                        res = query.apply(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                        res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                    if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                        result.add(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                 * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                 * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                 * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                 * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                 * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                 * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                 * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * Applies the specified queries to the set of NamedObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    private Set<ObjectInstance>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        objectInstancesFromFilteredNamedObjects(Set<NamedObject> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                                                QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        Set<ObjectInstance> result = new HashSet<ObjectInstance>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        // No query ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                final DynamicMBean obj = no.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                final String className = safeGetClassName(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                result.add(new ObjectInstance(no.getName(), className));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            // Access the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            MBeanServer oldServer = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                    final DynamicMBean obj = no.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    boolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                        res = query.apply(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                        res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                    if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                        String className = safeGetClassName(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                        result.add(new ObjectInstance(no.getName(), className));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                 * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                 * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                 * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                 * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                 * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                 * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                 * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    private static String safeGetClassName(DynamicMBean mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            return getClassName(mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                        "safeGetClassName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                        "Exception getting MBean class name", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * Applies the specified queries to the set of ObjectInstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    private Set<ObjectInstance>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            filterListOfObjectInstances(Set<ObjectInstance> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                                        QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        // Null query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            Set<ObjectInstance> result = new HashSet<ObjectInstance>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            // Access the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                boolean res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                MBeanServer oldServer = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    res = query.apply(oi.getObjectName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                    res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                     * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                     * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                     * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                     * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                     * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                     * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                     * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                    result.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * Get the existing wrapper for this listener, name, and mbean, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * there is one.  Otherwise, if "create" is true, create and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * return one.  Otherwise, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * We use a WeakHashMap so that if the only reference to a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * listener is in listenerWrappers, it can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * This requires a certain amount of care, because only the key in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * a WeakHashMap is weak; the value is strong.  We need to recover
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * the existing wrapper object (not just an object that is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * to it), so we would like listenerWrappers to map any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * ListenerWrapper to the canonical ListenerWrapper for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * (listener,name,mbean) set.  But we do not want this canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * wrapper to be referenced strongly.  Therefore we put it inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * a WeakReference and that is the value in the WeakHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    private NotificationListener getListenerWrapper(NotificationListener l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                                                    ObjectName name,
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1733
                                                    DynamicMBean mbean,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                                                    boolean create) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1735
        Object resource = getResource(mbean);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1736
        ListenerWrapper wrapper = new ListenerWrapper(l, name, resource);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        synchronized (listenerWrappers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            WeakReference<ListenerWrapper> ref = listenerWrappers.get(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                NotificationListener existing = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                if (existing != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    return existing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                ref = new WeakReference<ListenerWrapper>(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                listenerWrappers.put(wrapper, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                return wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    private static class ListenerWrapper implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        ListenerWrapper(NotificationListener l, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                        Object mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            this.listener = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            this.mbean = mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                                       Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            if (notification != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                if (notification.getSource() == mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    notification.setSource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
             * Listeners are not supposed to throw exceptions.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
             * this one does, we could remove it from the MBean.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
             * might indicate that a connector has stopped working,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
             * for instance, and there is no point in sending future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
             * notifications over that connection.  However, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
             * seems rather drastic, so instead we propagate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
             * exception and let the broadcaster handle it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            listener.handleNotification(notification, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1780
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            if (!(o instanceof ListenerWrapper))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            ListenerWrapper w = (ListenerWrapper) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            return (w.listener == listener && w.mbean == mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                    && w.name.equals(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
             * We compare all three, in case the same MBean object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
             * gets unregistered and then reregistered under a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
             * different name, or the same name gets assigned to two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
             * different MBean objects at different times.  We do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
             * comparisons in this order to avoid the slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
             * ObjectName.equals when possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1797
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            return (System.identityHashCode(listener) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                    System.identityHashCode(mbean));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
             * We do not include name.hashCode() in the hash because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
             * computing it is slow and usually we will not have two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
             * instances of ListenerWrapper with the same mbean but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
             * different ObjectNames.  That can happen if the MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
             * unregistered from one name and reregistered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
             * another, and there is no garbage collection between; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
             * if the same object is registered under two names (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
             * is not recommended because MBeanRegistration will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
             * break).  But even in these unusual cases the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
             * does not have to be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        private NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        private ObjectName name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        private Object mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    // SECURITY CHECKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    //----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    private static String getClassName(DynamicMBean mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        if (mbean instanceof DynamicMBean2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            return ((DynamicMBean2) mbean).getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            return mbean.getMBeanInfo().getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1830
    private static void checkMBeanPermission(String mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1831
                                             DynamicMBean mbean,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                                             String member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                                             ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                                             String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        if (sm != null) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1837
            checkMBeanPermission(mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1838
                                 safeGetClassName(mbean),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                                 member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                                 objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                                 actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1845
    private static void checkMBeanPermission(String mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1846
                                             String classname,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                             String member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                                             ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                                             String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        if (sm != null) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1852
            Permission perm = new MBeanPermission(mbeanServerName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1853
                                                  classname,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                                                  member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                                                  objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                                                  actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    private static void checkMBeanTrustPermission(final Class theClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            Permission perm = new MBeanTrustPermission("register");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            PrivilegedAction<ProtectionDomain> act =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                new PrivilegedAction<ProtectionDomain>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    public ProtectionDomain run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        return theClass.getProtectionDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            ProtectionDomain pd = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                new AccessControlContext(new ProtectionDomain[] { pd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            sm.checkPermission(perm, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1879
    // ------------------------------------------------------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1880
    //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1881
    // Dealing with registration of special MBeans in the repository.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1882
    //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1883
    // ------------------------------------------------------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1884
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1885
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1886
     * A RegistrationContext that makes it possible to perform additional
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1887
     * post registration actions (or post unregistration actions) outside
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1888
     * of the repository lock, once postRegister (or postDeregister) has
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1889
     * been called.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1890
     * The method {@code done()} will be called in registerMBean or
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1891
     * unregisterMBean, at the end.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1892
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1893
    private static interface ResourceContext extends RegistrationContext {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1894
        public void done();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1895
        /** An empty ResourceContext which does nothing **/
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1896
        public static final ResourceContext NONE = new ResourceContext() {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1897
            public void done() {}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1898
            public void registering() {}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1899
            public void unregistered() {}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1900
        };
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1901
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1902
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1903
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1904
     * Adds a MBean in the repository,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1905
     * sends MBeanServerNotification.REGISTRATION_NOTIFICATION,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1906
     * returns ResourceContext for special resources such as ClassLoaders
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1907
     * or JMXNamespaces. For regular MBean this method returns
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1908
     * ResourceContext.NONE.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1909
     * @return a ResourceContext for special resources such as ClassLoaders
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1910
     *         or JMXNamespaces.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1911
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1912
    private ResourceContext registerWithRepository(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1913
            final Object resource,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1914
            final DynamicMBean object,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1915
            final ObjectName logicalName)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1916
            throws InstanceAlreadyExistsException,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1917
            MBeanRegistrationException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1918
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1919
        // this will throw an exception if the pair (resource, logicalName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1920
        // violates namespace conventions - for instance, if logicalName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1921
        // ends with // but resource is not a JMXNamespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1922
        //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1923
        checkResourceObjectNameConstraints(resource, logicalName);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1924
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1925
        // Creates a registration context, if needed.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1926
        //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1927
        final ResourceContext context =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1928
                makeResourceContextFor(resource, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1929
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1930
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1931
        repository.addMBean(object, logicalName, context);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1932
        // May throw InstanceAlreadyExistsException
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1933
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1934
        // ---------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1935
        // Send create event
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1936
        // ---------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1937
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1938
            MBEANSERVER_LOGGER.logp(Level.FINER,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1939
                    DefaultMBeanServerInterceptor.class.getName(),
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1940
                    "addObject", "Send create notification of object " +
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1941
                    logicalName.getCanonicalName());
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1942
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1943
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1944
        sendNotification(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1945
                MBeanServerNotification.REGISTRATION_NOTIFICATION,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1946
                logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1947
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1948
        return context;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1949
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1950
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1951
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1952
     * Removes a MBean in the repository,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1953
     * sends MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1954
     * returns ResourceContext for special resources such as ClassLoaders
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1955
     * or JMXNamespaces, or null. For regular MBean this method returns
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1956
     * ResourceContext.NONE.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1957
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1958
     * @return a ResourceContext for special resources such as ClassLoaders
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1959
     *         or JMXNamespaces.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1960
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1961
    private ResourceContext unregisterFromRepository(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1962
            final Object resource,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1963
            final DynamicMBean object,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1964
            final ObjectName logicalName)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1965
            throws InstanceNotFoundException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1966
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1967
        // Creates a registration context, if needed.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1968
        //
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1969
        final ResourceContext context =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1970
                makeResourceContextFor(resource, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1971
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1972
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1973
        repository.remove(logicalName, context);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1974
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1975
        // ---------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1976
        // Send deletion event
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1977
        // ---------------------
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1978
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1979
            MBEANSERVER_LOGGER.logp(Level.FINER,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1980
                    DefaultMBeanServerInterceptor.class.getName(),
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1981
                    "unregisterMBean", "Send delete notification of object " +
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1982
                    logicalName.getCanonicalName());
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1983
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1984
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1985
        sendNotification(MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1986
                logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1987
        return context;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1988
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  1989
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1990
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1991
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1992
     * Checks that the ObjectName is legal with regards to the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1993
     * type of the MBean resource.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1994
     * If the MBean name is  domain:type=JMXDomain, the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1995
     *     MBean must be a JMXDomain.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1996
     * If the MBean name is  namespace//:type=JMXNamespace, the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1997
     *     MBean must be a JMXNamespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1998
     * If the MBean is a JMXDomain, its name
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  1999
     *      must be domain:type=JMXDomain.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2000
     * If the MBean is a JMXNamespace,  its name
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2001
     *      must be namespace//:type=JMXNamespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2002
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2003
    private void checkResourceObjectNameConstraints(Object resource,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2004
            ObjectName logicalName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2005
            throws MBeanRegistrationException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2006
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2007
            dispatcher.checkLocallyRegistrable(resource, logicalName);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2008
        } catch (Throwable x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2009
            DefaultMBeanServerInterceptor.throwMBeanRegistrationException(x, "validating ObjectName");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2010
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2011
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2012
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2013
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2014
     * Registers a JMXNamespace with the dispatcher.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2015
     * This method is called by the ResourceContext from within the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2016
     * repository lock.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2017
     * @param namespace    The JMXNamespace
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2018
     * @param logicalName  The JMXNamespaceMBean ObjectName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2019
     * @param postQueue    A queue that will be processed after postRegister.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2020
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2021
    private void addJMXNamespace(JMXNamespace namespace,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2022
            final ObjectName logicalName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2023
            final Queue<Runnable> postQueue) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2024
        dispatcher.addNamespace(logicalName, namespace, postQueue);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2025
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2026
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2027
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2028
     * Unregisters a JMXNamespace from the dispatcher.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2029
     * This method is called by the ResourceContext from within the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2030
     * repository lock.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2031
     * @param namespace    The JMXNamespace
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2032
     * @param logicalName  The JMXNamespaceMBean ObjectName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2033
     * @param postQueue    A queue that will be processed after postDeregister.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2034
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2035
    private void removeJMXNamespace(JMXNamespace namespace,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2036
            final ObjectName logicalName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2037
            final Queue<Runnable> postQueue) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2038
        dispatcher.removeNamespace(logicalName, namespace, postQueue);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2039
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2040
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2041
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2042
     * Registers a ClassLoader with the CLR.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2043
     * This method is called by the ResourceContext from within the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2044
     * repository lock.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2045
     * @param loader       The ClassLoader.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2046
     * @param logicalName  The ClassLoader MBean ObjectName.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2047
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2048
    private void addClassLoader(ClassLoader loader,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2049
            final ObjectName logicalName) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2050
        /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2051
         * Called when the newly registered MBean is a ClassLoader
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2052
         * If so, tell the ClassLoaderRepository (CLR) about it.  We do
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2053
         * this even if the loader is a PrivateClassLoader.  In that
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2054
         * case, the CLR remembers the loader for use when it is
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2055
         * explicitly named (e.g. as the loader in createMBean) but
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2056
         * does not add it to the list that is consulted by
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2057
         * ClassLoaderRepository.loadClass.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2058
         */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2059
        final ModifiableClassLoaderRepository clr =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2060
                instantiator.getClassLoaderRepository();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2061
        if (clr == null) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2062
            final RuntimeException wrapped =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2063
                    new IllegalArgumentException(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2064
                    "Dynamic addition of class loaders" +
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2065
                    " is not supported");
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2066
            throw new RuntimeOperationsException(wrapped,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2067
                    "Exception occurred trying to register" +
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2068
                    " the MBean as a class loader");
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2069
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2070
        clr.addClassLoader(logicalName, loader);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2071
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2072
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2073
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2074
     * Unregisters a ClassLoader from the CLR.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2075
     * This method is called by the ResourceContext from within the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2076
     * repository lock.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2077
     * @param loader       The ClassLoader.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2078
     * @param logicalName  The ClassLoader MBean ObjectName.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2079
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2080
    private void removeClassLoader(ClassLoader loader,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2081
            final ObjectName logicalName) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2082
        /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2083
         * Removes the  MBean from the default loader repository.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2084
         */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2085
        if (loader != server.getClass().getClassLoader()) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2086
            final ModifiableClassLoaderRepository clr =
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2087
                    instantiator.getClassLoaderRepository();
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2088
            if (clr != null) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2089
                clr.removeClassLoader(logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2090
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2091
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2092
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2093
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2094
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2095
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2096
     * Creates a ResourceContext for a JMXNamespace MBean.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2097
     * The resource context makes it possible to add the JMXNamespace to
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2098
     * (ResourceContext.registering) or resp. remove the JMXNamespace from
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2099
     * (ResourceContext.unregistered) the NamespaceDispatchInterceptor
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2100
     * when the associated MBean is added to or resp. removed from the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2101
     * repository.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2102
     * Note: JMXDomains are special sub classes of JMXNamespaces and
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2103
     *       are also handled by this object.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2104
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2105
     * @param namespace    The JMXNamespace MBean being registered or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2106
     *                     unregistered.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2107
     * @param logicalName  The name of the JMXNamespace MBean.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2108
     * @return a ResourceContext that takes in charge the addition or removal
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2109
     *         of the namespace to or from the NamespaceDispatchInterceptor.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2110
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2111
    private ResourceContext createJMXNamespaceContext(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2112
            final JMXNamespace namespace,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2113
            final ObjectName logicalName) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2114
        final Queue<Runnable> doneTaskQueue = new LinkedList<Runnable>();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2115
        return new ResourceContext() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2116
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2117
            public void registering() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2118
                addJMXNamespace(namespace, logicalName, doneTaskQueue);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2119
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2120
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2121
            public void unregistered() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2122
                removeJMXNamespace(namespace, logicalName,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2123
                                   doneTaskQueue);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2124
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2125
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2126
            public void done() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2127
                for (Runnable r : doneTaskQueue) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2128
                    try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2129
                        r.run();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2130
                    } catch (RuntimeException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2131
                        MBEANSERVER_LOGGER.log(Level.FINE,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2132
                                "Failed to process post queue for "+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2133
                                logicalName, x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2134
                    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2135
                }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2136
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2137
        };
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2138
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2139
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2140
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2141
     * Creates a ResourceContext for a ClassLoader MBean.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2142
     * The resource context makes it possible to add the ClassLoader to
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2143
     * (ResourceContext.registering) or resp. remove the ClassLoader from
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2144
     * (ResourceContext.unregistered) the CLR
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2145
     * when the associated MBean is added to or resp. removed from the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2146
     * repository.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2147
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2148
     * @param loader       The ClassLoader MBean being registered or
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2149
     *                     unregistered.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2150
     * @param logicalName  The name of the ClassLoader MBean.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2151
     * @return a ResourceContext that takes in charge the addition or removal
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2152
     *         of the loader to or from the CLR.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2153
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2154
    private ResourceContext createClassLoaderContext(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2155
            final ClassLoader loader,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2156
            final ObjectName logicalName) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2157
        return new ResourceContext() {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2158
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2159
            public void registering() {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2160
                addClassLoader(loader, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2161
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2162
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2163
            public void unregistered() {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2164
                removeClassLoader(loader, logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2165
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2166
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2167
            public void done() {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2168
            }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2169
        };
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2170
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2171
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2172
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2173
     * Creates a ResourceContext for the given resource.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2174
     * If the resource does not need a ResourceContext, returns
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2175
     * ResourceContext.NONE.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2176
     * At this time, only JMXNamespaces and ClassLoaders need a
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2177
     * ResourceContext.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2178
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2179
     * @param resource     The resource being registered or unregistered.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2180
     * @param logicalName  The name of the associated MBean.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2181
     * @return
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2182
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2183
    private ResourceContext makeResourceContextFor(Object resource,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2184
            ObjectName logicalName) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2185
        if (resource instanceof JMXNamespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2186
            return createJMXNamespaceContext((JMXNamespace) resource,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2187
                    logicalName);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2188
        }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2189
        if (resource instanceof ClassLoader) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2190
            return createClassLoaderContext((ClassLoader) resource,
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2191
                    logicalName);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2192
        }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2193
        return ResourceContext.NONE;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 526
diff changeset
  2194
    }
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2195
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 1002
diff changeset
  2196
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
}