blob: 33644d806685c5329f7dcc70cfd490db6f81bb9a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 */
25package sun.management.snmp.util;
26
27import com.sun.jmx.mbeanserver.Util;
28import com.sun.jmx.snmp.agent.SnmpUserDataFactory;
29import com.sun.jmx.snmp.SnmpPdu;
30import com.sun.jmx.snmp.SnmpStatusException;
31
32import java.util.Collections;
33import java.util.HashMap;
34import java.util.Map;
35
36
37public class JvmContextFactory implements SnmpUserDataFactory {
38
39 /**
40 * Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
41 * Allocate a contextual object containing some user data. This method
42 * is called once for each incoming SNMP request. The scope
43 * of this object will be the whole request. Since the request can be
44 * handled in several threads, the user should make sure that this
45 * object can be accessed in a thread-safe manner. The SNMP framework
46 * will never access this object directly - it will simply pass
47 * it to the <code>SnmpMibAgent</code> within
48 * <code>SnmpMibRequest</code> objects - from where it can be retrieved
49 * through the {@link com.sun.jmx.snmp.agent.SnmpMibRequest#getUserData() getUserData()} accessor.
50 * <code>null</code> is considered to be a valid return value.
51 *
52 * This method is called just after the SnmpPduPacket has been
53 * decoded.
54 *
55 * @param requestPdu The SnmpPduPacket received from the SNMP manager.
56 * <b>This parameter is owned by the SNMP framework and must be
57 * considered as transient.</b> If you wish to keep some of its
58 * content after this method returns (by storing it in the
59 * returned object for instance) you should clone that
60 * information.
61 *
62 * @return A newly allocated user-data contextual object, or
63 * <code>null</code>
64 * @exception SnmpStatusException If an SnmpStatusException is thrown,
65 * the request will be aborted.
66 *
67 * @since Java DMK 5.0
68 **/
69 public Object allocateUserData(SnmpPdu requestPdu)
70 throws SnmpStatusException {
71 return Collections.synchronizedMap(new HashMap<Object, Object>());
72 }
73
74 /**
75 * Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
76 * Release a previously allocated contextual object containing user-data.
77 * This method is called just before the responsePdu is sent back to the
78 * manager. It gives the user a chance to alter the responsePdu packet
79 * before it is encoded, and to free any resources that might have
80 * been allocated when creating the contextual object.
81 *
82 * @param userData The contextual object being released.
83 * @param responsePdu The SnmpPduPacket that will be sent back to the
84 * SNMP manager.
85 * <b>This parameter is owned by the SNMP framework and must be
86 * considered as transient.</b> If you wish to keep some of its
87 * content after this method returns you should clone that
88 * information.
89 *
90 * @exception SnmpStatusException If an SnmpStatusException is thrown,
91 * the responsePdu is dropped and nothing is returned to
92 * to the manager.
93 *
94 * @since Java DMK 5.0
95 **/
96 public void releaseUserData(Object userData, SnmpPdu responsePdu)
97 throws SnmpStatusException {
98 ((Map<?, ?>)userData).clear();
99 }
100
101
102 public static Map<Object, Object> getUserData() {
103 final Object userData =
104 com.sun.jmx.snmp.ThreadContext.get("SnmpUserData");
105
106 if (userData instanceof Map<?, ?>) return Util.cast(userData);
107 else return null;
108 }
109
110}