src/java.management/share/classes/sun/management/MonitorInfoCompositeData.java
author mchung
Thu, 25 Oct 2018 10:57:42 -0700
changeset 52288 2b29df6dfa68
parent 49077 b1c42b3cd19b
child 58766 54ffb15c4839
permissions -rw-r--r--
8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, 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: 4173
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: 4173
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: 4173
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
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 sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.MonitorInfo;
52288
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    29
import java.util.HashMap;
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    30
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.management.openmbean.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.openmbean.CompositeDataSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.openmbean.OpenDataException;
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    35
import javax.management.openmbean.OpenType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A CompositeData for MonitorInfo for the local management support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class avoids the performance penalty paid to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * construction of a CompositeData use in the local case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class MonitorInfoCompositeData extends LazyCompositeData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final MonitorInfo lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private MonitorInfoCompositeData(MonitorInfo mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        this.lock = mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public MonitorInfo getMonitorInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static CompositeData toCompositeData(MonitorInfo mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        MonitorInfoCompositeData micd = new MonitorInfoCompositeData(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return micd.getCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected CompositeData getCompositeData() {
52288
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    59
        StackTraceElement ste = lock.getLockedStackFrame();
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    60
        CompositeData steCData = ste != null ? StackTraceElementCompositeData.toCompositeData(ste)
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    61
                                             : null;
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    62
        // values may be null; can't use Map.of
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    63
        Map<String,Object> items = new HashMap<>();
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    64
        items.put(CLASS_NAME,         lock.getClassName());
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    65
        items.put(IDENTITY_HASH_CODE, lock.getIdentityHashCode());
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    66
        items.put(LOCKED_STACK_FRAME, steCData);
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    67
        items.put(LOCKED_STACK_DEPTH, lock.getLockedStackDepth());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
52288
2b29df6dfa68 8212795: ThreadInfoCompositeData.toCompositeData fails to map ThreadInfo to CompositeData
mchung
parents: 49077
diff changeset
    70
            return new CompositeDataSupport(MONITOR_INFO_COMPOSITE_TYPE, items);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // Should never reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    73
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    77
    private static final String CLASS_NAME         = "className";
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    78
    private static final String IDENTITY_HASH_CODE = "identityHashCode";
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    79
    private static final String LOCKED_STACK_FRAME = "lockedStackFrame";
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    80
    private static final String LOCKED_STACK_DEPTH = "lockedStackDepth";
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    81
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    82
    private static final String[] MONITOR_INFO_ATTRIBUTES = {
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    83
        CLASS_NAME,
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    84
        IDENTITY_HASH_CODE,
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    85
        LOCKED_STACK_FRAME,
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    86
        LOCKED_STACK_DEPTH
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    87
    };
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    88
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    89
    private static final CompositeType MONITOR_INFO_COMPOSITE_TYPE;
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    90
    private static final CompositeType V6_COMPOSITE_TYPE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    93
            MONITOR_INFO_COMPOSITE_TYPE = (CompositeType)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                MappedMXBeanType.toOpenType(MonitorInfo.class);
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    95
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    96
            OpenType<?>[] types = new OpenType<?>[MONITOR_INFO_ATTRIBUTES.length];
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    97
            for (int i = 0; i < MONITOR_INFO_ATTRIBUTES.length; i++) {
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    98
                String name = MONITOR_INFO_ATTRIBUTES[i];
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
    99
                types[i] = name.equals(LOCKED_STACK_FRAME)
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   100
                            ? StackTraceElementCompositeData.v5CompositeType()
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   101
                            : MONITOR_INFO_COMPOSITE_TYPE.getType(name);
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   102
            }
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   103
            V6_COMPOSITE_TYPE = new CompositeType("MonitorInfo",
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   104
                                                  "JDK 6 MonitorInfo",
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   105
                                                  MONITOR_INFO_ATTRIBUTES,
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   106
                                                  MONITOR_INFO_ATTRIBUTES,
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   107
                                                  types);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // Should never reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   110
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   114
    static CompositeType v6CompositeType() {
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   115
        return V6_COMPOSITE_TYPE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static String getClassName(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return getString(cd, CLASS_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static int getIdentityHashCode(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return getInt(cd, IDENTITY_HASH_CODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static StackTraceElement getLockedStackFrame(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        CompositeData ste = (CompositeData) cd.get(LOCKED_STACK_FRAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (ste != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return StackTraceElementCompositeData.from(ste);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static int getLockedStackDepth(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return getInt(cd, LOCKED_STACK_DEPTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /** Validate if the input CompositeData has the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * CompositeType (i.e. contain all attributes with expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * names and types).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static void validateCompositeData(CompositeData cd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (cd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new NullPointerException("Null CompositeData");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
49077
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   148
        if (!isTypeMatched(MONITOR_INFO_COMPOSITE_TYPE, cd.getCompositeType()) &&
b1c42b3cd19b 8198253: ThreadInfo.from(CompositeData) incorrectly accepts CompositeData with missing JDK 6 attributes
mchung
parents: 47216
diff changeset
   149
            !isTypeMatched(V6_COMPOSITE_TYPE, cd.getCompositeType())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "Unexpected composite type for MonitorInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
4173
d01972926813 6895875: Missing serialVersionUID in sun.management classes
mchung
parents: 715
diff changeset
   154
d01972926813 6895875: Missing serialVersionUID in sun.management classes
mchung
parents: 715
diff changeset
   155
    private static final long serialVersionUID = -5825215591822908529L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}