KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > agent > SnmpGenericObjectServer


1 /*
2  * @(#)file SnmpGenericObjectServer.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.12
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  */

10
11 package com.sun.jmx.snmp.agent;
12
13
14 // java imports
15
//
16
import java.util.Vector JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 // jmx imports
21
//
22
import javax.management.AttributeList JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.ReflectionException JavaDoc;
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.InvalidAttributeValueException JavaDoc;
30 import javax.management.InstanceAlreadyExistsException JavaDoc;
31 import javax.management.MBeanRegistrationException JavaDoc;
32 import javax.management.NotCompliantMBeanException JavaDoc;
33 import javax.management.RuntimeOperationsException JavaDoc;
34 import com.sun.jmx.snmp.SnmpOid;
35 import com.sun.jmx.snmp.SnmpValue;
36 import com.sun.jmx.snmp.SnmpVarBind;
37 import com.sun.jmx.snmp.SnmpStatusException;
38
39
40 /**
41  * <p>
42  * This class is a utility class that transforms SNMP GET / SET requests
43  * into standard JMX getAttributes() setAttributes() requests.
44  * </p>
45  *
46  * <p>
47  * The transformation relies on the metadata information provided by the
48  * {@link com.sun.jmx.snmp.agent.SnmpGenericMetaServer} object which is
49  * passed as the first parameter to every method. This SnmpGenericMetaServer
50  * object is usually a Metadata object generated by <code>mibgen</code>.
51  * </p>
52  *
53  * <p><b><i>
54  * This class is used internally by mibgen generated metadata objects and
55  * you should never need to use it directly.
56  * </b></i></p>
57  * <p><b>This API is a Sun Microsystems internal API and is subject
58  * to change without notice.</b></p>
59  **/

60
61 public class SnmpGenericObjectServer {
62
63     // ----------------------------------------------------------------------
64
//
65
// Protected variables
66
//
67
// ----------------------------------------------------------------------
68

69     /**
70      * The MBean server through which the MBeans will be accessed.
71      **/

72     protected final MBeanServer JavaDoc server;
73
74     // ----------------------------------------------------------------------
75
//
76
// Constructors
77
//
78
// ----------------------------------------------------------------------
79

80     /**
81      * Builds a new SnmpGenericObjectServer. Usually there will be a single
82      * object of this type per MIB.
83      *
84      * @param server The MBeanServer in which the MBean accessed by this
85      * MIB are registered.
86      **/

87     public SnmpGenericObjectServer(MBeanServer JavaDoc server) {
88     this.server = server;
89     }
90
91     /**
92      * Execute an SNMP GET request.
93      *
94      * <p>
95      * This method first builds the list of attributes that need to be
96      * retrieved from the MBean and then calls getAttributes() on the
97      * MBean server. Then it updates the SnmpMibSubRequest with the values
98      * retrieved from the MBean.
99      * </p>
100      *
101      * <p>
102      * The SNMP metadata information is obtained through the given
103      * <code>meta</code> object, which usually is an instance of a
104      * <code>mibgen</code> generated class.
105      * </p>
106      *
107      * <p><b><i>
108      * This method is called internally by <code>mibgen</code> generated
109      * objects and you should never need to call it directly.
110      * </i></b></p>
111      *
112      * @param meta The metadata object impacted by the subrequest
113      * @param name The ObjectName of the MBean impacted by this subrequest
114      * @param req The SNMP subrequest to execute on the MBean
115      * @param depth The depth of the SNMP object in the OID tree.
116      *
117      * @exception SnmpStatusException whenever an SNMP exception must be
118      * raised. Raising an exception will abort the request.<br>
119      * Exceptions should never be raised directly, but only by means of
120      * <code>
121      * req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
122      * </code>
123      **/

124     public void get(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
125             SnmpMibSubRequest req, int depth)
126     throws SnmpStatusException {
127
128     // java.lang.System.out.println(">>>>>>>>> GET " + name);
129

130     final int size = req.getSize();
131     final Object JavaDoc data = req.getUserData();
132     final String JavaDoc[] nameList = new String JavaDoc[size];
133     final SnmpVarBind[] varList = new SnmpVarBind[size];
134     final long[] idList = new long[size];
135     int i = 0;
136
137     for (Enumeration JavaDoc e=req.getElements(); e.hasMoreElements();) {
138             final SnmpVarBind var= (SnmpVarBind) e.nextElement();
139         try {
140         final long id = var.oid.getOidArc(depth);
141         nameList[i] = meta.getAttributeName(id);
142         varList[i] = var;
143         idList[i] = id;
144
145         // Check the access rights according to the MIB.
146
// The MBean might be less restrictive (have a getter
147
// while the MIB defines the variable as AFN)
148
//
149
meta.checkGetAccess(id,data);
150
151         //java.lang.System.out.println(nameList[i] + " added.");
152
i++;
153             } catch(SnmpStatusException x) {
154         //java.lang.System.out.println("exception for " + nameList[i]);
155
//x.printStackTrace();
156
req.registerGetException(var,x);
157         }
158     }
159
160     AttributeList JavaDoc result = null;
161     int errorCode = SnmpStatusException.noSuchInstance;
162
163     try {
164         result = server.getAttributes(name,nameList);
165     } catch (InstanceNotFoundException JavaDoc f) {
166         //java.lang.System.out.println(name + ": instance not found.");
167
//f.printStackTrace();
168
result = new AttributeList JavaDoc();
169     } catch (ReflectionException JavaDoc r) {
170         //java.lang.System.out.println(name + ": reflexion error.");
171
//r.printStackTrace();
172
result = new AttributeList JavaDoc();
173     } catch (Exception JavaDoc x) {
174         result = new AttributeList JavaDoc();
175     }
176
177
178     final Iterator JavaDoc it = result.iterator();
179     
180     for (int j=0; j < i; j++) {
181         if (!it.hasNext()) {
182         //java.lang.System.out.println(name + "variable[" + j +
183
// "] absent");
184
final SnmpStatusException x =
185             new SnmpStatusException(errorCode);
186         req.registerGetException(varList[j],x);
187         continue;
188         }
189
190         final Attribute JavaDoc att = (Attribute JavaDoc) it.next();
191
192         while ((j < i) && (! nameList[j].equals(att.getName()))) {
193         //java.lang.System.out.println(name + "variable[" +j +
194
// "] not found");
195
final SnmpStatusException x =
196             new SnmpStatusException(errorCode);
197         req.registerGetException(varList[j],x);
198         j++;
199         }
200
201         if ( j == i) break;
202         
203         try {
204         varList[j].value =
205             meta.buildSnmpValue(idList[j],att.getValue());
206         } catch (SnmpStatusException x) {
207         req.registerGetException(varList[j],x);
208         }
209         //java.lang.System.out.println(att.getName() + " retrieved.");
210
}
211     //java.lang.System.out.println(">>>>>>>>> END GET");
212
}
213
214     /**
215      * Get the value of an SNMP variable.
216      *
217      * <p><b><i>
218      * You should never need to use this method directly.
219      * </i></b></p>
220      *
221      * @param meta The impacted metadata object
222      * @param name The ObjectName of the impacted MBean
223      * @param id The OID arc identifying the variable we're trying to set.
224      * @param data User contextual data allocated through the
225      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
226      *
227      * @return The value of the variable.
228      *
229      * @exception SnmpStatusException whenever an SNMP exception must be
230      * raised. Raising an exception will abort the request. <br>
231      * Exceptions should never be raised directly, but only by means of
232      * <code>
233      * req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
234      * </code>
235      **/

236     public SnmpValue get(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
237              long id, Object JavaDoc data)
238     throws SnmpStatusException {
239     final String JavaDoc attname = meta.getAttributeName(id);
240     Object JavaDoc result = null;
241
242     try {
243         result = server.getAttribute(name,attname);
244     } catch (MBeanException JavaDoc m) {
245         Exception JavaDoc t = m.getTargetException();
246         if (t instanceof SnmpStatusException)
247         throw (SnmpStatusException) t;
248         throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
249     } catch (Exception JavaDoc e) {
250         throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
251     }
252
253     return meta.buildSnmpValue(id,result);
254     }
255
256     /**
257      * Execute an SNMP SET request.
258      *
259      * <p>
260      * This method first builds the list of attributes that need to be
261      * set on the MBean and then calls setAttributes() on the
262      * MBean server. Then it updates the SnmpMibSubRequest with the new
263      * values retrieved from the MBean.
264      * </p>
265      *
266      * <p>
267      * The SNMP metadata information is obtained through the given
268      * <code>meta</code> object, which usually is an instance of a
269      * <code>mibgen</code> generated class.
270      * </p>
271      *
272      * <p><b><i>
273      * This method is called internally by <code>mibgen</code> generated
274      * objects and you should never need to call it directly.
275      * </i></b></p>
276      *
277      * @param meta The metadata object impacted by the subrequest
278      * @param name The ObjectName of the MBean impacted by this subrequest
279      * @param req The SNMP subrequest to execute on the MBean
280      * @param depth The depth of the SNMP object in the OID tree.
281      *
282      * @exception SnmpStatusException whenever an SNMP exception must be
283      * raised. Raising an exception will abort the request. <br>
284      * Exceptions should never be raised directly, but only by means of
285      * <code>
286      * req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
287      * </code>
288      **/

289     public void set(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
290             SnmpMibSubRequest req, int depth)
291     throws SnmpStatusException {
292
293     final int size = req.getSize();
294     final AttributeList JavaDoc attList = new AttributeList JavaDoc(size);
295     final String JavaDoc[] nameList = new String JavaDoc[size];
296     final SnmpVarBind[] varList = new SnmpVarBind[size];
297     final long[] idList = new long[size];
298     int i = 0;
299
300     for (Enumeration JavaDoc e=req.getElements(); e.hasMoreElements();) {
301             final SnmpVarBind var= (SnmpVarBind) e.nextElement();
302         try {
303         final long id = var.oid.getOidArc(depth);
304         final String JavaDoc attname = meta.getAttributeName(id);
305         final Object JavaDoc attvalue=
306             meta.buildAttributeValue(id,var.value);
307         final Attribute JavaDoc att = new Attribute JavaDoc(attname,attvalue);
308         attList.add(att);
309         nameList[i] = attname;
310         varList[i] = var;
311         idList[i] = id;
312         i++;
313             } catch(SnmpStatusException x) {
314         req.registerSetException(var,x);
315         }
316     }
317
318     AttributeList JavaDoc result = null;
319     int errorCode = SnmpStatusException.noAccess;
320
321     try {
322         result = server.setAttributes(name,attList);
323     } catch (InstanceNotFoundException JavaDoc f) {
324         result = new AttributeList JavaDoc();
325         errorCode = SnmpStatusException.snmpRspInconsistentName;
326     } catch (ReflectionException JavaDoc r) {
327         errorCode = SnmpStatusException.snmpRspInconsistentName;
328         result = new AttributeList JavaDoc();
329     } catch (Exception JavaDoc x) {
330         result = new AttributeList JavaDoc();
331     }
332
333     final Iterator JavaDoc it = result.iterator();
334     
335     for (int j=0; j < i; j++) {
336         if (!it.hasNext()) {
337         final SnmpStatusException x =
338             new SnmpStatusException(errorCode);
339         req.registerSetException(varList[j],x);
340         continue;
341         }
342
343         final Attribute JavaDoc att = (Attribute JavaDoc) it.next();
344
345         while ((j < i) && (! nameList[j].equals(att.getName()))) {
346         final SnmpStatusException x =
347             new SnmpStatusException(SnmpStatusException.noAccess);
348         req.registerSetException(varList[j],x);
349         j++;
350         }
351
352         if ( j == i) break;
353         
354         try {
355         varList[j].value =
356             meta.buildSnmpValue(idList[j],att.getValue());
357         } catch (SnmpStatusException x) {
358         req.registerSetException(varList[j],x);
359         }
360         
361     }
362     }
363
364     /**
365      * Set the value of an SNMP variable.
366      *
367      * <p><b><i>
368      * You should never need to use this method directly.
369      * </i></b></p>
370      *
371      * @param meta The impacted metadata object
372      * @param name The ObjectName of the impacted MBean
373      * @param x The new requested SnmpValue
374      * @param id The OID arc identifying the variable we're trying to set.
375      * @param data User contextual data allocated through the
376      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
377      *
378      * @return The new value of the variable after the operation.
379      *
380      * @exception SnmpStatusException whenever an SNMP exception must be
381      * raised. Raising an exception will abort the request. <br>
382      * Exceptions should never be raised directly, but only by means of
383      * <code>
384      * req.registerSetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
385      * </code>
386      **/

387     public SnmpValue set(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
388              SnmpValue x, long id, Object JavaDoc data)
389     throws SnmpStatusException {
390     final String JavaDoc attname = meta.getAttributeName(id);
391     final Object JavaDoc attvalue=
392         meta.buildAttributeValue(id,x);
393     final Attribute JavaDoc att = new Attribute JavaDoc(attname,attvalue);
394
395     Object JavaDoc result = null;
396
397     try {
398         server.setAttribute(name,att);
399         result = server.getAttribute(name,attname);
400     } catch(InvalidAttributeValueException JavaDoc iv) {
401         throw new
402         SnmpStatusException(SnmpStatusException.snmpRspWrongValue);
403     } catch (InstanceNotFoundException JavaDoc f) {
404         throw new
405         SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
406     } catch (ReflectionException JavaDoc r) {
407         throw new
408         SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
409     } catch (MBeanException JavaDoc m) {
410         Exception JavaDoc t = m.getTargetException();
411         if (t instanceof SnmpStatusException)
412         throw (SnmpStatusException) t;
413         throw new
414         SnmpStatusException(SnmpStatusException.noAccess);
415     } catch (Exception JavaDoc e) {
416         throw new
417         SnmpStatusException(SnmpStatusException.noAccess);
418     }
419
420     return meta.buildSnmpValue(id,result);
421     }
422
423     /**
424      * Checks whether an SNMP SET request can be successfully performed.
425      *
426      * <p>
427      * For each variable in the subrequest, this method calls
428      * checkSetAccess() on the meta object, and then tries to invoke the
429      * check<i>AttributeName</i>() method on the MBean. If this method
430      * is not defined then it is assumed that the SET won't fail.
431      * </p>
432      *
433      * <p><b><i>
434      * This method is called internally by <code>mibgen</code> generated
435      * objects and you should never need to call it directly.
436      * </i></b></p>
437      *
438      * @param meta The metadata object impacted by the subrequest
439      * @param name The ObjectName of the MBean impacted by this subrequest
440      * @param req The SNMP subrequest to execute on the MBean
441      * @param depth The depth of the SNMP object in the OID tree.
442      *
443      * @exception SnmpStatusException if the requested SET operation must
444      * be rejected. Raising an exception will abort the request. <br>
445      * Exceptions should never be raised directly, but only by means of
446      * <code>
447      * req.registerCheckException(<i>VariableId</i>,<i>SnmpStatusException</i>)
448      * </code>
449      *
450      **/

451     public void check(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
452               SnmpMibSubRequest req, int depth)
453     throws SnmpStatusException {
454
455     final Object JavaDoc data = req.getUserData();
456
457     for (Enumeration JavaDoc e=req.getElements(); e.hasMoreElements();) {
458             final SnmpVarBind var= (SnmpVarBind) e.nextElement();
459         try {
460         final long id = var.oid.getOidArc(depth);
461         // call meta.check() here, and meta.check will call check()
462
check(meta,name,var.value,id,data);
463             } catch(SnmpStatusException x) {
464         req.registerCheckException(var,x);
465         }
466     }
467     }
468
469     /**
470      * Checks whether a SET operation can be performed on a given SNMP
471      * variable.
472      *
473      * @param meta The impacted metadata object
474      * @param name The ObjectName of the impacted MBean
475      * @param x The new requested SnmpValue
476      * @param id The OID arc identifying the variable we're trying to set.
477      * @param data User contextual data allocated through the
478      * {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
479      *
480      * <p>
481      * This method calls checkSetAccess() on the meta object, and then
482      * tries to invoke the check<i>AttributeName</i>() method on the MBean.
483      * If this method is not defined then it is assumed that the SET
484      * won't fail.
485      * </p>
486      *
487      * <p><b><i>
488      * This method is called internally by <code>mibgen</code> generated
489      * objects and you should never need to call it directly.
490      * </i></b></p>
491      *
492      * @exception SnmpStatusException if the requested SET operation must
493      * be rejected. Raising an exception will abort the request. <br>
494      * Exceptions should never be raised directly, but only by means of
495      * <code>
496      * req.registerCheckException(<i>VariableId</i>,<i>SnmpStatusException</i>)
497      * </code>
498      *
499      **/

500     // XXX xxx ZZZ zzz Maybe we should go through the MBeanInfo here?
501
public void check(SnmpGenericMetaServer meta, ObjectName JavaDoc name,
502               SnmpValue x, long id, Object JavaDoc data)
503     throws SnmpStatusException {
504     
505     meta.checkSetAccess(x,id,data);
506     try {
507         final String JavaDoc attname = meta.getAttributeName(id);
508         final Object JavaDoc attvalue= meta.buildAttributeValue(id,x);
509         final Object JavaDoc[] params = new Object JavaDoc[1];
510         final String JavaDoc[] signature = new String JavaDoc[1];
511
512         params[0] = attvalue;
513         signature[0] = attvalue.getClass().getName();
514         server.invoke(name,"check"+attname,params,signature);
515
516     } catch( SnmpStatusException e) {
517         throw e;
518     }
519     catch (InstanceNotFoundException JavaDoc i) {
520         throw new
521         SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
522     } catch (ReflectionException JavaDoc r) {
523         // checkXXXX() not defined => do nothing
524
} catch (MBeanException JavaDoc m) {
525         Exception JavaDoc t = m.getTargetException();
526         if (t instanceof SnmpStatusException)
527         throw (SnmpStatusException) t;
528         throw new SnmpStatusException(SnmpStatusException.noAccess);
529     } catch (Exception JavaDoc e) {
530         throw new
531         SnmpStatusException(SnmpStatusException.noAccess);
532     }
533     }
534
535     public void registerTableEntry(SnmpMibTable meta, SnmpOid rowOid,
536                    ObjectName JavaDoc objname, Object JavaDoc entry)
537     throws SnmpStatusException {
538         if (objname == null)
539            throw new
540          SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
541         try {
542             if (entry != null && !server.isRegistered(objname))
543                 server.registerMBean(entry, objname);
544     } catch (InstanceAlreadyExistsException JavaDoc e) {
545             throw new
546           SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
547     } catch (MBeanRegistrationException JavaDoc e) {
548             throw new SnmpStatusException(SnmpStatusException.snmpRspNoAccess);
549     } catch (NotCompliantMBeanException JavaDoc e) {
550             throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
551     } catch (RuntimeOperationsException JavaDoc e) {
552             throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
553         } catch(Exception JavaDoc e) {
554             throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
555         }
556     }
557
558 }
559
Popular Tags