KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package com.sun.jmx.snmp.agent;
13
14
15 // java imports
16
//
17
import java.io.Serializable JavaDoc;
18 import java.util.Enumeration JavaDoc;
19
20 // jmx imports
21
//
22
import com.sun.jmx.snmp.SnmpStatusException;
23 import com.sun.jmx.snmp.SnmpDefinitions;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import com.sun.jmx.snmp.SnmpVarBind;
27
28 // SNMP Runtime imports
29
//
30
import com.sun.jmx.trace.Trace;
31
32 /**
33  * A simple MIB agent that implements SNMP calls (get, set, getnext and getbulk) in a way that only errors or exceptions are returned. Every call done on this agent fails. Error handling is done according to the manager's SNMP protocol version.
34  * <P>It is used by <CODE>SnmpAdaptorServer</CODE> for its default agent behavior. When a received Oid doesn't match, this agent is called to fill the result list with errors.</P>
35  * <p><b>This API is a Sun Microsystems internal API and is subject
36  * to change without notice.</b></p>
37  * @since 1.5
38  *
39  */

40
41 public class SnmpErrorHandlerAgent extends SnmpMibAgent
42 implements Serializable JavaDoc {
43
44   public SnmpErrorHandlerAgent() {}
45
46     /**
47      * Initializes the MIB (with no registration of the MBeans into the
48      * MBean server). Does nothing.
49      *
50      * @exception IllegalAccessException The MIB cannot be initialized.
51      */

52     
53   public void init() throws IllegalAccessException JavaDoc {
54   }
55
56     /**
57      * Initializes the MIB but each single MBean representing the MIB
58      * is inserted into the MBean server.
59      *
60      * @param server The MBean server to register the service with.
61      * @param name The object name.
62      *
63      * @return The passed name paramter.
64      *
65      * @exception java.lang.Exception
66      */

67
68   public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
69     throws Exception JavaDoc {
70         return name;
71     }
72   
73     /**
74      * Gets the root object identifier of the MIB.
75      * <P>The root object identifier is the object identifier uniquely
76      * identifying the MIB.
77      *
78      * @return The returned oid is null.
79      */

80
81   public long[] getRootOid() {
82     return null;
83   }
84   
85     /**
86      * Processes a <CODE>get</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests.
87      *
88      * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
89      *
90      * @exception SnmpStatusException An error occured during the operation.
91      */

92
93   public void get(SnmpMibRequest inRequest) throws SnmpStatusException {
94     
95     if(isDebugOn()) trace("get","Get in Exception");
96     
97     if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
98       throw new SnmpStatusException(SnmpStatusException.noSuchName);
99     
100     Enumeration JavaDoc l = inRequest.getElements();
101     while(l.hasMoreElements()) {
102       SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
103       varbind.setNoSuchObject();
104     }
105   }
106
107     /**
108      * Checks if a <CODE>set</CODE> operation can be performed.
109      * If the operation can not be performed, the method should emit a
110      * <CODE>SnmpStatusException</CODE>.
111      *
112      * @param inRequest The SnmpMibRequest object holding the list of variables to
113      * be set. This list is composed of
114      * <CODE>SnmpVarBind</CODE> objects.
115      *
116      * @exception SnmpStatusException The <CODE>set</CODE> operation
117      * cannot be performed.
118      */

119
120   public void check(SnmpMibRequest inRequest) throws SnmpStatusException {
121
122     if(isDebugOn()) trace("check","Check in Exception");
123
124     throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
125   }
126
127     /**
128      * Processes a <CODE>set</CODE> operation. Should never be called (check previously called having failed).
129      *
130      * @param inRequest The SnmpMibRequest object holding the list of variable to be set.
131      *
132      * @exception SnmpStatusException An error occured during the operation.
133      */

134
135   public void set(SnmpMibRequest inRequest) throws SnmpStatusException {
136
137       if(isDebugOn()) trace("set","Set in Exception, CAN't be called");
138       
139     throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
140   }
141
142     /**
143      * Processes a <CODE>getNext</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests..
144      *
145      * @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved.
146      *
147      * @exception SnmpStatusException An error occured during the operation.
148      */

149
150   public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException {
151     
152       if(isDebugOn()) trace("getNext","GetNext in Exception");
153
154     if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
155       throw new SnmpStatusException(SnmpStatusException.noSuchName);
156     
157     Enumeration JavaDoc l = inRequest.getElements();
158     while(l.hasMoreElements()) {
159       SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
160       varbind.setEndOfMibView();
161     }
162   }
163   
164     /**
165      * Processes a <CODE>getBulk</CODE> operation. It will throw an exception if the request is a V1 one or it will set exceptions within the list for V2 ones.
166      *
167      * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
168      *
169      * @exception SnmpStatusException An error occured during the operation.
170      */

171     
172   public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat)
173     throws SnmpStatusException {
174       
175       if(isDebugOn()) trace("getBulk","GetBulk in Exception");
176  
177       if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
178     throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0);
179       
180       Enumeration JavaDoc l = inRequest.getElements();
181       while(l.hasMoreElements()) {
182     SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
183     varbind.setEndOfMibView();
184       }
185     }
186     
187     private boolean isDebugOn() {
188         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
189     }
190     
191     private void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
192         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
193     }
194     
195     private void trace(String JavaDoc func, String JavaDoc info) {
196         debug(dbgTag, func, info);
197     }
198
199     private String JavaDoc dbgTag = "SnmpErrorHandlerAgent";
200 }
201
202
203
Popular Tags