00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package com.scalagent.kjoram;
00025
00026 import com.scalagent.kjoram.jms.AbstractJmsReply;
00027
00028 import java.io.IOException;
00029 import java.io.InterruptedIOException;
00030 import java.util.Enumeration;
00031
00032 import com.scalagent.kjoram.excepts.IllegalStateException;
00033 import com.scalagent.kjoram.excepts.JMSException;
00034
00040 public abstract class Driver extends com.scalagent.kjoram.util.Daemon
00041 {
00043 private Connection cnx;
00044
00046 boolean stopping = false;
00047
00048
00054 protected Driver(Connection cnx)
00055 {
00056 super(cnx.toString());
00057 this.cnx = cnx;
00058
00059 if (JoramTracing.dbgClient)
00060 JoramTracing.log(JoramTracing.DEBUG, this + ": created.");
00061 }
00062
00064 public String toString()
00065 {
00066 return "Driver:" + cnx.toString();
00067 }
00068
00069
00071 public void run()
00072 {
00073 AbstractJmsReply delivery = null;
00074
00075 try {
00076 while (running) {
00077 canStop = true;
00078
00079
00080 try {
00081 if (JoramTracing.dbgClient)
00082 JoramTracing.log(JoramTracing.DEBUG, "Driver: waiting...");
00083 delivery = getDelivery();
00084 if (JoramTracing.dbgClient)
00085 JoramTracing.log(JoramTracing.DEBUG,"Driver: got a delivery!");
00086 if (delivery == null) {
00087 continue;
00088 }
00089 }
00090
00091 catch (InterruptedException exc) {
00092 if (JoramTracing.dbgClient)
00093 JoramTracing.log(JoramTracing.WARN,"Driver: caught an" +
00094 " InterruptedException: " + exc);
00095 continue;
00096 }
00097
00098 catch (IOException exc) {
00099 if (! cnx.closing) {
00100
00101 stopping = true;
00102
00103 IllegalStateException jmsExc =
00104 new IllegalStateException("The connection is broken,"
00105 + " the driver stops.");
00106 jmsExc.setLinkedException(exc);
00107
00108
00109 cnx.onException(jmsExc);
00110
00111
00112 if (JoramTracing.dbgClient)
00113 JoramTracing.log(JoramTracing.DEBUG, this + "interrupts synchronous"
00114 + " requesters.");
00115
00116 Integer reqId;
00117 Object obj;
00118 for (Enumeration e = cnx.requestsTable.keys();
00119 e.hasMoreElements();) {
00120 reqId = (Integer) e.nextElement();
00121 obj = cnx.requestsTable.remove(reqId);
00122 if (obj instanceof Lock) {
00123 synchronized(obj) {
00124 obj.notify();
00125 }
00126 }
00127 }
00128
00129
00130 if (JoramTracing.dbgClient)
00131 JoramTracing.log(JoramTracing.DEBUG,this + ": closes the connection.");
00132 try {
00133 cnx.close();
00134 }
00135 catch (JMSException jExc) {}
00136 }
00137 canStop = true;
00138 break;
00139 }
00140
00141 canStop = false;
00142 cnx.distribute(delivery);
00143 }
00144 }
00145 catch (Exception exc) {
00146 JMSException jmsExc = new JMSException("Exception while getting data"
00147 + " from the server.");
00148 jmsExc.setLinkedException(exc);
00149
00150
00151 cnx.onException(jmsExc);
00152 }
00153 finally {
00154 finish();
00155 }
00156 }
00157
00164 protected abstract AbstractJmsReply getDelivery() throws Exception;
00165
00167 public abstract void shutdown();
00168
00170 public void close()
00171 {
00172 if (JoramTracing.dbgClient)
00173 JoramTracing.log(JoramTracing.DEBUG, this + ": closed.");
00174 }
00175 }