KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > EventQueue


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)EventQueue.java 1.8 05/11/22
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail;
29
30 import java.io.*;
31 import java.util.Vector JavaDoc;
32 import javax.mail.event.MailEvent JavaDoc;
33
34 /**
35  * Package private class used by Store & Folder to dispatch events.
36  * This class implements an event queue, and a dispatcher thread that
37  * dequeues and dispatches events from the queue.
38  *
39  * Pieces stolen from sun.misc.Queue.
40  *
41  * @author Bill Shannon
42  */

43 class EventQueue implements Runnable JavaDoc {
44
45     class QueueElement {
46     QueueElement next = null;
47     QueueElement prev = null;
48     MailEvent JavaDoc event = null;
49     Vector JavaDoc vector = null;
50
51     QueueElement(MailEvent JavaDoc event, Vector JavaDoc vector) {
52         this.event = event;
53         this.vector = vector;
54     }
55     }
56
57     private QueueElement head = null;
58     private QueueElement tail = null;
59     private Thread JavaDoc qThread;
60
61     public EventQueue() {
62     qThread = new Thread JavaDoc(this, "JavaMail-EventQueue");
63     qThread.setDaemon(true); // not a user thread
64
qThread.start();
65     }
66
67     /**
68      * Enqueue an event.
69      */

70     public synchronized void enqueue(MailEvent JavaDoc event, Vector JavaDoc vector) {
71     QueueElement newElt = new QueueElement(event, vector);
72
73     if (head == null) {
74         head = newElt;
75         tail = newElt;
76     } else {
77         newElt.next = head;
78         head.prev = newElt;
79         head = newElt;
80     }
81     notify();
82     }
83
84     /**
85      * Dequeue the oldest object on the queue.
86      * Used only by the run() method.
87      *
88      * @return the oldest object on the queue.
89      * @exception java.lang.InterruptedException if another thread has
90      * interrupted this thread.
91      */

92     private synchronized QueueElement dequeue()
93                 throws InterruptedException JavaDoc {
94     while (tail == null)
95         wait();
96     QueueElement elt = tail;
97     tail = elt.prev;
98     if (tail == null) {
99         head = null;
100     } else {
101         tail.next = null;
102     }
103     elt.prev = elt.next = null;
104     return elt;
105     }
106
107     /**
108      * Pull events off the queue and dispatch them.
109      */

110     public void run() {
111     QueueElement qe;
112
113     try {
114         loop:
115         while ((qe = dequeue()) != null) {
116         MailEvent JavaDoc e = qe.event;
117         Vector JavaDoc v = qe.vector;
118
119         for (int i = 0; i < v.size(); i++)
120             try {
121             e.dispatch(v.elementAt(i));
122             } catch (Throwable JavaDoc t) {
123             if (t instanceof InterruptedException JavaDoc)
124                 break loop;
125             // ignore anything else thrown by the listener
126
}
127
128         qe = null; e = null; v = null;
129         }
130     } catch (InterruptedException JavaDoc e) {
131         // just die
132
}
133     }
134
135     /**
136      * Stop the dispatcher so we can be destroyed.
137      */

138     void stop() {
139     if (qThread != null) {
140         qThread.interrupt(); // kill our thread
141
qThread = null;
142     }
143     }
144 }
145
Popular Tags