KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.quikj.application.web.talk.plugin;
2
3 import com.quikj.server.framework.*;
4 import com.quikj.server.web.*;
5
6 import java.sql.*;
7 import java.io.*;
8 import java.util.*;
9
10 public class TalkPluginApp implements PluginAppInterface
11 {
12     public TalkPluginApp()
13     {
14         instance = this;
15     }
16     
17     public static TalkPluginApp Instance()
18     {
19         return instance;
20     }
21     
22     public boolean applicationInit(PluginParameters params)
23     {
24         if (AceLicenseManager.getInstance().licenseFeature("talk-plugin-application") == false)
25         {
26             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
27             "TalkPluginApp.applicationInit() -- Error obtaining license: "
28             + AceLicenseManager.getInstance().getErrorMessage());
29             return false;
30         }
31         
32         sqlDatabase = params.findKey("sql-database");
33         if (sqlDatabase == null) // not found
34
{
35             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
36             "TalkPluginApp.applicationInit() -- database name not specified");
37             return false;
38         }
39         
40         connection = JDBCConnection.getInstance().getNewConnection(sqlDatabase);
41         if (connection == null)
42         {
43             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
44             "TalkPluginApp.applicationInit() -- Connection to the database server failed. "
45             + JDBCConnection.getErrorMessage());
46             
47             return false;
48         }
49         
50         String JavaDoc cdr_required_s = params.findKey("cdr-required");
51         if (cdr_required_s != null)
52         {
53             if (cdr_required_s.equals("yes") == true)
54             {
55                 cdrRequired = true;
56                 opmRequired = true;
57             }
58             else if (cdr_required_s.equals("no") == true)
59             {
60                 cdrRequired = false;
61                 opmRequired = false;
62             }
63             else
64             {
65                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
66                 "TalkPluginApp.applicationInit() -- cdr-required contains invalid value.");
67                 
68                 cleanup();
69                 return false;
70             }
71         }
72         
73         if (cdrRequired == true)
74         {
75             String JavaDoc backup_cdr_dir = params.findKey("backup-cdr-dir");
76             if (backup_cdr_dir == null)
77             {
78                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
79                 "TalkPluginApp.applicationInit() -- backup CDR directory not specified");
80                 
81                 cleanup();
82                 return false;
83             }
84             
85             String JavaDoc backup_cdr_file = params.findKey("backup-cdr-file");
86             if (backup_cdr_dir == null)
87             {
88                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
89                 "TalkPluginApp.applicationInit() -- backup CDR file not specified");
90                 
91                 cleanup();
92                 return false;
93             }
94             
95             // establish another database connection for CDRs
96
cdrConnection = JDBCConnection.getInstance().getNewConnection(sqlDatabase);
97             if (cdrConnection == null)
98             {
99                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
100                 "TalkPluginApp.applicationInit() -- Connection to the database server failed. "
101                 + JDBCConnection.getErrorMessage());
102                 
103                 cleanup();
104                 return false;
105             }
106             
107             try
108             {
109                 cdrHandler = new CDRHandler(cdrConnection,
110                 backup_cdr_dir, backup_cdr_file);
111                 cdrHandler.start();
112             }
113             catch (IOException ex)
114             {
115                 AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
116                 "TalkPluginApp.applicationInit() -- CDR handler returned IO Exception: "
117                 + ex.getMessage()
118                 + " probably because the specified backup directory does not exist. "
119                 + "No CDRs will be recorded");
120                 
121                 try
122                 {
123                     cdrConnection.close();
124                 }
125                 catch (SQLException ex1)
126                 {
127                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
128                     "TalkPluginApp.applicationInit() -- Error closing CDR database connection: "
129                     + ex1.getMessage());
130                 }
131             }
132         }
133         
134         if (opmRequired == true)
135         {
136             // establish another database connection for OPMs
137
opmConnection = JDBCConnection.getInstance().getNewConnection(sqlDatabase);
138             if (opmConnection == null)
139             {
140                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
141                 "TalkPluginApp.applicationInit() -- Connection to the OPM database server failed. "
142                 + JDBCConnection.getErrorMessage());
143                 
144                 cleanup();
145                 return false;
146             }
147             
148             try
149             {
150                 opmHandler = new OPMHandler(opmConnection);
151                 opmHandler.start();
152             }
153             catch (IOException ex)
154             {
155                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
156                 "TalkPluginApp.applicationInit() -- OPM handler returned IO Exception: "
157                 + ex.getMessage()
158                 + ". No OPMs will be recorded");
159                 
160                 try
161                 {
162                     opmConnection.close();
163                 }
164                 catch (SQLException ex1)
165                 {
166                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
167                     "TalkPluginApp.applicationInit() -- Error closing OPM database connection: "
168                     + ex1.getMessage());
169                 }
170             }
171         }
172         
173         mailConfigDir = params.findKey("mailcfg-dir");
174         if (mailConfigDir != null)
175         {
176             mailConfigFile = params.findKey("mailcfg-file");
177             
178             if (mailConfigFile != null)
179             {
180                 try
181                 {
182                     mailService = new AceMailService(mailConfigDir, mailConfigFile);
183                     mailService.start();
184                 }
185                 catch (IOException ex)
186                 {
187                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
188                     "TalkPluginApp.applicationInit() -- An IO error occured while trying to create the Mail Service thread - "
189                     + ex.getMessage());
190                     
191                     cleanup();
192                     return false;
193                 }
194                 catch (AceException ex1)
195                 {
196                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
197                     "TalkPluginApp.applicationInit() -- An error occured while trying to create the Mail Service thread - "
198                     + ex1.getMessage());
199                     
200                     cleanup();
201                     return false;
202                 }
203             }
204         }
205                 
206
207         // the database connection has been opened, finally start the ServiceController thread
208
try
209         {
210             controller = new ServiceController(connection);
211             
212             controller.start();
213         }
214         catch (IOException ex)
215         {
216             // print error message
217
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
218             "TalkPluginApp.applicationInit() -- An IO error occured while trying to create the Service Controller thread - "
219             + ex.getMessage());
220             
221             cleanup();
222             return false;
223         }
224         catch (AceException ex)
225         {
226             // print error message
227
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
228             "TalkPluginApp.applicationInit() -- An error occured while trying to create the Service Controller thread - "
229             + ex.getMessage());
230             
231             cleanup();
232             return false;
233         }
234         
235         // if there is an access list, initialize it
236
int plugin_size = params.numParameters();
237         
238         for (int i = 0; i < plugin_size; i++)
239         {
240             if (params.keyAt(i).equals("registered-user-access") == true)
241             {
242                 String JavaDoc value = params.valueAt(i);
243                 
244                 StringTokenizer tokens = new StringTokenizer(value);
245                 if (tokens.countTokens() < 2)
246                 {
247                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
248                     "TalkPluginApp.applicationInit() -- Invalid value for registered-user-access: "
249                     + value + ", ignoring entry");
250                     continue;
251                 }
252                 
253                 boolean created = false;
254                 if (accessInfo == null) // one does not exist
255
{
256                     accessInfo = new AceNetworkAccess();
257                     created = true;
258                 }
259                 
260                 if (accessInfo.add(tokens.nextToken(), tokens.nextToken()) == false)
261                 {
262                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
263                     "TalkPluginApp.applicationInit() -- Invalid IP addresses specified for registered-user-access: "
264                     + value + ", ignoring entry");
265                     
266                     if (created == true) // just created
267
{
268                         accessInfo = null;
269                     }
270                 }
271             }
272         }
273         
274         return true;
275     }
276     
277     public String JavaDoc getApplicationName()
278     {
279         return "TALK";
280     }
281     
282     public String JavaDoc getApplicationDescription()
283     {
284         return "Web-based instant messaging application";
285     }
286     
287     public PluginAppClientInterface newInstance()
288     {
289         return new TalkPluginAppClient();
290     }
291     
292     public void dispose()
293     {
294         cleanup();
295     }
296     
297     public void cleanup()
298     {
299         if (controller != null)
300         {
301             controller.dispose();
302             controller = null;
303         }
304         
305         if (cdrHandler != null)
306         {
307             cdrHandler.dispose();
308             cdrHandler = null;
309             cdrConnection = null;
310         }
311         
312         if (cdrConnection != null)
313         {
314             try
315             {
316                 cdrConnection.close();
317             }
318             catch (SQLException ex)
319             {
320             }
321             
322             cdrConnection = null;
323         }
324         
325         if (opmHandler != null)
326         {
327             opmHandler.dispose();
328             opmHandler = null;
329             opmConnection = null;
330         }
331         
332         if (opmConnection != null)
333         {
334             try
335             {
336                 opmConnection.close();
337             }
338             catch (SQLException ex)
339             {
340             }
341             
342             opmConnection = null;
343         }
344         
345         if (mailService != null)
346         {
347             mailService.dispose();
348             mailService = null;
349         }
350         
351         // close the database connection
352
if (connection != null)
353         {
354             try
355             {
356                 connection.close();
357             }
358             catch (SQLException ex)
359             {
360             }
361             
362             connection = null;
363         }
364         
365         instance = null;
366         
367     }
368     
369     
370     public Connection getConnection()
371     {
372         return connection;
373     }
374     
375     public String JavaDoc getDatabaseName()
376     {
377         return sqlDatabase;
378     }
379     
380     /** Getter for property cdrRequired.
381      * @return Value of property cdrRequired.
382      */

383     public boolean isCdrRequired()
384     {
385         return cdrRequired;
386     }
387     
388     /** Getter for property cdrConnection.
389      * @return Value of property cdrConnection.
390      */

391     public java.sql.Connection JavaDoc getCdrConnection()
392     {
393         return cdrConnection;
394     }
395     
396     /** Getter for property opmRequired.
397      * @return Value of property opmRequired.
398      */

399     public boolean isOpmRequired()
400     {
401         return opmRequired;
402     }
403     
404     /** Getter for property opmConnection.
405      * @return Value of property opmConnection.
406      */

407     public java.sql.Connection JavaDoc getOpmConnection()
408     {
409         return opmConnection;
410     }
411     
412     /** Getter for property accessInfo.
413      * @return Value of property accessInfo.
414      */

415     public com.quikj.server.framework.AceNetworkAccess getAccessInfo()
416     {
417         return accessInfo;
418     }
419     
420     
421     private static TalkPluginApp instance = null;
422     
423     private String JavaDoc sqlDatabase = null;
424
425     private String JavaDoc mailConfigDir = null;
426     private String JavaDoc mailConfigFile = null;
427     private String JavaDoc cannedElementConfigDir = null;
428     private String JavaDoc cannedElementConfigFile = null;
429     
430     private Connection connection = null;
431     private Connection cdrConnection = null;
432     private Connection opmConnection = null;
433     private ServiceController controller = null;
434     private CDRHandler cdrHandler = null;
435     private OPMHandler opmHandler = null;
436     private AceMailService mailService = null;
437     
438     private boolean cdrRequired = false;
439     private boolean opmRequired = false;
440     private AceNetworkAccess accessInfo = null;
441 }
442
443
444
445
446
447
448
Popular Tags