1 // NAME 2 // $RCSfile: NTSharedResBean.java,v $ 3 // DESCRIPTION 4 // [given below in javadoc format] 5 // DELTA 6 // $Revision: 1.13 $ 7 // CREATED 8 // $Date: 2006/01/25 18:08:56 $ 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 collects the names of the shared resources installed on 63 * a NT server. The NT mib is described in the 64 * 65 * <a href="http://premium.microsoft.com/msdn/library/winresource/dnwinnt/f1d/d25/s86a2.htm">LAN Manager MIB II for Windows NT Objects</a> . 66 * 67 * You will have to register to the MSDN before accessing this page. 68 * </p> 69 * 70 * <p> 71 * The properties in the parent classes should be set, before calling 72 * the action() method. Via a PropertyChangeEvent the application/applet 73 * will be notified. 74 * </p> 75 * 76 * @see SNMPBean#setHost 77 * @see SNMPBean#setPort 78 * @see SNMPBean#setCommunityName 79 * @see SNMPRunBean#setUpdateInterval 80 * @see SNMPBean#addPropertyChangeListener 81 * @see SNMPBean#action 82 * @see GetNextPdu 83 * 84 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> 85 * @version $Revision: 1.13 $ $Date: 2006/01/25 18:08:56 $ 86 * 87 */ 88 public class NTSharedResBean extends SNMPRunBean implements Observer { 89 private static final String version_id = "@(#)$Id: NTSharedResBean.java,v 1.13 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd"; 90 91 public final static String svShareName = "1.3.6.1.4.1.77.1.2.27.1.1"; 92 93 private int svShareName_len; 94 private GetNextPdu pdu; 95 private Hashtable resourceHash; 96 97 private boolean isGetNextInFlight; 98 private Date lastUpdateDate = null; 99 100 /** 101 * The default constructor. 102 */ 103 public NTSharedResBean() { 104 resourceHash = new Hashtable(); 105 svShareName_len = svShareName.length(); 106 } 107 108 /** 109 * The constructor that will set the host and the port no. 110 * 111 * @param h the hostname 112 * @param p the port no 113 * @see SNMPBean#setHost 114 * @see SNMPBean#setPort 115 */ 116 public NTSharedResBean(String h, int p) { 117 this(h, p, null); 118 } 119 120 /** 121 * The constructor that will set the host, the port no and the local 122 * bind address. 123 * 124 * @param h the hostname 125 * @param p the port no 126 * @param b the local bind address 127 * @see SNMPBean#setHost 128 * @see SNMPBean#setPort 129 * @see SNMPBean#setBindAddress 130 * 131 * @since 4_14 132 */ 133 public NTSharedResBean(String h, int p, String b) { 134 this(); 135 setHost(h); 136 setPort(p); 137 setBindAddress(b); 138 } 139 140 /** 141 * Returns the date of the moment when this bean was last updated. 142 * This might be null when the first time the update was not finished. 143 * 144 * @return the last update date 145 */ 146 public Date getLastUpdateDate() { 147 return lastUpdateDate; 148 } 149 150 /** 151 * Returns the indices of the NT shared resources. 152 * The OID of this shared resource is a concatenation of the 153 * name (svShareName) OID and the shared resource specific index. 154 * The index should be used to get the other properties of this resource. 155 * 156 * @see #getIndex(String) 157 * @see #svShareName 158 */ 159 public Enumeration getIndices() { 160 return resourceHash.elements(); 161 } 162 163 /** 164 * Returns the index of one of the resources. 165 * The OID of this shared resource is a concatenation of the 166 * name (svShareName) OID and the shared resource specific index. 167 * The index should be used to get the other properties of this resource. 168 * 169 * @param name The name of the resource 170 * @return the resource index, might be null if no resource with such name 171 * exists 172 * @see #getIndices 173 * @see #getNames 174 */ 175 public String getIndex(String name) { 176 String ret = null; 177 if (name != null) { 178 ret = (String) resourceHash.get(name); 179 } 180 return ret; 181 } 182 183 /** 184 * Returns the names of the NT shared resources (the list 185 * of svShareName). 186 */ 187 public Enumeration getNames() { 188 return resourceHash.keys(); 189 } 190 191 /** 192 * Returns the number of NT shared resources. 193 */ 194 public synchronized int getCount() { 195 return resourceHash.size(); 196 } 197 198 /** 199 * This method starts the action of the bean. It will initialises 200 * all variables before starting. 201 * 202 * @see SNMPBean#action 203 */ 204 public void action() { 205 if (isHostPortReachable()) { 206 resourceHash.clear(); 207 lastUpdateDate = new Date(); 208 isGetNextInFlight = false; 209 setRunning(true); 210 } 211 } 212 213 /** 214 * Implements the running of the bean. 215 * 216 * It will send the Pdu, if the previous one is not still in flight. 217 * 218 * @see SNMPRunBean#isRunning() 219 */ 220 public void run() { 221 while (context != null && isRunning()) { 222 if (isGetNextInFlight == false) { 223 // start the GetNext loop again 224 isGetNextInFlight = true; 225 pdu = new GetNextPdu(context); 226 pdu.addObserver(this); 227 pdu.addOid(svShareName); 228 try { 229 pdu.send(); 230 } catch (PduException exc) { 231 System.out.println("PduException " + exc.getMessage()); 232 } catch (IOException exc) { 233 System.out.println("IOException " + exc.getMessage()); 234 } 235 } 236 237 try { 238 Thread.sleep(interval); 239 } catch (InterruptedException ix) { 240 ; 241 } 242 } 243 } 244 245 /** 246 * This method is called when the Pdu response is received. When all 247 * answers are received it will fire the property change event. 248 * 249 * The answers are stored in a hashtable, this is done because the speed 250 * can only be calculated with the previous answer. 251 * 252 * @see SNMPBean#addPropertyChangeListener 253 */ 254 public void update(Observable obs, Object ov) { 255 varbind var; 256 String hashKey; 257 String oid, index, name; 258 259 pdu = (GetNextPdu) obs; 260 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { 261 var = (varbind) ov; 262 oid = var.getOid().toString(); 263 if (oid.startsWith(svShareName)) { 264 // index is the part of the oid AFTER the svShareName 265 index = oid.substring(svShareName_len + 1); 266 267 name = ((AsnOctets) var.getValue()).getValue(); 268 269 // update the hashtable with the new answer 270 resourceHash.put(name, index); 271 272 // perform the GetNext on the just received answer 273 pdu = new GetNextPdu(context); 274 pdu.addObserver(this); 275 pdu.addOid(oid); 276 try { 277 pdu.send(); 278 } catch (PduException exc) { 279 System.out.println("PduException " + exc.getMessage()); 280 } catch (IOException exc) { 281 System.out.println("IOException " + exc.getMessage()); 282 } 283 } else { 284 // the GetNext loop has ended 285 lastUpdateDate = new Date(); 286 isGetNextInFlight = false; 287 firePropertyChange("resourceNames", null, null); 288 } 289 } else { 290 // the GetNext loop has ended 291 lastUpdateDate = new Date(); 292 isGetNextInFlight = false; 293 firePropertyChange("resourceNames", null, null); 294 } 295 } 296 297 }