1 // NAME 2 // $RCSfile: SNMPBean.java,v $ 3 // DESCRIPTION 4 // [given below in javadoc format] 5 // DELTA 6 // $Revision: 1.12 $ 7 // CREATED 8 // $Date: 2006/01/26 12:38:30 $ 9 // COPYRIGHT 10 // Westhawk Ltd 11 // TO DO 12 // 13 14 /* 15 * Copyright (C) 1998 - 2006 by Westhawk Ltd 16 * 17 * Permission to use, copy, modify, and distribute this software 18 * for any purpose and without fee is hereby granted, provided 19 * that the above copyright notices appear in all copies and that 20 * both the copyright notice and this permission notice appear in 21 * supporting documentation. 22 * This software is provided "as is" without express or implied 23 * warranty. 24 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> 25 */ 26 27 package uk.co.westhawk.snmp.beans; 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 uk.co.westhawk.snmp.stack.*; 52 import uk.co.westhawk.snmp.pdu.*; 53 import java.awt.*; 54 import java.util.*; 55 import java.text.*; 56 import java.lang.*; 57 import java.io.*; 58 import java.beans.*; 59 60 /** 61 * <p> 62 * This bean forms the base of the SNMP beans. 63 * </p> 64 * 65 * <p> 66 * It provides the setting of the properties that are always needed when 67 * sending a SNMP request: 68 * </p> 69 * <ul> 70 * <li>host, default is <em>localhost</em></li> 71 * <li>port, default is <em>161</em></li> 72 * <li>community name, default is <em>public</em></li> 73 * <li>bindAddr, default is <em>null</em></li> 74 * <li>socket type, default is <em>Standard</em></li> 75 * </ul> 76 * 77 * <p> 78 * The bean will only come into action when the method <em>action()</em> is 79 * called. This should be done after the properties have been set. 80 * </p> 81 * 82 * <p> 83 * This bean also provide the methods for adding and removing property 84 * change listeners and firing property change events. 85 * </p> 86 * 87 * <p> 88 * Note: This bean will create a socket. This might result in a 89 * SecurityException because of the Java security policy. 90 * </p> 91 * <ul> 92 * <li> 93 * If you are an applet: you're only connect back to its source web server 94 * </li> 95 * <li> 96 * If you are an application or servlet: java allows you to send it. The 97 * host might refuse your connection. 98 * </li> 99 * </ul> 100 * 101 * <p> 102 * The SNMP request will only succeed if: 103 * </p> 104 * <ul> 105 * <li> 106 * java security policy allows the opening of the socket 107 * </li> 108 * <li> 109 * the host/port combination is reachable from your location (you can 110 * check that with the ping (command line) command. 111 * </li> 112 * <li> 113 * the combination of OID and community name is correct 114 * </li> 115 * </ul> 116 * 117 * @see SNMPRunBean 118 * @see IsHostReachableBean 119 * @see OneInterfaceBean 120 * @see InterfaceIndexesBean 121 * 122 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> 123 * @version $Revision: 1.12 $ $Date: 2006/01/26 12:38:30 $ 124 * 125 */ 126 public abstract class SNMPBean { 127 private static final String version_id = "@(#)$Id: SNMPBean.java,v 1.12 2006/01/26 12:38:30 birgit Exp $ Copyright Westhawk Ltd"; 128 129 protected SnmpContext context = null; 130 131 protected String host = "localhost"; 132 protected String bindAddr = null; 133 protected int port = 161; 134 protected String community = "public"; 135 protected String socketType = SnmpContextBasisFace.STANDARD_SOCKET; 136 protected String message = ""; 137 138 protected Vector propertyChangeListener = null; 139 140 /** 141 * This method should send the SNMP request. All properties should be 142 * set before this method is called. 143 */ 144 public abstract void action() 145 throws PduException, IOException; 146 147 /** 148 * The default constructor. 149 */ 150 public SNMPBean() { 151 propertyChangeListener = new Vector(); 152 } 153 154 /** 155 * The constructor that will set the host and the port no. 156 * 157 * @param h the hostname 158 * @param p the port no 159 * @see #setHost 160 * @see #setPort 161 */ 162 public SNMPBean(String h, int p) { 163 this(h, p, null, SnmpContextBasisFace.STANDARD_SOCKET); 164 } 165 166 /** 167 * The constructor that will set the host, the port no and the local 168 * bind address. 169 * 170 * @param h the hostname 171 * @param p the port no 172 * @param b the local bind address 173 * @param t the socket type 174 * @see #setHost 175 * @see #setPort 176 * @see #setBindAddress 177 * @see #setSocketType 178 * 179 * @since 4_14 180 */ 181 public SNMPBean(String h, int p, String b, String t) { 182 this(); 183 setHost(h); 184 setPort(p); 185 setBindAddress(b); 186 setSocketType(t); 187 } 188 189 /** 190 * Returns the host (name or ipadress) which is addressed. This is the 191 * host where the SNMP server is running. 192 * 193 * @return the name of the host 194 * @see #setHost 195 */ 196 public String getHost() { 197 return host; 198 } 199 200 /** 201 * Sets the host (name or ipadress) which will be asked for the SNMP request. 202 * This is the host where the SNMP server is running. 203 * The default is <em>localhost</em>. 204 * 205 * This bean will only start the request when action is called. 206 * 207 * @see #getHost 208 */ 209 public void setHost(String h) { 210 if (h != null && h.length() > 0 && !host.equals(h)) { 211 host = h; 212 } 213 } 214 215 /** 216 * Returns the port no which is used. That is the no of the port on the 217 * host where the SNMP server is running. 218 * 219 * @see #setPort(int) 220 * @see #setPort(String) 221 */ 222 public int getPort() { 223 return port; 224 } 225 226 /** 227 * Sets the port no which is used. That should be the no of the port on the 228 * host where the SNMP server is running. 229 * The default is <em>161</em>. 230 * 231 * This bean will only start the request when action is called. 232 * 233 * @param p The number of the port 234 * @see #getPort 235 * @see #setPort(String) 236 */ 237 public void setPort(int p) { 238 if (p > 0 && port != p) { 239 port = p; 240 } 241 } 242 243 /** 244 * Sets the port no which is used as a String. 245 * 246 * @param p The number of the port as a String 247 * @see #getPort 248 * @see #setPort(int) 249 */ 250 public void setPort(String p) { 251 int pNo; 252 try { 253 pNo = Integer.valueOf(p.trim()).intValue(); 254 setPort(pNo); 255 } catch (NumberFormatException exp) { 256 } 257 } 258 259 /** 260 * Returns the community name that is used to send the SNMP request. 261 * 262 * @see #setCommunityName 263 */ 264 public String getCommunityName() { 265 return community; 266 } 267 268 /** 269 * Sets the community name that is used to send the SNMP request. The 270 * default is <em>public</em>. 271 * 272 * @see #getCommunityName 273 */ 274 public void setCommunityName(String c) { 275 if (c != null & !community.equals(c)) { 276 community = c; 277 if (context != null) { 278 context.setCommunity(community); 279 } 280 } 281 } 282 283 /** 284 * Returns the local bind address. 285 * 286 * @return the name of the local bind address 287 * @see #setBindAddress 288 * @since 4_14 289 */ 290 public String getBindAddress() { 291 return bindAddr; 292 } 293 294 /** 295 * Sets the local bind address. 296 * The default is <em>null</em>. 297 * 298 * This bean will only start the request when action is called. 299 * 300 * @see #getBindAddress 301 * @since 4_14 302 */ 303 public void setBindAddress(String b) { 304 bindAddr = b; 305 } 306 307 /** 308 * Returns the socket type. 309 * 310 * @return the socket type. 311 * @see #setSocketType 312 * @since 4_14 313 */ 314 public String getSocketType() { 315 return socketType; 316 } 317 318 /** 319 * Sets socket type. 320 * The default is <em>null</em>. 321 * 322 * This bean will only start the request when action is called. 323 * 324 * @see #getSocketType 325 * @see SnmpContextBasisFace#STANDARD_SOCKET 326 * @since 4_14 327 */ 328 public void setSocketType(String t) { 329 socketType = t; 330 } 331 332 /** 333 * Indicates whether the host is reachable. 334 * This method will try to make a new SnmpContext, if this works, it is 335 * reachable. 336 * If it does not work, the message will be set. 337 * 338 * @see SnmpContext 339 */ 340 protected boolean isHostPortReachable() { 341 boolean res = true; 342 if (host != null 343 && 344 host.length() > 0 345 && 346 port > 0) { 347 try { 348 if (context != null) { 349 context.destroy(); 350 context = null; 351 } 352 353 context = new SnmpContext(host, port, bindAddr, socketType); 354 context.setCommunity(community); 355 setMessage("Connection to host " + host 356 + " is made succesfully"); 357 } catch (IOException exc) { 358 res = false; 359 setMessage("IOException: " + exc.getMessage()); 360 } catch (RuntimeException exc) { 361 res = false; 362 setMessage("RuntimeException: " + exc.getMessage()); 363 } 364 } else { 365 res = false; 366 } 367 return res; 368 } 369 370 /** 371 * Returns the message (if any). The message is used to give the user 372 * feedback if anything is happened with the request or the connection. 373 * 374 * @return the message 375 */ 376 public String getMessage() { 377 return message; 378 } 379 380 /** 381 * Returns the message (if any). The message is used to give the user 382 * feedback if anything is happened (usually when something went wrong) 383 * with the connection. 384 * 385 * @param st the message string 386 * @see #getMessage() 387 */ 388 protected void setMessage(String st) { 389 message = st; 390 } 391 392 /** 393 * Add a property change listener. 394 * 395 * @see #removePropertyChangeListener 396 */ 397 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 398 propertyChangeListener.addElement(l); 399 } 400 401 /** 402 * Remove a property change listener. 403 * 404 * @see #addPropertyChangeListener 405 */ 406 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 407 propertyChangeListener.removeElement(l); 408 } 409 410 /** 411 * Fire the property event. 412 * 413 * @see #removePropertyChangeListener 414 * @see #addPropertyChangeListener 415 * @see PropertyChangeEvent 416 * @see PropertyChangeListener 417 */ 418 protected void firePropertyChange(String property, Object old_v, Object new_v) { 419 Vector listeners; 420 synchronized (this) { 421 listeners = (Vector) propertyChangeListener.clone(); 422 } 423 424 PropertyChangeEvent event = new PropertyChangeEvent(this, 425 property, old_v, new_v); 426 427 int sz = listeners.size(); 428 for (int i = 0; i < sz; i++) { 429 PropertyChangeListener l = (PropertyChangeListener) listeners.elementAt(i); 430 l.propertyChange(event); 431 } 432 } 433 434 }