View Javadoc
1   // NAME
2   //      $RCSfile: SnmpContextv2cPool.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.15 $
7   // CREATED
8   //      $Date: 2009/03/05 13:27:41 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2000 - 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.util.*;
53  
54  /**
55   * This class contains the pool of SNMP v2c contexts.
56   * It extends the SnmpContextPool and is similar in every way, except it
57   * uses a pool of SnmpContextv2c.
58   *
59   * <p>
60   * Thanks to Seon Lee (slee@virtc.com) for reporting thread safety
61   * problems.
62   * </p>
63   *
64   * @see SnmpContextv2c
65   * @see SnmpContextPool
66   * @see SnmpContextv3Pool
67   *
68   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
69   * @version $Revision: 3.15 $ $Date: 2009/03/05 13:27:41 $
70   */
71  public class SnmpContextv2cPool extends SnmpContextPool
72          implements SnmpContextv2cFace {
73      private static final String version_id = "@(#)$Id: SnmpContextv2cPool.java,v 3.15 2009/03/05 13:27:41 birgita Exp $ Copyright Westhawk Ltd";
74  
75      /**
76       * Constructor.
77       *
78       * @param host The host to which the PDU will be sent
79       * @param port The port where the SNMP server will be
80       * @see SnmpContextv2c#SnmpContextv2c(String, int)
81       */
82      public SnmpContextv2cPool(String host, int port) throws java.io.IOException {
83          super(host, port, STANDARD_SOCKET);
84      }
85  
86      /**
87       * Constructor.
88       * Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a
89       * fully qualified classname.
90       *
91       * @param host       The host to which the PDU will be sent
92       * @param port       The port where the SNMP server will be
93       * @param typeSocket The type of socket to use.
94       *
95       * @see SnmpContextv2c#SnmpContextv2c(String, int, String)
96       * @see SnmpContextBasisFace#STANDARD_SOCKET
97       * @see SnmpContextBasisFace#TCP_SOCKET
98       */
99      public SnmpContextv2cPool(String host, int port, String typeSocket)
100             throws java.io.IOException {
101         super(host, port, typeSocket);
102     }
103 
104     /**
105      * Constructor.
106      * Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a
107      * fully qualified classname.
108      *
109      * @param host       The host to which the PDU will be sent
110      * @param port       The port where the SNMP server will be
111      * @param comm       The community name.
112      * @param typeSocket The type of socket to use.
113      *
114      * @see SnmpContextBasisFace#STANDARD_SOCKET
115      * @see SnmpContextBasisFace#TCP_SOCKET
116      *
117      * @since 4_14
118      */
119     public SnmpContextv2cPool(String host, int port, String comm, String typeSocket)
120             throws java.io.IOException {
121         super(host, port, comm, null, typeSocket);
122     }
123 
124     /**
125      * Constructor.
126      * Parameter typeSocket should be either STANDARD_SOCKET, TCP_SOCKET or a
127      * fully qualified classname.
128      *
129      * @param host        The host to which the PDU will be sent
130      * @param port        The port where the SNMP server will be
131      * @param comm        The community name.
132      * @param bindAddress The local address the server will bind to
133      * @param typeSocket  The type of socket to use.
134      *
135      * @see SnmpContextBasisFace#STANDARD_SOCKET
136      * @see SnmpContextBasisFace#TCP_SOCKET
137      *
138      * @since 4_14
139      */
140     public SnmpContextv2cPool(String host, int port, String comm, String bindAddress, String typeSocket)
141             throws java.io.IOException {
142         super(host, port, comm, bindAddress, typeSocket);
143     }
144 
145     public int getVersion() {
146         return SnmpConstants.SNMP_VERSION_2c;
147     }
148 
149     /**
150      * Returns a v2c context from the pool.
151      * The pre-existing context (if there is any) is destroyed.
152      * This methods checks for an existing context that matches all our
153      * properties. If such a context does not exist, a new one is created and
154      * added to the pool.
155      *
156      * This method actually returns a SnmpContextv2c, although it doesn't
157      * look like it.
158      *
159      * @return A context (v2c) from the pool
160      * @see #getHashKey
161      * @see SnmpContext
162      * @see SnmpContextv2c
163      */
164     protected SnmpContext getMatchingContext() throws java.io.IOException {
165         SnmpContextPoolItem item = null;
166         SnmpContextv2c newContext = null;
167         String hashKey = getHashKey();
168 
169         destroy();
170         synchronized (contextPool) {
171             int count = 0;
172             if (contextPool.containsKey(hashKey)) {
173                 item = (SnmpContextPoolItem) contextPool.get(hashKey);
174                 newContext = (SnmpContextv2c) item.getContext();
175                 count = item.getCounter();
176             } else {
177                 newContext = new SnmpContextv2c(hostname, hostPort, bindAddr, socketType);
178                 newContext.setCommunity(community);
179                 item = new SnmpContextPoolItem(newContext);
180                 contextPool.put(hashKey, item);
181             }
182             count++;
183             item.setCounter(count);
184         }
185         return newContext;
186     }
187 
188     /**
189      * This method is not supported. It will throw a CloneNotSupportedException.
190      *
191      * @since 4_14
192      */
193     public Object clone() throws CloneNotSupportedException {
194         throw new CloneNotSupportedException();
195     }
196 
197 }