Path: blob/master/src/java.management/share/classes/sun/management/MemoryUsageCompositeData.java
41152 views
/*1* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.lang.management.MemoryUsage;28import javax.management.openmbean.CompositeType;29import javax.management.openmbean.CompositeData;30import javax.management.openmbean.CompositeDataSupport;31import javax.management.openmbean.OpenDataException;3233/**34* A CompositeData for MemoryUsage for the local management support.35* This class avoids the performance penalty paid to the36* construction of a CompositeData use in the local case.37*/38public class MemoryUsageCompositeData extends LazyCompositeData {39@SuppressWarnings("serial") // Not statically typed as Serializable40private final MemoryUsage usage;4142private MemoryUsageCompositeData(MemoryUsage u) {43this.usage = u;44}4546public MemoryUsage getMemoryUsage() {47return usage;48}4950public static CompositeData toCompositeData(MemoryUsage u) {51MemoryUsageCompositeData mucd = new MemoryUsageCompositeData(u);52return mucd.getCompositeData();53}5455protected CompositeData getCompositeData() {56// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH57// memoryUsageItemNames!58final Object[] memoryUsageItemValues = {59usage.getInit(),60usage.getUsed(),61usage.getCommitted(),62usage.getMax(),63};6465try {66return new CompositeDataSupport(memoryUsageCompositeType,67memoryUsageItemNames,68memoryUsageItemValues);69} catch (OpenDataException e) {70// Should never reach here71throw new AssertionError(e);72}73}7475private static final CompositeType memoryUsageCompositeType;76static {77try {78memoryUsageCompositeType = (CompositeType)79MappedMXBeanType.toOpenType(MemoryUsage.class);80} catch (OpenDataException e) {81// Should never reach here82throw new AssertionError(e);83}84}8586static CompositeType getMemoryUsageCompositeType() {87return memoryUsageCompositeType;88}8990private static final String INIT = "init";91private static final String USED = "used";92private static final String COMMITTED = "committed";93private static final String MAX = "max";9495private static final String[] memoryUsageItemNames = {96INIT,97USED,98COMMITTED,99MAX,100};101102public static long getInit(CompositeData cd) {103return getLong(cd, INIT);104}105public static long getUsed(CompositeData cd) {106return getLong(cd, USED);107}108public static long getCommitted(CompositeData cd) {109return getLong(cd, COMMITTED);110}111public static long getMax(CompositeData cd) {112return getLong(cd, MAX);113}114115/** Validate if the input CompositeData has the expected116* CompositeType (i.e. contain all attributes with expected117* names and types).118*/119public static void validateCompositeData(CompositeData cd) {120if (cd == null) {121throw new NullPointerException("Null CompositeData");122}123124if (!isTypeMatched(memoryUsageCompositeType, cd.getCompositeType())) {125throw new IllegalArgumentException(126"Unexpected composite type for MemoryUsage");127}128}129130private static final long serialVersionUID = -8504291541083874143L;131}132133134