/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.management.snmp.jvminstr;
// java imports
//
import com.sun.jmx.mbeanserver.Util;
import java.io.Serializable;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Collections;
// jmx imports
//
import javax.management.MBeanServer;
import javax.management.ObjectName;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpStatusException;
// jdmk imports
//
import com.sun.jmx.snmp.agent.SnmpMib;
import com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
import com.sun.jmx.snmp.agent.SnmpMibTable;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryPoolMXBean;
import sun.management.snmp.jvmmib.JvmMemMgrPoolRelTableMeta;
import sun.management.snmp.util.SnmpTableCache;
import sun.management.snmp.util.SnmpCachedData;
import sun.management.snmp.util.SnmpTableHandler;
import sun.management.snmp.util.MibLogger;
import sun.management.snmp.util.JvmContextFactory;
/**
* The class is used for implementing the "JvmMemMgrPoolRelTable" group.
*/
public class JvmMemMgrPoolRelTableMetaImpl extends JvmMemMgrPoolRelTableMeta
implements Serializable {
/**
* A concrete implementation of {@link SnmpTableCache}, for the
* jvmMemMgrPoolRelTable.
**/
private static class JvmMemMgrPoolRelTableCache
extends SnmpTableCache {
final private JvmMemMgrPoolRelTableMetaImpl meta;
/**
* Create a weak cache for the jvmMemMgrPoolRelTable.
* @param validity validity of the cached data, in ms.
**/
JvmMemMgrPoolRelTableCache(JvmMemMgrPoolRelTableMetaImpl meta,
long validity) {
this.validity = validity;
this.meta = meta;
}
/**
* Call getTableDatas(JvmContextFactory.getUserData()).
**/
public SnmpTableHandler getTableHandler() {
final Map userData = JvmContextFactory.getUserData();
return getTableDatas(userData);
}
/**
* Builds a map pool-name => pool-index from the SnmpTableHandler
* of the JvmMemPoolTable.
**/
private static Map buildPoolIndexMap(SnmpTableHandler handler) {
// optimization...
if (handler instanceof SnmpCachedData)
return buildPoolIndexMap((SnmpCachedData)handler);
// not optimizable... too bad.
final Map m = new HashMap();
SnmpOid index=null;
while ((index = handler.getNext(index))!=null) {
final MemoryPoolMXBean mpm =
(MemoryPoolMXBean)handler.getData(index);
if (mpm == null) continue;
final String name = mpm.getName();
if (name == null) continue;
m.put(name,index);
}
return m;
}
/**
* Builds a map pool-name => pool-index from the SnmpTableHandler
* of the JvmMemPoolTable.
* Optimized algorithm.
**/
private static Map buildPoolIndexMap(SnmpCachedData cached) {
if (cached == null) return Collections.emptyMap();
final SnmpOid[] indexes = cached.indexes;
final Object[] datas = cached.datas;
final int len = indexes.length;
final Map m = new HashMap(len);
for (int i=0; iManagementFactory.getMemoryMBean().getMemoryManagers()
* and caches the value.
* This ensures that
* ManagementFactory.getMemoryMBean().getMemoryManagers()
* is not called more than once per request, thus ensuring a
* consistent view of the table.
**/
protected SnmpCachedData updateCachedDatas(Object userData) {
// Get the MemoryManager table
final SnmpTableHandler mmHandler =
meta.getManagerHandler(userData);
// Get the MemoryPool table
final SnmpTableHandler mpHandler =
meta.getPoolHandler(userData);
// Time stamp for the cache
final long time = System.currentTimeMillis();
// Build a Map poolname -> index
final Map poolIndexMap = buildPoolIndexMap(mpHandler);
// For each memory manager, get the list of memory pools
// For each memory pool, find its index in the memory pool table
// Create a row in the relation table.
final TreeMap table =
new TreeMap(SnmpCachedData.oidComparator);
updateTreeMap(table,userData,mmHandler,mpHandler,poolIndexMap);
return new SnmpCachedData(time,table);
}
/**
* Get the list of memory pool associated with the
* given MemoryManagerMXBean.
**/
protected String[] getMemoryPools(Object userData,
MemoryManagerMXBean mmm, long mmarc) {
final String listTag =
"JvmMemManager." + mmarc + ".getMemoryPools";
String[] result=null;
if (userData instanceof Map) {
result = (String[])((Map)userData).get(listTag);
if (result != null) return result;
}
if (mmm!=null) {
result = mmm.getMemoryPoolNames();
}
if ((result!=null)&&(userData instanceof Map)) {
Map