KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > plugin > CDRHandler


1 /*
2  * CDRHandler.java
3  *
4  * Created on June 29, 2002, 12:19 PM
5  */

6
7 package com.quikj.application.web.talk.plugin;
8
9 import com.quikj.server.framework.*;
10
11 import java.io.*;
12 import java.util.*;
13 import java.sql.*;
14
15 /**
16  *
17  * @author amit
18  */

19 public class CDRHandler extends com.quikj.server.framework.AceThread
20 {
21     private static CDRHandler instance = null;
22     private Connection connection;
23     private String JavaDoc path;
24     private FileWriter outputFile = null;
25     private int pingTimerId = -1;
26
27     
28     /** Creates a new instance of CDRHandler */
29     public CDRHandler(Connection connection, String JavaDoc dir, String JavaDoc file)
30     throws IOException
31     {
32         super("WebTalkCDRHandler");
33         
34         path = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA,
35         dir, file);
36         
37         outputFile = new FileWriter(path, true);
38         
39         this.connection = connection;
40         instance = this;
41     }
42     
43     public static CDRHandler getInstance()
44     {
45         return instance;
46     }
47     
48     public void dispose()
49     {
50         // interrupt the wait (kill this thread)
51
interruptWait(AceSignalMessage.SIGNAL_TERM, "disposed");
52
53         if (pingTimerId != -1)
54         {
55             try
56             {
57                 AceTimer.Instance().cancelTimer(pingTimerId);
58                 pingTimerId = -1;
59             }
60             catch (IOException ex)
61             {
62                 ;
63             }
64         }
65         
66         if (connection != null)
67         {
68             try
69             {
70                 connection.close();
71                 connection = null;
72             }
73             catch (SQLException ex)
74             {
75             }
76         }
77         
78         if (outputFile != null)
79         {
80             try
81             {
82                 outputFile.close();
83                 outputFile = null;
84             }
85             catch (IOException ex)
86             {
87                 ;
88             }
89         }
90         
91         super.dispose();
92         instance = null;
93     }
94     
95     public boolean sendCDR(CDRInterface cdr)
96     {
97         return sendMessage(new CDRMessage(cdr));
98     }
99     
100     public void run()
101     {
102         pingDatabase();
103         
104         while (true)
105         {
106             AceMessageInterface message = waitMessage();
107             if (message == null)
108             {
109                 // print error message
110
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
111                 getName()
112                 + "- CDRHandler.run() -- A null message was received while waiting for a message - "
113                 + getErrorMessage());
114                 
115                 break;
116             }
117             
118             if ((message instanceof AceSignalMessage) == true)
119             {
120                 // A signal message is received
121

122                 // print informational message
123
AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
124                 getName()
125                 + " - CDRHandler.run() -- A signal "
126                 + ((AceSignalMessage)message).getSignalId()
127                 + " is received : "
128                 + ((AceSignalMessage)message).getMessage());
129                 break;
130             }
131             else if ((message instanceof CDRHandler.CDRMessage) == true)
132             {
133                 processCDR(((CDRHandler.CDRMessage)message).getCDR());
134             }
135             else if ((message instanceof AceTimerMessage) == true)
136             {
137                 pingDatabase();
138             }
139             else
140             {
141                 AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
142                 getName()
143                 + " - CDRHandler.run() -- An unknow message of type "
144                 + message.messageType()
145                 + " is received");
146             }
147         }
148         
149         dispose();
150     }
151     
152     private void processCDR(CDRInterface cdr)
153     {
154         try
155         {
156             try
157             {
158                 PreparedStatement s = cdr.generateSQLCDR();
159                 int rowcount = s.executeUpdate();
160                 
161                 if (rowcount != 1)
162                 {
163                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
164                     getName()
165                     + "- CDRHandler.processCDR() -- Couldn't store CDR record in the database");
166                     
167                     outputFile.write(cdr.generateXMLCDR());
168                     outputFile.flush();
169                 }
170                 
171                 s.close();
172             }
173             catch (SQLException ex)
174             {
175                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
176                 getName()
177                 + "- CDRHandler.processCDR() -- SQLException encountered, error: "
178                 + ex.getMessage());
179                 
180                 Thread.dumpStack();
181                 outputFile.write(cdr.generateXMLCDR());
182                 outputFile.flush();
183             }
184         }
185         catch (IOException ex)
186         {
187             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
188             getName()
189             + "- CDRHandler.processCDR() -- IOException encountered, error: "
190             + ex.getMessage()
191             + " while writing CDR "
192             + cdr.generateXMLCDR());
193         }
194     }
195     
196     private void pingDatabase()
197     {
198         try
199         {
200             Statement s = connection.createStatement();
201             ResultSet r = s.executeQuery("show databases");
202             //System.out.println("sent CDR ping");
203

204             if (r.next() == false)
205             {
206                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
207                 getName()
208                 + "- CDRHandler.pingDatabase() -- error pinging the database");
209             }
210             else
211             {
212                 //System.out.println("CDR ping OK");
213
}
214             
215             r.close();
216             s.close();
217             
218         }
219         catch (SQLException ex)
220         {
221             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
222             getName()
223             + "- CDRHandler.pingDatabase() -- SQLException encountered, error: "
224             + ex.getMessage());
225         }
226         
227         try
228         {
229             pingTimerId = AceTimer.Instance().startTimer(1 * 3600 * 1000, 0L);
230         }
231         catch (IOException ex)
232         {
233         }
234     }
235     
236     public static String JavaDoc getDateString(java.util.Date JavaDoc timestamp)
237     {
238         Calendar cal = Calendar.getInstance();
239         cal.setTime(timestamp);
240         
241         String JavaDoc date_string = cal.get(Calendar.YEAR) + "-"
242         + (cal.get(Calendar.MONTH) + 1) + "-"
243         + cal.get(Calendar.DAY_OF_MONTH) + " "
244         + cal.get(Calendar.HOUR_OF_DAY) + ":"
245         + cal.get(Calendar.MINUTE) + ":"
246         + cal.get(Calendar.SECOND);
247         
248         return date_string;
249     }
250     
251     public Connection getConnection()
252     {
253         return connection;
254     }
255     
256     class CDRMessage implements AceMessageInterface
257     {
258         private CDRInterface cdr;
259         
260         public CDRMessage(CDRInterface cdr)
261         {
262             this.cdr = cdr;
263         }
264         
265         public String JavaDoc messageType()
266         {
267             return "WebTalkCDRMessage";
268         }
269         
270         public CDRInterface getCDR()
271         {
272             return cdr;
273         }
274     }
275 }
276
Popular Tags