1 /*
   2  * Copyright (c) 2000, 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 com.sun.jmx.snmp.agent;
  26 
  27 // java imports
  28 //
  29 import java.io.Serializable;
  30 import java.util.Enumeration;
  31 import com.sun.jmx.snmp.SnmpVarBind;
  32 import com.sun.jmx.snmp.SnmpStatusException;
  33 
  34 // SNMP Runtime imports
  35 //
  36 
  37 /**
  38  * <p>
  39  * This class is a utility class that transform SNMP GET / SET requests
  40  * into series of get<i>AttributeName</i>() set<i>AttributeName</i>()
  41  * invoked on the MBean.
  42  * </p>
  43  *
  44  * <p>
  45  * The transformation relies on the metadata information provided by the
  46  * {@link com.sun.jmx.snmp.agent.SnmpStandardMetaServer} object which is
  47  * passed as first parameter to every method. This SnmpStandardMetaServer
  48  * object is usually a Metadata object generated by <code>mibgen</code>.
  49  * </p>
  50  *
  51  * <p>
  52  * The MBean is not invoked directly by this class but through the
  53  * metadata object which holds a reference on it.
  54  * </p>
  55  *
  56  * <p><b><i>
  57  * This class is used internally by mibgen generated metadata objects and
  58  * you should never need to use it directly.
  59  * </b></i></p>
  60  * <p><b>This API is a Sun Microsystems internal API  and is subject
  61  * to change without notice.</b></p>
  62  **/
  63 
  64 public class SnmpStandardObjectServer implements Serializable {
  65     private static final long serialVersionUID = -4641068116505308488L;
  66 
  67     /**
  68      * Generic handling of the <CODE>get</CODE> operation.
  69      * <p> The default implementation of this method is to loop over the
  70      * varbind list associated with the sub-request and to call
  71      * <CODE>get(var.oid.getOidArc(depth), data);</CODE>
  72      * <pre>
  73      * public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
  74      *                 int depth)
  75      *    throws SnmpStatusException {
  76      *
  77      *    final Object data = req.getUserData();
  78      *
  79      *    for (Enumeration e= req.getElements(); e.hasMoreElements();) {
  80      *
  81      *        final SnmpVarBind var= (SnmpVarBind) e.nextElement();
  82      *
  83      *        try {
  84      *            // This method will generate a SnmpStatusException
  85      *            // if `depth' is out of bounds.
  86      *            //
  87      *            final long id = var.oid.getOidArc(depth);
  88      *            var.value = meta.get(id, data);
  89      *        } catch(SnmpStatusException x) {
  90      *            req.registerGetException(var,x);
  91      *        }
  92      *    }
  93      * }
  94      * </pre>
  95      * <p> You can override this method if you need to implement some
  96      * specific policies for minimizing the accesses made to some remote
  97      * underlying resources.
  98      * <p>
  99      *
 100      * @param meta  A pointer to the generated meta-data object which
 101      *              implements the <code>SnmpStandardMetaServer</code>
 102      *              interface.
 103      *
 104      * @param req   The sub-request that must be handled by this node.
 105      *
 106      * @param depth The depth reached in the OID tree.
 107      *
 108      * @exception SnmpStatusException An error occurred while accessing
 109      *  the MIB node.
 110      */
 111     public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
 112                     int depth)
 113         throws SnmpStatusException {
 114 
 115         final Object data = req.getUserData();
 116 
 117         for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
 118             final SnmpVarBind var= e.nextElement();
 119             try {
 120                 final long id = var.oid.getOidArc(depth);
 121                 var.value = meta.get(id, data);
 122             } catch(SnmpStatusException x) {
 123                 req.registerGetException(var,x);
 124             }
 125         }
 126     }
 127 
 128     /**
 129      * Generic handling of the <CODE>set</CODE> operation.
 130      * <p> The default implementation of this method is to loop over the
 131      * varbind list associated with the sub-request and to call
 132      * <CODE>set(var.value, var.oid.getOidArc(depth), data);</CODE>
 133      * <pre>
 134      * public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
 135      *                 int depth)
 136      *    throws SnmpStatusException {
 137      *
 138      *    final Object data = req.getUserData();
 139      *
 140      *    for (Enumeration e= req.getElements(); e.hasMoreElements();) {
 141      *
 142      *        final SnmpVarBind var= (SnmpVarBind) e.nextElement();
 143      *
 144      *        try {
 145      *            // This method will generate a SnmpStatusException
 146      *            // if `depth' is out of bounds.
 147      *            //
 148      *            final long id = var.oid.getOidArc(depth);
 149      *            var.value = meta.set(var.value, id, data);
 150      *        } catch(SnmpStatusException x) {
 151      *            req.registerSetException(var,x);
 152      *        }
 153      *    }
 154      * }
 155      * </pre>
 156      * <p> You can override this method if you need to implement some
 157      * specific policies for minimizing the accesses made to some remote
 158      * underlying resources.
 159      * <p>
 160      *
 161      * @param meta  A pointer to the generated meta-data object which
 162      *              implements the <code>SnmpStandardMetaServer</code>
 163      *              interface.
 164      *
 165      * @param req   The sub-request that must be handled by this node.
 166      *
 167      * @param depth The depth reached in the OID tree.
 168      *
 169      * @exception SnmpStatusException An error occurred while accessing
 170      *  the MIB node.
 171      */
 172     public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
 173                     int depth)
 174         throws SnmpStatusException {
 175 
 176         final Object data = req.getUserData();
 177 
 178         for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
 179             SnmpVarBind var = e.nextElement();
 180             try {
 181                 // This method will generate a SnmpStatusException
 182                 // if `depth' is out of bounds.
 183                 //
 184                 final long id = var.oid.getOidArc(depth);
 185                 var.value = meta.set(var.value, id, data);
 186             } catch(SnmpStatusException x) {
 187                 req.registerSetException(var,x);
 188             }
 189         }
 190     }
 191 
 192     /**
 193      * Generic handling of the <CODE>check</CODE> operation.
 194      * <p> The default implementation of this method is to loop over the
 195      * varbind list associated with the sub-request and to call
 196      * <CODE>check(var.value, var.oid.getOidArc(depth), data);</CODE>
 197      * <pre>
 198      * public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
 199      *                   int depth)
 200      *    throws SnmpStatusException {
 201      *
 202      *    final Object data = req.getUserData();
 203      *
 204      *    for (Enumeration e= req.getElements(); e.hasMoreElements();) {
 205      *
 206      *        final SnmpVarBind var= (SnmpVarBind) e.nextElement();
 207      *
 208      *        try {
 209      *            // This method will generate a SnmpStatusException
 210      *            // if `depth' is out of bounds.
 211      *            //
 212      *            final long id = var.oid.getOidArc(depth);
 213      *            meta.check(var.value, id, data);
 214      *        } catch(SnmpStatusException x) {
 215      *            req.registerCheckException(var,x);
 216      *        }
 217      *    }
 218      * }
 219      * </pre>
 220      * <p> You can override this method if you need to implement some
 221      * specific policies for minimizing the accesses made to some remote
 222      * underlying resources, or if you need to implement some consistency
 223      * checks between the different values provided in the varbind list.
 224      * <p>
 225      *
 226      * @param meta  A pointer to the generated meta-data object which
 227      *              implements the <code>SnmpStandardMetaServer</code>
 228      *              interface.
 229      *
 230      * @param req   The sub-request that must be handled by this node.
 231      *
 232      * @param depth The depth reached in the OID tree.
 233      *
 234      * @exception SnmpStatusException An error occurred while accessing
 235      *  the MIB node.
 236      */
 237     public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
 238                       int depth)
 239         throws SnmpStatusException {
 240 
 241         final Object data = req.getUserData();
 242 
 243         for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
 244             final SnmpVarBind var = e.nextElement();
 245             try {
 246                 // This method will generate a SnmpStatusException
 247                 // if `depth' is out of bounds.
 248                 //
 249                 final long id = var.oid.getOidArc(depth);
 250                 meta.check(var.value,id,data);
 251             } catch(SnmpStatusException x) {
 252                 req.registerCheckException(var,x);
 253             }
 254         }
 255     }
 256 }