jdk/src/java.management/share/classes/com/sun/jmx/mbeanserver/MBeanInstantiator.java
author dfuchs
Fri, 20 Jan 2017 18:41:12 +0000
changeset 43235 da1786d695b6
parent 25859 3317bb8137f4
permissions -rw-r--r--
8172971: java.management could use System.Logger Summary: java.management is updated to use System.Logger instead of java.util.logging.Logger. Reviewed-by: mchung, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2017, 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;
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
    43
import java.lang.System.Logger.Level;
2
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) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   189
            if (MBEANSERVER_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   190
                MBEANSERVER_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        "The parameter class could not be found", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                      "The parameter class could not be found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (RuntimeException e) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   196
            if (MBEANSERVER_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   197
                MBEANSERVER_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        "Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Instantiates an object given its class, using its empty constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   210
    public Object instantiate(Class<?> theClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws ReflectionException, MBeanException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   212
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   213
        checkMBeanPermission(theClass, null, null, "instantiate");
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   214
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   215
        Object moi;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   219
        Constructor<?> cons = findConstructor(theClass, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                NoSuchMethodException("No such constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Instantiate the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ReflectUtil.checkPackageAccess(theClass);
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   227
            ensureClassAccess(theClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            moi= cons.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // Wrap the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Throwable t = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (t instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                throw new RuntimeMBeanException((RuntimeException)t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                   "RuntimeException thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else if (t instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw new RuntimeErrorException((Error) t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                   "Error thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                throw new MBeanException((Exception) t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                   "Exception thrown in the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (NoSuchMethodError error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                NoSuchMethodException("No constructor"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                          "No such constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            "Exception thrown trying to invoke the MBean's empty constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return moi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Instantiates an object given its class, the parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * signature of its constructor The call returns a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   267
    public Object instantiate(Class<?> theClass, Object params[],
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                              String signature[], ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        throws ReflectionException, MBeanException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   270
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   271
        checkMBeanPermission(theClass, null, null, "instantiate");
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   272
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // Instantiate the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   276
        final Class<?>[] tab;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   277
        Object moi;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Build the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ClassLoader aLoader= theClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // Build the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            tab =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                ((signature == null)?null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                 findSignatureClasses(signature,aLoader));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // Exception IllegalArgumentException raised in Jdk1.1.8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    "The constructor parameter classes could not be loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // Query the metadata service to get the right constructor
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   295
        Constructor<?> cons = findConstructor(theClass, tab);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                NoSuchMethodException("No such constructor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            ReflectUtil.checkPackageAccess(theClass);
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   303
            ensureClassAccess(theClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            moi = cons.newInstance(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        catch (NoSuchMethodError error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new ReflectionException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                NoSuchMethodException("No such constructor found"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                          "No such constructor" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                "Exception thrown trying to invoke the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        catch (IllegalAccessException 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 (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            // Wrap the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            Throwable th = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (th instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new RuntimeMBeanException((RuntimeException)th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                      "RuntimeException thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else if (th instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                throw new RuntimeErrorException((Error) th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                      "Error thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                throw new MBeanException((Exception) th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                      "Exception thrown in the MBean's constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return moi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * De-serializes a byte array in the context of a classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param loader the classloader to use for de-serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param data The byte array to be de-sererialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return  The de-serialized object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @exception OperationsException Any of the usual Input/Output related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public ObjectInputStream deserialize(ClassLoader loader, byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        throws OperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Check parameter validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                IllegalArgumentException(), "Null data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (data.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                IllegalArgumentException(), "Empty data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // Object deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        ByteArrayInputStream bIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        ObjectInputStream    objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        bIn   = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            objIn = new ObjectInputStreamWithLoader(bIn,loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new OperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                     "An IOException occurred trying to de-serialize the data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * De-serializes a byte array in the context of a given MBean class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <P>The class loader is the one that loaded the class with name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * "className".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <P>The name of the class loader to be used for loading the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * class is specified. If null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * MBean Server, its own class loader will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param className The name of the class whose class loader should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *  be used for the de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param data The byte array to be de-sererialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param loaderName The name of the class loader to be used for loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * the specified class. If null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * MBean Server, its own class loader will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return  The de-serialized object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @exception InstanceNotFoundException The specified class loader MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @exception OperationsException Any of the usual Input/Output related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @exception ReflectionException The specified class could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * by the specified class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public ObjectInputStream deserialize(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                         ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                         byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                         ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        throws InstanceNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
               OperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
               ReflectionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // Check parameter validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                IllegalArgumentException(), "Null data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (data.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                IllegalArgumentException(), "Empty data passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            throw new  RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
             IllegalArgumentException(), "Null className passed in parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   420
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   421
        ReflectUtil.checkPackageAccess(className);
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   422
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // Load the class using the agent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            theClass = findClass(className, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // Get the class loader MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                ClassLoader instance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   432
                instance = getClassLoader(loaderName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (instance == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    throw new ClassNotFoundException(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                theClass = Class.forName(className, false, instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                               "The MBean class could not be loaded by the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                               loaderName.toString() + " class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
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 deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        ByteArrayInputStream bIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        ObjectInputStream    objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        bIn   = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            objIn = new ObjectInputStreamWithLoader(bIn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                           theClass.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new OperationsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    "An IOException occurred trying to de-serialize the data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return objIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Instantiates an object using the list of all class loaders registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * in the MBean Interceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * (using its {@link javax.management.loading.ClassLoaderRepository}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <P>It returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public Object instantiate(String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return instantiate(className, (Object[]) null, (String[]) null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Instantiates an object using the class Loader specified by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <CODE>ObjectName</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <P>If the loader name is null, a default one has to be provided (for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * MBean Server, the ClassLoader that loaded it will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <P>It returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param className The class name of the MBean to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param loaderName The object name of the class loader to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception InstanceNotFoundException The specified class loader is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * registered in the MBeanServerInterceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public Object instantiate(String className, ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        throws ReflectionException, MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
               InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return instantiate(className, loaderName, (Object[]) null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                           (String[]) null, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Instantiates an object using the list of all class loaders registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * in the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * (using its {@link javax.management.loading.ClassLoaderRepository}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <P>The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <P>The newly created object is not registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param params An array containing the parameters of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param signature An array containing the signature of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public Object instantiate(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                              Object params[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                              String signature[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   561
        Class<?> theClass = findClassWithDefaultLoaderRepository(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return instantiate(theClass, params, signature, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Instantiates an object. The class loader to be used is identified by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <P>If the object name of the loader is null, a default has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * provided (for example, for a MBean Server, the ClassLoader that loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * it will be used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <P>The object's class should have a public constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <P>The call returns a reference to the newly created object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <P>The newly created object is not registered in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param className The class name of the object to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param params An array containing the parameters of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @param signature An array containing the signature of the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param loaderName The object name of the class loader to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return The newly instantiated object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @exception ReflectionException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <CODE>java.lang.ClassNotFoundException</CODE> or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @exception MBeanException The constructor of the object has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @exception InstanceNotFoundException The specified class loader is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * registered in the MBean Interceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception RuntimeOperationsException Wraps a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public Object instantiate(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                              ObjectName loaderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                              Object params[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                              String signature[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                              ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        throws ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
               MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // ------------------------------
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   609
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (loaderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            theClass = findClass(className, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            theClass = findClass(className, loaderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return instantiate(theClass, params, signature, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Return the Default Loader Repository used by this instantiator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public ModifiableClassLoaderRepository getClassLoaderRepository() {
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   624
        checkMBeanPermission((String)null, null, null, "getClassLoaderRepository");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return clr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Load a class with the specified loader, or with this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * class loader if the specified loader is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   632
    static Class<?> loadClass(String className, ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        throws ReflectionException {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   634
        Class<?> theClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            throw new RuntimeOperationsException(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                IllegalArgumentException("The class name cannot be null"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                              "Exception occurred during object instantiation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   640
        ReflectUtil.checkPackageAccess(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                loader = MBeanInstantiator.class.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                theClass = Class.forName(className, false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                theClass = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            throw new ReflectionException(e,
1705
08644ea24ce6 6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents: 1702
diff changeset
   651
            "The MBean class could not be loaded");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        return theClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Load the classes specified in the signature with the given loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * or with this object class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   662
    static Class<?>[] loadSignatureClasses(String signature[],
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   663
                                           ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        throws  ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (signature == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        final ClassLoader aLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
           (loader==null?MBeanInstantiator.class.getClassLoader():loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        final int length= signature.length;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   670
        final Class<?> tab[]=new Class<?>[length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (length == 0) return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            for (int i= 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                // Start handling primitive types (int. boolean and so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                // forth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   679
                final Class<?> primCla = primitiveClasses.get(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                if (primCla != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    tab[i] = primCla;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                // Ok we do not have a primitive type ! We need to build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                // the signature of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                // We need to load the class through the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // loader of the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                //
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   691
                ReflectUtil.checkPackageAccess(signature[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                tab[i] = Class.forName(signature[i], false, aLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        } catch (ClassNotFoundException e) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   695
            if (MBEANSERVER_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   696
                MBEANSERVER_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        "The parameter class could not be found", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            throw new ReflectionException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                      "The parameter class could not be found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } catch (RuntimeException e) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   702
            if (MBEANSERVER_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   703
                MBEANSERVER_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        "Unexpected exception", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return tab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        try {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   713
            return ConstructorUtil.getConstructor(c, params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    private static final Map<String, Class<?>> primitiveClasses = Util.newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    static {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   722
        for (Class<?> c : new Class<?>[] {byte.class, short.class, int.class,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   723
                                          long.class, float.class, double.class,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   724
                                          char.class, boolean.class})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            primitiveClasses.put(c.getName(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   727
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   728
    private static void checkMBeanPermission(Class<?> clazz,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   729
                                             String member,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   730
                                             ObjectName objectName,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   731
                                             String actions) {
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   732
        if (clazz != null) {
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   733
            checkMBeanPermission(clazz.getName(), member, objectName, actions);
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   734
        }
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   735
    }
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   736
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   737
    private static void checkMBeanPermission(String classname,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   738
                                             String member,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   739
                                             ObjectName objectName,
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   740
                                             String actions)
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   741
        throws SecurityException {
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   742
        SecurityManager sm = System.getSecurityManager();
18203
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   743
        if (sm != null) {
1d6a38af051d 8008623: Better handling of MBeanServers
jbachorik
parents: 16878
diff changeset
   744
            Permission perm = new MBeanPermission(classname,
16109
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   745
                                                  member,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   746
                                                  objectName,
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   747
                                                  actions);
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   748
            sm.checkPermission(perm);
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   749
        }
eb00701a5769 8006446: Restrict MBeanServer access
dfuchs
parents: 5506
diff changeset
   750
    }
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   751
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 18207
diff changeset
   752
    private static void ensureClassAccess(Class<?> clazz)
16878
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   753
            throws IllegalAccessException
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   754
    {
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   755
        int mod = clazz.getModifiers();
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   756
        if (!Modifier.isPublic(mod)) {
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   757
            throw new IllegalAccessException("Class is not public and can't be instantiated");
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   758
        }
14afc4636dda 8006435: Improvements in JMX
dsamersoff
parents: 16109
diff changeset
   759
    }
18207
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   760
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   761
    private ClassLoader getClassLoader(final ObjectName name) {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   762
        if(clr == null){
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   763
            return null;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   764
        }
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   765
        // Restrict to getClassLoader permission only
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   766
        Permissions permissions = new Permissions();
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   767
        permissions.add(new MBeanPermission("*", null, name, "getClassLoader"));
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   768
        ProtectionDomain protectionDomain = new ProtectionDomain(null, permissions);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   769
        ProtectionDomain[] domains = {protectionDomain};
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   770
        AccessControlContext ctx = new AccessControlContext(domains);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   771
        ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   772
            public ClassLoader run() {
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   773
                return clr.getClassLoader(name);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   774
            }
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   775
        }, ctx);
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   776
        return loader;
64dcc0ad2298 8008128: Better API coherence for JMX
jfdenise
parents: 18203
diff changeset
   777
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
}