Save This Page
Home » glassfish-v2ur2-b04-src » javax » mail » [javadoc | source]
    1   /*
    2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    3    *
    4    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    5    *
    6    * The contents of this file are subject to the terms of either the GNU
    7    * General Public License Version 2 only ("GPL") or the Common Development
    8    * and Distribution License("CDDL") (collectively, the "License").  You
    9    * may not use this file except in compliance with the License. You can obtain
   10    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   11    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   12    * language governing permissions and limitations under the License.
   13    *
   14    * When distributing the software, include this License Header Notice in each
   15    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   16    * Sun designates this particular file as subject to the "Classpath" exception
   17    * as provided by Sun in the GPL Version 2 section of the License file that
   18    * accompanied this code.  If applicable, add the following below the License
   19    * Header, with the fields enclosed by brackets [] replaced by your own
   20    * identifying information: "Portions Copyrighted [year]
   21    * [name of copyright owner]"
   22    *
   23    * Contributor(s):
   24    *
   25    * If you wish your version of this file to be governed by only the CDDL or
   26    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   27    * elects to include this software in this distribution under the [CDDL or GPL
   28    * Version 2] license."  If you don't indicate a single choice of license, a
   29    * recipient has the option to distribute your version of this file under
   30    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   31    * its licensees as provided above.  However, if you add GPL Version 2 code
   32    * and therefore, elected the GPL Version 2 license, then the option applies
   33    * only if the new code is made subject to such option by the copyright
   34    * holder.
   35    */
   36   
   37   /*
   38    * @(#)EventQueue.java	1.11 07/05/04
   39    */
   40   
   41   package javax.mail;
   42   
   43   import java.io;
   44   import java.util.Vector;
   45   import javax.mail.event.MailEvent;
   46   
   47   /**
   48    * Package private class used by Store & Folder to dispatch events.
   49    * This class implements an event queue, and a dispatcher thread that
   50    * dequeues and dispatches events from the queue.
   51    *
   52    * Pieces stolen from sun.misc.Queue.
   53    *
   54    * @author	Bill Shannon
   55    */
   56   class EventQueue implements Runnable {
   57   
   58       static class QueueElement {
   59   	QueueElement next = null;
   60   	QueueElement prev = null;
   61   	MailEvent event = null;
   62   	Vector vector = null;
   63   
   64   	QueueElement(MailEvent event, Vector vector) {
   65   	    this.event = event;
   66   	    this.vector = vector;
   67   	}
   68       }
   69   
   70       private QueueElement head = null;
   71       private QueueElement tail = null;
   72       private Thread qThread;
   73   
   74       public EventQueue() {
   75   	qThread = new Thread(this, "JavaMail-EventQueue");
   76   	qThread.setDaemon(true);  // not a user thread
   77   	qThread.start();
   78       }
   79   
   80       /**
   81        * Enqueue an event.
   82        */
   83       public synchronized void enqueue(MailEvent event, Vector vector) {
   84   	QueueElement newElt = new QueueElement(event, vector);
   85   
   86   	if (head == null) {
   87   	    head = newElt;
   88   	    tail = newElt;
   89   	} else {
   90   	    newElt.next = head;
   91   	    head.prev = newElt;
   92   	    head = newElt;
   93   	}
   94   	notifyAll();
   95       }
   96   
   97       /**
   98        * Dequeue the oldest object on the queue.
   99        * Used only by the run() method.
  100        *
  101        * @return    the oldest object on the queue.
  102        * @exception java.lang.InterruptedException if another thread has
  103        *              interrupted this thread.
  104        */
  105       private synchronized QueueElement dequeue()
  106   				throws InterruptedException {
  107   	while (tail == null)
  108   	    wait();
  109   	QueueElement elt = tail;
  110   	tail = elt.prev;
  111   	if (tail == null) {
  112   	    head = null;
  113   	} else {
  114   	    tail.next = null;
  115   	}
  116   	elt.prev = elt.next = null;
  117   	return elt;
  118       }
  119   
  120       /**
  121        * Pull events off the queue and dispatch them.
  122        */
  123       public void run() {
  124   	QueueElement qe;
  125   
  126   	try {
  127   	    loop:
  128   	    while ((qe = dequeue()) != null) {
  129   		MailEvent e = qe.event;
  130   		Vector v = qe.vector;
  131   
  132   		for (int i = 0; i < v.size(); i++)
  133   		    try {
  134   			e.dispatch(v.elementAt(i));
  135   		    } catch (Throwable t) {
  136   			if (t instanceof InterruptedException)
  137   			    break loop;
  138   			// ignore anything else thrown by the listener
  139   		    }
  140   
  141   		qe = null; e = null; v = null;
  142   	    }
  143   	} catch (InterruptedException e) {
  144   	    // just die
  145   	}
  146       }
  147   
  148       /**
  149        * Stop the dispatcher so we can be destroyed.
  150        */
  151       void stop() {
  152   	if (qThread != null) {
  153   	    qThread.interrupt();	// kill our thread
  154   	    qThread = null;
  155   	}
  156       }
  157   }

Save This Page
Home » glassfish-v2ur2-b04-src » javax » mail » [javadoc | source]