1/*
2 * Copyright (c) 2003, 2011, 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.ManagementFactory;
29import java.lang.management.MemoryManagerMXBean;
30import java.lang.management.MemoryPoolMXBean;
31
32import javax.management.MBeanNotificationInfo;
33import javax.management.ObjectName;
34
35/**
36 * Implementation class for a memory manager.
37 * Standard and committed hotspot-specific metrics if any.
38 *
39 * ManagementFactory.getMemoryManagerMXBeans() returns a list
40 * of instances of this class.
41 */
42class MemoryManagerImpl extends NotificationEmitterSupport
43    implements MemoryManagerMXBean {
44
45    private final String  name;
46    private final boolean isValid;
47    private MemoryPoolMXBean[] pools;
48
49    MemoryManagerImpl(String name) {
50        this.name = name;
51        this.isValid = true;
52        this.pools = null;
53    }
54
55    public String getName() {
56        return name;
57    }
58
59    public boolean isValid() {
60        return isValid;
61    }
62
63    public String[] getMemoryPoolNames() {
64        MemoryPoolMXBean[] ps = getMemoryPools();
65
66        String[] names = new String[ps.length];
67        for (int i = 0; i < ps.length; i++) {
68            names[i] = ps[i].getName();
69        }
70        return names;
71    }
72
73    synchronized MemoryPoolMXBean[] getMemoryPools() {
74        if (pools == null) {
75            pools = getMemoryPools0();
76        }
77        return pools;
78    }
79    private native MemoryPoolMXBean[] getMemoryPools0();
80
81    private MBeanNotificationInfo[] notifInfo = null;
82    public MBeanNotificationInfo[] getNotificationInfo() {
83        synchronized (this) {
84            if(notifInfo == null) {
85                notifInfo = new MBeanNotificationInfo[0];
86            }
87        }
88        return notifInfo;
89    }
90
91    public ObjectName getObjectName() {
92        return Util.newObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE, getName());
93    }
94
95}
96