KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > daemon > SnmpSubNextRequestHandler


1 /*
2  * @(#)file SnmpSubNextRequestHandler.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.25
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
12 package com.sun.jmx.snmp.daemon;
13
14 // java imports
15
//
16
import java.util.Vector JavaDoc;
17
18 // jmx imports
19
//
20
import com.sun.jmx.snmp.SnmpEngine;
21 import com.sun.jmx.snmp.SnmpPdu;
22 import com.sun.jmx.snmp.SnmpValue;
23 import com.sun.jmx.snmp.SnmpVarBind;
24 import com.sun.jmx.snmp.SnmpVarBindList;
25 import com.sun.jmx.snmp.SnmpOid;
26 import com.sun.jmx.snmp.SnmpDefinitions;
27 import com.sun.jmx.snmp.SnmpStatusException;
28 // SNMP Runtime import
29
//
30
import com.sun.jmx.snmp.agent.SnmpMibAgent;
31 import com.sun.jmx.snmp.agent.SnmpMibRequest;
32 import com.sun.jmx.snmp.daemon.SnmpAdaptorServer;
33 import com.sun.jmx.snmp.internal.SnmpIncomingRequest;
34
35 /* NPCTE fix for bugId 4492741, esc 0 */
36 import com.sun.jmx.snmp.ThreadContext;
37 /* end of NPCTE fix for bugId 4492741 */
38
39 class SnmpSubNextRequestHandler extends SnmpSubRequestHandler {
40     private SnmpAdaptorServer server = null;
41     /**
42      * The constuctor initialize the subrequest with the whole varbind
43      * list contained in the original request.
44      */

45     protected SnmpSubNextRequestHandler(SnmpAdaptorServer server,
46                     SnmpMibAgent agent,
47                     SnmpPdu req) {
48         super(agent,req);
49     init(req, server);
50     }
51     
52     protected SnmpSubNextRequestHandler(SnmpEngine engine,
53                     SnmpAdaptorServer server,
54                     SnmpIncomingRequest incRequest,
55                     SnmpMibAgent agent,
56                     SnmpPdu req) {
57     super(engine, incRequest, agent, req);
58     init(req, server);
59     if(isDebugOn())
60         debug("SnmpSubNextRequestHandler", "Constructor :" + this);
61     }
62     
63     private void init(SnmpPdu req, SnmpAdaptorServer server) {
64     this.server = server;
65     
66         // The translation table is easy in this case ...
67
//
68
final int max= translation.length;
69         final SnmpVarBind[] list= req.varBindList;
70         final NonSyncVector nonSyncVarBind = ((NonSyncVector)varBind);
71         for(int i=0; i < max; i++) {
72             translation[i]= i;
73             // we need to allocate a new SnmpVarBind. Otherwise the first
74
// sub request will modify the list...
75
//
76
final SnmpVarBind newVarBind =
77         new SnmpVarBind(list[i].oid, list[i].value);
78             nonSyncVarBind.addNonSyncElement(newVarBind);
79         }
80     }
81
82     public void run() {
83     
84         try {
85         /* NPCTE fix for bugId 4492741, esc 0, 16-August-2001 */
86             final ThreadContext oldContext =
87                 ThreadContext.push("SnmpUserData",data);
88         try {
89         if (isTraceOn()) {
90             trace("run", "[" + Thread.currentThread() +
91               "]:getNext operation on " + agent.getMibName());
92         }
93       
94         // Always call with V2. So the merge of the responses will
95
// be easier.
96
//
97
agent.getNext(createMibRequest(varBind, snmpVersionTwo, data));
98         } finally {
99                 ThreadContext.restore(oldContext);
100             }
101         /* end of NPCTE fix for bugId 4492741 */
102       
103       
104         } catch(SnmpStatusException x) {
105             errorStatus = x.getStatus() ;
106             errorIndex= x.getErrorIndex();
107             if (isDebugOn()) {
108                 debug("run", "[" + Thread.currentThread() +
109               "]:an Snmp error occured during the operation");
110         debug("run",x);
111             }
112         }
113         catch(Exception JavaDoc x) {
114             errorStatus = SnmpDefinitions.snmpRspGenErr ;
115         if (isTraceOn()) {
116                 trace("run", "[" + Thread.currentThread() +
117               "]:a generic error occured during the operation");
118         }
119             if (isDebugOn()) {
120         debug("run","Error is: " + x);
121         debug("run",x);
122             }
123         }
124         if (isTraceOn()) {
125             trace("run", "[" + Thread.currentThread() +
126           "]:operation completed");
127         }
128     }
129
130     /**
131      * The method updates the varbind list of the subrequest.
132      */

133     protected void updateRequest(SnmpVarBind var, int pos) {
134     if(isDebugOn())
135         debug("updateRequest", "Copy :" + var);
136         int size= varBind.size();
137         translation[size]= pos;
138     final SnmpVarBind newVarBind =
139         new SnmpVarBind(var.oid, var.value);
140     if(isDebugOn())
141         debug("updateRequest", "Copied :" + newVarBind);
142     
143     varBind.addElement(newVarBind);
144     }
145     /**
146      * The method updates a given var bind list with the result of a
147      * previsouly invoked operation.
148      * Prior to calling the method, one must make sure that the operation was
149      * successful. As such the method getErrorIndex or getErrorStatus should be
150      * called.
151      */

152     protected void updateResult(SnmpVarBind[] result) {
153   
154         final int max=varBind.size();
155         for(int i= 0; i< max ; i++) {
156             // May be we should control the position ...
157
//
158
final int index= translation[i];
159             final SnmpVarBind elmt=
160         (SnmpVarBind)((NonSyncVector)varBind).elementAtNonSync(i);
161
162             final SnmpVarBind vb= result[index];
163             if (vb == null) {
164                 result[index]= elmt;
165         /* NPCTE fix for bugid 4381195 esc 0. <J.C.> < 17-Oct-2000> */
166             // if ((elmt != null) && (elmt.value == null) &&
167
// (version == snmpVersionTwo))
168
// elmt.value = SnmpVarBind.endOfMibView;
169
/* end of NPCTE fix for bugid 4381195 */
170                 continue;
171             }
172
173             final SnmpValue val= vb.value;
174             if ((val == null)|| (val == SnmpVarBind.endOfMibView)){
175         /* NPCTE fix for bugid 4381195 esc 0. <J.C.> < 17-Oct-2000> */
176         if ((elmt != null) &&
177             (elmt.value != SnmpVarBind.endOfMibView))
178             result[index]= elmt;
179         // else if ((val == null) && (version == snmpVersionTwo))
180
// vb.value = SnmpVarBind.endOfMibView;
181
continue;
182         /* end of NPCTE fix for bugid 4381195 */
183             }
184      
185         /* NPCTE fix for bugid 4381195 esc 0. <J.C.> < 17-Oct-2000> */
186         if (elmt == null) continue;
187         /* end of NPCTE fix for bugid 4381195 */
188
189         if (elmt.value == SnmpVarBind.endOfMibView) continue;
190
191
192             // Now we need to take the smallest oid ...
193
//
194
int comp = elmt.oid.compareTo(vb.oid);
195             if (comp < 0) {
196           // Take the smallest (lexicographically)
197
//
198
result[index]= elmt;
199             }
200         else {
201         if(comp == 0) {
202             // Must compare agent used for reply
203
// Take the deeper within the reply
204
if(isDebugOn()) {
205             trace("updateResult"," oid overlapping. Oid : " +
206                   elmt.oid + "value :" + elmt.value);
207             trace("updateResult","Already present varBind : " +
208                   vb);
209             }
210             
211             SnmpOid oid = vb.oid;
212             SnmpMibAgent deeperAgent = server.getAgentMib(oid);
213
214             if(isDebugOn())
215             trace("updateResult","Deeper agent : " + deeperAgent);
216             if(deeperAgent == agent) {
217             if(isDebugOn())
218             trace("updateResult","The current agent is the deeper one. Update the value with the current one");
219             result[index].value = elmt.value;
220             }
221
222             /*
223               Vector v = new Vector();
224               SnmpMibRequest getReq = createMibRequest(v,
225               version,
226               null);
227               SnmpVarBind realValue = new SnmpVarBind(oid);
228               getReq.addVarBind(realValue);
229               try {
230               deeperAgent.get(getReq);
231               } catch(SnmpStatusException e) {
232               e.printStackTrace();
233               }
234               
235               if(isDebugOn())
236               trace("updateResult", "Biggest priority value is : " +
237               realValue.value);
238               
239               result[index].value = realValue.value;
240             */

241         }
242         }
243     }
244     }
245
246     protected String JavaDoc makeDebugTag() {
247         return "SnmpSubNextRequestHandler";
248     }
249 }
250
Popular Tags