jdk/test/javax/management/mxbean/CustomTypeTest.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 1322 d038148778cc
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 687
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     4
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     8
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    14
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    18
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    21
 * have any questions.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    22
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    23
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    24
/* @test %M% %I%
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    25
 * @bug 6562936
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    26
 * @run compile customtypes/package-info.java
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    27
 * @run main CustomTypeTest
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    28
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    29
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    30
import java.io.InvalidObjectException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    31
import java.lang.management.ManagementFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    32
import java.lang.reflect.Array;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    33
import java.lang.reflect.InvocationHandler;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    34
import java.lang.reflect.Method;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    35
import java.lang.reflect.Proxy;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    36
import java.lang.reflect.Type;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    37
import java.util.ArrayList;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    38
import java.util.Arrays;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    39
import java.util.Collection;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    40
import java.util.Date;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    41
import java.util.Iterator;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    42
import java.util.List;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    43
import javax.management.JMX;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    44
import javax.management.MBeanServer;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    45
import javax.management.ObjectName;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    46
import javax.management.StandardMBean;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    47
import javax.management.Descriptor;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    48
import javax.management.MBeanServerInvocationHandler;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    49
import javax.management.NotCompliantMBeanException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    50
import javax.management.openmbean.ArrayType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    51
import javax.management.openmbean.CompositeData;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    52
import javax.management.openmbean.CompositeDataSupport;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    53
import javax.management.openmbean.CompositeType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    54
import javax.management.openmbean.MXBeanMapping;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    55
import javax.management.openmbean.MXBeanMappingClass;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    56
import javax.management.openmbean.MXBeanMappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    57
import javax.management.openmbean.MXBeanMappingFactoryClass;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    58
import javax.management.openmbean.OpenDataException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    59
import javax.management.openmbean.OpenType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    60
import javax.management.openmbean.SimpleType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    61
import javax.management.openmbean.TabularData;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    62
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    63
import static javax.management.JMX.MBeanOptions;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    64
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    65
import customtypes.*;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    66
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    67
public class CustomTypeTest {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    68
    @MXBeanMappingClass(LinkedListMapping.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    69
    public static class LinkedList {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    70
        private final String name;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    71
        private final LinkedList next;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    72
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    73
        public LinkedList(String name, LinkedList next) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    74
            this.name = name;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    75
            this.next = next;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    76
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    77
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    78
        public String getName() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    79
            return name;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    80
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    81
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    82
        public LinkedList getNext() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    83
            return next;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    84
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    85
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    86
        public String toString() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    87
            if (next == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    88
                return "(" + name + ")";
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    89
            else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    90
                return "(" + name + " " + next + ")";
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    91
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    92
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    93
        public boolean equals(Object x) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    94
            if (!(x instanceof LinkedList))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    95
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    96
            LinkedList other = (LinkedList) x;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    97
            return (this.name.equals(other.name) &&
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    98
                    (this.next == null ? other.next == null :
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    99
                        this.next.equals(other.next)));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   100
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   101
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   102
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   103
    public static class LinkedListMapping extends MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   104
        public LinkedListMapping(Type type) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   105
            super(LinkedList.class, ArrayType.getArrayType(SimpleType.STRING));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   106
            if (type != LinkedList.class) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   107
                throw new OpenDataException("Mapping only valid for " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   108
                        LinkedList.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   109
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   110
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   111
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   112
        public Object fromOpenValue(Object openValue) throws InvalidObjectException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   113
            String[] array = (String[]) openValue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   114
            LinkedList list = null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   115
            for (int i = array.length - 1; i >= 0; i--)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   116
                list = new LinkedList(array[i], list);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   117
            return list;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   118
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   119
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   120
        public Object toOpenValue(Object javaValue) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   121
            ArrayList<String> array = new ArrayList<String>();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   122
            for (LinkedList list = (LinkedList) javaValue; list != null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   123
                    list = list.getNext())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   124
                array.add(list.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   125
            return array.toArray(new String[0]);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   126
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   127
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   128
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   129
    public static interface LinkedListMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   130
        public LinkedList getLinkedList();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   131
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   132
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   133
    public static class LinkedListImpl implements LinkedListMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   134
        public LinkedList getLinkedList() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   135
            return new LinkedList("car", new LinkedList("cdr", null));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   136
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   137
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   138
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   139
    public static class ObjectMXBeanMapping extends MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   140
        private static final CompositeType wildcardType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   141
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   142
        static {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   143
            try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   144
                wildcardType =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   145
                    new CompositeType(Object.class.getName(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   146
                                      "Wildcard type for Object",
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   147
                                      new String[0],        // itemNames
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   148
                                      new String[0],        // itemDescriptions
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   149
                                      new OpenType<?>[0]);  // itemTypes
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   150
            } catch (OpenDataException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   151
                throw new RuntimeException(e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   152
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   153
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   154
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   155
        public ObjectMXBeanMapping() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   156
            super(Object.class, wildcardType);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   157
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   158
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   159
        public Object fromOpenValue(Object openValue) throws InvalidObjectException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   160
            if (!(openValue instanceof CompositeData)) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   161
                throw new InvalidObjectException("Not a CompositeData: " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   162
                        openValue.getClass());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   163
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   164
            CompositeData cd = (CompositeData) openValue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   165
            if (!cd.containsKey("value")) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   166
                throw new InvalidObjectException("CompositeData does not " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   167
                        "contain a \"value\" item: " + cd);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   168
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   169
            Object x = cd.get("value");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   170
            if (!(x instanceof CompositeData || x instanceof TabularData ||
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   171
                    x instanceof Object[]))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   172
                return x;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   173
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   174
            String typeName = (String) cd.get("type");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   175
            if (typeName == null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   176
                throw new InvalidObjectException("CompositeData does not " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   177
                        "contain a \"type\" item: " + cd);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   178
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   179
            Class<?> c;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   180
            try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   181
                c = Class.forName(typeName);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   182
            } catch (ClassNotFoundException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   183
                InvalidObjectException ioe =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   184
                        new InvalidObjectException("Could not find type");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   185
                ioe.initCause(e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   186
                throw ioe;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   187
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   188
            MXBeanMapping mapping;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   189
            try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   190
                mapping = objectMappingFactory.mappingForType(c, objectMappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   191
            } catch (OpenDataException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   192
                InvalidObjectException ioe =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   193
                        new InvalidObjectException("Could not map object's " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   194
                            "type " + c.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   195
                ioe.initCause(e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   196
                throw ioe;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   197
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   198
            return mapping.fromOpenValue(x);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   199
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   200
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   201
        public Object toOpenValue(Object javaValue) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   202
            OpenType<?> openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   203
            Object openValue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   204
            String typeName;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   205
            if (javaValue == null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   206
                openType = SimpleType.VOID;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   207
                openValue = null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   208
                typeName = null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   209
            } else {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   210
                Class<?> c = javaValue.getClass();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   211
                if (c.equals(Object.class))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   212
                    throw new OpenDataException("Cannot map Object to an open value");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   213
                MXBeanMapping mapping =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   214
                        objectMappingFactory.mappingForType(c, objectMappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   215
                openType = mapping.getOpenType();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   216
                openValue = mapping.toOpenValue(javaValue);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   217
                typeName = c.getName();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   218
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   219
            CompositeType ct = new CompositeType(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   220
                    (javaValue == null) ? "null" : openType.getClassName(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   221
                    "Open Mapping for Object",
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   222
                    new String[] {"type", "value"},
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   223
                    new String[] {"type", "value"},
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   224
                    new OpenType<?>[] {SimpleType.STRING, openType});
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   225
            return new CompositeDataSupport(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   226
                    ct,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   227
                    new String[] {"type", "value"},
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   228
                    new Object[] {typeName, openValue});
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   229
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   230
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   231
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   232
    public static class ObjectMappingFactory extends MXBeanMappingFactory {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   233
        private static MXBeanMapping objectMapping =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   234
                new ObjectMXBeanMapping();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   235
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   236
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   237
        public MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   238
        throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   239
            if (t.equals(Object.class))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   240
                return objectMapping;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   241
            else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   242
                return MXBeanMappingFactory.DEFAULT.mappingForType(t, f);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   243
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   244
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   245
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   246
    private static MXBeanMappingFactory objectMappingFactory =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   247
            new ObjectMappingFactory();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   248
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   249
    public static interface ObjectMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   250
        public Object getObject();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   251
        public Object[] getObjects();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   252
        public List<Object> getObjectList();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   253
        public Object[][] getMoreObjects();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   254
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   255
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   256
    public static class ObjectImpl implements ObjectMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   257
        public Object getObject() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   258
            return 123;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   259
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   260
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   261
        private static Object[] objects = {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   262
            "foo", 3, 3.14f, 3.14, 3L, new Date(), ObjectName.WILDCARD,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   263
            new byte[3], new char[3], new int[3][3],
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   264
            new LinkedListImpl().getLinkedList(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   265
        };
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   266
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   267
        public Object[] getObjects() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   268
            return objects;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   269
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   270
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   271
        public List<Object> getObjectList() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   272
            return Arrays.asList(getObjects());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   273
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   274
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   275
        public Object[][] getMoreObjects() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   276
            return new Object[][] {{getObjects()}};
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   277
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   278
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   279
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   280
    @MXBeanMappingFactoryClass(ObjectMappingFactory.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   281
    public static interface AnnotatedObjectMXBean extends ObjectMXBean {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   282
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   283
    public static class AnnotatedObjectImpl extends ObjectImpl
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   284
            implements AnnotatedObjectMXBean {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   285
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   286
    public static class BrokenMappingFactory extends MXBeanMappingFactory {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   287
        public MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   288
        throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   289
            throw new OpenDataException(t.toString());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   290
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   291
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   292
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   293
    public static class ReallyBrokenMappingFactory extends BrokenMappingFactory {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   294
        public ReallyBrokenMappingFactory() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   295
            throw new RuntimeException("Oops");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   296
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   297
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   298
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   299
    @MXBeanMappingFactoryClass(BrokenMappingFactory.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   300
    public static interface BrokenMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   301
        public int getX();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   302
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   303
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   304
    public static class BrokenImpl implements BrokenMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   305
        public int getX() {return 0;}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   306
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   307
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   308
    @MXBeanMappingFactoryClass(ReallyBrokenMappingFactory.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   309
    public static interface ReallyBrokenMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   310
        public int getX();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   311
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   312
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   313
    public static class ReallyBrokenImpl implements ReallyBrokenMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   314
        public int getX() {return 0;}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   315
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   316
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   317
    public static class BrokenMapping extends MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   318
        public BrokenMapping(Type t) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   319
            super(t, SimpleType.STRING);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   320
            throw new RuntimeException("Oops");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   321
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   322
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   323
        public Object fromOpenValue(Object openValue) throws InvalidObjectException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   324
            throw new AssertionError();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   325
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   326
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   327
        public Object toOpenValue(Object javaValue) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   328
            throw new AssertionError();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   329
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   330
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   331
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   332
    @MXBeanMappingClass(BrokenMapping.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   333
    public static class BrokenType {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   334
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   335
    public static interface BrokenTypeMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   336
        BrokenType getBroken();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   337
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   338
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   339
    public static class BrokenTypeImpl implements BrokenTypeMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   340
        public BrokenType getBroken() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   341
            throw new AssertionError();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   342
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   343
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   344
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   345
    public static void main(String[] args) throws Exception {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   346
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   347
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   348
        System.out.println("Test @MXBeanMappingClass");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   349
        ObjectName linkedName = new ObjectName("d:type=LinkedList");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   350
        LinkedListMXBean linkedListMXBean = new LinkedListImpl();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   351
        LinkedList list1 = linkedListMXBean.getLinkedList();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   352
        mbs.registerMBean(linkedListMXBean, linkedName);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   353
        LinkedListMXBean linkedProxy =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   354
                JMX.newMXBeanProxy(mbs, linkedName, LinkedListMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   355
        MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   356
                Proxy.getInvocationHandler(linkedProxy);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   357
        if (!mbsih.isMXBean())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   358
            fail("not MXBean proxy");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   359
        LinkedList list2 = linkedProxy.getLinkedList();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   360
        if (list1 == list2)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   361
            fail("lists identical!");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   362
            // They should have gone through the mapping and back,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   363
            // and the mapping doesn't do anything that would allow it
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   364
            // to restore the identical object.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   365
        if (!list1.equals(list2))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   366
            fail("lists different: " + list1 + " vs " + list2);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   367
        System.out.println("...success");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   368
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   369
        System.out.println("Test StandardMBean with MXBeanMappingFactory");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   370
        ObjectMXBean wildcardMBean = new ObjectImpl();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   371
        MBeanOptions options = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   372
        options.setMXBeanMappingFactory(objectMappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   373
        if (!options.isMXBean())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   374
            fail("Setting MXBeanMappingFactory should imply MXBean");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   375
        StandardMBean wildcardStandardMBean =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   376
                new StandardMBean(wildcardMBean, ObjectMXBean.class, options);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   377
        testWildcardMBean(mbs, wildcardMBean, wildcardStandardMBean,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   378
                          options, ObjectMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   379
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   380
        System.out.println("Test @MXBeanMappingFactoryClass on interface");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   381
        ObjectMXBean annotatedWildcardMBean = new AnnotatedObjectImpl();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   382
        testWildcardMBean(mbs, annotatedWildcardMBean, annotatedWildcardMBean,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   383
                          null, AnnotatedObjectMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   384
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   385
        System.out.println("Test @MXBeanMappingFactoryClass on package");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   386
        CustomMXBean custom = zeroProxy(CustomMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   387
        ObjectName customName = new ObjectName("d:type=Custom");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   388
        mbs.registerMBean(custom, customName);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   389
        Object x = mbs.getAttribute(customName, "X");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   390
        if (!(x instanceof String))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   391
            fail("Should be String: " + x + " (a " + x.getClass().getName() + ")");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   392
        CustomMXBean customProxy =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   393
                JMX.newMXBeanProxy(mbs, customName, CustomMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   394
        x = customProxy.getX();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   395
        if (!(x instanceof Integer) || (Integer) x != 0)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   396
            fail("Wrong return from proxy: " + x + " (a " + x.getClass().getName() + ")");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   397
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   398
        System.out.println("Test MXBeanMappingFactory exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   399
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   400
            mbs.registerMBean(new BrokenImpl(), new ObjectName("d:type=Broken"));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   401
            fail("Register did not throw exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   402
        } catch (NotCompliantMBeanException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   403
            System.out.println("...OK: threw: " + e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   404
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   405
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   406
        System.out.println("Test MXBeanMappingFactory constructor exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   407
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   408
            mbs.registerMBean(new ReallyBrokenImpl(), new ObjectName("d:type=Broken"));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   409
            fail("Register did not throw exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   410
        } catch (IllegalArgumentException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   411
            System.out.println("...OK: threw: " + e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   412
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   413
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   414
        System.out.println("Test MXBeanMappingFactory exception with StandardMBean");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   415
        MXBeanMappingFactory brokenF = new BrokenMappingFactory();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   416
        MBeanOptions brokenO = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   417
        brokenO.setMXBeanMappingFactory(brokenF);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   418
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   419
            new StandardMBean(wildcardMBean, ObjectMXBean.class, brokenO);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   420
            fail("StandardMBean with broken factory did not throw exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   421
        } catch (IllegalArgumentException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   422
            if (!(e.getCause() instanceof NotCompliantMBeanException)) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   423
                fail("StandardMBean with broken factory threw wrong exception: "
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   424
                        + e.getCause());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   425
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   426
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   427
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   428
        System.out.println("Test MXBeanMappingClass exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   429
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   430
            mbs.registerMBean(new BrokenTypeImpl(), new ObjectName("d:type=Broken"));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   431
            fail("Broken MXBeanMappingClass did not throw exception");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   432
        } catch (NotCompliantMBeanException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   433
            System.out.println("...OK: threw: " + e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   434
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   435
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   436
        if (failure == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   437
            System.out.println("TEST PASSED");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   438
        else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   439
            throw new Exception("TEST FAILED: " + failure);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   440
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   441
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   442
    private static void testWildcardMBean(MBeanServer mbs, ObjectMXBean impl,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   443
                                          Object mbean,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   444
                                          MBeanOptions proxyOptions,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   445
                                          Class<? extends ObjectMXBean> intf)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   446
    throws Exception {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   447
        ObjectName wildcardName = new ObjectName("d:type=Object");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   448
        mbs.registerMBean(mbean, wildcardName);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   449
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   450
            testWildcardMBean2(mbs, impl, wildcardName, proxyOptions, intf);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   451
        } finally {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   452
            mbs.unregisterMBean(wildcardName);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   453
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   454
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   455
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   456
    private static void testWildcardMBean2(MBeanServer mbs, ObjectMXBean impl,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   457
                                           ObjectName wildcardName,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   458
                                           MBeanOptions proxyOptions,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   459
                                           Class<? extends ObjectMXBean> intf)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   460
    throws Exception {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   461
        if (proxyOptions == null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   462
            proxyOptions = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   463
            MXBeanMappingFactory f = MXBeanMappingFactory.forInterface(intf);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   464
            proxyOptions.setMXBeanMappingFactory(f);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   465
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   466
        Descriptor d = mbs.getMBeanInfo(wildcardName).getDescriptor();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   467
        String factoryName = (String)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   468
                d.getFieldValue(JMX.MXBEAN_MAPPING_FACTORY_CLASS_FIELD);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   469
        if (!ObjectMappingFactory.class.getName().equals(factoryName)) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   470
            fail("Descriptor has wrong MXBeanMappingFactory: " + factoryName +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   471
                    " should be " + ObjectMappingFactory.class.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   472
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   473
        ObjectMXBean wildcardProxy =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   474
            JMX.newMBeanProxy(mbs, wildcardName, intf, proxyOptions);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   475
        MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   476
                Proxy.getInvocationHandler(wildcardProxy);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   477
        MBeanOptions opts = mbsih.getMBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   478
        if (!opts.equals(proxyOptions)) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   479
            fail("Proxy options differ from request: " + opts + " vs " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   480
                    proxyOptions);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   481
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   482
        Method[] wildcardMethods = ObjectMXBean.class.getMethods();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   483
        for (Method m : wildcardMethods) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   484
            System.out.println("..." + m.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   485
            Object orig = m.invoke(impl);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   486
            Object copy = m.invoke(wildcardProxy);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   487
            if (!deepEquals(orig, copy)) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   488
                fail("objects differ: " + deepToString(orig) + " vs " +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   489
                        deepToString(copy));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   490
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   491
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   492
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   493
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   494
    private static <T> T zeroProxy(Class<T> intf) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   495
        return intf.cast(Proxy.newProxyInstance(intf.getClassLoader(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   496
                new Class<?>[] {intf},
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   497
                new ZeroInvocationHandler()));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   498
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   499
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   500
    private static class ZeroInvocationHandler implements InvocationHandler {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   501
        public Object invoke(Object proxy, Method method, Object[] args)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   502
        throws Throwable {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   503
            return 0;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   504
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   505
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   506
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   507
    private static boolean deepEquals(Object x, Object y) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   508
        if (x == y)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   509
            return true;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   510
        if (x == null || y == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   511
            return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   512
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   513
        if (x instanceof Collection<?>) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   514
            if (!(y instanceof Collection<?>))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   515
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   516
            Collection<?> xcoll = (Collection<?>) x;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   517
            Collection<?> ycoll = (Collection<?>) y;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   518
            if (xcoll.size() != ycoll.size())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   519
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   520
            Iterator<?> xit = xcoll.iterator();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   521
            Iterator<?> yit = ycoll.iterator();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   522
            while (xit.hasNext()) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   523
                if (!deepEquals(xit.next(), yit.next()))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   524
                    return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   525
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   526
            return true;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   527
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   528
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   529
        Class<?> xclass = x.getClass();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   530
        Class<?> yclass = y.getClass();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   531
        if (xclass.isArray()) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   532
            if (!yclass.isArray())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   533
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   534
            if (!xclass.getComponentType().equals(yclass.getComponentType()))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   535
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   536
            int len = Array.getLength(x);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   537
            if (Array.getLength(y) != len)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   538
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   539
            for (int i = 0; i < len; i++) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   540
                if (!deepEquals(Array.get(x, i), Array.get(y, i)))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   541
                    return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   542
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   543
            return true;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   544
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   545
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   546
//        return x.equals(y);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   547
        if (x.equals(y))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   548
            return true;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   549
        System.out.println("Not equal: <" + x + "> and <" + y + ">");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   550
        return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   551
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   552
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   553
    private static String deepToString(Object x) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   554
        if (x == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   555
            return "null";
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   556
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   557
        if (x instanceof Collection<?>) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   558
            Collection<?> xcoll = (Collection<?>) x;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   559
            StringBuilder sb = new StringBuilder("[");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   560
            for (Object e : xcoll) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   561
                if (sb.length() > 1)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   562
                    sb.append(", ");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   563
                sb.append(deepToString(e));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   564
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   565
            sb.append("]");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   566
            return sb.toString();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   567
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   568
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   569
        if (x instanceof Object[]) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   570
            Object[] xarr = (Object[]) x;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   571
            return deepToString(Arrays.asList(xarr));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   572
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   573
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   574
        if (x.getClass().isArray()) { // primitive array
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   575
            String s = Arrays.deepToString(new Object[] {x});
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   576
            return s.substring(1, s.length() - 1);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   577
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   578
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   579
        return x.toString();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   580
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   581
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   582
    private static void fail(String msg) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   583
        System.out.println("TEST FAILED: " + msg);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   584
        if (msg.length() > 100)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   585
            msg = msg.substring(0, 100) + "...";
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   586
        failure = msg;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   587
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   588
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   589
    private static String failure;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   590
}