1 /*
   2  * Copyright (c) 2004, 2019, 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 
  26 package sun.management;
  27 
  28 import java.lang.management.MemoryUsage;
  29 import javax.management.openmbean.CompositeType;
  30 import javax.management.openmbean.CompositeData;
  31 import javax.management.openmbean.CompositeDataSupport;
  32 import javax.management.openmbean.OpenDataException;
  33 
  34 /**
  35  * A CompositeData for MemoryUsage for the local management support.
  36  * This class avoids the performance penalty paid to the
  37  * construction of a CompositeData use in the local case.
  38  */
  39 public class MemoryUsageCompositeData extends LazyCompositeData {
  40     @SuppressWarnings("serial") // Not statically typed as Serializable
  41     private final MemoryUsage usage;
  42 
  43     private MemoryUsageCompositeData(MemoryUsage u) {
  44         this.usage = u;
  45     }
  46 
  47     public MemoryUsage getMemoryUsage() {
  48         return usage;
  49     }
  50 
  51     public static CompositeData toCompositeData(MemoryUsage u) {
  52         MemoryUsageCompositeData mucd = new MemoryUsageCompositeData(u);
  53         return mucd.getCompositeData();
  54     }
  55 
  56     protected CompositeData getCompositeData() {
  57         // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
  58         // memoryUsageItemNames!
  59         final Object[] memoryUsageItemValues = {
  60             usage.getInit(),
  61             usage.getUsed(),
  62             usage.getCommitted(),
  63             usage.getMax(),
  64         };
  65 
  66         try {
  67             return new CompositeDataSupport(memoryUsageCompositeType,
  68                                             memoryUsageItemNames,
  69                                             memoryUsageItemValues);
  70         } catch (OpenDataException e) {
  71             // Should never reach here
  72             throw new AssertionError(e);
  73         }
  74     }
  75 
  76     private static final CompositeType memoryUsageCompositeType;
  77     static {
  78         try {
  79             memoryUsageCompositeType = (CompositeType)
  80                 MappedMXBeanType.toOpenType(MemoryUsage.class);
  81         } catch (OpenDataException e) {
  82             // Should never reach here
  83             throw new AssertionError(e);
  84         }
  85     }
  86 
  87     static CompositeType getMemoryUsageCompositeType() {
  88         return memoryUsageCompositeType;
  89     }
  90 
  91     private static final String INIT      = "init";
  92     private static final String USED      = "used";
  93     private static final String COMMITTED = "committed";
  94     private static final String MAX       = "max";
  95 
  96     private static final String[] memoryUsageItemNames = {
  97         INIT,
  98         USED,
  99         COMMITTED,
 100         MAX,
 101     };
 102 
 103     public static long getInit(CompositeData cd) {
 104         return getLong(cd, INIT);
 105     }
 106     public static long getUsed(CompositeData cd) {
 107         return getLong(cd, USED);
 108     }
 109     public static long getCommitted(CompositeData cd) {
 110         return getLong(cd, COMMITTED);
 111     }
 112     public static long getMax(CompositeData cd) {
 113         return getLong(cd, MAX);
 114     }
 115 
 116     /** Validate if the input CompositeData has the expected
 117      * CompositeType (i.e. contain all attributes with expected
 118      * names and types).
 119      */
 120     public static void validateCompositeData(CompositeData cd) {
 121         if (cd == null) {
 122             throw new NullPointerException("Null CompositeData");
 123         }
 124 
 125         if (!isTypeMatched(memoryUsageCompositeType, cd.getCompositeType())) {
 126             throw new IllegalArgumentException(
 127                 "Unexpected composite type for MemoryUsage");
 128         }
 129     }
 130 
 131     private static final long serialVersionUID = -8504291541083874143L;
 132 }