jstrachan    2003/08/27 09:28:31

  Added:       messenger/src/java/org/apache/commons/messenger Lock.java
               messenger/src/test/org/apache/commons/messenger
                        LockTest.java
  Log:
  added a simple Lock class which I'm about to use in another Messenger imnplementation
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/Lock.java
  
  Index: Lock.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: Lock.java,v 1.1 2003/08/27 16:28:31 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import javax.jms.JMSException;
  
  /** <p><code>Lock</code> is a simple lock.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class Lock {
  
      private Thread owner;
      private int count;
  
      /**
       * Acquires the lock, blocking until the lock can be acquired
       * @throws InterruptedException
       */
      public void acquire() {
          Thread caller = Thread.currentThread();
          synchronized (this) {
              if (caller == owner) {
                  count++;
              }
              else {
                  while (owner != null) {
                      try {
                          wait();
                      }
                      catch (InterruptedException ex) {
                          // ignore
                      }
                  }
                  owner = caller;
                  count = 1;
              }
          }
      }
  
      /**
       * Release the lock.
       **/
      public synchronized void release() throws JMSException {
          if (Thread.currentThread() != owner) {
              throw new JMSException("Cannot release lock - not the current owner");
          }
          else {
              if (--count == 0) {
                  owner = null;
                  notify();
              }
          }
      }
  
      /**
       * @return
       */
      public synchronized boolean hasLock() {
          return Thread.currentThread() == owner;
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/test/org/apache/commons/messenger/LockTest.java
  
  Index: LockTest.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: LockTest.java,v 1.1 2003/08/27 16:28:31 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import javax.jms.JMSException;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  /** Test harness for the Lock class
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class LockTest extends TestCase {
  
      public static Test suite() {
          return new TestSuite(LockTest.class);
      }
  
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public LockTest(String testName) {
          super(testName);
      }
  
      public void testNormalLockOperation() throws Exception {
          Lock lock = new Lock();
                assertFalse("should not have the lock yet", lock.hasLock());
                
          lock.acquire();
          assertTrue("have lock", lock.hasLock());
  
          lock.release();
          assertFalse("have released the lock", lock.hasLock());
  
          try {
              lock.release();
              fail("Should have thrown an exception");
          }
          catch (JMSException e) {
              // ignore
          }
      }
  
      public void testNestedLockOperation() throws Exception {
          Lock lock = new Lock();
          lock.acquire();
          assertTrue("have lock", lock.hasLock());
  
          lock.acquire();
          assertTrue("have lock", lock.hasLock());
  
          lock.release();
          assertTrue("have lock", lock.hasLock());
  
          lock.release();
          assertFalse("have released the lock", lock.hasLock());
  
          try {
              lock.release();
              fail("Should have thrown an exception");
          }
          catch (JMSException e) {
              // ignore
          }
      }
  
      public void testReleaseBeforeAcquireShouldFail() throws Exception {
          Lock lock = new Lock();
          try {
              lock.release();
              fail("Should have thrown an exception");
          }
          catch (JMSException e) {
              // ignore
          }
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to