0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.management.snmp.jvminstr;
0N/A
0N/A
0N/A// java imports
0N/A//
0N/Aimport com.sun.jmx.mbeanserver.Util;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.TreeMap;
0N/A
0N/A// jmx imports
0N/A//
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/Aimport com.sun.jmx.snmp.SnmpStatusException;
0N/A
0N/A// jdmk imports
0N/A//
0N/Aimport com.sun.jmx.snmp.agent.SnmpMib;
0N/Aimport com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
0N/A
0N/Aimport java.lang.management.MemoryManagerMXBean;
0N/Aimport java.lang.management.ManagementFactory;
0N/A
0N/Aimport sun.management.snmp.jvmmib.JvmMemManagerTableMeta;
0N/Aimport sun.management.snmp.util.SnmpTableCache;
0N/Aimport sun.management.snmp.util.SnmpNamedListTableCache;
0N/Aimport sun.management.snmp.util.SnmpTableHandler;
0N/Aimport sun.management.snmp.util.MibLogger;
0N/Aimport sun.management.snmp.util.JvmContextFactory;
0N/A
0N/A/**
0N/A * The class is used for implementing the "JvmMemManagerTable" table.
0N/A *
0N/A * This custom implementation show how to implement an SNMP table
0N/A * over a weak cache, recomputing the cahed data when needed.
0N/A */
0N/Apublic class JvmMemManagerTableMetaImpl extends JvmMemManagerTableMeta {
0N/A
0N/A /**
0N/A * A concrete implementation of {@link SnmpNamedListTableCache}, for the
0N/A * jvmMemManagerTable.
0N/A **/
0N/A private static class JvmMemManagerTableCache
0N/A extends SnmpNamedListTableCache {
0N/A /**
0N/A * Create a weak cache for the jvmMemManagerTable.
0N/A * @param validity validity of the cached data, in ms.
0N/A **/
0N/A JvmMemManagerTableCache(long validity) {
0N/A this.validity = validity;
0N/A }
0N/A
0N/A /**
0N/A * Use the MemoryManagerMXBean name as key.
0N/A * @param context A {@link TreeMap} as allocated by the parent
0N/A * {@link SnmpNamedListTableCache} class.
0N/A * @param rawDatas List of {@link MemoryManagerMXBean}, as
0N/A * returned by
0N/A * <code>ManagementFactory.getMemoryMBean().getMemoryManagers()</code>
0N/A * @param rank The <var>rank</var> of <var>item</var> in the list.
0N/A * @param item The <var>rank</var><super>th</super>
0N/A * <code>MemoryManagerMXBean</code> in the list.
0N/A * @return <code>((MemoryManagerMXBean)item).getName()</code>
0N/A **/
0N/A protected String getKey(Object context, List rawDatas,
0N/A int rank, Object item) {
0N/A if (item == null) return null;
0N/A final String name = ((MemoryManagerMXBean)item).getName();
0N/A log.debug("getKey", "key=" + name);
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Call <code>getTableHandler(JvmContextFactory.getUserData())</code>.
0N/A **/
0N/A public SnmpTableHandler getTableHandler() {
0N/A final Map userData = JvmContextFactory.getUserData();
0N/A return getTableDatas(userData);
0N/A }
0N/A
0N/A /**
0N/A * Return the key used to cache the raw data of this table.
0N/A **/
0N/A protected String getRawDatasKey() {
0N/A return "JvmMemManagerTable.getMemoryManagers";
0N/A }
0N/A
0N/A /**
0N/A * Call ManagementFactory.getMemoryManagerMXBeans() to
0N/A * load the raw data of this table.
0N/A **/
0N/A protected List loadRawDatas(Map userData) {
0N/A return ManagementFactory.getMemoryManagerMXBeans();
0N/A }
0N/A
0N/A }
0N/A
0N/A // The weak cache for this table.
0N/A protected SnmpTableCache cache;
0N/A
0N/A /**
0N/A * Constructor for the table. Initialize metadata for
0N/A * "JvmMemManagerTableMeta".
0N/A * The reference on the MBean server is updated so the entries
0N/A * created through an SNMP SET will be AUTOMATICALLY REGISTERED
0N/A * in Java DMK.
0N/A */
0N/A public JvmMemManagerTableMetaImpl(SnmpMib myMib,
0N/A SnmpStandardObjectServer objserv) {
0N/A super(myMib,objserv);
0N/A this.cache = new
0N/A JvmMemManagerTableCache(((JVM_MANAGEMENT_MIB_IMPL)myMib).
0N/A validity());
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected SnmpOid getNextOid(Object userData)
0N/A throws SnmpStatusException {
0N/A // null means get the first OID.
0N/A return getNextOid(null,userData);
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected SnmpOid getNextOid(SnmpOid oid, Object userData)
0N/A throws SnmpStatusException {
0N/A final boolean dbg = log.isDebugOn();
0N/A if (dbg) log.debug("getNextOid", "previous=" + oid);
0N/A
0N/A
0N/A // Get the data handler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(userData);
0N/A if (handler == null) {
0N/A // This should never happen.
0N/A // If we get here it's a bug.
0N/A //
0N/A if (dbg) log.debug("getNextOid", "handler is null!");
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A }
0N/A
0N/A // Get the next oid
0N/A //
0N/A final SnmpOid next = handler.getNext(oid);
0N/A if (dbg) log.debug("getNextOid", "next=" + next);
0N/A
0N/A // if next is null: we reached the end of the table.
0N/A //
0N/A if (next == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A return next;
0N/A }
0N/A
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected boolean contains(SnmpOid oid, Object userData) {
0N/A
0N/A // Get the handler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(userData);
0N/A
0N/A // handler should never be null.
0N/A //
0N/A if (handler == null)
0N/A return false;
0N/A
0N/A return handler.contains(oid);
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A public Object getEntry(SnmpOid oid)
0N/A throws SnmpStatusException {
0N/A
0N/A if (oid == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // Get the request contextual cache (userData).
0N/A //
0N/A final Map<Object, Object> m = JvmContextFactory.getUserData();
0N/A
0N/A // We know in the case of this table that the index is an integer,
0N/A // it is thus the first OID arc of the index OID.
0N/A //
0N/A final long index = oid.getOidArc(0);
0N/A
0N/A // We're going to use this name to store/retrieve the entry in
0N/A // the request contextual cache.
0N/A //
0N/A // Revisit: Probably better programming to put all these strings
0N/A // in some interface.
0N/A //
0N/A final String entryTag = ((m==null)?null:("JvmMemManagerTable.entry." +
0N/A index));
0N/A
0N/A // If the entry is in the cache, simply return it.
0N/A //
0N/A if (m != null) {
0N/A final Object entry = m.get(entryTag);
0N/A if (entry != null) return entry;
0N/A }
0N/A
0N/A // The entry was not in the cache, make a new one.
0N/A //
0N/A // Get the data hanler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(m);
0N/A
0N/A // handler should never be null.
0N/A //
0N/A if (handler == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // Get the data associated with our entry.
0N/A //
0N/A final Object data = handler.getData(oid);
0N/A
0N/A // data may be null if the OID we were given is not valid.
0N/A //
0N/A if (data == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // make the new entry (transient object that will be kept only
0N/A // for the duration of the request.
0N/A //
0N/A final Object entry =
0N/A new JvmMemManagerEntryImpl((MemoryManagerMXBean)data,(int)index);
0N/A
0N/A // Put the entry in the cache in case we need it later while processing
0N/A // the request.
0N/A //
0N/A if (m != null && entry != null) {
0N/A m.put(entryTag,entry);
0N/A }
0N/A
0N/A return entry;
0N/A }
0N/A
0N/A /**
0N/A * Get the SnmpTableHandler that holds the jvmMemManagerTable data.
0N/A * First look it up in the request contextual cache, and if it is
0N/A * not found, obtain it from the weak cache.
0N/A * <br>The request contextual cache will be released at the end of the
0N/A * current requests, and is used only to process this request.
0N/A * <br>The weak cache is shared by all requests, and is only
0N/A * recomputed when it is found to be obsolete.
0N/A * <br>Note that the data put in the request contextual cache is
0N/A * never considered to be obsolete, in order to preserve data
0N/A * coherency.
0N/A **/
0N/A protected SnmpTableHandler getHandler(Object userData) {
0N/A final Map<Object, Object> m;
0N/A if (userData instanceof Map) m=Util.cast(userData);
0N/A else m=null;
0N/A
0N/A // Look in the contextual cache.
0N/A if (m != null) {
0N/A final SnmpTableHandler handler =
0N/A (SnmpTableHandler)m.get("JvmMemManagerTable.handler");
0N/A if (handler != null) return handler;
0N/A }
0N/A
0N/A // No handler in contextual cache, make a new one.
0N/A final SnmpTableHandler handler = cache.getTableHandler();
0N/A
0N/A if (m != null && handler != null )
0N/A m.put("JvmMemManagerTable.handler",handler);
0N/A
0N/A return handler;
0N/A }
0N/A
0N/A static final MibLogger log =
0N/A new MibLogger(JvmMemManagerTableMetaImpl.class);
0N/A}