1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
146 usage(System.err);
147 System.exit(-1);
148 }
149 }
150
151
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
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 }
169
170 public static void notifySendListeners(Class klass, long jobid,
171 String file, String reason, String time, String nextAttempt)
172 throws InstantiationException, IllegalAccessException {
173
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
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 }
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
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
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 }
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 }
276
277 }
278