1/*
2 * Copyright (c) 2004, 2008, 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 sun.management;
27
28import java.lang.management.MemoryNotificationInfo;
29import java.lang.management.MemoryUsage;
30import javax.management.openmbean.CompositeData;
31import javax.management.openmbean.CompositeType;
32import javax.management.openmbean.CompositeDataSupport;
33import javax.management.openmbean.OpenDataException;
34
35/**
36 * A CompositeData for MemoryNotificationInfo 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 MemoryNotifInfoCompositeData extends LazyCompositeData {
41    private final MemoryNotificationInfo memoryNotifInfo;
42
43    private MemoryNotifInfoCompositeData(MemoryNotificationInfo info) {
44        this.memoryNotifInfo = info;
45    }
46
47    public MemoryNotificationInfo getMemoryNotifInfo() {
48        return memoryNotifInfo;
49    }
50
51    public static CompositeData toCompositeData(MemoryNotificationInfo info) {
52        MemoryNotifInfoCompositeData mnicd =
53            new MemoryNotifInfoCompositeData(info);
54        return mnicd.getCompositeData();
55    }
56
57    protected CompositeData getCompositeData() {
58        // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
59        // memoryNotifInfoItemNames!
60        final Object[] memoryNotifInfoItemValues = {
61            memoryNotifInfo.getPoolName(),
62            MemoryUsageCompositeData.toCompositeData(memoryNotifInfo.getUsage()),
63            memoryNotifInfo.getCount(),
64        };
65
66        try {
67            return new CompositeDataSupport(memoryNotifInfoCompositeType,
68                                            memoryNotifInfoItemNames,
69                                            memoryNotifInfoItemValues);
70        } catch (OpenDataException e) {
71            // Should never reach here
72            throw new AssertionError(e);
73        }
74    }
75
76    private static final CompositeType memoryNotifInfoCompositeType;
77    static {
78        try {
79            memoryNotifInfoCompositeType = (CompositeType)
80                MappedMXBeanType.toOpenType(MemoryNotificationInfo.class);
81        } catch (OpenDataException e) {
82            // Should never reach here
83            throw new AssertionError(e);
84        }
85    }
86
87    private static final String POOL_NAME = "poolName";
88    private static final String USAGE     = "usage";
89    private static final String COUNT     = "count";
90    private static final String[] memoryNotifInfoItemNames = {
91        POOL_NAME,
92        USAGE,
93        COUNT,
94    };
95
96
97    public static String getPoolName(CompositeData cd) {
98        String poolname = getString(cd, POOL_NAME);
99        if (poolname == null) {
100            throw new IllegalArgumentException("Invalid composite data: " +
101                "Attribute " + POOL_NAME + " has null value");
102        }
103        return poolname;
104    }
105
106    public static MemoryUsage getUsage(CompositeData cd) {
107        CompositeData usageData = (CompositeData) cd.get(USAGE);
108        return MemoryUsage.from(usageData);
109    }
110
111    public static long getCount(CompositeData cd) {
112        return getLong(cd, COUNT);
113    }
114
115    /** Validate if the input CompositeData has the expected
116     * CompositeType (i.e. contain all attributes with expected
117     * names and types).
118     */
119    public static void validateCompositeData(CompositeData cd) {
120        if (cd == null) {
121            throw new NullPointerException("Null CompositeData");
122        }
123
124        if (!isTypeMatched(memoryNotifInfoCompositeType, cd.getCompositeType())) {
125            throw new IllegalArgumentException(
126                "Unexpected composite type for MemoryNotificationInfo");
127        }
128    }
129
130    private static final long serialVersionUID = -1805123446483771291L;
131}
132