1 21 package net.sf.hajdbc.distributable; 22 23 import java.util.concurrent.TimeUnit ; 24 import java.util.concurrent.locks.Condition ; 25 import java.util.concurrent.locks.Lock ; 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 42 public class DistributableLock implements Lock , TwoPhaseVotingListener 43 { 44 private LockManager lockManager; 45 private Channel channel; 46 private int timeout; 47 private Lock lock; 48 49 57 public DistributableLock(String name, String protocol, int timeout, Lock lock) throws Exception 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 74 public Channel getChannel() 75 { 76 return this.channel; 77 } 78 79 82 public void lock() 83 { 84 while (!this.tryLock()); 85 } 86 87 90 public void lockInterruptibly() throws InterruptedException 91 { 92 while (!this.tryLock()) 93 { 94 if (Thread.currentThread().isInterrupted()) 95 { 96 throw new InterruptedException (); 97 } 98 } 99 } 100 101 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 e) 117 { 118 throw new IllegalStateException (e); 119 } 120 catch (ChannelException e) 121 { 122 throw new IllegalStateException (e); 123 } 124 } 125 126 129 public boolean tryLock(long timeout, TimeUnit 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 147 public void unlock() 148 { 149 try 150 { 151 this.lockManager.unlock(this.getObject(), this.getOwner()); 152 } 153 catch (ClassCastException e) 154 { 155 throw new IllegalStateException (e); 156 } 157 catch (ChannelException e) 158 { 159 throw new IllegalStateException (e); 160 } 161 catch (LockNotReleasedException e) 162 { 163 throw new IllegalStateException (e); 164 } 165 } 166 167 170 public Condition newCondition() 171 { 172 return this.lock.newCondition(); 173 } 174 175 178 public boolean prepare(Object decree) 179 { 180 if (DistributedLockManager.AcquireLockDecree.class.isInstance(decree)) 181 { 182 return this.lock.tryLock(); 183 } 184 185 return true; 186 } 187 188 191 public boolean commit(Object decree) 192 { 193 if (DistributedLockManager.ReleaseLockDecree.class.isInstance(decree)) 194 { 195 this.lock.unlock(); 196 } 197 198 return true; 199 } 200 201 204 public void abort(Object decree) 205 { 206 if (DistributedLockManager.AcquireLockDecree.class.isInstance(decree)) 207 { 208 this.lock.unlock(); 209 } 210 } 211 212 215 public void stop() 216 { 217 this.channel.close(); 218 } 219 220 private Object getObject() 221 { 222 return ""; 223 } 224 225 private Object getOwner() 226 { 227 return Thread.currentThread().getId(); 228 } 229 } 230 | Popular Tags |