1 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 18 public class OPMHandler extends com.quikj.server.framework.AceThread 19 { 20 private static OPMHandler instance = null; 21 private Connection connection; 22 23 24 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 interruptWait(AceSignalMessage.SIGNAL_TERM, "disposed"); 43 44 try 45 { 46 connection.close(); 47 } 48 catch (SQLException ex) 49 { 50 ; 52 } 53 54 super.dispose(); 55 instance = null; 56 } 57 58 public boolean executeSQL(String 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 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 83 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 cmd) 110 { 111 try 112 { 113 Statement s = connection.createStatement(); 114 int rowcount = s.executeUpdate(cmd); 115 116 if (rowcount < 1) { 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 sql; 138 139 public SQLCommand(String sql) 140 { 141 this.sql = sql; 142 } 143 144 public String messageType() 145 { 146 return "WebTalkOPMSQLCommand"; 147 } 148 149 public String getSQL() 150 { 151 return sql; 152 } 153 } 154 } 155 | Popular Tags |