1 // NAME
2 // $RCSfile: TrapReceivedSupport.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 1.6 $
7 // CREATED
8 // $Date: 2006/01/17 17:43:53 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 2001 - 2006 by Westhawk Ltd
16 * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17 *
18 * Permission to use, copy, modify, and distribute this software
19 * for any purpose and without fee is hereby granted, provided
20 * that the above copyright notices appear in all copies and that
21 * both the copyright notice and this permission notice appear in
22 * supporting documentation.
23 * This software is provided "as is" without express or implied
24 * warranty.
25 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26 */
27 package uk.co.westhawk.snmp.event;
28
29 /*-
30 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31 * SNMP Java Client
32 * ჻჻჻჻჻჻
33 * Copyright 2023 MetricsHub, Westhawk
34 * ჻჻჻჻჻჻
35 * This program is free software: you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as
37 * published by the Free Software Foundation, either version 3 of the
38 * License, or (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Lesser Public License for more details.
44 *
45 * You should have received a copy of the GNU General Lesser Public
46 * License along with this program. If not, see
47 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
48 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49 */
50
51 import java.util.*;
52 import uk.co.westhawk.snmp.stack.*;
53
54 /**
55 * This is a utility class that can be used by classes that support trap
56 * listener functionality.
57 * You can use an instance of this class as a member field
58 * of your class and delegate various work to it.
59 *
60 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
61 * @version $Revision: 1.6 $ $Date: 2006/01/17 17:43:53 $
62 */
63 public class TrapReceivedSupport {
64 public static final String version_id = "@(#)$Id: TrapReceivedSupport.java,v 1.6 2006/01/17 17:43:53 birgit Exp $ Copyright Westhawk Ltd";
65
66 private Object source;
67 private transient Vector trapListeners;
68
69 /**
70 * The constructor.
71 *
72 * @param src The source (SnmpContext) of the trap events when they are fired.
73 */
74 public TrapReceivedSupport(Object src) {
75 source = src;
76 }
77
78 /**
79 * Removes all the listeners.
80 */
81 public synchronized void empty() {
82 if (trapListeners != null) {
83 trapListeners.removeAllElements();
84 }
85 }
86
87 /**
88 * Returns the number of listeners.
89 *
90 * @return The number of listeners.
91 */
92 public synchronized int getListenerCount() {
93 int c = 0;
94 if (trapListeners != null) {
95 c = trapListeners.size();
96 }
97 return c;
98 }
99
100 /**
101 * Adds the specified trap listener to receive traps.
102 */
103 public synchronized void addTrapListener(TrapListener listener) {
104 if (trapListeners == null) {
105 trapListeners = new Vector(5);
106 }
107 if (trapListeners.contains(listener) == false) {
108 trapListeners.addElement(listener);
109 }
110 }
111
112 /**
113 * Removes the specified trap listener.
114 */
115 public synchronized void removeTrapListener(TrapListener listener) {
116 if (trapListeners != null) {
117 trapListeners.removeElement(listener);
118 }
119 }
120
121 /**
122 * Fires a decoded trap event.
123 * The event is fired to all listeners, whether they consume it or not.
124 * This behaviour is different from the undecoded trap event.
125 *
126 * @param pdu The decoded trap pdu.
127 */
128 public void fireTrapReceived(Pdu pdu, int hostPort) {
129 Vector copyOfListeners = null;
130 if (trapListeners != null) {
131 synchronized (trapListeners) {
132 copyOfListeners = (Vector) trapListeners.clone();
133 }
134 }
135
136 if (copyOfListeners != null) {
137 int sz = copyOfListeners.size();
138 for (int i = sz - 1; i >= 0; i--) {
139 TrapListener listener = (TrapListener) copyOfListeners.elementAt(i);
140
141 TrapEvent evt = new TrapEvent(source, pdu, hostPort);
142 listener.trapReceived(evt);
143 }
144 }
145 }
146
147 }