1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.management.internal;
27
28import com.sun.management.VMOption;
29import com.sun.management.VMOption.Origin;
30import javax.management.openmbean.CompositeType;
31import javax.management.openmbean.CompositeData;
32import javax.management.openmbean.CompositeDataSupport;
33import javax.management.openmbean.OpenDataException;
34import sun.management.LazyCompositeData;
35import sun.management.MappedMXBeanType;
36
37/**
38 * A CompositeData for VMOption for the local management support.
39 * This class avoids the performance penalty paid to the
40 * construction of a CompositeData use in the local case.
41 */
42public class VMOptionCompositeData extends LazyCompositeData {
43    private final VMOption option;
44
45    private VMOptionCompositeData(VMOption option) {
46        this.option = option;
47    }
48
49    public VMOption getVMOption() {
50        return option;
51    }
52
53    public static CompositeData toCompositeData(VMOption option) {
54        VMOptionCompositeData vcd = new VMOptionCompositeData(option);
55        return vcd.getCompositeData();
56    }
57
58    protected CompositeData getCompositeData() {
59        // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
60        // vmOptionItemNames!
61        final Object[] vmOptionItemValues = {
62            option.getName(),
63            option.getValue(),
64            option.isWriteable(),
65            option.getOrigin().toString(),
66        };
67
68        try {
69            return new CompositeDataSupport(vmOptionCompositeType,
70                                            vmOptionItemNames,
71                                            vmOptionItemValues);
72        } catch (OpenDataException e) {
73            // Should never reach here
74            throw new AssertionError(e);
75        }
76    }
77
78    private static final CompositeType vmOptionCompositeType;
79    static {
80        try {
81            vmOptionCompositeType = (CompositeType)
82                MappedMXBeanType.toOpenType(VMOption.class);
83        } catch (OpenDataException e) {
84            // Should never reach here
85            throw new AssertionError(e);
86        }
87    }
88
89    static CompositeType getVMOptionCompositeType() {
90        return vmOptionCompositeType;
91    }
92
93    private static final String NAME      = "name";
94    private static final String VALUE     = "value";
95    private static final String WRITEABLE = "writeable";
96    private static final String ORIGIN    = "origin";
97
98    private static final String[] vmOptionItemNames = {
99        NAME,
100        VALUE,
101        WRITEABLE,
102        ORIGIN,
103    };
104
105    public static String getName(CompositeData cd) {
106        return getString(cd, NAME);
107    }
108    public static String getValue(CompositeData cd) {
109        return getString(cd, VALUE);
110    }
111    public static Origin getOrigin(CompositeData cd) {
112        String o = getString(cd, ORIGIN);
113        return Enum.valueOf(Origin.class, o);
114    }
115    public static boolean isWriteable(CompositeData cd) {
116        return getBoolean(cd, WRITEABLE);
117    }
118
119    /** Validate if the input CompositeData has the expected
120     * CompositeType (i.e. contain all attributes with expected
121     * names and types).
122     */
123    public static void validateCompositeData(CompositeData cd) {
124        if (cd == null) {
125            throw new NullPointerException("Null CompositeData");
126        }
127
128        if (!isTypeMatched(vmOptionCompositeType, cd.getCompositeType())) {
129            throw new IllegalArgumentException(
130                "Unexpected composite type for VMOption");
131        }
132    }
133
134    private static final long serialVersionUID = -2395573975093578470L;
135}
136