jdk/src/jdk.snmp/share/classes/sun/management/snmp/util/JvmContextFactory.java
author sundar
Mon, 15 Sep 2014 15:18:13 +0530
changeset 26647 a6acc63c2a31
parent 25859 3317bb8137f4
permissions -rw-r--r--
8058422: Users should be able to overwrite "context" and "engine" variables Reviewed-by: lagergren, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.management.snmp.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.jmx.mbeanserver.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.snmp.agent.SnmpUserDataFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.snmp.SnmpPdu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.snmp.SnmpStatusException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class JvmContextFactory implements SnmpUserDataFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Allocate a contextual object containing some user data. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * is called once for each incoming SNMP request. The scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * of this object will be the whole request. Since the request can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * handled in several threads, the user should make sure that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * object can be accessed in a thread-safe manner. The SNMP framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * will never access this object directly - it will simply pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * it to the <code>SnmpMibAgent</code> within
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * <code>SnmpMibRequest</code> objects - from where it can be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * through the {@link com.sun.jmx.snmp.agent.SnmpMibRequest#getUserData() getUserData()} accessor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * <code>null</code> is considered to be a valid return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * This method is called just after the SnmpPduPacket has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * decoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param requestPdu The SnmpPduPacket received from the SNMP manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *        <b>This parameter is owned by the SNMP framework and must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *        considered as transient.</b> If you wish to keep some of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *        content after this method returns (by storing it in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *        returned object for instance) you should clone that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *        information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @return A newly allocated user-data contextual object, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception SnmpStatusException If an SnmpStatusException is thrown,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *            the request will be aborted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @since Java DMK 5.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public Object allocateUserData(SnmpPdu requestPdu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws SnmpStatusException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return Collections.synchronizedMap(new HashMap<Object, Object>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Release a previously allocated contextual object containing user-data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * This method is called just before the responsePdu is sent back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * manager. It gives the user a chance to alter the responsePdu packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * before it is encoded, and to free any resources that might have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * been allocated when creating the contextual object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param userData The contextual object being released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param responsePdu The SnmpPduPacket that will be sent back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *        SNMP manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *        <b>This parameter is owned by the SNMP framework and must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *        considered as transient.</b> If you wish to keep some of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *        content after this method returns you should clone that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *        information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @exception SnmpStatusException If an SnmpStatusException is thrown,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *            the responsePdu is dropped and nothing is returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *            to the manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @since Java DMK 5.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void releaseUserData(Object userData, SnmpPdu responsePdu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws SnmpStatusException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ((Map<?, ?>)userData).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static Map<Object, Object> getUserData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        final Object userData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            com.sun.jmx.snmp.ThreadContext.get("SnmpUserData");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (userData instanceof Map<?, ?>) return Util.cast(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        else return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}