View Javadoc

1   // SampleListener.java - gnu.hylafax implementation of the faxstat utility
2   // $Id: SampleListener.java,v 1.4 2007/02/21 00:07:49 sjardine Exp $
3   //
4   // - basically 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  // TODO make this class more flexible
28  //  
29  
30  package gnu.hylafax.util;
31  
32  
33  import gnu.hylafax.job.ReceiveEvent;
34  import gnu.hylafax.job.ReceiveListener;
35  import gnu.hylafax.job.SendEvent;
36  import gnu.hylafax.job.SendListener;
37  
38  import java.sql.Connection;
39  import java.sql.DriverManager;
40  import java.sql.PreparedStatement;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /***
46   * This class implements an example fax job listener.
47   * 
48   * @author $Author: sjardine $
49   * @version $Id: SampleListener.java,v 1.4 2007/02/21 00:07:49 sjardine Exp $
50   * @see gnu.hylafax.job.SendNotifier
51   * @see gnu.hylafax.job.SendEvent
52   * @see gnu.hylafax.job.ReceiveNotifier
53   * @see gnu.hylafax.job.ReceiveEvent
54   * @see gnu.hylafax.util.Notifier
55   */
56  public class SampleListener implements SendListener, ReceiveListener {
57      
58      private final static Log log = LogFactory.getLog(SampleListener.class);
59  
60  	public final static String KEY_DBUSER = "notifier.db.user";
61  
62  	public final static String KEY_DBPASSWORD = "notifier.db.password";
63  
64  	public final static String KEY_DBDRIVER = "notifier.db.driver";
65  
66  	public final static String KEY_DBURI = "notifier.db.uri";
67  
68  	String DB_USER = System.getProperties().getProperty(KEY_DBUSER);
69  
70  	String DB_PASSWORD = System.getProperties().getProperty(KEY_DBPASSWORD);
71  
72  	String DB_URI = System.getProperties().getProperty(KEY_DBURI);
73  
74  	String DB_CLASS = System.getProperties().getProperty(KEY_DBDRIVER);
75  
76  	/***
77  	 * 
78  	 * 
79  	 */
80  	public void onSendEvent(SendEvent event) {
81  		try {
82  
83  			Class.forName(DB_CLASS);
84  			Connection connection = DriverManager.getConnection(DB_URI,
85  					DB_USER, DB_PASSWORD);
86  			String sql = "insert into send (reason,filename,jobid,jobtime,next) values (?,?,?,?,?)";
87  			PreparedStatement stmt = connection.prepareStatement(sql);
88  			stmt.setString(1, event.getReason());
89  			stmt.setString(2, event.getFilename());
90  			stmt.setLong(3, event.getJobId());
91  			stmt.setLong(4, event.getElapsedTime());
92  			stmt.setString(5, event.getNextAttempt());
93  			stmt.execute();
94  			stmt.close();
95  			connection.close();
96  
97  		} catch (Exception e) {
98  			log.error(e.getMessage(), e);
99  		}
100 	}// onSendEvent
101 
102 	/***
103 	 * This method is called when a fax-received event occurs.
104 	 */
105 	public void onReceiveEvent(ReceiveEvent event) {
106 		try {
107 
108 			Class.forName(DB_CLASS);
109 			Connection connection = DriverManager.getConnection(DB_URI,
110 					DB_USER, DB_PASSWORD);
111 			String sql = "insert into receive (filename,modem,commid,message,cidnumber,cidname) values (?,?,?,?,?,?)";
112 			PreparedStatement stmt = connection.prepareStatement(sql);
113 			stmt.setString(1, event.getFilename());
114 			stmt.setString(2, event.getModem());
115 			stmt.setString(3, event.getCommunicationIdentifier());
116 			stmt.setString(4, event.getMessage());
117 			stmt.setString(5, event.getCidNumber());
118 			stmt.setString(6, event.getCidName());
119 			stmt.execute();
120 			stmt.close();
121 			connection.close();
122 
123 		} catch (Exception e) {
124 			log.error(e.getMessage(), e);
125 		}
126 	}// onReceiveEvent
127 
128 }// SampleListener
129 // SampleListener.java