1 // NAME 2 // $RCSfile: NTUserNamesBean.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 network users 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 NTUserNamesBean extends SNMPRunBean implements Observer { 89 private static final String version_id = "@(#)$Id: NTUserNamesBean.java,v 1.13 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd"; 90 91 public final static String svUserName = "1.3.6.1.4.1.77.1.2.25.1.1"; 92 93 private int svUserName_len; 94 private GetNextPdu pdu; 95 private Hashtable userHash; 96 97 private boolean isGetNextInFlight; 98 private Date lastUpdateDate = null; 99 100 /** 101 * The default constructor. 102 */ 103 public NTUserNamesBean() { 104 userHash = new Hashtable(); 105 svUserName_len = svUserName.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 NTUserNamesBean(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 NTUserNamesBean(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 network users. 152 * The OID of this network user is a concatenation of the 153 * name (svUserName) OID and the network user specific index. 154 * The index should be used to get the other properties of this user. 155 * 156 * @see #getIndex(String) 157 * @see #svUserName 158 */ 159 public Enumeration getIndices() { 160 return userHash.elements(); 161 } 162 163 /** 164 * Returns the index of one of the users. 165 * The OID of this network user is a concatenation of the 166 * name (svUserName) OID and the network user specific index. 167 * The index should be used to get the other properties of this user. 168 * 169 * @param name The name of the user 170 * @return the user index, might be null if no user with such name 171 * exists 172 * @see #getIndices 173 * @see #getNames 174 */ 175 public String getIndex(String name) { 176 return (String) userHash.get(name); 177 } 178 179 /** 180 * Returns the names of the NT network users (the list 181 * of svUserName). 182 */ 183 public Enumeration getNames() { 184 return userHash.keys(); 185 } 186 187 /** 188 * Returns the number of NT network users. 189 */ 190 public synchronized int getCount() { 191 return userHash.size(); 192 } 193 194 /** 195 * This method starts the action of the bean. It will initialises 196 * all variables before starting. 197 */ 198 public void action() { 199 if (isHostPortReachable()) { 200 userHash.clear(); 201 lastUpdateDate = new Date(); 202 isGetNextInFlight = false; 203 setRunning(true); 204 } 205 } 206 207 /** 208 * Implements the running of the bean. 209 * 210 * It will send the Pdu, if the previous one is not still in flight. 211 * 212 * @see SNMPRunBean#isRunning() 213 */ 214 public void run() { 215 while (context != null && isRunning()) { 216 if (isGetNextInFlight == false) { 217 // start the GetNext loop again 218 isGetNextInFlight = true; 219 pdu = new GetNextPdu(context); 220 pdu.addObserver(this); 221 pdu.addOid(svUserName); 222 try { 223 pdu.send(); 224 } catch (PduException exc) { 225 System.out.println("PduException " + exc.getMessage()); 226 } catch (IOException exc) { 227 System.out.println("IOException " + exc.getMessage()); 228 } 229 } 230 231 try { 232 Thread.sleep(interval); 233 } catch (InterruptedException ix) { 234 ; 235 } 236 } 237 } 238 239 /** 240 * This method is called when the Pdu response is received. When all 241 * answers are received it will fire the property change event. 242 * 243 * The answers are stored in a hashtable, this is done because the speed 244 * can only be calculated with the previous answer. 245 * 246 * @see SNMPBean#addPropertyChangeListener 247 */ 248 public void update(Observable obs, Object ov) { 249 varbind var; 250 String hashKey; 251 String oid, index, name; 252 253 pdu = (GetNextPdu) obs; 254 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { 255 var = (varbind) ov; 256 oid = var.getOid().toString(); 257 if (oid.startsWith(svUserName)) { 258 // index is the part of the oid AFTER the svUserName 259 index = oid.substring(svUserName_len + 1); 260 261 name = ((AsnOctets) var.getValue()).getValue(); 262 263 // update the hashtable with the new answer 264 userHash.put(name, index); 265 266 // perform the GetNext on the just received answer 267 pdu = new GetNextPdu(context); 268 pdu.addObserver(this); 269 pdu.addOid(oid); 270 try { 271 pdu.send(); 272 } catch (PduException exc) { 273 System.out.println("PduException " + exc.getMessage()); 274 } catch (IOException exc) { 275 System.out.println("IOException " + exc.getMessage()); 276 } 277 } else { 278 // the GetNext loop has ended 279 lastUpdateDate = new Date(); 280 isGetNextInFlight = false; 281 firePropertyChange("userNames", null, null); 282 } 283 } else { 284 // the GetNext loop has ended 285 lastUpdateDate = new Date(); 286 isGetNextInFlight = false; 287 firePropertyChange("userNames", null, null); 288 } 289 } 290 291 }