J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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.JvmRTInputArgsTableMeta; |
| 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 "JvmRTInputArgsTable" group. |
| 70 | */ |
| 71 | public class JvmRTInputArgsTableMetaImpl extends JvmRTInputArgsTableMeta { |
| 72 | |
| 73 | private SnmpTableCache cache; |
| 74 | |
| 75 | /** |
| 76 | * A concrete implementation of {@link SnmpTableCache}, for the |
| 77 | * JvmRTInputArgsTable. |
| 78 | **/ |
| 79 | private static class JvmRTInputArgsTableCache extends SnmpTableCache { |
| 80 | private JvmRTInputArgsTableMetaImpl meta; |
| 81 | |
| 82 | JvmRTInputArgsTableCache(JvmRTInputArgsTableMetaImpl meta, |
| 83 | long validity) { |
| 84 | this.meta = meta; |
| 85 | this.validity = validity; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Call <code>getTableDatas(JvmContextFactory.getUserData())</code>. |
| 90 | **/ |
| 91 | public SnmpTableHandler getTableHandler() { |
| 92 | final Map userData = JvmContextFactory.getUserData(); |
| 93 | return getTableDatas(userData); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Return a table handler containing the Thread indexes. |
| 99 | * Indexes are computed from the ThreadId. |
| 100 | **/ |
| 101 | protected SnmpCachedData updateCachedDatas(Object userData) { |
| 102 | |
| 103 | |
| 104 | // We are getting all the input args |
| 105 | final String[] args = JvmRuntimeImpl.getInputArguments(userData); |
| 106 | |
| 107 | // Time stamp for the cache |
| 108 | final long time = System.currentTimeMillis(); |
| 109 | SnmpOid indexes[] = new SnmpOid[args.length]; |
| 110 | |
| 111 | for(int i = 0; i < args.length; i++) { |
| 112 | indexes[i] = new SnmpOid(i + 1); |
| 113 | } |
| 114 | |
| 115 | return new SnmpCachedData(time, indexes, args); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Constructor for the table. Initialize metadata for |
| 121 | * "JvmRTInputArgsTableMeta". |
| 122 | * The reference on the MBean server is updated so the entries |
| 123 | * created through an SNMP SET will be AUTOMATICALLY REGISTERED |
| 124 | * in Java DMK. |
| 125 | */ |
| 126 | public JvmRTInputArgsTableMetaImpl(SnmpMib myMib, |
| 127 | SnmpStandardObjectServer objserv) { |
| 128 | super(myMib, objserv); |
| 129 | cache = new JvmRTInputArgsTableCache(this, -1); |
| 130 | } |
| 131 | |
| 132 | // See com.sun.jmx.snmp.agent.SnmpMibTable |
| 133 | protected SnmpOid getNextOid(Object userData) |
| 134 | throws SnmpStatusException { |
| 135 | // null means get the first OID. |
| 136 | return getNextOid(null,userData); |
| 137 | } |
| 138 | |
| 139 | // See com.sun.jmx.snmp.agent.SnmpMibTable |
| 140 | protected SnmpOid getNextOid(SnmpOid oid, Object userData) |
| 141 | throws SnmpStatusException { |
| 142 | final boolean dbg = log.isDebugOn(); |
| 143 | if (dbg) log.debug("getNextOid", "previous=" + oid); |
| 144 | |
| 145 | |
| 146 | // Get the data handler. |
| 147 | // |
| 148 | SnmpTableHandler handler = getHandler(userData); |
| 149 | if (handler == null) { |
| 150 | // This should never happen. |
| 151 | // If we get here it's a bug. |
| 152 | // |
| 153 | if (dbg) log.debug("getNextOid", "handler is null!"); |
| 154 | throw new SnmpStatusException(SnmpStatusException.noSuchInstance); |
| 155 | } |
| 156 | |
| 157 | // Get the next oid |
| 158 | // |
| 159 | final SnmpOid next = handler.getNext(oid); |
| 160 | if (dbg) log.debug("*** **** **** **** getNextOid", "next=" + next); |
| 161 | |
| 162 | // if next is null: we reached the end of the table. |
| 163 | // |
| 164 | if (next == null) |
| 165 | throw new SnmpStatusException(SnmpStatusException.noSuchInstance); |
| 166 | |
| 167 | return next; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | // See com.sun.jmx.snmp.agent.SnmpMibTable |
| 172 | protected boolean contains(SnmpOid oid, Object userData) { |
| 173 | |
| 174 | // Get the handler. |
| 175 | // |
| 176 | SnmpTableHandler handler = getHandler(userData); |
| 177 | |
| 178 | // handler should never be null. |
| 179 | // |
| 180 | if (handler == null) |
| 181 | return false; |
| 182 | |
| 183 | return handler.contains(oid); |
| 184 | } |
| 185 | |
| 186 | // See com.sun.jmx.snmp.agent.SnmpMibTable |
| 187 | public Object getEntry(SnmpOid oid) |
| 188 | throws SnmpStatusException { |
| 189 | final boolean dbg = log.isDebugOn(); |
| 190 | if (dbg) log.debug("getEntry", "oid [" + oid + "]"); |
| 191 | if (oid == null || oid.getLength() != 1) { |
| 192 | if (dbg) log.debug("getEntry", "Invalid oid [" + oid + "]"); |
| 193 | throw new SnmpStatusException(SnmpStatusException.noSuchInstance); |
| 194 | } |
| 195 | |
| 196 | // Get the request contextual cache (userData). |
| 197 | // |
| 198 | final Map<Object, Object> m = JvmContextFactory.getUserData(); |
| 199 | |
| 200 | // We're going to use this name to store/retrieve the entry in |
| 201 | // the request contextual cache. |
| 202 | // |
| 203 | // Revisit: Probably better programming to put all these strings |
| 204 | // in some interface. |
| 205 | // |
| 206 | final String entryTag = ((m==null)?null: |
| 207 | ("JvmRTInputArgsTable.entry." + |
| 208 | oid.toString())); |
| 209 | |
| 210 | // If the entry is in the cache, simply return it. |
| 211 | // |
| 212 | if (m != null) { |
| 213 | final Object entry = m.get(entryTag); |
| 214 | if (entry != null) { |
| 215 | if (dbg) |
| 216 | log.debug("getEntry", "Entry is already in the cache"); |
| 217 | return entry; |
| 218 | } else if (dbg) log.debug("getEntry", "Entry is not in the cache"); |
| 219 | } |
| 220 | |
| 221 | // The entry was not in the cache, make a new one. |
| 222 | // |
| 223 | // Get the data hanler. |
| 224 | // |
| 225 | SnmpTableHandler handler = getHandler(m); |
| 226 | |
| 227 | // handler should never be null. |
| 228 | // |
| 229 | if (handler == null) |
| 230 | throw new SnmpStatusException(SnmpStatusException.noSuchInstance); |
| 231 | |
| 232 | // Get the data associated with our entry. |
| 233 | // |
| 234 | final Object data = handler.getData(oid); |
| 235 | |
| 236 | // data may be null if the OID we were given is not valid. |
| 237 | // |
| 238 | if (data == null) |
| 239 | throw new SnmpStatusException(SnmpStatusException.noSuchInstance); |
| 240 | |
| 241 | // make the new entry (transient object that will be kept only |
| 242 | // for the duration of the request. |
| 243 | // |
| 244 | if (dbg) log.debug("getEntry","data is a: " + |
| 245 | data.getClass().getName()); |
| 246 | final Object entry = |
| 247 | new JvmRTInputArgsEntryImpl((String) data, (int) oid.getOidArc(0)); |
| 248 | |
| 249 | // Put the entry in the cache in case we need it later while processing |
| 250 | // the request. |
| 251 | // |
| 252 | if (m != null && entry != null) { |
| 253 | m.put(entryTag,entry); |
| 254 | } |
| 255 | |
| 256 | return entry; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Get the SnmpTableHandler that holds the jvmThreadInstanceTable data. |
| 261 | * First look it up in the request contextual cache, and if it is |
| 262 | * not found, obtain it from the weak cache. |
| 263 | * <br>The request contextual cache will be released at the end of the |
| 264 | * current requests, and is used only to process this request. |
| 265 | * <br>The weak cache is shared by all requests, and is only |
| 266 | * recomputed when it is found to be obsolete. |
| 267 | * <br>Note that the data put in the request contextual cache is |
| 268 | * never considered to be obsolete, in order to preserve data |
| 269 | * coherency. |
| 270 | **/ |
| 271 | protected SnmpTableHandler getHandler(Object userData) { |
| 272 | final Map<Object, Object> m; |
| 273 | if (userData instanceof Map) m=Util.cast(userData); |
| 274 | else m=null; |
| 275 | |
| 276 | // Look in the contextual cache. |
| 277 | if (m != null) { |
| 278 | final SnmpTableHandler handler = |
| 279 | (SnmpTableHandler)m.get("JvmRTInputArgsTable.handler"); |
| 280 | if (handler != null) return handler; |
| 281 | } |
| 282 | |
| 283 | // No handler in contextual cache, make a new one. |
| 284 | final SnmpTableHandler handler = cache.getTableHandler(); |
| 285 | |
| 286 | if (m != null && handler != null ) |
| 287 | m.put("JvmRTInputArgsTable.handler",handler); |
| 288 | |
| 289 | return handler; |
| 290 | } |
| 291 | |
| 292 | static final MibLogger log = |
| 293 | new MibLogger(JvmRTInputArgsTableMetaImpl.class); |
| 294 | } |