jdk/src/jdk.snmp/share/classes/sun/management/snmp/jvminstr/JvmRTBootClassPathTableMetaImpl.java
changeset 27220 315d620f5726
parent 27219 c4918033790e
parent 27214 d9f500808d8a
child 27222 422e90d83a4e
equal deleted inserted replaced
27219:c4918033790e 27220:315d620f5726
     1 /*
       
     2  * Copyright (c) 2004, 2012, 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 package sun.management.snmp.jvminstr;
       
    26 
       
    27 // java imports
       
    28 //
       
    29 import com.sun.jmx.mbeanserver.Util;
       
    30 import java.util.List;
       
    31 import java.util.Map;
       
    32 
       
    33 // jmx imports
       
    34 //
       
    35 import javax.management.MBeanServer;
       
    36 import javax.management.ObjectName;
       
    37 import com.sun.jmx.snmp.SnmpCounter;
       
    38 import com.sun.jmx.snmp.SnmpCounter64;
       
    39 import com.sun.jmx.snmp.SnmpGauge;
       
    40 import com.sun.jmx.snmp.SnmpInt;
       
    41 import com.sun.jmx.snmp.SnmpUnsignedInt;
       
    42 import com.sun.jmx.snmp.SnmpIpAddress;
       
    43 import com.sun.jmx.snmp.SnmpTimeticks;
       
    44 import com.sun.jmx.snmp.SnmpOpaque;
       
    45 import com.sun.jmx.snmp.SnmpString;
       
    46 import com.sun.jmx.snmp.SnmpStringFixed;
       
    47 import com.sun.jmx.snmp.SnmpOid;
       
    48 import com.sun.jmx.snmp.SnmpNull;
       
    49 import com.sun.jmx.snmp.SnmpValue;
       
    50 import com.sun.jmx.snmp.SnmpVarBind;
       
    51 import com.sun.jmx.snmp.SnmpStatusException;
       
    52 
       
    53 // jdmk imports
       
    54 //
       
    55 import com.sun.jmx.snmp.agent.SnmpIndex;
       
    56 import com.sun.jmx.snmp.agent.SnmpMib;
       
    57 import com.sun.jmx.snmp.agent.SnmpMibTable;
       
    58 import com.sun.jmx.snmp.agent.SnmpMibSubRequest;
       
    59 import com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
       
    60 
       
    61 import sun.management.snmp.jvmmib.JvmRTBootClassPathTableMeta;
       
    62 import sun.management.snmp.util.SnmpCachedData;
       
    63 import sun.management.snmp.util.SnmpTableCache;
       
    64 import sun.management.snmp.util.SnmpTableHandler;
       
    65 import sun.management.snmp.util.MibLogger;
       
    66 import sun.management.snmp.util.JvmContextFactory;
       
    67 
       
    68 /**
       
    69  * The class is used for implementing the "JvmRTBootClassPathTable".
       
    70   */
       
    71 public class JvmRTBootClassPathTableMetaImpl
       
    72     extends JvmRTBootClassPathTableMeta {
       
    73 
       
    74     static final long serialVersionUID = -8659886610487538299L;
       
    75 
       
    76     private SnmpTableCache cache;
       
    77 
       
    78      /**
       
    79      * A concrete implementation of {@link SnmpTableCache}, for the
       
    80      * JvmRTBootClassPathTable.
       
    81      **/
       
    82     private static class JvmRTBootClassPathTableCache extends SnmpTableCache {
       
    83         static final long serialVersionUID = -2637458695413646098L;
       
    84         private JvmRTBootClassPathTableMetaImpl meta;
       
    85 
       
    86         JvmRTBootClassPathTableCache(JvmRTBootClassPathTableMetaImpl meta,
       
    87                                  long validity) {
       
    88             this.meta = meta;
       
    89             this.validity = validity;
       
    90         }
       
    91 
       
    92         /**
       
    93          * Call <code>getTableDatas(JvmContextFactory.getUserData())</code>.
       
    94          **/
       
    95         public SnmpTableHandler getTableHandler() {
       
    96             final Map<Object,Object> userData = JvmContextFactory.getUserData();
       
    97             return getTableDatas(userData);
       
    98         }
       
    99 
       
   100 
       
   101         /**
       
   102          * Return a table handler containing the Thread indexes.
       
   103          * Indexes are computed from the ThreadId.
       
   104          **/
       
   105         protected SnmpCachedData updateCachedDatas(Object userData) {
       
   106 
       
   107 
       
   108             // We are getting all the input args
       
   109             final String[] path =
       
   110                 JvmRuntimeImpl.getBootClassPath(userData);
       
   111 
       
   112             // Time stamp for the cache
       
   113             final long time = System.currentTimeMillis();
       
   114             final int len = path.length;
       
   115 
       
   116             SnmpOid indexes[] = new SnmpOid[len];
       
   117 
       
   118             for(int i = 0; i < len; i++) {
       
   119                 indexes[i] = new SnmpOid(i + 1);
       
   120             }
       
   121 
       
   122             return new SnmpCachedData(time, indexes, path);
       
   123         }
       
   124     }
       
   125 
       
   126     /**
       
   127      * Constructor for the table. Initialize metadata for
       
   128      * "JvmRTBootClassPathTableMeta".
       
   129      * The reference on the MBean server is updated so the entries
       
   130      * created through an SNMP SET will be AUTOMATICALLY REGISTERED
       
   131      * in Java DMK.
       
   132      */
       
   133     public JvmRTBootClassPathTableMetaImpl(SnmpMib myMib,
       
   134                                        SnmpStandardObjectServer objserv) {
       
   135         super(myMib, objserv);
       
   136         cache = new JvmRTBootClassPathTableCache(this, -1);
       
   137     }
       
   138 
       
   139     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   140     protected SnmpOid getNextOid(Object userData)
       
   141         throws SnmpStatusException {
       
   142         // null means get the first OID.
       
   143         return getNextOid(null,userData);
       
   144     }
       
   145 
       
   146     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   147     protected SnmpOid getNextOid(SnmpOid oid, Object userData)
       
   148         throws SnmpStatusException {
       
   149         final boolean dbg = log.isDebugOn();
       
   150         if (dbg) log.debug("getNextOid", "previous=" + oid);
       
   151 
       
   152 
       
   153         // Get the data handler.
       
   154         //
       
   155         SnmpTableHandler handler = getHandler(userData);
       
   156         if (handler == null) {
       
   157             // This should never happen.
       
   158             // If we get here it's a bug.
       
   159             //
       
   160             if (dbg) log.debug("getNextOid", "handler is null!");
       
   161             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   162         }
       
   163 
       
   164         // Get the next oid
       
   165         //
       
   166         final SnmpOid next = handler.getNext(oid);
       
   167         if (dbg) log.debug("*** **** **** **** getNextOid", "next=" + next);
       
   168 
       
   169         // if next is null: we reached the end of the table.
       
   170         //
       
   171         if (next == null)
       
   172             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   173 
       
   174         return next;
       
   175     }
       
   176 
       
   177 
       
   178     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   179     protected boolean contains(SnmpOid oid, Object userData) {
       
   180 
       
   181         // Get the handler.
       
   182         //
       
   183         SnmpTableHandler handler = getHandler(userData);
       
   184 
       
   185         // handler should never be null.
       
   186         //
       
   187         if (handler == null)
       
   188             return false;
       
   189 
       
   190         return handler.contains(oid);
       
   191     }
       
   192 
       
   193     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   194     public Object getEntry(SnmpOid oid)
       
   195         throws SnmpStatusException {
       
   196         final boolean dbg = log.isDebugOn();
       
   197         if (dbg) log.debug("getEntry", "oid [" + oid + "]");
       
   198         if (oid == null || oid.getLength() != 1) {
       
   199             if (dbg) log.debug("getEntry", "Invalid oid [" + oid + "]");
       
   200             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   201         }
       
   202 
       
   203         // Get the request contextual cache (userData).
       
   204         //
       
   205         final Map<Object, Object> m = JvmContextFactory.getUserData();
       
   206 
       
   207         // We're going to use this name to store/retrieve the entry in
       
   208         // the request contextual cache.
       
   209         //
       
   210         // Revisit: Probably better programming to put all these strings
       
   211         //          in some interface.
       
   212         //
       
   213         final String entryTag = ((m==null)?null:
       
   214                                  ("JvmRTBootClassPathTable.entry." +
       
   215                                   oid.toString()));
       
   216 
       
   217         // If the entry is in the cache, simply return it.
       
   218         //
       
   219         if (m != null) {
       
   220             final Object entry = m.get(entryTag);
       
   221             if (entry != null) {
       
   222                 if (dbg)
       
   223                     log.debug("getEntry", "Entry is already in the cache");
       
   224                 return entry;
       
   225             } else
       
   226                 if (dbg) log.debug("getEntry", "Entry is not in the cache");
       
   227         }
       
   228 
       
   229         // The entry was not in the cache, make a new one.
       
   230         //
       
   231         // Get the data hanler.
       
   232         //
       
   233         SnmpTableHandler handler = getHandler(m);
       
   234 
       
   235         // handler should never be null.
       
   236         //
       
   237         if (handler == null)
       
   238             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   239 
       
   240         // Get the data associated with our entry.
       
   241         //
       
   242         final Object data = handler.getData(oid);
       
   243 
       
   244         // data may be null if the OID we were given is not valid.
       
   245         //
       
   246         if (data == null)
       
   247             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   248 
       
   249         // make the new entry (transient object that will be kept only
       
   250         // for the duration of the request.
       
   251         //
       
   252         if (dbg)
       
   253             log.debug("getEntry","data is a: " + data.getClass().getName());
       
   254         final Object entry =
       
   255             new JvmRTBootClassPathEntryImpl((String) data,
       
   256                                             (int) oid.getOidArc(0));
       
   257 
       
   258         // Put the entry in the cache in case we need it later while processing
       
   259         // the request.
       
   260         //
       
   261         if (m != null && entry != null) {
       
   262             m.put(entryTag,entry);
       
   263         }
       
   264 
       
   265         return entry;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Get the SnmpTableHandler that holds the jvmThreadInstanceTable data.
       
   270      * First look it up in the request contextual cache, and if it is
       
   271      * not found, obtain it from the weak cache.
       
   272      * <br>The request contextual cache will be released at the end of the
       
   273      * current requests, and is used only to process this request.
       
   274      * <br>The weak cache is shared by all requests, and is only
       
   275      * recomputed when it is found to be obsolete.
       
   276      * <br>Note that the data put in the request contextual cache is
       
   277      *     never considered to be obsolete, in order to preserve data
       
   278      *     coherency.
       
   279      **/
       
   280     protected SnmpTableHandler getHandler(Object userData) {
       
   281         final Map<Object, Object> m;
       
   282         if (userData instanceof Map) m=Util.cast(userData);
       
   283         else m=null;
       
   284 
       
   285         // Look in the contextual cache.
       
   286         if (m != null) {
       
   287             final SnmpTableHandler handler =
       
   288                 (SnmpTableHandler)m.get("JvmRTBootClassPathTable.handler");
       
   289             if (handler != null) return handler;
       
   290         }
       
   291 
       
   292         // No handler in contextual cache, make a new one.
       
   293         final SnmpTableHandler handler = cache.getTableHandler();
       
   294 
       
   295         if (m != null && handler != null )
       
   296             m.put("JvmRTBootClassPathTable.handler",handler);
       
   297 
       
   298         return handler;
       
   299     }
       
   300 
       
   301     static final MibLogger log =
       
   302         new MibLogger(JvmRTBootClassPathTableMetaImpl.class);
       
   303 }