001    package railo.commons.lock;
002    
003    import java.util.concurrent.TimeUnit;
004    import java.util.concurrent.locks.ReentrantLock;
005    
006    public class SimpleLock<L> implements Lock {
007            
008            private ReentrantLock lock;
009            private L label;
010            
011            public SimpleLock(L label) {
012                    this.lock=new ReentrantLock(true);
013                    this.label=label;
014            }
015    
016            
017            public void lock(long timeout) throws LockException, LockInterruptedException {
018                    if(timeout<=0) throw new LockException("timeout must be a postive number");
019                    
020                    try {
021                            if(!lock.tryLock(timeout, TimeUnit.MILLISECONDS)){
022                                    throw new LockException(timeout);
023                            }
024                    } 
025                    catch (InterruptedException e) {
026                            throw new LockInterruptedException(e);
027                    }
028                    
029            }
030    
031    
032            public void unlock()    {
033                    lock.unlock();
034            }
035            
036            /**
037         * Returns an estimate of the number of threads waiting to
038         * acquire this lock.  The value is only an estimate because the number of
039         * threads may change dynamically while this method traverses
040         * internal data structures.  This method is designed for use in
041         * monitoring of the system state, not for synchronization
042         * control.
043         *
044         * @return the estimated number of threads waiting for this lock
045         */
046        public int getQueueLength() {
047                    return lock.getQueueLength();
048            }
049        
050    
051            public L getLabel(){
052                    return label;
053            }
054    }