KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > RequestHandlerSupport


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.jmx.adaptor.snmp.agent;
23
24 import java.net.InetAddress JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.opennms.protocols.snmp.SnmpAgentSession;
30 import org.opennms.protocols.snmp.SnmpObjectId;
31 import org.opennms.protocols.snmp.SnmpOctetString;
32 import org.opennms.protocols.snmp.SnmpPduPacket;
33 import org.opennms.protocols.snmp.SnmpPduRequest;
34 import org.opennms.protocols.snmp.SnmpSyntax;
35 import org.opennms.protocols.snmp.SnmpVarBind;
36
37 /**
38  * Implement RequestHandler with simple tracing of incoming requests.
39  *
40  * Derived classes are expected to implement actual behaviour.
41  *
42  * @author <a HREF="mailto:krishnaraj@ieee.org">Krishnaraj S</a>
43  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
44  * @version $Revision: 44604 $
45  */

46 public class RequestHandlerSupport implements RequestHandler
47 {
48    // Protected Data ------------------------------------------------
49

50    /** Logger object */
51    protected Logger log;
52    
53    /** the MBeanServer */
54    protected MBeanServer JavaDoc server;
55    
56    /** the file name to get mapping info from */
57    protected String JavaDoc resourceName;
58    
59    /** the agent clock */
60    protected Clock clock;
61    
62    // Constructors --------------------------------------------------
63

64    /**
65     * Default CTOR
66     */

67    public RequestHandlerSupport()
68    {
69       // empty
70
}
71    
72    // RequestHandler Implementation ---------------------------------
73

74    /**
75     * Initialize
76     */

77    public void initialize(String JavaDoc resourceName, MBeanServer JavaDoc server, Logger log, Clock uptime)
78       throws Exception JavaDoc
79    {
80       this.resourceName = resourceName;
81       this.server = server;
82       this.log = log;
83       this.clock = uptime;
84    }
85    
86    // SnmpAgentHandler Implementation -------------------------------
87

88    /**
89     * <P>This method is defined to handle SNMP Get requests
90     * that are received by the session. The request has already
91     * been validated by the system. This routine will build a
92     * response and pass it back to the caller.</P>
93     *
94     * @param pdu The SNMP pdu
95     * @param getNext The agent is requesting the lexically NEXT item after each
96     * item in the pdu.
97     *
98     * @return SnmpPduRequest filled in with the proper response, or null if cannot process
99     * NOTE: this might be changed to throw an exception.
100     */

101    public SnmpPduRequest snmpReceivedGet(SnmpPduPacket pdu, boolean getNext)
102    {
103       SnmpPduRequest response = null;
104       int pduLength = pdu.getLength();
105       
106       log.debug("requestId=" + pdu.getRequestId() + ", pduLength=" + pduLength);
107    
108       SnmpVarBind[] vblist = new SnmpVarBind[pduLength];
109       int errorStatus = SnmpPduPacket.ErrNoError;
110       int errorIndex = 0;
111
112       //Process for each varibind in the request
113
for (int i = 0; i < pduLength ; i++ )
114       {
115          SnmpVarBind vb = pdu.getVarBindAt(i);
116          SnmpObjectId oid = vb.getName();
117          if (getNext)
118          {
119             log.debug(
120                "Should call getNextOid() to find out what is the next valid OID " +
121                "instance in the supported MIB tree. Assign that OID to the VB List " +
122                "and then proceed same as that of get request" );
123          }
124          vblist[i] = new SnmpVarBind(oid);
125          
126          log.debug("oid=" + oid.toString());
127     
128          log.debug("Should call the respective interface to retrieve current value for this OID" );
129
130          SnmpSyntax result = null;
131          
132          if (result == null)
133          {
134             errorStatus = SnmpPduPacket.ErrNoSuchName;
135             errorIndex = i + 1;
136             //log.debug("Error Occured " + vb.getName().toString());
137
}
138          else
139          {
140             vblist[i].setValue(result);
141             log.debug("Varbind[" + i + "] := " + vblist[i].getName().toString());
142             log.debug(" --> " + vblist[i].getValue().toString());
143          }
144       }
145       response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
146       response.setErrorStatus(errorStatus);
147       response.setErrorIndex(errorIndex);
148       return response;
149    }
150
151    /**
152     * <P>This method is defined to handle SNMP Set requests
153     * that are received by the session. The request has already
154     * been validated by the system. This routine will build a
155     * response and pass it back to the caller.</P>
156     *
157     * @param pdu The SNMP pdu
158     *
159     * @return SnmpPduRequest filled in with the proper response, or null if cannot process
160     * NOTE: this might be changed to throw an exception.
161     */

162    public SnmpPduRequest snmpReceivedSet(SnmpPduPacket pdu)
163    {
164       SnmpPduRequest response = null;
165       int errorStatus = SnmpPduPacket.ErrNoError;
166       int errorIndex = 0;
167       int k = pdu.getLength();
168       SnmpVarBind[] vblist = new SnmpVarBind[k];
169      
170       for (int i = 0; i < k ; i++ )
171       {
172          SnmpVarBind vb = pdu.getVarBindAt(i);
173          vblist[i] = new SnmpVarBind(vb);
174          SnmpObjectId oid = vb.getName();
175          
176          SnmpSyntax result = null;
177          log.debug("Should call the respective interface to assign a value for this OID" );
178           
179          if (result != null)
180          {
181             errorStatus = SnmpPduPacket.ErrReadOnly;
182             errorIndex = i + 1;
183             log.debug("Error occured " + vb.getName().toString());
184          }
185          
186          log.debug("Varbind[" + i + "] := " + vb.getName().toString());
187          log.debug(" --> " + vb.getValue().toString());
188       }
189      
190       response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
191       response.setErrorStatus(errorStatus);
192       response.setErrorIndex(errorIndex);
193
194       return response;
195    }
196  
197    /**
198     * <P>This method is defined to handle SNMP requests
199     * that are received by the session. The parameters
200     * allow the handler to determine the host, port, and
201     * community string of the received PDU</P>
202     *
203     * @param session The SNMP session
204     * @param manager The remote sender
205     * @param port The remote senders port
206     * @param community The community string
207     * @param pdu The SNMP pdu
208     *
209     */

210    public void snmpReceivedPdu(SnmpAgentSession session, InetAddress JavaDoc manager, int port,
211                                SnmpOctetString community, SnmpPduPacket pdu)
212    {
213       log.error("Message from manager " + manager.toString() + " on port " + port);
214       int cmd = pdu.getCommand();
215       log.error("Unsupported PDU command......... " + cmd);
216    }
217  
218    /**
219     * <P>This method is invoked if an error occurs in
220     * the session. The error code that represents
221     * the failure will be passed in the second parameter,
222     * 'error'. The error codes can be found in the class
223     * SnmpAgentSession class.</P>
224     *
225     * <P>If a particular PDU is part of the error condition
226     * it will be passed in the third parameter, 'pdu'. The
227     * pdu will be of the type SnmpPduRequest or SnmpPduTrap
228     * object. The handler should use the "instanceof" operator
229     * to determine which type the object is. Also, the object
230     * may be null if the error condition is not associated
231     * with a particular PDU.</P>
232     *
233     * @param session The SNMP Session
234     * @param error The error condition value.
235     * @param ref The PDU reference, or potentially null.
236     * It may also be an exception.
237     */

238    public void SnmpAgentSessionError(SnmpAgentSession session, int error, Object JavaDoc ref)
239    {
240       log.error("An error occured in the trap session");
241       log.error("Session error code = " + error);
242       if(ref != null)
243       {
244          log.error("Session error reference: " + ref.toString());
245       }
246      
247       if(error == SnmpAgentSession.ERROR_EXCEPTION)
248       {
249          synchronized(session)
250          {
251             session.notify(); // close the session
252
}
253       }
254    }
255 }
256
Popular Tags