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