KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * OPMHandler.java
3  *
4  * Created on March 3, 2003, 7:18 AM
5  */

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

18 public class OPMHandler extends com.quikj.server.framework.AceThread
19 {
20     private static OPMHandler instance = null;
21     private Connection connection;
22     
23     
24     /** Creates a new instance of OPMHandler */
25     public OPMHandler(Connection connection)
26     throws IOException
27     {
28         super("WebTalkOPMHandler");
29         
30         this.connection = connection;
31         instance = this;
32     }
33     
34     public static OPMHandler getInstance()
35     {
36         return instance;
37     }
38     
39     public void dispose()
40     {
41         // interrupt the wait (kill this thread)
42
interruptWait(AceSignalMessage.SIGNAL_TERM, "disposed");
43         
44         try
45         {
46             connection.close();
47         }
48         catch (SQLException ex)
49         {
50             //ignore
51
;
52         }
53         
54         super.dispose();
55         instance = null;
56     }
57     
58     public boolean executeSQL(String JavaDoc sql)
59     {
60         return sendMessage(new SQLCommand(sql));
61     }
62     
63     public void run()
64     {
65         while (true)
66         {
67             AceMessageInterface message = waitMessage();
68             if (message == null)
69             {
70                 // print error message
71
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
72                 getName()
73                 + "- OPMHandler.run() -- A null message was received while waiting for a message - "
74                 + getErrorMessage());
75                 
76                 break;
77             }
78             
79             if ((message instanceof AceSignalMessage) == true)
80             {
81                 // A signal message is received
82

83                 // print informational message
84
AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
85                 getName()
86                 + " - OPMHandler.run() -- A signal "
87                 + ((AceSignalMessage)message).getSignalId()
88                 + " is received : "
89                 + ((AceSignalMessage)message).getMessage());
90                 break;
91             }
92             else if ((message instanceof OPMHandler.SQLCommand) == true)
93             {
94                 processSQLCommand(((OPMHandler.SQLCommand)message).getSQL());
95             }
96             else
97             {
98                 AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
99                 getName()
100                 + " - OPMHandler.run() -- An unknown message of type "
101                 + message.messageType()
102                 + " is received");
103             }
104         }
105         
106         dispose();
107     }
108     
109     private void processSQLCommand(String JavaDoc cmd)
110     {
111         try
112         {
113             Statement s = connection.createStatement();
114             int rowcount = s.executeUpdate(cmd);
115             
116             if (rowcount < 1) // not an accurate check
117
{
118                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
119                 getName()
120                 + "- OPMHandler.processSQLCommand() -- Couldn't store OPM record(s) in the database, SQL command = " +
121                 cmd);
122             }
123             
124             s.close();
125         }
126         catch (SQLException ex)
127         {
128             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
129             getName()
130             + "- OPMHandler.processSQLCommand() -- SQLException encountered, error = "
131             + ex.getMessage() + ", SQL command = " + cmd);
132         }
133     }
134     
135     class SQLCommand implements AceMessageInterface
136     {
137         private String JavaDoc sql;
138         
139         public SQLCommand(String JavaDoc sql)
140         {
141             this.sql = sql;
142         }
143         
144         public String JavaDoc messageType()
145         {
146             return "WebTalkOPMSQLCommand";
147         }
148         
149         public String JavaDoc getSQL()
150         {
151             return sql;
152         }
153     }
154 }
155
Popular Tags