View Javadoc
1   // NAME
2   //      $RCSfile: OneNTPrintQBean.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 information about one NT print queue installed
63   * on 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 #setIndex(String)
81   * @see SNMPBean#addPropertyChangeListener
82   * @see SNMPBean#action
83   * @see GetPdu_vec
84   * @see NTPrintQBean
85   * 
86   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
87   * @version $Revision: 1.13 $ $Date: 2006/01/25 18:08:56 $
88   */
89  public class OneNTPrintQBean extends SNMPRunBean implements Observer {
90      private static final String version_id = "@(#)$Id: OneNTPrintQBean.java,v 1.13 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd";
91  
92      private GetPdu_vec pdu;
93  
94      private boolean isPduInFlight;
95      private Date lastUpdateDate = null;
96  
97      private String index = "";
98      private String name = "";
99      private int jobs = 0;
100 
101     private final static int NR_OID = 2;
102     private final static String svPrintQName = "1.3.6.1.4.1.77.1.2.29.1.1";
103     private final static String svPrintQNumJobs = "1.3.6.1.4.1.77.1.2.29.1.2";
104 
105     /**
106      * The default constructor.
107      */
108     public OneNTPrintQBean() {
109     }
110 
111     /**
112      * The constructor that will set the host and the port no.
113      *
114      * @param h the hostname
115      * @param p the port no
116      * @see SNMPBean#setHost
117      * @see SNMPBean#setPort
118      */
119     public OneNTPrintQBean(String h, int p) {
120         this(h, p, null);
121     }
122 
123     /**
124      * The constructor that will set the host, the port no and the local
125      * bind address.
126      *
127      * @param h the hostname
128      * @param p the port no
129      * @param b the local bind address
130      * @see SNMPBean#setHost
131      * @see SNMPBean#setPort
132      * @see SNMPBean#setBindAddress
133      *
134      * @since 4_14
135      */
136     public OneNTPrintQBean(String h, int p, String b) {
137         this();
138         setHost(h);
139         setPort(p);
140         setBindAddress(b);
141     }
142 
143     /**
144      * Sets the index of the NT print queue that will be requested.
145      * 
146      * @param ind the index
147      * @see #getIndex()
148      * @see NTPrintQBean#getIndex(String)
149      */
150     public void setIndex(String ind) {
151         if (ind != null && ind.length() > 0) {
152             index = ind;
153         }
154     }
155 
156     /**
157      * Returns the index of the NT print queue.
158      * 
159      * @return the index
160      * @see #setIndex(String)
161      */
162     public String getIndex() {
163         return index;
164     }
165 
166     /**
167      * Returns the name of the NT print queue.
168      * 
169      * @return the name
170      */
171     public String getName() {
172         return name;
173     }
174 
175     /**
176      * Returns the number of jobs currently in this NT print queue.
177      * 
178      * @return the number of jobs
179      */
180     public int getNumJobs() {
181         return jobs;
182     }
183 
184     /**
185      * Returns the date of the moment when this bean was last updated.
186      * This might be null when the first time the update was not finished.
187      *
188      * @return the last update date
189      */
190     public Date getLastUpdateDate() {
191         return lastUpdateDate;
192     }
193 
194     /**
195      * This method starts sending the SNMP request. All properties should be
196      * set before this method is called.
197      *
198      * The actual sending will take place in the run method.
199      * It makes a new snmp context and initialises all variables before
200      * starting.
201      */
202     public void action() {
203         if (isHostPortReachable()) {
204             lastUpdateDate = new Date();
205             isPduInFlight = false;
206             setRunning(true);
207         }
208     }
209 
210     /**
211      * The run method according to the Runnable interface.
212      * This method will send the Pdu request, if the previous one is not
213      * still in flight.
214      * 
215      * @see SNMPRunBean#isRunning()
216      */
217     public void run() {
218         while (context != null && isRunning()) {
219             String ind = getIndex();
220             if (isPduInFlight == false && ind != null && ind.length() > 0) {
221                 isPduInFlight = true;
222                 pdu = new GetPdu_vec(context, NR_OID);
223                 pdu.addObserver(this);
224                 pdu.addOid(svPrintQName + "." + ind);
225                 pdu.addOid(svPrintQNumJobs + "." + ind);
226                 try {
227                     pdu.send();
228                 } catch (PduException exc) {
229                     System.out.println("PduException " + exc.getMessage());
230                 } catch (IOException exc) {
231                     System.out.println("IOException " + exc.getMessage());
232                 }
233             }
234 
235             try {
236                 Thread.sleep(interval);
237             } catch (InterruptedException ix) {
238                 ;
239             }
240         }
241     }
242 
243     /**
244      * The update method according to the Observer interface, it will be
245      * called when the Pdu response is received.
246      * The property change event is fired.
247      *
248      * @see SNMPBean#addPropertyChangeListener
249      */
250     public void update(Observable obs, Object ov) {
251         pdu = (GetPdu_vec) obs;
252         varbind[] varbinds = (varbind[]) ov;
253 
254         if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
255             name = ((AsnOctets) varbinds[0].getValue()).getValue();
256             jobs = ((AsnInteger) varbinds[1].getValue()).getValue();
257 
258             lastUpdateDate = new Date();
259             isPduInFlight = false;
260             firePropertyChange("NTService", null, null);
261         }
262     }
263 
264 }