com/scalagent/kjoram/Driver.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - ScalAgent Distributed Technologies
00004  * Copyright (C) 1996 - Dyade
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA.
00020  *
00021  * Initial developer(s): Frederic Maistre (INRIA)
00022  * Contributor(s): Nicolas Tachker (ScalAgent)
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         // Waiting for an asynchronous delivery:
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         // Catching an InterruptedException:
00091         catch (InterruptedException exc) {
00092           if (JoramTracing.dbgClient)
00093             JoramTracing.log(JoramTracing.WARN,"Driver: caught an" +
00094                              " InterruptedException: " + exc);
00095           continue;
00096         }
00097         // Catching an IOException:
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             // Passing the asynchronous exception to the connection:
00109             cnx.onException(jmsExc);
00110 
00111             // Interrupting the synchronous requesters:
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             // Closing the connection:
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         // Passing the reply to the connection:
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       // Passing the asynchronous exception to the connection:
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 }

Generated on Tue Sep 16 16:14:23 2008 for joram by  doxygen 1.5.0