001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.lang.management;
019:
020: import java.lang.management.MemoryManagerMXBean;
021: import java.lang.management.MemoryPoolMXBean;
022: import java.lang.management.MemoryType;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: /**
028: * Runtime type for {@link MemoryManagerMXBean}
029: *
030: * @since 1.5
031: */
032: public class MemoryManagerMXBeanImpl extends DynamicMXBeanImpl
033: implements MemoryManagerMXBean {
034: protected final String name;
035:
036: protected final int id;
037:
038: private List<MemoryPoolMXBean> managedPoolList;
039:
040: /**
041: * Sets the metadata for this bean.
042: *
043: * @param name
044: * @param id
045: * @param memBean
046: */
047: MemoryManagerMXBeanImpl(String name, int id,
048: MemoryMXBeanImpl memBean) {
049: this .name = name;
050: this .id = id;
051: initializeInfo();
052: managedPoolList = new LinkedList<MemoryPoolMXBean>();
053: createMemoryPools(id, memBean);
054: }
055:
056: /**
057: *
058: */
059: protected void initializeInfo() {
060: setMBeanInfo(ManagementUtils
061: .getMBeanInfo(MemoryManagerMXBean.class.getName()));
062: }
063:
064: /**
065: * Instantiate the MemoryPoolMXBeans representing the memory managed by this
066: * manager, and store them in the managedPoolList.
067: *
068: * @param managerID
069: * @param memBean
070: */
071: private native void createMemoryPools(int managerID,
072: MemoryMXBeanImpl memBean);
073:
074: /**
075: * A helper method called from within the native
076: * {@link #createMemoryPools(int, MemoryMXBeanImpl)} method to construct new
077: * MemoryPoolMXBeans representing memory managed by this manager and add
078: * them to the {@link #managedPoolList}.
079: *
080: * @param name
081: * the name of the corresponding memory pool
082: * @param isHeap
083: * boolean indication of the memory pool type. <code>true</code>
084: * indicates that the memory is heap memory while
085: * <code>false</code> indicates non-heap memory
086: * @param internalID
087: * numerical identifier associated with the memory pool for the
088: * benefit of the VM
089: * @param memBean
090: * the {@link MemoryMXBeanImpl} that will send event
091: * notifications related to this memory pool
092: */
093: @SuppressWarnings("unused")
094: // IMPORTANT: for use by VM
095: private void createMemoryPoolHelper(String name, boolean isHeap,
096: int internalID, MemoryMXBeanImpl memBean) {
097: managedPoolList.add(new MemoryPoolMXBeanImpl(name,
098: isHeap ? MemoryType.HEAP : MemoryType.NON_HEAP,
099: internalID, memBean));
100: }
101:
102: /**
103: * Retrieves the list of memory pool beans managed by this manager.
104: *
105: * @return the list of <code>MemoryPoolMXBean</code> instances
106: */
107: List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
108: return managedPoolList;
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see java.lang.management.MemoryManagerMXBean#getMemoryPoolNames()
115: */
116: public String[] getMemoryPoolNames() {
117: String[] names = new String[managedPoolList.size()];
118: int idx = 0;
119: Iterator<MemoryPoolMXBean> iter = managedPoolList.iterator();
120: while (iter.hasNext()) {
121: names[idx++] = iter.next().getName();
122: }
123: return names;
124: }
125:
126: /*
127: * (non-Javadoc)
128: *
129: * @see java.lang.management.MemoryManagerMXBean#getName()
130: */
131: public String getName() {
132: return this .name;
133: }
134:
135: /**
136: * @return <code>true</code> if the memory manager is still valid in the
137: * virtual machine ; otherwise <code>false</code>.
138: * @see #isValid()
139: */
140: private native boolean isValidImpl();
141:
142: /*
143: * (non-Javadoc)
144: *
145: * @see java.lang.management.MemoryManagerMXBean#isValid()
146: */
147: public boolean isValid() {
148: return this.isValidImpl();
149: }
150: }
|