KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > mp > DefaultCounterListener


1 /*_############################################################################
2   _##
3   _## SNMP4J - DefaultCounterListener.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23
24
25 package org.snmp4j.mp;
26
27 import org.snmp4j.event.CounterListener;
28 import org.snmp4j.event.CounterEvent;
29 import java.util.Hashtable JavaDoc;
30 import org.snmp4j.smi.OID;
31 import org.snmp4j.smi.VariableBinding;
32 import org.snmp4j.smi.Counter32;
33 import org.snmp4j.smi.Variable;
34
35 /**
36  * The <code>DefaultCounterListener</code> is the default implementation of
37  * the <code>CounterListener</code> interface. For any counter incrementation
38  * event it checks whether the referenced counter object already exists. If not,
39  * it will be created and initialized with one. Otherwise, the current value
40  * will be incremented by one. In either case, the current value will be
41  * returned in the event object.
42  * <p>
43  * To use a <code>DefaultCounterListener</code> with SNMP4J, add it to the
44  * default <code>CounterSupport</code> by:
45  * <pre>
46  * CounterSupport.getInstance().addCounterListener(new DefaultCounterListener());
47  * </pre>
48  *
49  * @author Frank Fock
50  * @version 1.0.1
51  */

52 public class DefaultCounterListener implements CounterListener {
53
54   private Hashtable JavaDoc counters = new Hashtable JavaDoc(50);
55
56   /**
57    * Default constructor.
58    */

59   public DefaultCounterListener() {
60   }
61
62   public synchronized void incrementCounter(CounterEvent event) {
63     OID id = event.getOid();
64     VariableBinding counter = (VariableBinding) counters.get(id);
65     if (counter == null) {
66       counter = new VariableBinding(id, new Counter32(1));
67       counters.put(id, counter);
68     }
69     else {
70       ((Counter32)counter.getVariable()).increment();
71     }
72     // write back current value
73
event.setCurrentValue((Variable)
74                           ((VariableBinding)counter).getVariable().clone());
75   }
76 }
77
Popular Tags