SemaphoreControlledChannel.java

  1. /*
  2.   File: SemaphoreControlledChannel.java

  3.   Originally written by Doug Lea and released into the public domain.
  4.   This may be used for any purposes whatsoever without acknowledgment.
  5.   Thanks for the assistance and support of Sun Microsystems Labs,
  6.   and everyone contributing, testing, and using this code.

  7.   History:
  8.   Date       Who                What
  9.   16Jun1998  dl               Create public version
  10.    5Aug1998  dl               replaced int counters with longs
  11.   08dec2001  dl               reflective constructor now uses longs too.
  12. */

  13. package org.dbunit.util.concurrent;

  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;

  16. import java.lang.reflect.Constructor;
  17. import java.lang.reflect.InvocationTargetException;

  18. /**
  19.  * Abstract class for channels that use Semaphores to
  20.  * control puts and takes.
  21.  * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
  22.  *
  23.  * @author Doug Lea
  24.  * @author Last changed by: $Author$
  25.  * @version $Revision$ $Date$
  26.  * @since ? (pre 2.1)
  27.  */
  28. public abstract class SemaphoreControlledChannel implements BoundedChannel {

  29.     /**
  30.      * Logger for this class
  31.      */
  32.     private static final Logger logger = LoggerFactory.getLogger(SemaphoreControlledChannel.class);

  33.   protected final Semaphore putGuard_;
  34.   protected final Semaphore takeGuard_;
  35.   protected int capacity_;

  36.   /**
  37.    * Create a channel with the given capacity and default
  38.    * semaphore implementation
  39.    * @exception IllegalArgumentException if capacity less or equal to zero
  40.    **/

  41.   public SemaphoreControlledChannel(int capacity)
  42.    throws IllegalArgumentException {
  43.     if (capacity <= 0) throw new IllegalArgumentException();
  44.     capacity_ = capacity;
  45.     putGuard_ = new Semaphore(capacity);
  46.     takeGuard_ = new Semaphore(0);
  47.   }


  48.   /**
  49.    * Create a channel with the given capacity and
  50.    * semaphore implementations instantiated from the supplied class
  51.    * @exception IllegalArgumentException if capacity less or equal to zero.
  52.    * @exception NoSuchMethodException If class does not have constructor
  53.    * that intializes permits
  54.    * @exception SecurityException if constructor information
  55.    * not accessible
  56.    * @exception InstantiationException if semaphore class is abstract
  57.    * @exception IllegalAccessException if constructor cannot be called
  58.    * @exception InvocationTargetException if semaphore constructor throws an
  59.    * exception
  60.    **/
  61.   public SemaphoreControlledChannel(int capacity, Class semaphoreClass)
  62.    throws IllegalArgumentException,
  63.           NoSuchMethodException,
  64.           SecurityException,
  65.           InstantiationException,
  66.           IllegalAccessException,
  67.           InvocationTargetException {
  68.     if (capacity <= 0) throw new IllegalArgumentException();
  69.     capacity_ = capacity;
  70.     Class[] longarg = { Long.TYPE };
  71.     Constructor ctor = semaphoreClass.getDeclaredConstructor(longarg);
  72.     Long[] cap = {Long.valueOf(capacity)};
  73.     putGuard_ = (Semaphore)(ctor.newInstance(cap));
  74.     Long[] zero = {0L};
  75.     takeGuard_ = (Semaphore)(ctor.newInstance(zero));
  76.   }



  77.   public int  capacity() {
  78.         logger.debug("capacity() - start");
  79.  return capacity_; }

  80.   /**
  81.    * Return the number of elements in the buffer.
  82.    * This is only a snapshot value, that may change
  83.    * immediately after returning.
  84.    **/

  85.   public int size() {
  86.         logger.debug("size() - start");
  87.  return (int)(takeGuard_.permits());  }

  88.   /**
  89.    * Internal mechanics of put.
  90.    **/
  91.   protected abstract void insert(Object x);

  92.   /**
  93.    * Internal mechanics of take.
  94.    **/
  95.   protected abstract Object extract();

  96.   public void put(Object x) throws InterruptedException {
  97.         logger.debug("put(x=" + x + ") - start");

  98.     if (x == null) throw new IllegalArgumentException();
  99.     if (Thread.interrupted()) throw new InterruptedException();
  100.     putGuard_.acquire();
  101.     try {
  102.       insert(x);
  103.       takeGuard_.release();
  104.     }
  105.     catch (ClassCastException ex) {
  106.       putGuard_.release();
  107.       throw ex;
  108.     }
  109.   }

  110.   public boolean offer(Object x, long msecs) throws InterruptedException {
  111.         logger.debug("offer(x=" + x + ", msecs=" + msecs + ") - start");

  112.     if (x == null) throw new IllegalArgumentException();
  113.     if (Thread.interrupted()) throw new InterruptedException();
  114.     if (!putGuard_.attempt(msecs))
  115.       return false;
  116.     else {
  117.       try {
  118.         insert(x);
  119.         takeGuard_.release();
  120.         return true;
  121.       }
  122.       catch (ClassCastException ex) {
  123.         putGuard_.release();
  124.         throw ex;
  125.       }
  126.     }
  127.   }

  128.   public Object take() throws InterruptedException {
  129.         logger.debug("take() - start");

  130.     if (Thread.interrupted()) throw new InterruptedException();
  131.     takeGuard_.acquire();
  132.     try {
  133.       Object x = extract();
  134.       putGuard_.release();
  135.       return x;
  136.     }
  137.     catch (ClassCastException ex) {
  138.       takeGuard_.release();
  139.       throw ex;
  140.     }
  141.   }

  142.   public Object poll(long msecs) throws InterruptedException {
  143.         logger.debug("poll(msecs=" + msecs + ") - start");

  144.     if (Thread.interrupted()) throw new InterruptedException();
  145.     if (!takeGuard_.attempt(msecs))
  146.       return null;
  147.     else {
  148.       try {
  149.         Object x = extract();
  150.         putGuard_.release();
  151.         return x;
  152.       }
  153.       catch (ClassCastException ex) {
  154.         takeGuard_.release();
  155.         throw ex;
  156.       }
  157.     }
  158.   }

  159. }