1 24 package com.scalagent.kjoram; 25 26 import com.scalagent.kjoram.jms.AbstractJmsReply; 27 28 import java.io.IOException ; 29 import java.io.InterruptedIOException ; 30 import java.util.Enumeration ; 31 32 import com.scalagent.kjoram.excepts.IllegalStateException; 33 import com.scalagent.kjoram.excepts.JMSException; 34 35 40 public abstract class Driver extends com.scalagent.kjoram.util.Daemon 41 { 42 43 private Connection cnx; 44 45 46 boolean stopping = false; 47 48 49 54 protected Driver(Connection cnx) 55 { 56 super(cnx.toString()); 57 this.cnx = cnx; 58 59 if (JoramTracing.dbgClient) 60 JoramTracing.log(JoramTracing.DEBUG, this + ": created."); 61 } 62 63 64 public String toString() 65 { 66 return "Driver:" + cnx.toString(); 67 } 68 69 70 71 public void run() 72 { 73 AbstractJmsReply delivery = null; 74 75 try { 76 while (running) { 77 canStop = true; 78 79 try { 81 if (JoramTracing.dbgClient) 82 JoramTracing.log(JoramTracing.DEBUG, "Driver: waiting..."); 83 delivery = getDelivery(); 84 if (JoramTracing.dbgClient) 85 JoramTracing.log(JoramTracing.DEBUG,"Driver: got a delivery!"); 86 if (delivery == null) { 87 continue; 88 } 89 } 90 catch (InterruptedException exc) { 92 if (JoramTracing.dbgClient) 93 JoramTracing.log(JoramTracing.WARN,"Driver: caught an" + 94 " InterruptedException: " + exc); 95 continue; 96 } 97 catch (IOException exc) { 99 if (! cnx.closing) { 100 101 stopping = true; 102 103 IllegalStateException jmsExc = 104 new IllegalStateException ("The connection is broken," 105 + " the driver stops."); 106 jmsExc.setLinkedException(exc); 107 108 cnx.onException(jmsExc); 110 111 if (JoramTracing.dbgClient) 113 JoramTracing.log(JoramTracing.DEBUG, this + "interrupts synchronous" 114 + " requesters."); 115 116 Integer reqId; 117 Object obj; 118 for (Enumeration e = cnx.requestsTable.keys(); 119 e.hasMoreElements();) { 120 reqId = (Integer ) e.nextElement(); 121 obj = cnx.requestsTable.remove(reqId); 122 if (obj instanceof Lock) { 123 synchronized(obj) { 124 obj.notify(); 125 } 126 } 127 } 128 129 if (JoramTracing.dbgClient) 131 JoramTracing.log(JoramTracing.DEBUG,this + ": closes the connection."); 132 try { 133 cnx.close(); 134 } 135 catch (JMSException jExc) {} 136 } 137 canStop = true; 138 break; 139 } 140 canStop = false; 142 cnx.distribute(delivery); 143 } 144 } 145 catch (Exception exc) { 146 JMSException jmsExc = new JMSException("Exception while getting data" 147 + " from the server."); 148 jmsExc.setLinkedException(exc); 149 150 cnx.onException(jmsExc); 152 } 153 finally { 154 finish(); 155 } 156 } 157 158 164 protected abstract AbstractJmsReply getDelivery() throws Exception ; 165 166 167 public abstract void shutdown(); 168 169 170 public void close() 171 { 172 if (JoramTracing.dbgClient) 173 JoramTracing.log(JoramTracing.DEBUG, this + ": closed."); 174 } 175 } 176 | Popular Tags |