1 package org.sapia.ubik.rmi.server.command; 2 3 import org.sapia.ubik.net.Timer; 4 5 6 17 public class ResponseLock { 18 private static int _count = 0; 19 private Object _response; 20 private ResponseQueue _queue; 21 private String _id; 22 private boolean _ready; 23 24 27 ResponseLock(ResponseQueue parent) { 28 _queue = parent; 29 _id = generateId(); 30 } 31 32 37 public String getId() { 38 return _id; 39 } 40 41 44 public void release() { 45 _queue.removeLock(_id); 46 } 47 48 59 public synchronized Object waitResponse(long timeout) 60 throws InterruptedException , ResponseTimeOutException { 61 Timer timer = new Timer(timeout); 62 63 while (!_ready) { 64 wait(timeout); 65 66 if (timer.isOver() && !_ready) { 67 release(); 68 throw new ResponseTimeOutException(); 69 } 70 } 71 72 release(); 73 74 return _response; 75 } 76 77 82 public synchronized void setResponse(Object r) { 83 _response = r; 84 _ready = true; 85 notify(); 86 } 87 88 private static synchronized String generateId() { 89 if (_count > 999) { 90 _count = 0; 91 } 92 93 return "" + System.currentTimeMillis() + (_count++); 94 } 95 } 96 | Popular Tags |