jdk/src/share/classes/sun/management/snmp/jvminstr/JvmThreadInstanceTableMetaImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.management.snmp.jvminstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// java imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.mbeanserver.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.ThreadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// jmx imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.jmx.snmp.SnmpCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.jmx.snmp.SnmpCounter64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import com.sun.jmx.snmp.SnmpGauge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import com.sun.jmx.snmp.SnmpInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.jmx.snmp.SnmpUnsignedInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.jmx.snmp.SnmpIpAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.jmx.snmp.SnmpTimeticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import com.sun.jmx.snmp.SnmpOpaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import com.sun.jmx.snmp.SnmpString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import com.sun.jmx.snmp.SnmpStringFixed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import com.sun.jmx.snmp.SnmpOid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import com.sun.jmx.snmp.SnmpNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import com.sun.jmx.snmp.SnmpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import com.sun.jmx.snmp.SnmpVarBind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import com.sun.jmx.snmp.SnmpStatusException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// jdmk imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import com.sun.jmx.snmp.agent.SnmpIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import com.sun.jmx.snmp.agent.SnmpMib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import com.sun.jmx.snmp.agent.SnmpMibTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import com.sun.jmx.snmp.agent.SnmpMibSubRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.management.snmp.jvmmib.JvmThreadInstanceEntryMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.management.snmp.jvmmib.JvmThreadInstanceTableMeta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.management.snmp.util.SnmpTableCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.management.snmp.util.SnmpCachedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import sun.management.snmp.util.SnmpTableHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.management.snmp.util.MibLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.management.snmp.util.JvmContextFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The class is used for implementing the "JvmThreadInstanceTable" group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class JvmThreadInstanceTableMetaImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    extends JvmThreadInstanceTableMeta {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Maximum depth of the stacktrace that might be returned through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * SNMP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Since we do not export the stack trace through SNMP, we set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * MAX_STACK_TRACE_DEPTH=0 so that ThreadMXBean.getThreadInfo(long) does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * not compute the stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int MAX_STACK_TRACE_DEPTH=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Translate from a long to a Oid. Arc follow the long big-endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param l The long to make the index from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return The arc array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static SnmpOid makeOid(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        long[] x =  new long [8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        x[0] = (l >> 56) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        x[1] =  (l >> 48) & 0x00FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        x[2] =  (l >> 40) & 0x0000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        x[3] =  (l >> 32) & 0x000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        x[4] =  (l >> 24) & 0x00000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        x[5] =  (l >> 16) & 0x0000000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        x[6] =  (l >> 8)  & 0x000000000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        x[7] =  l         & 0x00000000000000FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return new SnmpOid(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Translate an Oid to a thread id. Arc follow the long big-endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param oid The oid to make the id from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return The thread id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static long makeId(SnmpOid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        long id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        long[] arcs = oid.longValue(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        id |= arcs[0] << 56;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        id |= arcs[1] << 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        id |= arcs[2] << 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        id |= arcs[3] << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        id |= arcs[4] << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        id |= arcs[5] << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        id |= arcs[6] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        id |= arcs[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * A concrete implementation of {@link SnmpTableCache}, for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * JvmThreadInstanceTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static class JvmThreadInstanceTableCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        extends SnmpTableCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        final private JvmThreadInstanceTableMetaImpl meta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Create a weak cache for the JvmThreadInstanceTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * @param validity validity of the cached data, in ms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        JvmThreadInstanceTableCache(JvmThreadInstanceTableMetaImpl meta,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                   long validity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            this.validity = validity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            this.meta     = meta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Call <code>getTableDatas(JvmContextFactory.getUserData())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public SnmpTableHandler getTableHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            final Map userData = JvmContextFactory.getUserData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return getTableDatas(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Return a table handler containing the Thread indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * Indexes are computed from the ThreadId.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        protected SnmpCachedData updateCachedDatas(Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // We are getting all the thread ids. WARNING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Some of them will be not valid when accessed for data...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // See getEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            long[] id = JvmThreadingImpl.getThreadMXBean().getAllThreadIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Time stamp for the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            final long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            SnmpOid indexes[] = new SnmpOid[id.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            final TreeMap<SnmpOid, Object> table =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    new TreeMap<SnmpOid, Object>(SnmpCachedData.oidComparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            for(int i = 0; i < id.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                log.debug("", "Making index for thread id [" + id[i] +"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                //indexes[i] = makeOid(id[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                SnmpOid oid = makeOid(id[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                table.put(oid, oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return new SnmpCachedData(time, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // The weak cache for this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected SnmpTableCache cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Constructor for the table. Initialize metadata for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * "JvmThreadInstanceTableMeta".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The reference on the MBean server is updated so the entries created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * through an SNMP SET will be AUTOMATICALLY REGISTERED in Java DMK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public JvmThreadInstanceTableMetaImpl(SnmpMib myMib,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                          SnmpStandardObjectServer objserv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        super(myMib, objserv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        cache = new JvmThreadInstanceTableCache(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            ((JVM_MANAGEMENT_MIB_IMPL)myMib).validity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        log.debug("JvmThreadInstanceTableMetaImpl", "Create Thread meta");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // See com.sun.jmx.snmp.agent.SnmpMibTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected SnmpOid getNextOid(Object userData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throws SnmpStatusException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        log.debug("JvmThreadInstanceTableMetaImpl", "getNextOid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // null means get the first OID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return getNextOid(null,userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // See com.sun.jmx.snmp.agent.SnmpMibTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected SnmpOid getNextOid(SnmpOid oid, Object userData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throws SnmpStatusException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        log.debug("getNextOid", "previous=" + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Get the data handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        SnmpTableHandler handler = getHandler(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // If we get here it's a bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            log.debug("getNextOid", "handler is null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // Get the next oid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        SnmpOid next = oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            next = handler.getNext(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (next == null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (getJvmThreadInstance(userData,next) != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        log.debug("*** **** **** **** getNextOid", "next=" + next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // if next is null: we reached the end of the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // See com.sun.jmx.snmp.agent.SnmpMibTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected boolean contains(SnmpOid oid, Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // Get the handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        SnmpTableHandler handler = getHandler(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // handler should never be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (handler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if(!handler.contains(oid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        JvmThreadInstanceEntryImpl inst = getJvmThreadInstance(userData, oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return (inst != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // See com.sun.jmx.snmp.agent.SnmpMibTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public Object getEntry(SnmpOid oid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        throws SnmpStatusException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        log.debug("*** **** **** **** getEntry", "oid [" + oid + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (oid == null || oid.getLength() != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            log.debug("getEntry", "Invalid oid [" + oid + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Get the request contextual cache (userData).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        final Map m = JvmContextFactory.getUserData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Get the handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        SnmpTableHandler handler = getHandler(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // handler should never be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (handler == null || !handler.contains(oid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        final JvmThreadInstanceEntryImpl entry = getJvmThreadInstance(m,oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (entry == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Get the SnmpTableHandler that holds the jvmThreadInstanceTable data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * First look it up in the request contextual cache, and if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * not found, obtain it from the weak cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <br>The request contextual cache will be released at the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * current requests, and is used only to process this request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <br>The weak cache is shared by all requests, and is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * recomputed when it is found to be obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <br>Note that the data put in the request contextual cache is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *     never considered to be obsolete, in order to preserve data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *     coherency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    protected SnmpTableHandler getHandler(Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        final Map<Object, Object> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (userData instanceof Map) m=Util.cast(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        else m=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Look in the contextual cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            final SnmpTableHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                (SnmpTableHandler)m.get("JvmThreadInstanceTable.handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (handler != null) return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // No handler in contextual cache, make a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        final SnmpTableHandler handler = cache.getTableHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (m != null && handler != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            m.put("JvmThreadInstanceTable.handler",handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private ThreadInfo getThreadInfo(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return JvmThreadingImpl.getThreadMXBean().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                  getThreadInfo(id,MAX_STACK_TRACE_DEPTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private ThreadInfo getThreadInfo(SnmpOid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return getThreadInfo(makeId(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private JvmThreadInstanceEntryImpl getJvmThreadInstance(Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                                            SnmpOid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        JvmThreadInstanceEntryImpl cached = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        String entryTag = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        Map<Object, Object> map = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        final boolean dbg = log.isDebugOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (userData instanceof Map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            map = Util.cast(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            // We're going to use this name to store/retrieve the entry in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // the request contextual cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // Revisit: Probably better programming to put all these strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            //          in some interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            entryTag = "JvmThreadInstanceTable.entry." + oid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            cached = (JvmThreadInstanceEntryImpl) map.get(entryTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // If the entry is in the cache, simply return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (cached != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (dbg) log.debug("*** getJvmThreadInstance",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                               "Entry found in cache: " + entryTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (dbg) log.debug("*** getJvmThreadInstance", "Entry [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                           oid + "] is not in cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // Entry not in cache. We will create one if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        ThreadInfo info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            info = getThreadInfo(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (RuntimeException r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            log.trace("*** getJvmThreadInstance",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                      "Failed to get thread info for rowOid: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            log.debug("*** getJvmThreadInstance",r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // No thread by that id => no entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if(info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (dbg) log.debug("*** getJvmThreadInstance",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                               "No entry by that oid [" + oid + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        cached = new JvmThreadInstanceEntryImpl(info, oid.toByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (map != null) map.put(entryTag, cached);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (dbg) log.debug("*** getJvmThreadInstance",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                           "Entry created for Thread OID [" + oid + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     static final MibLogger log =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        new MibLogger(JvmThreadInstanceTableMetaImpl.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
}