KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > FifoQueue


1 /* ----- BEGIN LICENSE BLOCK -----
2  * Version: MPL 1.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the DataShare server.
15  *
16  * The Initial Developer of the Original Code is
17  * Ball Aerospace & Technologies Corp, Fairborn, Ohio
18  * Portions created by the Initial Developer are Copyright (C) 2001
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s): Charles Wood <cwood@ball.com>
22  *
23  * ----- END LICENSE BLOCK ----- */

24 /* RCS $Id: FifoQueue.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
25  * $Log: FifoQueue.java,v $
26  * Revision 1.2 2002/01/29 20:50:17 lizellaman
27  * Added LoggingInterface, modified the PropertiesInterface implementation
28  *
29  * Revision 1.1.1.1 2001/10/23 13:37:19 lizellaman
30  * initial sourceforge release
31  *
32  */

33
34 package org.datashare;
35
36 import java.util.LinkedList JavaDoc;
37
38 /**
39  * this class is used where we have serializable objects that we want to pass between two
40  * threads in a FIFO/QUEUE fashion, one thread will source the data, and another thread to sink
41  * the data. NOTE!!!! The read() method will block if no object is available to read.
42  * Note that this class can be used to provide the sink thread to feed a consumer by
43  * calling setConsumer() to give it a class/method to call when data is available.
44  */

45 public class FifoQueue implements Runnable JavaDoc
46    {
47    //private int bufferedObjectCount = 0;
48
private LinkedList JavaDoc list = new LinkedList JavaDoc();
49    private boolean closed = false; // set to true if we close this pipe
50

51    //private int count = 0;
52
private FifoConsumer consumer = null;
53    private Thread JavaDoc myThread = null;
54
55    /**
56     * constructor
57     */

58    public FifoQueue()
59       {
60       }
61
62    /**
63     * used only if a setConsumer() call is made, calls the newFifoDataAvailable method of
64     * the consumer (set in setConsumer) whenever objects are put into the FIFO via the write method.
65     */

66    public void
67    run()
68       {
69       while(!closed)
70          {
71          try{
72             Object JavaDoc object = read();
73             if(consumer != null)
74                consumer.newFifoDataAvailable(object);
75             }
76          catch(Exception JavaDoc e)
77             {
78             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
79                SessionUtilities.getLoggingInterface().GENERALSTATUS,
80                "Problems in FifoQueue, thread named " + this.myThread.getName());
81             e.printStackTrace();
82             }
83          }
84       }
85
86    /**
87     * used only if this class is to be used as a Thread to wait for data that will then
88     * be supplied to this consumer as it becomes available, will start thread that supplies
89     * the consumer with objects as they are put into the FIFO with the write method.
90     */

91    public void
92    setConsumer(FifoConsumer consumer)
93       {
94       if(this.consumer == null)
95          {
96          this.consumer = consumer;
97          myThread = new Thread JavaDoc(this, "FifoQueueThread-for-"+consumer);
98          myThread.setDaemon(true);
99          myThread.start();
100          }
101       else
102          {
103          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
104             SessionUtilities.getLoggingInterface().GENERALSTATUS,
105             "FifoQueue.setConsumer, queue already has a consumer-> " + this.consumer);
106          Thread.dumpStack();
107          }
108       }
109
110    /**
111     * used to put the Objects into a buffer
112     * @param object Object to be added to our FIFO
113     */

114    public void write(Object JavaDoc object)
115       {
116       //SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
117
// SessionUtilities.getLoggingInterface().GENERALSTATUS,
118
// "FifoQueue writing an object for thread " + Thread.currentThread().getName());
119
synchronized(list)
120          {
121          list.add(object);
122          list.notifyAll();
123          }
124       }
125
126    /**
127     * Returns the oldest object in our FIFO, or blocks until an object is available, may issue null object when FifoQueue is closed
128     */

129    public Object JavaDoc read()
130       {
131       Object JavaDoc object = null;
132       if(!closed)
133          {
134          synchronized(list)
135             {
136             // if list is empty, wait for notify
137
if(list.isEmpty())
138                {
139                try{
140                   //SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
141
// SessionUtilities.getLoggingInterface().GENERALSTATUS,
142
// "FifoQueue waiting for an object to read by thread " + Thread.currentThread().getName());
143
list.wait();
144                   }
145                catch(InterruptedException JavaDoc ie)
146                   {
147                   SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING,
148                      SessionUtilities.getLoggingInterface().GENERALSTATUS,
149                      "FifoQueue had interrupted exception...");
150                   ie.printStackTrace();
151                   }
152                }
153             try{
154                if(!closed) // if closed, we want to issue a null object
155
object = list.remove(0); // remove first element
156
}
157             catch(Exception JavaDoc e)
158                {
159                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
160                   SessionUtilities.getLoggingInterface().GENERALSTATUS,
161                   "Problems getting object from pipe for reader " + Thread.currentThread().getName());
162                e.printStackTrace();
163                object = null;
164                }
165             }
166          }
167
168       if(object == null)
169          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
170             SessionUtilities.getLoggingInterface().GENERALSTATUS,
171             "FifoQueue issuing null object to thread " + Thread.currentThread().getName() +" (to close queue)");
172
173       return object;
174       }
175
176    /**
177     * used to return the number of objects currently in the buffer
178     */

179    public int
180    size()
181       {
182       return list.size();
183       }
184
185    /**
186     * used when the pipe is no longer needed, will cause a blocking read to release
187     * with a null object returned, will also stop the thread if a consumer was set
188     * with setConsumer().
189     */

190    public void
191    close()
192       {
193       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
194          SessionUtilities.getLoggingInterface().GENERALSTATUS,
195          "Closing FifoQueue");
196       closed = true;
197       consumer = null;
198       reset();
199       synchronized(list)
200          {
201          list.notifyAll(); // should release read and may issue null object
202
}
203       }
204
205    /**
206     * empties the FIFO
207     */

208    public void
209    reset()
210       {
211       synchronized(list)
212          {
213          if(!list.isEmpty())
214             {
215             list.clear();
216             }
217          }
218       }
219
220    }
221
Popular Tags