1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package uk.co.westhawk.snmp.stack;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.util.*;
53 import uk.co.westhawk.snmp.event.*;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public class SnmpContextPool implements SnmpContextFace {
92 private static final String version_id = "@(#)$Id: SnmpContextPool.java,v 3.22 2009/03/05 13:27:41 birgita Exp $ Copyright Westhawk Ltd";
93
94 protected static Hashtable contextPool;
95
96 protected SnmpContext context = null;
97 protected String hostname, socketType, bindAddr;
98 protected int hostPort;
99 protected String community = SnmpContextFace.DEFAULT_COMMUNITY;
100
101
102
103
104
105
106
107
108 public SnmpContextPool(String host, int port) throws java.io.IOException {
109 this(host, port, SnmpContextFace.DEFAULT_COMMUNITY, null, STANDARD_SOCKET);
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public SnmpContextPool(String host, int port, String typeSocket)
126 throws java.io.IOException {
127 this(host, port, SnmpContextFace.DEFAULT_COMMUNITY, null, typeSocket);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public SnmpContextPool(String host, int port, String comm, String typeSocket)
146 throws java.io.IOException {
147 this(host, port, comm, null, typeSocket);
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public SnmpContextPool(String host, int port, String comm, String bindAddress, String typeSocket)
166 throws java.io.IOException {
167 initPools();
168 hostname = host;
169 hostPort = port;
170 community = comm;
171 bindAddr = bindAddress;
172 socketType = typeSocket;
173
174 context = getMatchingContext();
175 }
176
177 private static synchronized void initPools() {
178 if (contextPool == null) {
179 contextPool = new Hashtable(5);
180 }
181 }
182
183 public int getVersion() {
184 return SnmpConstants.SNMP_VERSION_1;
185 }
186
187 public String getHost() {
188 return hostname;
189 }
190
191 public int getPort() {
192 return hostPort;
193 }
194
195 public String getBindAddress() {
196 return bindAddr;
197 }
198
199 public String getTypeSocket() {
200 return socketType;
201 }
202
203 public String getSendToHostAddress() {
204 String res = null;
205 if (context != null) {
206 res = context.getSendToHostAddress();
207 }
208 return res;
209 }
210
211 public String getReceivedFromHostAddress() {
212 String res = null;
213 if (context != null) {
214 res = context.getReceivedFromHostAddress();
215 }
216 return res;
217 }
218
219 public String getCommunity() {
220 return community;
221 }
222
223 public void setCommunity(String newCommunity) {
224 if (newCommunity != null
225 &&
226 newCommunity.equals(community) == false) {
227 community = newCommunity;
228 try {
229 context = getMatchingContext();
230 } catch (java.io.IOException exc) {
231 }
232 }
233 }
234
235 public boolean addPdu(Pdu pdu)
236 throws java.io.IOException, PduException {
237 if (context == null) {
238 context = getMatchingContext();
239 }
240 return context.addPdu(pdu);
241 }
242
243 public boolean removePdu(int requestId) {
244 boolean res = false;
245 if (context != null) {
246 res = context.removePdu(requestId);
247 }
248 return res;
249 }
250
251
252
253
254 public byte[] encodePacket(byte msg_type, int rId, int errstat,
255 int errind, Enumeration ve, Object obj)
256 throws java.io.IOException, EncodingException {
257 byte[] res = null;
258 if (context != null) {
259 res = context.encodePacket(msg_type, rId, errstat, errind, ve,
260 obj);
261 }
262 return res;
263 }
264
265 public void sendPacket(byte[] packet) {
266 if (context != null) {
267 context.sendPacket(packet);
268 }
269 }
270
271
272
273
274
275
276 public void destroy() {
277 synchronized (contextPool) {
278 if (context != null) {
279 String hashKey = context.getHashKey();
280
281 int count = 0;
282 SnmpContextPoolItem item = (SnmpContextPoolItem) contextPool.get(hashKey);
283 if (item != null) {
284 count = item.getCounter();
285 count--;
286 item.setCounter(count);
287 }
288
289 if (count <= 0) {
290 contextPool.remove(hashKey);
291 context.destroy();
292 }
293 context = null;
294 }
295 }
296 }
297
298
299
300
301
302
303
304
305
306 public void destroyPool() {
307 Hashtable copyOfPool = null;
308
309 synchronized (contextPool) {
310 synchronized (contextPool) {
311 copyOfPool = (Hashtable) contextPool.clone();
312 }
313 contextPool.clear();
314 }
315 context = null;
316
317 Enumeration keys = copyOfPool.keys();
318 while (keys.hasMoreElements()) {
319 String key = (String) keys.nextElement();
320 SnmpContextPoolItem item = (SnmpContextPoolItem) copyOfPool.get(key);
321 if (item != null) {
322 SnmpContextBasisFace cntxt = (SnmpContextBasisFace) item.getContext();
323 cntxt.destroy();
324 }
325 }
326 copyOfPool.clear();
327 }
328
329 public boolean isDestroyed() {
330 boolean isDestroyed = true;
331 if (context != null) {
332 isDestroyed = context.isDestroyed();
333 }
334 return isDestroyed;
335 }
336
337
338
339
340
341
342
343
344
345
346
347 protected SnmpContext getMatchingContext() throws java.io.IOException {
348 SnmpContextPoolItem item = null;
349 SnmpContext newContext = null;
350 String hashKey = getHashKey();
351
352 destroy();
353 synchronized (contextPool) {
354 int count = 0;
355 if (contextPool.containsKey(hashKey)) {
356 item = (SnmpContextPoolItem) contextPool.get(hashKey);
357 newContext = (SnmpContext) item.getContext();
358 count = item.getCounter();
359 } else {
360 newContext = new SnmpContext(hostname, hostPort, bindAddr, socketType);
361 newContext.setCommunity(community);
362 item = new SnmpContextPoolItem(newContext);
363 contextPool.put(hashKey, item);
364 }
365 count++;
366 item.setCounter(count);
367 }
368 return newContext;
369 }
370
371
372
373
374
375
376 public void dumpContexts(String title) {
377 System.out.println(title + " " + contextPool.size() + " context(s)");
378 Enumeration keys = contextPool.keys();
379 int i = 0;
380 while (keys.hasMoreElements()) {
381 String key = (String) keys.nextElement();
382 SnmpContextPoolItem item = (SnmpContextPoolItem) contextPool.get(key);
383 if (item != null) {
384 int count = item.getCounter();
385 SnmpContext cntxt = (SnmpContext) item.getContext();
386
387 if (cntxt == context) {
388 System.out.println("\tcurrent context: ");
389 }
390 System.out.println("\tcontext " + i + ": " + key + ", count: " + count
391 + ", " + cntxt.toString() + "\n"
392 + ", " + cntxt.getDebugString());
393 i++;
394 }
395 }
396 }
397
398
399
400
401
402
403
404 public String getHashKey() {
405 String str = hostname
406 + "_" + hostPort
407 + "_" + bindAddr
408 + "_" + socketType
409 + "_" + community
410 + "_v" + getVersion();
411 return str;
412 }
413
414
415
416
417
418
419
420 public void addTrapListener(TrapListener l) throws java.io.IOException {
421 if (context != null) {
422 context.addTrapListener(l);
423 }
424 }
425
426
427
428
429
430
431
432
433 public void removeTrapListener(TrapListener l) throws java.io.IOException {
434 if (context != null) {
435 context.removeTrapListener(l);
436 }
437 }
438
439 public void addTrapListener(TrapListener l, int port) throws java.io.IOException {
440 if (context != null) {
441 context.addTrapListener(l, port);
442 }
443 }
444
445 public void removeTrapListener(TrapListener l, int port) throws java.io.IOException {
446 if (context != null) {
447 context.removeTrapListener(l, port);
448 }
449 }
450
451 public void addTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException {
452 if (context != null) {
453 context.addTrapListener(l, lcontext);
454 }
455 }
456
457 public void removeTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException {
458 if (context != null) {
459 context.removeTrapListener(l, lcontext);
460 }
461 }
462
463 public void addRequestPduListener(RequestPduListener l) throws java.io.IOException {
464 if (context != null) {
465 context.addRequestPduListener(l);
466 }
467 }
468
469 public void removeRequestPduListener(RequestPduListener l) throws java.io.IOException {
470 if (context != null) {
471 context.removeRequestPduListener(l);
472 }
473 }
474
475 public void addRequestPduListener(RequestPduListener l, int port) throws java.io.IOException {
476 if (context != null) {
477 context.addRequestPduListener(l, port);
478 }
479 }
480
481 public void removeRequestPduListener(RequestPduListener l, int port) throws java.io.IOException {
482 if (context != null) {
483 context.removeRequestPduListener(l, port);
484 }
485 }
486
487 public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException {
488 if (context != null) {
489 context.addRequestPduListener(l, lcontext);
490 }
491 }
492
493 public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext)
494 throws java.io.IOException {
495 if (context != null) {
496 context.removeRequestPduListener(l, lcontext);
497 }
498 }
499
500
501
502
503
504
505 public Pdu processIncomingPdu(byte[] message)
506 throws DecodingException, java.io.IOException {
507 Pdu pdu = null;
508 if (context != null) {
509 pdu = context.processIncomingPdu(message);
510 }
511 return pdu;
512 }
513
514
515
516
517
518
519 public String toString() {
520 String res = "";
521 if (context != null) {
522 res = context.toString();
523 }
524 return res;
525 }
526
527
528
529
530
531
532 public Object clone() throws CloneNotSupportedException {
533 throw new CloneNotSupportedException();
534 }
535
536 }