blob: 15921a888bc573fa505d93d04bbfbde84e42e1ee [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.management;
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;
34
35/**
36 * A CompositeData for VMOption for the local management support.
37 * This class avoids the performance penalty paid to the
38 * construction of a CompositeData use in the local case.
39 */
40public class VMOptionCompositeData extends LazyCompositeData {
41 private final VMOption option;
42
43 private VMOptionCompositeData(VMOption option) {
44 this.option = option;
45 }
46
47 public VMOption getVMOption() {
48 return option;
49 }
50
51 public static CompositeData toCompositeData(VMOption option) {
52 VMOptionCompositeData vcd = new VMOptionCompositeData(option);
53 return vcd.getCompositeData();
54 }
55
56 protected CompositeData getCompositeData() {
57 // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
58 // vmOptionItemNames!
59 final Object[] vmOptionItemValues = {
60 option.getName(),
61 option.getValue(),
62 new Boolean(option.isWriteable()),
63 option.getOrigin().toString(),
64 };
65
66 try {
67 return new CompositeDataSupport(vmOptionCompositeType,
68 vmOptionItemNames,
69 vmOptionItemValues);
70 } catch (OpenDataException e) {
71 // Should never reach here
72 throw Util.newInternalError(e);
73 }
74 }
75
76 private static final CompositeType vmOptionCompositeType;
77 static {
78 try {
79 vmOptionCompositeType = (CompositeType)
80 MappedMXBeanType.toOpenType(VMOption.class);
81 } catch (OpenDataException e) {
82 // Should never reach here
83 throw Util.newInternalError(e);
84 }
85 }
86
87 static CompositeType getVMOptionCompositeType() {
88 return vmOptionCompositeType;
89 }
90
91 private static final String NAME = "name";
92 private static final String VALUE = "value";
93 private static final String WRITEABLE = "writeable";
94 private static final String ORIGIN = "origin";
95
96 private static final String[] vmOptionItemNames = {
97 NAME,
98 VALUE,
99 WRITEABLE,
100 ORIGIN,
101 };
102
103 public static String getName(CompositeData cd) {
104 return getString(cd, NAME);
105 }
106 public static String getValue(CompositeData cd) {
107 return getString(cd, VALUE);
108 }
109 public static Origin getOrigin(CompositeData cd) {
110 String o = getString(cd, ORIGIN);
111 return Enum.valueOf(Origin.class, o);
112 }
113 public static boolean isWriteable(CompositeData cd) {
114 return getBoolean(cd, WRITEABLE);
115 }
116
117 /** Validate if the input CompositeData has the expected
118 * CompositeType (i.e. contain all attributes with expected
119 * names and types).
120 */
121 public static void validateCompositeData(CompositeData cd) {
122 if (cd == null) {
123 throw new NullPointerException("Null CompositeData");
124 }
125
126 if (!isTypeMatched(vmOptionCompositeType, cd.getCompositeType())) {
127 throw new IllegalArgumentException(
128 "Unexpected composite type for VMOption");
129 }
130 }
131}