001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.local;
022
023import java.util.HashMap;
024import java.util.Map;
025import java.util.concurrent.TimeUnit;
026import java.util.concurrent.locks.Condition;
027import java.util.concurrent.locks.Lock;
028import java.util.concurrent.locks.ReadWriteLock;
029
030import net.sf.hajdbc.LockManager;
031import net.sf.hajdbc.util.concurrent.SemaphoreReadWriteLock;
032
033/**
034 * @author Paul Ferraro
035 */
036public class LocalLockManager implements LockManager
037{
038        private Map<String, ReadWriteLock> lockMap = new HashMap<String, ReadWriteLock>();
039
040        /**
041         * @see net.sf.hajdbc.LockManager#readLock(java.lang.String)
042         */
043        @Override
044        public Lock readLock(String object)
045        {
046                Lock lock = this.getReadWriteLock(null).readLock();
047                
048                return (object == null) ? lock : new GlobalLock(lock, this.getReadWriteLock(object).readLock());
049        }
050        
051        /**
052         * @see net.sf.hajdbc.LockManager#writeLock(java.lang.String)
053         */
054        @Override
055        public Lock writeLock(String object)
056        {
057                ReadWriteLock readWriteLock = this.getReadWriteLock(null);
058                
059                return (object == null) ? readWriteLock.writeLock() : new GlobalLock(readWriteLock.readLock(), this.getReadWriteLock(object).writeLock());
060        }
061        
062        private synchronized ReadWriteLock getReadWriteLock(String object)
063        {
064                ReadWriteLock lock = this.lockMap.get(object);
065                
066                if (lock == null)
067                {
068                        lock = new SemaphoreReadWriteLock(true);
069                        
070                        this.lockMap.put(object, lock);
071                }
072                
073                return lock;
074        }
075        
076        private static class GlobalLock implements Lock
077        {
078                private Lock globalLock;
079                private Lock lock;
080                
081                GlobalLock(Lock globalLock, Lock lock)
082                {
083                        this.globalLock = globalLock;
084                        this.lock = lock;
085                }
086                
087                @Override
088                public void lock()
089                {
090                        this.globalLock.lock();
091                        this.lock.lock();
092                }
093
094                @Override
095                public void lockInterruptibly() throws InterruptedException
096                {
097                        this.globalLock.lockInterruptibly();
098                        
099                        try
100                        {
101                                this.lock.lockInterruptibly();
102                        }
103                        catch (InterruptedException e)
104                        {
105                                this.globalLock.unlock();
106                                throw e;
107                        }
108                }
109
110                @Override
111                public boolean tryLock()
112                {
113                        if (this.globalLock.tryLock())
114                        {
115                                if (this.lock.tryLock())
116                                {
117                                        return true;
118                                }
119
120                                this.globalLock.unlock();
121                        }
122
123                        return false;
124                }
125
126                @Override
127                public boolean tryLock(long time, TimeUnit unit) throws InterruptedException
128                {
129                        if (this.globalLock.tryLock(time, unit))
130                        {
131                                if (this.lock.tryLock(time, unit))
132                                {
133                                        return true;
134                                }
135
136                                this.globalLock.unlock();
137                        }
138
139                        return false;
140                }
141
142                @Override
143                public void unlock()
144                {
145                        this.lock.unlock();
146                        this.globalLock.unlock();
147                }
148
149                @Override
150                public Condition newCondition()
151                {
152                        throw new UnsupportedOperationException();
153                }
154        }
155
156        /**
157         * @see net.sf.hajdbc.Lifecycle#start()
158         */
159        @Override
160        public void start() throws Exception
161        {
162                // Do nothing
163        }
164
165        /**
166         * @see net.sf.hajdbc.Lifecycle#stop()
167         */
168        @Override
169        public void stop()
170        {
171                // Do nothing
172        }
173}