jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanInstantiator.java
author sjiang
Thu, 12 Jun 2014 10:32:19 +0200
changeset 24871 224e298c3978
parent 18207 64dcc0ad2298
permissions -rw-r--r--
8044865: Fix raw and unchecked lint warnings in management-related code Reviewed-by: darcy, sla, egahlin, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
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.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
    35
import java.lang.reflect.Modifier;
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
    36
import java.security.AccessControlContext;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
    37
import java.security.AccessController;
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
    38
import java.security.Permission;
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
    39
import java.security.Permissions;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
    40
import java.security.PrivilegedAction;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
    41
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanException;
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
    47
import javax.management.MBeanPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.OperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.RuntimeErrorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.RuntimeMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.RuntimeOperationsException;
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
    55
import sun.reflect.misc.ConstructorUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.reflect.misc.ReflectUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Implements the MBeanInstantiator interface. Provides methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * instantiating objects, finding the class given its name and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * different class loaders, deserializing objects in the context of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class MBeanInstantiator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final ModifiableClassLoaderRepository clr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //    private MetaData meta = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    MBeanInstantiator(ModifiableClassLoaderRepository clr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.clr = clr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * This methods tests if the MBean class makes it possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * instantiate an MBean of this class in the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * e.g. it must have a public constructor, be a concrete class...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    80
    public void testCreation(Class<?> c) throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Introspector.testCreation(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Loads the class with the specified name using this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Default Loader Repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    88
    public Class<?> findClassWithDefaultLoaderRepository(String className)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    91
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                IllegalArgumentException("The class name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                             "Exception occurred during object instantiation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
    98
        ReflectUtil.checkPackageAccess(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (clr == null) throw new ClassNotFoundException(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            theClass = clr.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        catch (ClassNotFoundException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new ReflectionException(ee,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
       "The MBean class could not be loaded by the default loader repository");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return theClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Gets the class for the specified class name using the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Interceptor's classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   116
    public Class<?> findClass(String className, ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throws ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return loadClass(className,loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Gets the class for the specified class name using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   126
    public Class<?> findClass(String className, ObjectName aLoader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws ReflectionException, InstanceNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (aLoader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                IllegalArgumentException(), "Null loader passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Retrieve the class loader from the repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ClassLoader loader = null;
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   135
        synchronized (this) {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   136
            loader = getClassLoader(aLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new InstanceNotFoundException("The loader named " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                       aLoader + " is not registered in the MBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return findClass(className,loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Return an array of Class corresponding to the given signature, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the specified class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   150
    public Class<?>[] findSignatureClasses(String signature[],
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   151
                                           ClassLoader loader)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   152
        throws ReflectionException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (signature == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        final ClassLoader aLoader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        final int length= signature.length;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   157
        final Class<?> tab[]=new Class<?>[length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (length == 0) return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            for (int i= 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // Start handling primitive types (int. boolean and so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                // forth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   166
                final Class<?> primCla = primitiveClasses.get(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (primCla != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    tab[i] = primCla;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   172
                ReflectUtil.checkPackageAccess(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // Ok we do not have a primitive type ! We need to build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (aLoader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    // We need to load the class through the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    // loader of the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    tab[i] = Class.forName(signature[i], false, aLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    // Load through the default class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    tab[i] = findClass(signature[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                       this.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        MBeanInstantiator.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        "findSignatureClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        "The parameter class could not be found", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                      "The parameter class could not be found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        MBeanInstantiator.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        "findSignatureClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        "Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Instantiates an object given its class, using its empty constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   214
    public Object instantiate(Class<?> theClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        throws ReflectionException, MBeanException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   216
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   217
        checkMBeanPermission(theClass, null, null, "instantiate");
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   218
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   219
        Object moi;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   223
        Constructor<?> cons = findConstructor(theClass, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                NoSuchMethodException("No such constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Instantiate the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            ReflectUtil.checkPackageAccess(theClass);
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   231
            ensureClassAccess(theClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            moi= cons.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // Wrap the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            Throwable t = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (t instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throw new RuntimeMBeanException((RuntimeException)t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                   "RuntimeException thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } else if (t instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw new RuntimeErrorException((Error) t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                   "Error thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                throw new MBeanException((Exception) t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                   "Exception thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (NoSuchMethodError error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                NoSuchMethodException("No constructor"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          "No such constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return moi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Instantiates an object given its class, the parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * signature of its constructor The call returns a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   271
    public Object instantiate(Class<?> theClass, Object params[],
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                              String signature[], ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        throws ReflectionException, MBeanException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   274
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   275
        checkMBeanPermission(theClass, null, null, "instantiate");
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   276
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Instantiate the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   280
        final Class<?>[] tab;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   281
        Object moi;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // Build the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            ClassLoader aLoader= theClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // Build the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            tab =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                ((signature == null)?null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                 findSignatureClasses(signature,aLoader));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // Exception IllegalArgumentException raised in Jdk1.1.8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    "The constructor parameter classes could not be loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // Query the metadata service to get the right constructor
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   299
        Constructor<?> cons = findConstructor(theClass, tab);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                NoSuchMethodException("No such constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            ReflectUtil.checkPackageAccess(theClass);
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   307
            ensureClassAccess(theClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            moi = cons.newInstance(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        catch (NoSuchMethodError error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                NoSuchMethodException("No such constructor found"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                          "No such constructor" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                "Exception thrown trying to invoke the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                "Exception thrown trying to invoke the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // Wrap the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            Throwable th = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (th instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                throw new RuntimeMBeanException((RuntimeException)th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                      "RuntimeException thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } else if (th instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                throw new RuntimeErrorException((Error) th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                      "Error thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                throw new MBeanException((Exception) th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                      "Exception thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return moi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * De-serializes a byte array in the context of a classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param loader the classloader to use for de-serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param data The byte array to be de-sererialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @return  The de-serialized object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @exception OperationsException Any of the usual Input/Output related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public ObjectInputStream deserialize(ClassLoader loader, byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        throws OperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // Check parameter validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                IllegalArgumentException(), "Null data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (data.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                IllegalArgumentException(), "Empty data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // Object deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        ByteArrayInputStream bIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ObjectInputStream    objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        bIn   = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            objIn = new ObjectInputStreamWithLoader(bIn,loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throw new OperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                     "An IOException occurred trying to de-serialize the data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * De-serializes a byte array in the context of a given MBean class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <P>The class loader is the one that loaded the class with name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * "className".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <P>The name of the class loader to be used for loading the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * class is specified. If null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * MBean Server, its own class loader will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param className The name of the class whose class loader should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *  be used for the de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param data The byte array to be de-sererialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param loaderName The name of the class loader to be used for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * the specified class. If null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * MBean Server, its own class loader will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return  The de-serialized object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @exception InstanceNotFoundException The specified class loader MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @exception OperationsException Any of the usual Input/Output related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @exception ReflectionException The specified class could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * by the specified class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public ObjectInputStream deserialize(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                         ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                         byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                         ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        throws InstanceNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
               OperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
               ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // Check parameter validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                IllegalArgumentException(), "Null data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (data.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                IllegalArgumentException(), "Empty data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             IllegalArgumentException(), "Null className passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   424
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   425
        ReflectUtil.checkPackageAccess(className);
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   426
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // Load the class using the agent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            theClass = findClass(className, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            // Get the class loader MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                ClassLoader instance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   436
                instance = getClassLoader(loaderName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (instance == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    throw new ClassNotFoundException(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                theClass = Class.forName(className, false, instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                               "The MBean class could not be loaded by the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                               loaderName.toString() + " class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // Object deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        ByteArrayInputStream bIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        ObjectInputStream    objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        bIn   = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            objIn = new ObjectInputStreamWithLoader(bIn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                           theClass.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new OperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    "An IOException occurred trying to de-serialize the data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Instantiates an object using the list of all class loaders registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * in the MBean Interceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * (using its {@link javax.management.loading.ClassLoaderRepository}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <P>It returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public Object instantiate(String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return instantiate(className, (Object[]) null, (String[]) null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Instantiates an object using the class Loader specified by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <CODE>ObjectName</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <P>If the loader name is null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * MBean Server, the ClassLoader that loaded it will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <P>It returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param className The class name of the MBean to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param loaderName The object name of the class loader to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @exception InstanceNotFoundException The specified class loader is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * registered in the MBeanServerInterceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public Object instantiate(String className, ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        throws ReflectionException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
               InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return instantiate(className, loaderName, (Object[]) null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                           (String[]) null, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Instantiates an object using the list of all class loaders registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * in the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * (using its {@link javax.management.loading.ClassLoaderRepository}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <P>The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param params An array containing the parameters of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @param signature An array containing the signature of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public Object instantiate(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                              Object params[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                              String signature[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   565
        Class<?> theClass = findClassWithDefaultLoaderRepository(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return instantiate(theClass, params, signature, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Instantiates an object. The class loader to be used is identified by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <P>If the object name of the loader is null, a default has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * provided (for example, for a MBean Server, the ClassLoader that loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * it will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <P>The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <P>The newly created object is not registered in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param params An array containing the parameters of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param signature An array containing the signature of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param loaderName The object name of the class loader to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @exception InstanceNotFoundException The specified class loader is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public Object instantiate(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                              ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                              Object params[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                              String signature[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
               MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   613
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            theClass = findClass(className, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            theClass = findClass(className, loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return instantiate(theClass, params, signature, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Return the Default Loader Repository used by this instantiator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public ModifiableClassLoaderRepository getClassLoaderRepository() {
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   628
        checkMBeanPermission((String)null, null, null, "getClassLoaderRepository");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return clr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Load a class with the specified loader, or with this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * class loader if the specified loader is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   636
    static Class<?> loadClass(String className, ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        throws ReflectionException {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   638
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                IllegalArgumentException("The class name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                              "Exception occurred during object instantiation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   644
        ReflectUtil.checkPackageAccess(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                loader = MBeanInstantiator.class.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                theClass = Class.forName(className, false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                theClass = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw new ReflectionException(e,
1705
08644ea24ce6 6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents: 1702
diff changeset
   655
            "The MBean class could not be loaded");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return theClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Load the classes specified in the signature with the given loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * or with this object class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   666
    static Class<?>[] loadSignatureClasses(String signature[],
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   667
                                           ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        throws  ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (signature == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        final ClassLoader aLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
           (loader==null?MBeanInstantiator.class.getClassLoader():loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        final int length= signature.length;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   674
        final Class<?> tab[]=new Class<?>[length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (length == 0) return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            for (int i= 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                // Start handling primitive types (int. boolean and so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                // forth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   683
                final Class<?> primCla = primitiveClasses.get(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (primCla != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    tab[i] = primCla;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // Ok we do not have a primitive type ! We need to build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                // the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                // We need to load the class through the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                // loader of the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                //
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   695
                ReflectUtil.checkPackageAccess(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                tab[i] = Class.forName(signature[i], false, aLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        MBeanInstantiator.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        "findSignatureClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        "The parameter class could not be found", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                      "The parameter class could not be found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                MBEANSERVER_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        MBeanInstantiator.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        "findSignatureClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        "Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        try {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   721
            return ConstructorUtil.getConstructor(c, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    private static final Map<String, Class<?>> primitiveClasses = Util.newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    static {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   730
        for (Class<?> c : new Class<?>[] {byte.class, short.class, int.class,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   731
                                          long.class, float.class, double.class,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   732
                                          char.class, boolean.class})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            primitiveClasses.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   735
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   736
    private static void checkMBeanPermission(Class<?> clazz,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   737
                                             String member,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   738
                                             ObjectName objectName,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   739
                                             String actions) {
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   740
        if (clazz != null) {
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   741
            checkMBeanPermission(clazz.getName(), member, objectName, actions);
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   742
        }
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   743
    }
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   744
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   745
    private static void checkMBeanPermission(String classname,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   746
                                             String member,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   747
                                             ObjectName objectName,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   748
                                             String actions)
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   749
        throws SecurityException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   750
        SecurityManager sm = System.getSecurityManager();
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   751
        if (sm != null) {
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   752
            Permission perm = new MBeanPermission(classname,
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   753
                                                  member,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   754
                                                  objectName,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   755
                                                  actions);
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   756
            sm.checkPermission(perm);
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   757
        }
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   758
    }
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   759
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 18207
diff changeset
   760
    private static void ensureClassAccess(Class<?> clazz)
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   761
            throws IllegalAccessException
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   762
    {
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   763
        int mod = clazz.getModifiers();
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   764
        if (!Modifier.isPublic(mod)) {
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   765
            throw new IllegalAccessException("Class is not public and can't be instantiated");
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   766
        }
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   767
    }
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   768
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   769
    private ClassLoader getClassLoader(final ObjectName name) {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   770
        if(clr == null){
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   771
            return null;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   772
        }
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   773
        // Restrict to getClassLoader permission only
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   774
        Permissions permissions = new Permissions();
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   775
        permissions.add(new MBeanPermission("*", null, name, "getClassLoader"));
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   776
        ProtectionDomain protectionDomain = new ProtectionDomain(null, permissions);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   777
        ProtectionDomain[] domains = {protectionDomain};
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   778
        AccessControlContext ctx = new AccessControlContext(domains);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   779
        ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   780
            public ClassLoader run() {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   781
                return clr.getClassLoader(name);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   782
            }
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   783
        }, ctx);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   784
        return loader;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   785
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
}