1 // NAME
2 // $RCSfile: DefaultTrapContext.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.12 $
7 // CREATED
8 // $Date: 2009/03/05 13:12:50 $
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
28 package uk.co.westhawk.snmp.stack;
29
30 /*-
31 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32 * SNMP Java Client
33 * ჻჻჻჻჻჻
34 * Copyright 2023 MetricsHub, Westhawk
35 * ჻჻჻჻჻჻
36 * This program is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU Lesser General Public License as
38 * published by the Free Software Foundation, either version 3 of the
39 * License, or (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Lesser Public License for more details.
45 *
46 * You should have received a copy of the GNU General Lesser Public
47 * License along with this program. If not, see
48 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50 */
51
52 import java.io.*;
53 import java.util.*;
54
55 import uk.co.westhawk.snmp.event.*;
56 import uk.co.westhawk.snmp.net.*;
57 import uk.co.westhawk.snmp.util.*;
58
59
60 /**
61 * The DefaultTrapContext class will enable this stack to receive traps.
62 * Only one (1) instance of the DefaultTrapContext can exist. The
63 * context will only start receiving (or listen for) traps when there is
64 * at least one listener registered. Two kind of listeners can be added;
65 * the normal and unhandled trap listeners.
66 * The normal trap listeners are added via the
67 * <code>addTrapListener()</code> method,
68 * the unhandled trap listeners are added via the
69 * <code>addUnhandledTrapListener()</code>.
70 *
71 * <p>
72 * Use one of the <code>getInstance()</code> methods to get the instance and add a trap
73 * listener. This class will fire undecoded trap events, i.e. the raw
74 * data is sent and no attempt is made to decode the data into a pdu.
75 * </p>
76 *
77 * <p>
78 * The SnmpContext classes provide functionality for decoded trap
79 * events. These classes will register themselves to the
80 * DefaultTrapContext object and only pass the event on if it matches
81 * their configuration.
82 * </p>
83 *
84 * <p>
85 * <a id=note></a>
86 * Note that because only one instance of this class
87 * can exist, the first call of <code>getInstance()</code> will define
88 * the settings
89 * (i.e. port number and socket type) for the lifetime of the stack. All
90 * the subsequent calls of <code>getInstance()</code> will return the existing
91 * instance, irrespective of the arguments.
92 * </p>
93 *
94 * <p>
95 * On UNIX and Linux operating systems the default port where trap are
96 * sent (i.e. <em>162</em>) can only be opened as root.
97 * </p>
98 *
99 * <p>
100 * Note, this class is now deprecated. We are (very) slowly trying to
101 * move to a more general way of receiving packets and adding agent
102 * functionality. ListeningContext and ListeningContextPool allow the
103 * stack to listen to more than one port.
104 * </p>
105 *
106 * @deprecated As of 4_14, replaced by {@link ListeningContext} and
107 * {@link ListeningContextPool}
108 *
109 * @see AbstractSnmpContext#addTrapListener
110 * @see ListeningContext
111 * @see ListeningContextPool
112 *
113 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
114 * @version $Revision: 3.12 $ $Date: 2009/03/05 13:12:50 $
115 */
116 public class DefaultTrapContext extends ListeningContext {
117 private static final String version_id = "@(#)$Id: DefaultTrapContext.java,v 3.12 2009/03/05 13:12:50 birgita Exp $ Copyright Westhawk Ltd";
118
119 private static DefaultTrapContext current = null;
120
121 /**
122 * Constructor.
123 * The Standard socket type will be used.
124 *
125 * @param port The local port where traps are received
126 * @see SnmpContextBasisFace#STANDARD_SOCKET
127 */
128 protected DefaultTrapContext(int port) throws IOException {
129 this(port, SnmpContextBasisFace.STANDARD_SOCKET);
130 }
131
132 /**
133 * Constructor.
134 *
135 * The typeSocket will indicate which type of socket to use. This way
136 * different handlers can be provided.
137 * It should be either STANDARD_SOCKET, TCP_SOCKET or a
138 * fully qualified classname.
139 *
140 * @param port The local port where traps are received
141 * @param typeSocketA The type of socket to use.
142 *
143 * @see SnmpContextBasisFace#STANDARD_SOCKET
144 * @see SnmpContextBasisFace#TCP_SOCKET
145 */
146 protected DefaultTrapContext(int port, String typeSocketA)
147 throws IOException {
148 super(port, typeSocketA);
149 }
150
151 /**
152 * Returns the instance of DefaultTrapContext. It will create the
153 * instance if it didn't exists.
154 * See <a href=#note>the note</a> above.
155 */
156 public static synchronized DefaultTrapContext getInstance(int port)
157 throws IOException {
158 if (current == null) {
159 current = new DefaultTrapContext(port);
160 }
161 return current;
162 }
163
164 /**
165 * Returns the instance of DefaultTrapContext. It will create the
166 * instance if it didn't exists.
167 * See <a href=#note>the note</a> above.
168 */
169 public static synchronized DefaultTrapContext getInstance(int port, String typeSocketA)
170 throws IOException {
171 if (current == null) {
172 current = new DefaultTrapContext(port, typeSocketA);
173 }
174 return current;
175 }
176
177 }