KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > distributable > DistributableLock


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.distributable;
22
23 import java.util.concurrent.TimeUnit JavaDoc;
24 import java.util.concurrent.locks.Condition JavaDoc;
25 import java.util.concurrent.locks.Lock JavaDoc;
26
27 import org.jgroups.Channel;
28 import org.jgroups.ChannelException;
29 import org.jgroups.JChannel;
30 import org.jgroups.blocks.DistributedLockManager;
31 import org.jgroups.blocks.LockManager;
32 import org.jgroups.blocks.LockNotGrantedException;
33 import org.jgroups.blocks.LockNotReleasedException;
34 import org.jgroups.blocks.TwoPhaseVotingAdapter;
35 import org.jgroups.blocks.TwoPhaseVotingListener;
36 import org.jgroups.blocks.VotingAdapter;
37
38 /**
39  * @author Paul Ferraro
40  * @since 1.1
41  */

42 public class DistributableLock implements Lock JavaDoc, TwoPhaseVotingListener
43 {
44     private LockManager lockManager;
45     private Channel channel;
46     private int timeout;
47     private Lock JavaDoc lock;
48     
49     /**
50      * Constructs a new DistributableLock.
51      * @param name a channel name
52      * @param protocol a channel protocol
53      * @param timeout a timeout for channel messages.
54      * @param lock a local lock
55      * @throws Exception
56      */

57     public DistributableLock(String JavaDoc name, String JavaDoc protocol, int timeout, Lock JavaDoc lock) throws Exception JavaDoc
58     {
59         this.timeout = timeout;
60         this.lock = lock;
61         this.channel = new JChannel(protocol);
62         this.channel.connect(name);
63         
64         TwoPhaseVotingAdapter adapter = new TwoPhaseVotingAdapter(new VotingAdapter(this.channel));
65         
66         adapter.addListener(this);
67         
68         this.lockManager = new DistributedLockManager(adapter, name);
69     }
70     
71     /**
72      * @return the channel used by the lock manager
73      */

74     public Channel getChannel()
75     {
76         return this.channel;
77     }
78     
79     /**
80      * @see java.util.concurrent.locks.Lock#lock()
81      */

82     public void lock()
83     {
84         while (!this.tryLock());
85     }
86
87     /**
88      * @see java.util.concurrent.locks.Lock#lockInterruptibly()
89      */

90     public void lockInterruptibly() throws InterruptedException JavaDoc
91     {
92         while (!this.tryLock())
93         {
94             if (Thread.currentThread().isInterrupted())
95             {
96                 throw new InterruptedException JavaDoc();
97             }
98         }
99     }
100
101     /**
102      * @see java.util.concurrent.locks.Lock#tryLock()
103      */

104     public boolean tryLock()
105     {
106         try
107         {
108             this.lockManager.lock(this.getObject(), this.getOwner(), this.timeout);
109             
110             return true;
111         }
112         catch (LockNotGrantedException e)
113         {
114             return false;
115         }
116         catch (ClassCastException JavaDoc e)
117         {
118             throw new IllegalStateException JavaDoc(e);
119         }
120         catch (ChannelException e)
121         {
122             throw new IllegalStateException JavaDoc(e);
123         }
124     }
125
126     /**
127      * @see java.util.concurrent.locks.Lock#tryLock(long, java.util.concurrent.TimeUnit)
128      */

129     public boolean tryLock(long timeout, TimeUnit JavaDoc unit)
130     {
131         long stopTime = System.currentTimeMillis() + unit.toMillis(timeout);
132         
133         while (!this.tryLock())
134         {
135             if (System.currentTimeMillis() >= stopTime)
136             {
137                 return false;
138             }
139         }
140         
141         return true;
142     }
143
144     /**
145      * @see java.util.concurrent.locks.Lock#unlock()
146      */

147     public void unlock()
148     {
149         try
150         {
151             this.lockManager.unlock(this.getObject(), this.getOwner());
152         }
153         catch (ClassCastException JavaDoc e)
154         {
155             throw new IllegalStateException JavaDoc(e);
156         }
157         catch (ChannelException e)
158         {
159             throw new IllegalStateException JavaDoc(e);
160         }
161         catch (LockNotReleasedException e)
162         {
163             throw new IllegalStateException JavaDoc(e);
164         }
165     }
166
167     /**
168      * @see java.util.concurrent.locks.Lock#newCondition()
169      */

170     public Condition JavaDoc newCondition()
171     {
172         return this.lock.newCondition();
173     }
174     
175     /**
176      * @see org.jgroups.blocks.TwoPhaseVotingListener#prepare(java.lang.Object)
177      */

178     public boolean prepare(Object JavaDoc decree)
179     {
180         if (DistributedLockManager.AcquireLockDecree.class.isInstance(decree))
181         {
182             return this.lock.tryLock();
183         }
184         
185         return true;
186     }
187
188     /**
189      * @see org.jgroups.blocks.TwoPhaseVotingListener#commit(java.lang.Object)
190      */

191     public boolean commit(Object JavaDoc decree)
192     {
193         if (DistributedLockManager.ReleaseLockDecree.class.isInstance(decree))
194         {
195             this.lock.unlock();
196         }
197         
198         return true;
199     }
200
201     /**
202      * @see org.jgroups.blocks.TwoPhaseVotingListener#abort(java.lang.Object)
203      */

204     public void abort(Object JavaDoc decree)
205     {
206         if (DistributedLockManager.AcquireLockDecree.class.isInstance(decree))
207         {
208             this.lock.unlock();
209         }
210     }
211     
212     /**
213      * Stops the channel.
214      */

215     public void stop()
216     {
217         this.channel.close();
218     }
219
220     private Object JavaDoc getObject()
221     {
222         return "";
223     }
224     
225     private Object JavaDoc getOwner()
226     {
227         return Thread.currentThread().getId();
228     }
229 }
230
Popular Tags