jdk/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 526 61ba2d5ea9da
child 834 dc74d4ddc28e
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
// JMX import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.AttributeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.InstanceAlreadyExistsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.JMRuntimeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.MalformedObjectNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.MBeanPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.management.MBeanRegistrationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.management.MBeanServerDelegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.MBeanServerNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.MBeanTrustPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.management.NotificationBroadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.management.ObjectInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.management.QueryEval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.management.QueryExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.management.RuntimeErrorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.management.RuntimeMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
// JMX RI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import com.sun.jmx.mbeanserver.DynamicMBean2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import com.sun.jmx.mbeanserver.ModifiableClassLoaderRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import com.sun.jmx.mbeanserver.MBeanInstantiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import com.sun.jmx.mbeanserver.Repository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
import com.sun.jmx.mbeanserver.NamedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import com.sun.jmx.mbeanserver.Introspector;
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    87
import com.sun.jmx.mbeanserver.Util;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import com.sun.jmx.remote.util.EnvHelp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * This is the default class for MBean manipulation on the agent side. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * contains the methods necessary for the creation, registration, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * deletion of MBeans as well as the access methods for registered MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * This is the core component of the JMX infrastructure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Every MBean which is added to the MBean server becomes manageable: its attributes and operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * become remotely accessible through the connectors/adaptors connected to that MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * A Java object cannot be registered in the MBean server unless it is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * When an MBean is registered or unregistered in the MBean server an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * {@link javax.management.MBeanServerNotification MBeanServerNotification}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Notification is emitted. To register an object as listener to MBeanServerNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * you should call the MBean server method {@link #addNotificationListener addNotificationListener} with <CODE>ObjectName</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * the <CODE>ObjectName</CODE> of the {@link javax.management.MBeanServerDelegate MBeanServerDelegate}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * This <CODE>ObjectName</CODE> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <CODE>JMImplementation:type=MBeanServerDelegate</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
public class DefaultMBeanServerInterceptor implements MBeanServerInterceptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** The MBeanInstantiator object used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private final transient MBeanInstantiator instantiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** The MBean server object that is associated to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private transient MBeanServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** The MBean server object taht associated to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *  DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final transient MBeanServerDelegate delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** The Repository object used by the DefaultMBeanServerInterceptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private final transient Repository repository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /** Wrappers for client listeners.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* See the comment before addNotificationListener below.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private final transient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        WeakHashMap<ListenerWrapper, WeakReference<ListenerWrapper>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            listenerWrappers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                new WeakHashMap<ListenerWrapper,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                WeakReference<ListenerWrapper>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** The default domain of the object names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private final String domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /** True if the repository perform queries, false otherwise */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private boolean queryByRepo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /** The sequence number identifyng the notifications sent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // Now sequence number is handled by MBeanServerDelegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // private int sequenceNumber=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Creates a DefaultMBeanServerInterceptor with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * repository instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>Do not forget to call <code>initialize(outer,delegate)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * before using this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param outer A pointer to the MBeanServer object that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *        passed to the MBeans when invoking their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *        {@link javax.management.MBeanRegistration} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param delegate A pointer to the MBeanServerDelegate associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *        with the new MBeanServer. The new MBeanServer must register
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *        this MBean in its MBean repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param instantiator The MBeanInstantiator that will be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *        instantiate MBeans and take care of class loading issues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param repository The repository to use for this MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public DefaultMBeanServerInterceptor(MBeanServer         outer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                         MBeanServerDelegate delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                         MBeanInstantiator   instantiator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                         Repository          repository)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (outer == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            IllegalArgumentException("outer MBeanServer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (delegate == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            IllegalArgumentException("MBeanServerDelegate cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (instantiator == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            IllegalArgumentException("MBeanInstantiator cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (repository == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            IllegalArgumentException("Repository cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.server   = outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.delegate = delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.instantiator = instantiator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.repository   = repository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.domain       = repository.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public ObjectInstance createMBean(String className, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return createMBean(className, name, (Object[]) null, (String[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                      ObjectName loaderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
               NotCompliantMBeanException, InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return createMBean(className, name, loaderName, (Object[]) null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                           (String[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                      Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
               NotCompliantMBeanException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return createMBean(className, name, null, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                               params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (InstanceNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            /* Can only happen if loaderName doesn't exist, but we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
               passed null, so we shouldn't get this exception.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                new IllegalArgumentException("Unexpected exception: " + e), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                      ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                      Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
               NotCompliantMBeanException, InstanceNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return createMBean(className, name, loaderName, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                           params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private ObjectInstance createMBean(String className, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                       ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                       boolean withDefaultLoaderRepository,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                       Object[] params, String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        throws ReflectionException, InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
               MBeanRegistrationException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
               NotCompliantMBeanException, InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Class theClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                new IllegalArgumentException("The class name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                      "Exception occurred during MBean creation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (name.isPattern()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    new IllegalArgumentException("Invalid name->" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                                 name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                final String msg = "Exception occurred during MBean creation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throw new RuntimeOperationsException(wrapped, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        checkMBeanPermission(className, null, null, "instantiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        checkMBeanPermission(className, null, name, "registerMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /* Load the appropriate class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (withDefaultLoaderRepository) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        "createMBean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        "ClassName = " + className + ", ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            theClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                instantiator.findClassWithDefaultLoaderRepository(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        "createMBean", "ClassName = " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        ", ObjectName = " + name + ", Loader name = null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            theClass = instantiator.findClass(className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                  server.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            loaderName = nonDefaultDomain(loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        "createMBean", "ClassName = " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        ", ObjectName = " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        ", Loader name = " + loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            theClass = instantiator.findClass(className, loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        checkMBeanTrustPermission(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Check that the MBean can be instantiated by the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Introspector.testCreation(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Check the JMX MBean compliance of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Introspector.checkCompliance(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Object moi= instantiator.instantiate(theClass, params,  signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                             server.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        final String infoClassName = getNewMBeanClassName(moi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return registerObject(infoClassName, moi, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public ObjectInstance registerMBean(Object object, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throws InstanceAlreadyExistsException, MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        NotCompliantMBeanException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Class theClass = object.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Introspector.checkCompliance(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        final String infoClassName = getNewMBeanClassName(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        checkMBeanPermission(infoClassName, null, name, "registerMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        checkMBeanTrustPermission(theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return registerObject(infoClassName, object, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static String getNewMBeanClassName(Object mbeanToRegister)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (mbeanToRegister instanceof DynamicMBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            DynamicMBean mbean = (DynamicMBean) mbeanToRegister;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                name = mbean.getMBeanInfo().getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // Includes case where getMBeanInfo() returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                NotCompliantMBeanException ncmbe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    new NotCompliantMBeanException("Bad getMBeanInfo()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                ncmbe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                throw ncmbe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                final String msg = "MBeanInfo has null class name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return mbeanToRegister.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private final Set<ObjectName> beingUnregistered =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        new HashSet<ObjectName>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public void unregisterMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throws InstanceNotFoundException, MBeanRegistrationException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                new IllegalArgumentException("Object name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                      "Exception occurred trying to unregister the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /* The semantics of preDeregister are tricky.  If it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
           exception, then the unregisterMBean fails.  This allows an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
           MBean to refuse to be unregistered.  If it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
           successfully, then the unregisterMBean can proceed.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
           this case the preDeregister may have cleaned up some state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
           and will not expect to be called a second time.  So if two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
           threads try to unregister the same MBean at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
           then one of them must wait for the other one to either (a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
           call preDeregister and get an exception or (b) call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
           preDeregister successfully and unregister the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
           Suppose thread T1 is unregistering an MBean and thread T2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
           is trying to unregister the same MBean, so waiting for T1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
           Then a deadlock is possible if the preDeregister for T1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
           ends up needing a lock held by T2.  Given the semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
           just described, there does not seem to be any way to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
           this.  This will not happen to code where it is clear for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
           any given MBean what thread may unregister that MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
           On the other hand we clearly do not want a thread that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
           unregistering MBean A to have to wait for another thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
           that is unregistering another MBean B (see bug 6318664).  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
           deadlock in this situation could reasonably be considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
           gratuitous.  So holding a global lock across the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
           preDeregister call would be bad.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
           So we have a set of ObjectNames that some thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
           currently unregistering.  When a thread wants to unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
           a name, it must first check if the name is in the set, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
           if so it must wait.  When a thread successfully unregisters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
           a name it removes the name from the set and notifies any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
           waiting threads that the set has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
           This implies that we must be very careful to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
           the name is removed from the set and waiters notified, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
           matter what code path is taken.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        synchronized (beingUnregistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            while (beingUnregistered.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    beingUnregistered.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    throw new MBeanRegistrationException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    // pretend the exception came from preDeregister;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // in another execution sequence it could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            beingUnregistered.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            exclusiveUnregisterMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            synchronized (beingUnregistered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                beingUnregistered.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                beingUnregistered.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private void exclusiveUnregisterMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            throws InstanceNotFoundException, MBeanRegistrationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // may throw InstanceNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        checkMBeanPermission(instance, null, name, "unregisterMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (instance instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            preDeregisterInvoke((MBeanRegistration) instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        repository.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // may throw InstanceNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * Checks if the unregistered MBean is a ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * If so, it removes the  MBean from the default loader repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (resource instanceof ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            && resource != server.getClass().getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            final ModifiableClassLoaderRepository clr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                instantiator.getClassLoaderRepository();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (clr != null) clr.removeClassLoader(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Send deletion event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    "unregisterMBean", "Send delete notification of object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    name.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        sendNotification(MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                         name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (instance instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            postDeregisterInvoke((MBeanRegistration) instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public ObjectInstance getObjectInstance(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        checkMBeanPermission(instance, null, name, "getObjectInstance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        final String className = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return new ObjectInstance(name, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            // Check if the caller has the right to invoke 'queryMBeans'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            checkMBeanPermission((String) null, null, null, "queryMBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // Perform query without "query".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            Set<ObjectInstance> list = queryMBeansImpl(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            // Check if the caller has the right to invoke 'queryMBeans'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            // on each specific classname/objectname in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            Set<ObjectInstance> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                new HashSet<ObjectInstance>(list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    checkMBeanPermission(oi.getClassName(), null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                         oi.getObjectName(), "queryMBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    allowedList.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    // OK: Do not add this ObjectInstance to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // Apply query to allowed MBeans only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return filterListOfObjectInstances(allowedList, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // Perform query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return queryMBeansImpl(name, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private Set<ObjectInstance> queryMBeansImpl(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                                QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Query the MBeans on the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   521
        Set<NamedObject> list = repository.query(name, query);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (queryByRepo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // The repository performs the filtering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            query = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return (objectInstancesFromFilteredNamedObjects(list, query));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        Set<ObjectName> queryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // Check if the caller has the right to invoke 'queryNames'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            checkMBeanPermission((String) null, null, null, "queryNames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // Perform query without "query".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            Set<ObjectInstance> list = queryMBeansImpl(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            // Check if the caller has the right to invoke 'queryNames'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            // on each specific classname/objectname in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            Set<ObjectInstance> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                new HashSet<ObjectInstance>(list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    checkMBeanPermission(oi.getClassName(), null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                         oi.getObjectName(), "queryNames");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    allowedList.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    // OK: Do not add this ObjectInstance to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            // Apply query to allowed MBeans only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            Set<ObjectInstance> queryObjectInstanceList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                filterListOfObjectInstances(allowedList, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            queryList = new HashSet<ObjectName>(queryObjectInstanceList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for (ObjectInstance oi : queryObjectInstanceList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                queryList.add(oi.getObjectName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            // Perform query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            queryList = queryNamesImpl(name, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return queryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private Set<ObjectName> queryNamesImpl(ObjectName name, QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        // Query the MBeans on the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
   577
        Set<NamedObject> list = repository.query(name, query);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (queryByRepo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            // The repository performs the filtering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            query = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return (objectNamesFromFilteredNamedObjects(list, query));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public boolean isRegistered(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            throw new RuntimeOperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                     new IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                     "Object name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
//      /* Permission check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
//      checkMBeanPermission(null, null, name, "isRegistered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return (repository.contains(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public String[] getDomains()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // Check if the caller has the right to invoke 'getDomains'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            checkMBeanPermission((String) null, null, null, "getDomains");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            // Return domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            String[] domains = repository.getDomains();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // Check if the caller has the right to invoke 'getDomains'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // on each specific domain in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            List<String> result = new ArrayList<String>(domains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            for (int i = 0; i < domains.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                try {
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   619
                    ObjectName domain = Util.newObjectName(domains[i] + ":x=x");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    checkMBeanPermission((String) null, null, domain, "getDomains");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    result.add(domains[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    // OK: Do not add this domain to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // Make an array from result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return repository.getDomains();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public Integer getMBeanCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return (repository.getCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public Object getAttribute(ObjectName name, String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        throws MBeanException, AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
               InstanceNotFoundException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                IllegalArgumentException("Attribute cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    "getAttribute",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    "Attribute = " + attribute + ", ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        final DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        checkMBeanPermission(instance, attribute, name, "getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return instance.getAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        } catch (AttributeNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            rethrowMaybeMBeanException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            throw new AssertionError(); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public AttributeList getAttributes(ObjectName name, String[] attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        throws InstanceNotFoundException, ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                IllegalArgumentException("ObjectName name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                IllegalArgumentException("Attributes cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                "Exception occurred trying to invoke the getter on the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    "getAttributes", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        final DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        final String[] allowedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (sm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            allowedAttributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            final String classname = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // Check if the caller has the right to invoke 'getAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            checkMBeanPermission(classname, null, name, "getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // Check if the caller has the right to invoke 'getAttribute'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            // on each specific attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            List<String> allowedList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                new ArrayList<String>(attributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            for (String attr : attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    checkMBeanPermission(classname, attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                         name, "getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    allowedList.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    // OK: Do not add this attribute to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            allowedAttributes = allowedList.toArray(new String[0]);
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        checkMBeanPermission(instance, attribute.getName(),
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
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            checkMBeanPermission(classname, null, name, "setAttribute");
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();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    checkMBeanPermission(classname, attribute.getName(),
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        checkMBeanPermission(instance, operationName, name, "invoke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return instance.invoke(operationName, params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            rethrowMaybeMBeanException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /* Centralize some of the tedious exception wrapping demanded by the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
       spec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    private static void rethrow(Throwable t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            throws ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            throw t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        } catch (ReflectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        } catch (RuntimeOperationsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        } catch (RuntimeErrorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            throw new RuntimeMBeanException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            throw new RuntimeErrorException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        } catch (Throwable t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            throw new RuntimeException("Unexpected exception", t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private static void rethrowMaybeMBeanException(Throwable t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            throws ReflectionException, MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (t instanceof MBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            throw (MBeanException) t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        rethrow(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Register <code>object</code> in the repository, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * given <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * This method is called by the various createMBean() flavours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * and by registerMBean() after all MBean compliance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * have been performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * This method does not performed any kind of test compliance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * and the caller should make sure that the given <code>object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * is MBean compliant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * This methods performed all the basic steps needed for object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * registration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <li>If the <code>object</code> implements the MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *     interface, it invokes preRegister() on the object.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <li>Then the object is added to the repository with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *     <code>name</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * <li>Finally, if the <code>object</code> implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *     MBeanRegistration interface, it invokes postRegister()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *     on the object.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @param object A reference to a MBean compliant object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @param name   The ObjectName of the <code>object</code> MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @return the actual ObjectName with which the object was registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @exception InstanceAlreadyExistsException if an object is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *            registered with that name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @exception MBeanRegistrationException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *            registration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    private ObjectInstance registerObject(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                                          Object object, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        throws InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
               MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if (object == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                new IllegalArgumentException("Cannot add null object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                        "Exception occurred trying to register the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        DynamicMBean mbean = Introspector.makeDynamicMBean(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return registerDynamicMBean(classname, mbean, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    private ObjectInstance registerDynamicMBean(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                                DynamicMBean mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                                ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        throws InstanceAlreadyExistsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
               MBeanRegistrationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
               NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        name = nonDefaultDomain(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    "registerMBean", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        ObjectName logicalName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (mbean instanceof MBeanRegistration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            MBeanRegistration reg = (MBeanRegistration) mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            logicalName = preRegisterInvoke(reg, name, server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (mbean instanceof DynamicMBean2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    ((DynamicMBean2) mbean).preRegister2(server, logicalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    postRegisterInvoke(reg, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    if (e instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    if (e instanceof InstanceAlreadyExistsException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        throw (InstanceAlreadyExistsException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            if (logicalName != name && logicalName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                logicalName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    ObjectName.getInstance(nonDefaultDomain(logicalName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        checkMBeanPermission(classname, null, logicalName, "registerMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        final ObjectInstance result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        if (logicalName!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            result = new ObjectInstance(logicalName, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            internal_addObject(mbean, logicalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (mbean instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                postRegisterInvoke((MBeanRegistration) mbean, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                new IllegalArgumentException("No object name specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        "Exception occurred trying to register the MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if (mbean instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            postRegisterInvoke((MBeanRegistration) mbean, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * Checks if the newly registered MBean is a ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         * If so, tell the ClassLoaderRepository (CLR) about it.  We do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * this even if the object is a PrivateClassLoader.  In that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * case, the CLR remembers the loader for use when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * explicitly named (e.g. as the loader in createMBean) but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * does not add it to the list that is consulted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * ClassLoaderRepository.loadClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        final Object resource = getResource(mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (resource instanceof ClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            final ModifiableClassLoaderRepository clr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                instantiator.getClassLoaderRepository();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            if (clr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                final RuntimeException wrapped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                     "Dynamic addition of class loaders is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                throw new RuntimeOperationsException(wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
           "Exception occurred trying to register the MBean as a class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            clr.addClassLoader(logicalName, (ClassLoader) resource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    private static ObjectName preRegisterInvoke(MBeanRegistration moi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                                ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                                                MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            throws InstanceAlreadyExistsException, MBeanRegistrationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        final ObjectName newName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            newName = moi.preRegister(mbs, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                           "RuntimeException thrown in preRegister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        } catch (Error er) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                throw new RuntimeErrorException(er,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                           "Error thrown in preRegister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        } catch (MBeanRegistrationException r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            throw r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            throw new MBeanRegistrationException(ex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                          "Exception thrown in preRegister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (newName != null) return newName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        else return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    private static void postRegisterInvoke(MBeanRegistration moi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                                           boolean registrationDone,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                           boolean registerFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (registerFailed && moi instanceof DynamicMBean2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            ((DynamicMBean2) moi).registerFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        try {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
  1042
            moi.postRegister(registrationDone);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                      "RuntimeException thrown in postRegister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        } catch (Error er) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            throw new RuntimeErrorException(er,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                      "Error thrown in postRegister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    private static void preDeregisterInvoke(MBeanRegistration moi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            throws MBeanRegistrationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            moi.preDeregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                         "RuntimeException thrown in preDeregister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        } catch (Error er) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            throw new RuntimeErrorException(er,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                         "Error thrown in preDeregister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        } catch (MBeanRegistrationException t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            throw t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            throw new MBeanRegistrationException(ex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                         "Exception thrown in preDeregister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private static void postDeregisterInvoke(MBeanRegistration moi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            moi.postDeregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                         "RuntimeException thrown in postDeregister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        } catch (Error er) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            throw new RuntimeErrorException(er,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                         "Error thrown in postDeregister method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Gets a specific MBean controlled by the DefaultMBeanServerInterceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * The name must have a non-default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    private DynamicMBean getMBean(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                IllegalArgumentException("Object name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                               "Exception occurred trying to get an MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 287
diff changeset
  1094
        DynamicMBean obj = repository.retrieve(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                        "getMBean", name + " : Found no object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            throw new InstanceNotFoundException(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    private static Object getResource(DynamicMBean mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (mbean instanceof DynamicMBean2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            return ((DynamicMBean2) mbean).getResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    private ObjectName nonDefaultDomain(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (name == null || name.getDomain().length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        /* The ObjectName looks like ":a=b", and that's what its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
           toString() will return in this implementation.  So
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
           we can just stick the default domain in front of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
           to get a non-default-domain name.  We depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
           fact that toString() works like that and that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
           leaves wildcards in place (so we can detect an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
           if one is supplied where it shouldn't be).  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        final String completeName = domain + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            return new ObjectName(completeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        } catch (MalformedObjectNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                "Unexpected default domain problem: " + completeName + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            throw EnvHelp.initCause(new IllegalArgumentException(msg), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    public String getDefaultDomain()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * Notification handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * This is not trivial, because the MBeanServer translates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * source of a received notification from a reference to an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * into the ObjectName of that MBean.  While that does make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * notification sending easier for MBean writers, it comes at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * considerable cost.  We need to replace the source of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * notification, which is basically wrong if there are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * listeners registered directly with the MBean (without going
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * through the MBean server).  We also need to wrap the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * supplied by the client of the MBeanServer with a listener that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * performs the substitution before forwarding.  This is why we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * strongly discourage people from putting MBean references in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * source of their notifications.  Instead they should arrange to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * put the ObjectName there themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * However, existing code relies on the substitution, so we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * stuck with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * Here's how we handle it.  When you add a listener, we make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * ListenerWrapper around it.  We look that up in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * listenerWrappers map, and if there was already a wrapper for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * that listener with the given ObjectName, we reuse it.  This map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * is a WeakHashMap, so a listener that is no longer registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * with any MBean can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * We cannot use simpler solutions such as always creating a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * wrapper or always registering the same listener with the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * and using the handback to find the client's original listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * The reason is that we need to support the removeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * variant that removes all (listener,filter,handback) triples on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * a broadcaster that have a given listener.  And we do not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * any way to inspect a broadcaster's internal list of triples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * So the same client listener must always map to the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * listener registered with the broadcaster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Another possible solution would be to map from ObjectName to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * list of listener wrappers (or IdentityHashMap of listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * wrappers), making this list the first time a listener is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * on a given MBean, and removing it when the MBean is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * This is probably more costly in memory, but could be useful if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * some day we don't want to rely on weak references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    public void addNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                                        NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    "addNotificationListener", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        checkMBeanPermission(instance, null, name, "addNotificationListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        NotificationBroadcaster broadcaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                getNotificationBroadcaster(name, instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                                           NotificationBroadcaster.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        // Check listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        // ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                IllegalArgumentException("Null listener"),"Null listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        NotificationListener listenerWrapper =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            getListenerWrapper(listener, name, broadcaster, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        broadcaster.addNotificationListener(listenerWrapper, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    public void addNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                        ObjectName listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        // Get listener object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        DynamicMBean instance = getMBean(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (!(resource instanceof NotificationListener)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                IllegalArgumentException(listener.getCanonicalName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                "The MBean " + listener.getCanonicalName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                "does not implement the NotificationListener interface") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // Add a listener on an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    "addNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        server.addNotificationListener(name,(NotificationListener) resource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                                       filter, handback) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                                           NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        removeNotificationListener(name, listener, null, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                           NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        removeNotificationListener(name, listener, filter, handback, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                                           ObjectName listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        NotificationListener instance = getListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    "removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        server.removeNotificationListener(name, instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                           ObjectName listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            throws InstanceNotFoundException, ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        NotificationListener instance = getListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    "removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    "ObjectName = " + name + ", Listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        server.removeNotificationListener(name, instance, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    private NotificationListener getListener(ObjectName listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // Get listener object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        // ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        DynamicMBean instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            instance = getMBean(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        } catch (InstanceNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            throw EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                          new ListenerNotFoundException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (!(resource instanceof NotificationListener)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            final RuntimeException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                new IllegalArgumentException(listener.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                "MBean " + listener.getCanonicalName() + " does not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                "implement " + NotificationListener.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            throw new RuntimeOperationsException(exc, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        return (NotificationListener) resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    private void removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                            NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                            NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                            Object handback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                                            boolean removeAll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            throws InstanceNotFoundException, ListenerNotFoundException {
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", "ObjectName = " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        checkMBeanPermission(instance, null, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                             "removeNotificationListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        /* We could simplify the code by assigning broadcaster after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
           assigning listenerWrapper, but that would change the error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
           behavior when both the broadcaster and the listener are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
           erroneous.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        Class<? extends NotificationBroadcaster> reqClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            removeAll ? NotificationBroadcaster.class : NotificationEmitter.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        NotificationBroadcaster broadcaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            getNotificationBroadcaster(name, instance, reqClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        NotificationListener listenerWrapper =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            getListenerWrapper(listener, name, resource, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        if (listenerWrapper == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            throw new ListenerNotFoundException("Unknown listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        if (removeAll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            broadcaster.removeNotificationListener(listenerWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            NotificationEmitter emitter = (NotificationEmitter) broadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            emitter.removeNotificationListener(listenerWrapper,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                                               filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                                               handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    private static <T extends NotificationBroadcaster>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            T getNotificationBroadcaster(ObjectName name, Object instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                                         Class<T> reqClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        if (instance instanceof DynamicMBean2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            instance = ((DynamicMBean2) instance).getResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        if (reqClass.isInstance(instance))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            return reqClass.cast(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        final RuntimeException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            new IllegalArgumentException(name.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            "MBean " + name.getCanonicalName() + " does not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            "implement " + reqClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        throw new RuntimeOperationsException(exc, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    public MBeanInfo getMBeanInfo(ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        throws InstanceNotFoundException, IntrospectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
               ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        DynamicMBean moi = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        final MBeanInfo mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            mbi = moi.getMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        } catch (RuntimeMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        } catch (RuntimeErrorException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            throw new RuntimeMBeanException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    "getMBeanInfo threw RuntimeException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            throw new RuntimeErrorException(e, "getMBeanInfo threw Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (mbi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            throw new JMRuntimeException("MBean " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                                         "has no MBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        checkMBeanPermission(mbi.getClassName(), null, name, "getMBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        return mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    public boolean isInstanceOf(ObjectName name, String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        DynamicMBean instance = getMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        checkMBeanPermission(instance, null, name, "isInstanceOf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            if (instance instanceof DynamicMBean2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                Object resource = ((DynamicMBean2) instance).getResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                ClassLoader loader = resource.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                Class<?> c = Class.forName(className, false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                return c.isInstance(resource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            final String cn = getClassName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (cn.equals(className))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            final ClassLoader cl = instance.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            final Class<?> classNameClass = Class.forName(className, false, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            if (classNameClass.isInstance(instance))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            final Class<?> instanceClass = Class.forName(cn, false, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            return classNameClass.isAssignableFrom(instanceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            /* Could be SecurityException or ClassNotFoundException */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                        "isInstanceOf", "Exception calling isInstanceOf", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * <p>Return the {@link java.lang.ClassLoader} that was used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * loading the class of the named MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @param mbeanName The ObjectName of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * @return The ClassLoader used for that MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @exception InstanceNotFoundException if the named MBean is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    public ClassLoader getClassLoaderFor(ObjectName mbeanName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        DynamicMBean instance = getMBean(mbeanName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        checkMBeanPermission(instance, null, mbeanName, "getClassLoaderFor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        return getResource(instance).getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * <p>Return the named {@link java.lang.ClassLoader}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @param loaderName The ObjectName of the ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @return The named ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * @exception InstanceNotFoundException if the named ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    public ClassLoader getClassLoader(ObjectName loaderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            checkMBeanPermission((String) null, null, null, "getClassLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            return server.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        DynamicMBean instance = getMBean(loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        checkMBeanPermission(instance, null, loaderName, "getClassLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        Object resource = getResource(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        /* Check if the given MBean is a ClassLoader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (!(resource instanceof ClassLoader))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            throw new InstanceNotFoundException(loaderName.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                                                " is not a classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        return (ClassLoader) resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * Adds a MBean in the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    private void internal_addObject(DynamicMBean object, ObjectName logicalName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        throws InstanceAlreadyExistsException {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        // Let the repository do the work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
           repository.addMBean(object, logicalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }  catch (InstanceAlreadyExistsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            if (object instanceof MBeanRegistration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                postRegisterInvoke((MBeanRegistration) object, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        // Send create event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                    "addObject", "Send create notification of object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    logicalName.getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        sendNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                         logicalName ) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * Sends an MBeanServerNotifications with the specified type for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * MBean with the specified ObjectName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    private void sendNotification(String NotifType, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        // Create notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        // ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        MBeanServerNotification notif = new MBeanServerNotification(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            NotifType,MBeanServerDelegate.DELEGATE_NAME,0,name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            MBEANSERVER_LOGGER.logp(Level.FINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    "sendNotification", NotifType + " " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        delegate.sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * Applies the specified queries to the set of NamedObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    private Set<ObjectName>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        objectNamesFromFilteredNamedObjects(Set<NamedObject> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                            QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        Set<ObjectName> result = new HashSet<ObjectName>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        // No query ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                result.add(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            // Access the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            MBeanServer oldServer = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    boolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                        res = query.apply(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                        res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                        result.add(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                 * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                 * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                 * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                 * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                 * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                 * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                 * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * Applies the specified queries to the set of NamedObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    private Set<ObjectInstance>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        objectInstancesFromFilteredNamedObjects(Set<NamedObject> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                                                QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        Set<ObjectInstance> result = new HashSet<ObjectInstance>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        // No query ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                final DynamicMBean obj = no.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                final String className = safeGetClassName(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                result.add(new ObjectInstance(no.getName(), className));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            // Access the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            MBeanServer oldServer = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                for (NamedObject no : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                    final DynamicMBean obj = no.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    boolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                        res = query.apply(no.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                        res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        String className = safeGetClassName(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                        result.add(new ObjectInstance(no.getName(), className));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                 * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                 * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                 * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                 * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                 * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                 * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                 * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    private static String safeGetClassName(DynamicMBean mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            return getClassName(mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                        DefaultMBeanServerInterceptor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                        "safeGetClassName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                        "Exception getting MBean class name", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * Applies the specified queries to the set of ObjectInstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    private Set<ObjectInstance>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            filterListOfObjectInstances(Set<ObjectInstance> list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                                        QueryExp query) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        // Null query.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            Set<ObjectInstance> result = new HashSet<ObjectInstance>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            // Access the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            for (ObjectInstance oi : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                boolean res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                MBeanServer oldServer = QueryEval.getMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                query.setMBeanServer(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                    res = query.apply(oi.getObjectName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                    res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                     * query.setMBeanServer is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                     * QueryEval.setMBeanServer so put back the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                     * value.  Since that method uses a ThreadLocal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                     * variable, this code is only needed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                     * unusual case where the user creates a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                     * QueryExp that calls a nested query on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                     * MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                    query.setMBeanServer(oldServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    result.add(oi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * Get the existing wrapper for this listener, name, and mbean, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * there is one.  Otherwise, if "create" is true, create and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * return one.  Otherwise, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * We use a WeakHashMap so that if the only reference to a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * listener is in listenerWrappers, it can be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * This requires a certain amount of care, because only the key in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * a WeakHashMap is weak; the value is strong.  We need to recover
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * the existing wrapper object (not just an object that is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * to it), so we would like listenerWrappers to map any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * ListenerWrapper to the canonical ListenerWrapper for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * (listener,name,mbean) set.  But we do not want this canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * wrapper to be referenced strongly.  Therefore we put it inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * a WeakReference and that is the value in the WeakHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    private NotificationListener getListenerWrapper(NotificationListener l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                                                    ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                                                    Object mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                                                    boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        ListenerWrapper wrapper = new ListenerWrapper(l, name, mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        synchronized (listenerWrappers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            WeakReference<ListenerWrapper> ref = listenerWrappers.get(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                NotificationListener existing = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                if (existing != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    return existing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                ref = new WeakReference<ListenerWrapper>(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                listenerWrappers.put(wrapper, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                return wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
    private static class ListenerWrapper implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        ListenerWrapper(NotificationListener l, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                        Object mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            this.listener = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            this.mbean = mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                                       Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            if (notification != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                if (notification.getSource() == mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                    notification.setSource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
             * Listeners are not supposed to throw exceptions.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
             * this one does, we could remove it from the MBean.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
             * might indicate that a connector has stopped working,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
             * for instance, and there is no point in sending future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
             * notifications over that connection.  However, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
             * seems rather drastic, so instead we propagate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
             * exception and let the broadcaster handle it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            listener.handleNotification(notification, handback);
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 boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            if (!(o instanceof ListenerWrapper))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            ListenerWrapper w = (ListenerWrapper) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            return (w.listener == listener && w.mbean == mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    && w.name.equals(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
             * We compare all three, in case the same MBean object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
             * gets unregistered and then reregistered under a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
             * different name, or the same name gets assigned to two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
             * different MBean objects at different times.  We do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
             * comparisons in this order to avoid the slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
             * ObjectName.equals when possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            return (System.identityHashCode(listener) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                    System.identityHashCode(mbean));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
             * We do not include name.hashCode() in the hash because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
             * computing it is slow and usually we will not have two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
             * instances of ListenerWrapper with the same mbean but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
             * different ObjectNames.  That can happen if the MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
             * unregistered from one name and reregistered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
             * another, and there is no garbage collection between; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
             * if the same object is registered under two names (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
             * is not recommended because MBeanRegistration will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
             * break).  But even in these unusual cases the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
             * does not have to be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        private NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        private ObjectName name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        private Object mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    // SECURITY CHECKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    //----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    private static String getClassName(DynamicMBean mbean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        if (mbean instanceof DynamicMBean2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            return ((DynamicMBean2) mbean).getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            return mbean.getMBeanInfo().getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    private static void checkMBeanPermission(DynamicMBean mbean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                                             String member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                                             ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                                             String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            checkMBeanPermission(safeGetClassName(mbean),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                                 member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                                 objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                                 actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    private static void checkMBeanPermission(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                             String member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                                             ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                                             String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            Permission perm = new MBeanPermission(classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                                                  member,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                                  objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                                  actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    private static void checkMBeanTrustPermission(final Class theClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            Permission perm = new MBeanTrustPermission("register");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            PrivilegedAction<ProtectionDomain> act =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                new PrivilegedAction<ProtectionDomain>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                    public ProtectionDomain run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                        return theClass.getProtectionDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            ProtectionDomain pd = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                new AccessControlContext(new ProtectionDomain[] { pd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            sm.checkPermission(perm, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
}