KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > Driver


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram;
25
26 import com.scalagent.kjoram.jms.AbstractJmsReply;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InterruptedIOException JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 import com.scalagent.kjoram.excepts.IllegalStateException;
33 import com.scalagent.kjoram.excepts.JMSException;
34
35 /**
36  * Each <code>Connection</code> holds a <code>Driver</code> daemon for
37  * listening to asynchronous replies coming from the connected server.
38  *
39  */

40 public abstract class Driver extends com.scalagent.kjoram.util.Daemon
41 {
42   /** The connection the driver belongs to. */
43   private Connection cnx;
44
45   /** <code>true</code> if the driver is stopping. */
46   boolean stopping = false;
47
48   
49   /**
50    * Constructs a <code>Driver</code> daemon.
51    *
52    * @param cnx The connection the driver belongs to.
53    */

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   /** String view of a <code>Driver</code> instance. */
64   public String JavaDoc toString()
65   {
66     return "Driver:" + cnx.toString();
67   }
68
69
70   /** The driver's listening loop. */
71   public void run()
72   {
73     AbstractJmsReply delivery = null;
74
75     try {
76       while (running) {
77         canStop = true;
78   
79         // Waiting for an asynchronous delivery:
80
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         // Catching an InterruptedException:
91
catch (InterruptedException JavaDoc exc) {
92           if (JoramTracing.dbgClient)
93             JoramTracing.log(JoramTracing.WARN,"Driver: caught an" +
94                              " InterruptedException: " + exc);
95           continue;
96         }
97         // Catching an IOException:
98
catch (IOException JavaDoc exc) {
99           if (! cnx.closing) {
100             
101             stopping = true;
102
103             IllegalStateException JavaDoc jmsExc =
104               new IllegalStateException JavaDoc("The connection is broken,"
105                                         + " the driver stops.");
106             jmsExc.setLinkedException(exc);
107
108             // Passing the asynchronous exception to the connection:
109
cnx.onException(jmsExc);
110
111             // Interrupting the synchronous requesters:
112
if (JoramTracing.dbgClient)
113               JoramTracing.log(JoramTracing.DEBUG, this + "interrupts synchronous"
114                                + " requesters.");
115
116             Integer JavaDoc reqId;
117             Object JavaDoc obj;
118             for (Enumeration JavaDoc e = cnx.requestsTable.keys();
119                  e.hasMoreElements();) {
120               reqId = (Integer JavaDoc) e.nextElement();
121               obj = cnx.requestsTable.remove(reqId);
122               if (obj instanceof Lock) {
123                 synchronized(obj) {
124                   obj.notify();
125                 }
126               }
127             }
128
129             // Closing the connection:
130
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         // Passing the reply to the connection:
141
canStop = false;
142         cnx.distribute(delivery);
143       }
144     }
145     catch (Exception JavaDoc exc) {
146       JMSException jmsExc = new JMSException("Exception while getting data"
147                                              + " from the server.");
148       jmsExc.setLinkedException(exc);
149   
150       // Passing the asynchronous exception to the connection:
151
cnx.onException(jmsExc);
152     }
153     finally {
154       finish();
155     }
156   }
157
158   /**
159    * Returns an <code>AbstractJmsReply</code> delivered by the connected
160    * server.
161    *
162    * @exception Exception If a problem occurs when getting the delivery.
163    */

164   protected abstract AbstractJmsReply getDelivery() throws Exception JavaDoc;
165
166   /** Shuts the driver down. */
167   public abstract void shutdown();
168
169   /** Releases the driver's resources. */
170   public void close()
171   {
172     if (JoramTracing.dbgClient)
173       JoramTracing.log(JoramTracing.DEBUG, this + ": closed.");
174   }
175 }
176
Popular Tags