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