View Javadoc

1   // Notifier.java - gnu.hylafax implementation of the faxstat utility
2   // $Id: Notifier.java,v 1.4 2007/05/07 18:26:53 sjardine Exp $
3   //
4   // - gives an example for getting the status callbacks on a FAX job
5   //
6   // Copyright 2003, Joe Phillips <joe.phillips@innovationsw.com>
7   // Copyright 2003, Innovation Software Group LLC - http://www.innovationsw.com/
8   //
9   // This library is free software; you can redistribute it and/or
10  // modify it under the terms of the GNU Library General Public
11  // License as published by the Free Software Foundation; either
12  // version 2 of the License, or (at your option) any later version.
13  //
14  // This library is distributed in the hope that it will be useful,
15  // but WITHOUT ANY WARRANTY; without even the implied warranty of
16  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  // Library General Public License for more details.
18  //
19  // You should have received a copy of the GNU Library General Public
20  // License along with this library; if not, write to the Free
21  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  //
23  //
24  // for information on the HylaFAX FAX server see
25  //  http://www.hylafax.org/
26  //
27  //  
28  
29  package gnu.hylafax.util;
30  
31  
32  import java.io.PrintStream;
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.FileNotFoundException;
36  import java.io.IOException;
37  import java.util.Properties;
38  
39  import gnu.getopt.*;
40  
41  
42  import gnu.hylafax.job.SendListener;
43  import gnu.hylafax.job.SendEvent;
44  import gnu.hylafax.job.ReceiveListener;
45  import gnu.hylafax.job.ReceiveEvent;
46  import gnu.hylafax.job.SendAndReceiveNotifier;
47  
48  /***
49   * This class implements a callback program as supplied in the notify script
50   * with the HylaFAX distribution. The following command line options are
51   * supported.
52   * <P>
53   * 
54   * <PRE>
55   * 
56   * -f <file> queue file name -r <reason> reason code -t <time> time spent on
57   * this job -n <time> ETA of next attempt if <reason> is 'requeued' -c <class>
58   * The Java class name of the gnu.hylafax.job.Listener to notify -j <jobid> The
59   * HylaFAX JOBID
60   * 
61   * </PRE>
62   * 
63   * <P>
64   * Refer to the notify man page (from the HylaFAX distribution) for more
65   * information. This program depends on the gnu.getopt package for command line
66   * parsing. gnu.getopt (java-getopt) can be found at <a
67   * href="http://www.urbanophile.com/arenn/">http://www.urbanophile.com/arenn/
68   * </a>
69   * 
70   * @author $Author: sjardine $
71   * @version $Id: Notifier.java,v 1.4 2007/05/07 18:26:53 sjardine Exp $
72   */
73  public class Notifier extends SendAndReceiveNotifier {
74  	public final static boolean OP_SEND = true;
75  
76  	public final static boolean OP_RECEIVE = false;
77  
78  	public final static String KEY_PROPERTIES = "notifier.properties";
79  
80  	/***
81  	 * 
82  	 * 
83  	 */
84  	public static void main(String arguments[]) throws ClassNotFoundException,
85  			InstantiationException, IllegalAccessException,
86  			FileNotFoundException, IOException {
87  		String file = null;
88  		String reason = null;
89  		String time = null;
90  		String nextAttempt = null;
91  		Class klass = null;
92  		long jobid = -1;
93  		String commid = null;
94  		String message = null;
95  		String modem = null;
96  		String cidname = null;
97  		String cidnumber = null;
98  		boolean OP = OP_SEND;
99  
100 		Getopt g = new Getopt("Notifier", arguments, "SRf:r:t:n:c:j:M:m:i:I:N");
101 		char opt;
102 		while ((short) (opt = (char) g.getopt()) != -1) {
103 			switch (opt) {
104 			case 'f':
105 				file = g.getOptarg();
106 				break;
107 			case 'r':
108 				reason = g.getOptarg();
109 				break;
110 			case 't':
111 				time = g.getOptarg();
112 				break;
113 			case 'n':
114 				nextAttempt = g.getOptarg();
115 				break;
116 			case 'c':
117 				klass = Class.forName(g.getOptarg());
118 				break;
119 			case 'j':
120 				jobid = Long.parseLong(g.getOptarg());
121 				break;
122 			case 'm':
123 				modem = g.getOptarg();
124 				break;
125 			case 'M':
126 				message = g.getOptarg();
127 				break;
128 			case 'i':
129 				commid = g.getOptarg();
130 				break;
131 			case 'N':
132 				cidname = g.getOptarg();
133 				break;
134 			case 'I':
135 				cidnumber = g.getOptarg();
136 				break;
137 			case 'R':
138 				OP = OP_RECEIVE;
139 				break;
140 			case 'S':
141 				OP = OP_SEND;
142 				break;
143 			case '?':
144 			default:
145 				// error
146 				usage(System.err);
147 				System.exit(-1);
148 			}
149 		}// while processing options
150 
151 		// load properties
152 		//
153 		File props = new File(System.getProperty(KEY_PROPERTIES));
154 		if (props.exists() && props.canRead()) {
155 			Properties p = new Properties(System.getProperties());
156 			p.load(new FileInputStream(props));
157 			System.setProperties(p);
158 		}
159 
160 		// notify listeners
161 		//
162 		if (OP == OP_SEND) {
163 			notifySendListeners(klass, jobid, file, reason, time, nextAttempt);
164 		} else {
165 			notifyReceiveListeners(klass, file, modem, commid, message,
166 					cidnumber, cidname);
167 		}
168 	}// main
169 
170 	public static void notifySendListeners(Class klass, long jobid,
171 			String file, String reason, String time, String nextAttempt)
172 			throws InstantiationException, IllegalAccessException {
173 		// verify options
174 		//
175 		if ((file == null) || ("".equals(file))) {
176 			usage(System.err);
177 			System.exit(-1);
178 		}
179 		if ((reason == null) || ("".equals(reason))) {
180 			usage(System.err);
181 			System.exit(-1);
182 		}
183 		if ((time == null) || ("".equals(time))) {
184 			usage(System.err);
185 			System.exit(-1);
186 		}
187 		if (reason.equals(SendEvent.REASON_REQUEUED)
188 				&& ((nextAttempt == null) || ("".equals(nextAttempt)))) {
189 			usage(System.err);
190 			System.exit(-1);
191 		}
192 		if (klass == null) {
193 			usage(System.err);
194 			System.exit(-1);
195 		}
196 		if (jobid < 0) {
197 			usage(System.err);
198 			System.exit(-1);
199 		}
200 
201 		// Notify Listeners
202 		//
203 
204 		SendEvent event = new SendEvent();
205 		event.setReason(reason);
206 		event.setFilename(file);
207 		event.setElapsedTime(time);
208 		event.setNextAttempt(nextAttempt);
209 		event.setJobId(jobid);
210 
211 		SendListener l = (SendListener) klass.newInstance();
212 
213 		Notifier n = new Notifier();
214 		n.addSendListener(l);
215 		n.notifySendListeners(event);
216 
217 	}// notifySendListeners
218 
219 	public static void notifyReceiveListeners(Class klass, String file,
220 			String modem, String commid, String message, String cidnumber,
221 			String cidname) throws InstantiationException,
222 			IllegalAccessException {
223 		// verify options
224 		//
225 		if ((file == null) || ("".equals(file))) {
226 			usage(System.err);
227 			System.exit(-1);
228 		}
229 		if ((modem == null) || ("".equals(modem))) {
230 			usage(System.err);
231 			System.exit(-1);
232 		}
233 		if ((commid == null) || ("".equals(commid))) {
234 			usage(System.err);
235 			System.exit(-1);
236 		}
237 		if (klass == null) {
238 			usage(System.err);
239 			System.exit(-1);
240 		}
241 
242 		// Notify Listeners
243 		//
244 
245 		ReceiveEvent event = new ReceiveEvent();
246 		event.setFilename(file);
247 		event.setModem(modem);
248 		event.setCommunicationIdentifier(commid);
249 		event.setMessage(message);
250 		event.setCidNumber(cidnumber);
251 		event.setCidName(cidname);
252 
253 		ReceiveListener l = (ReceiveListener) klass.newInstance();
254 
255 		Notifier n = new Notifier();
256 		n.addReceiveListener(l);
257 		n.notifyReceiveListeners(event);
258 
259 	}// notifyReceiveListeners
260 
261 	/***
262 	 * prints program usage
263 	 */
264 	public static void usage(PrintStream out) {
265 		String msg = "usage: Notifier -f <qfile> -r <reason> -t <time> -c <id> [ -n <next-attempt> ]\n"
266 				+ "Notify listeners of Job state changes.\n\n"
267 				+ "Options:\n"
268 				+ "\t-f <file>\tQueue filename\n"
269 				+ "\t-r <reason>\tReason for state change, see notify(8C)\n"
270 				+ "\t-t <time>\tJob elapsed time\n"
271 				+ "\t-c <class>\tJava class name of Listener to notify\n"
272 				+ "\t-j <jobid>\tThe HylaFAX JOBID\n"
273 				+ "\t-n <next-attempt>\tETA for next attempt (if <reason> is 'requeued')\n\n";
274 		out.print(msg);
275 	}// usage
276 
277 }// Notifier
278 // Notifier.java