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