1 20 package org.apache.mina.common.support; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import org.apache.mina.common.IoFuture; 26 import org.apache.mina.common.IoFutureListener; 27 import org.apache.mina.common.IoSession; 28 29 35 public class DefaultIoFuture implements IoFuture { 36 private final IoSession session; 37 38 private final Object lock; 39 40 private IoFutureListener firstListener; 41 42 private List <IoFutureListener> otherListeners; 43 44 private Object result; 45 46 private boolean ready; 47 48 53 public DefaultIoFuture(IoSession session) { 54 this.session = session; 55 this.lock = this; 56 } 57 58 public IoSession getSession() { 59 return session; 60 } 61 62 public void join() { 63 synchronized (lock) { 64 while (!ready) { 65 try { 66 lock.wait(); 67 } catch (InterruptedException e) { 68 } 69 } 70 } 71 } 72 73 public boolean join(long timeoutMillis) { 74 long startTime = (timeoutMillis <= 0) ? 0 : System.currentTimeMillis(); 75 long waitTime = timeoutMillis; 76 77 synchronized (lock) { 78 if (ready) { 79 return ready; 80 } else if (waitTime <= 0) { 81 return ready; 82 } 83 84 for (;;) { 85 try { 86 lock.wait(waitTime); 87 } catch (InterruptedException e) { 88 } 89 90 if (ready) { 91 return true; 92 } else { 93 waitTime = timeoutMillis 94 - (System.currentTimeMillis() - startTime); 95 if (waitTime <= 0) { 96 return ready; 97 } 98 } 99 } 100 } 101 } 102 103 public boolean isReady() { 104 synchronized (lock) { 105 return ready; 106 } 107 } 108 109 112 protected void setValue(Object newValue) { 113 synchronized (lock) { 114 if (ready) { 116 return; 117 } 118 119 result = newValue; 120 ready = true; 121 lock.notifyAll(); 122 123 notifyListeners(); 124 } 125 } 126 127 130 protected Object getValue() { 131 synchronized (lock) { 132 return result; 133 } 134 } 135 136 public void addListener(IoFutureListener listener) { 137 if (listener == null) { 138 throw new NullPointerException ("listener"); 139 } 140 141 synchronized (lock) { 142 if (firstListener == null) { 143 firstListener = listener; 144 } else { 145 if (otherListeners == null) { 146 otherListeners = new ArrayList <IoFutureListener>(1); 147 } 148 otherListeners.add(listener); 149 } 150 if (ready) { 151 listener.operationComplete(this); 152 } 153 } 154 } 155 156 public void removeListener(IoFutureListener listener) { 157 if (listener == null) { 158 throw new NullPointerException ("listener"); 159 } 160 161 synchronized (lock) { 162 if (listener == firstListener) { 163 if (otherListeners != null && !otherListeners.isEmpty()) { 164 firstListener = otherListeners.remove(0); 165 } else { 166 firstListener = null; 167 } 168 } else if (otherListeners != null) { 169 otherListeners.remove(listener); 170 } 171 } 172 } 173 174 private void notifyListeners() { 175 synchronized (lock) { 176 if (firstListener != null) { 177 firstListener.operationComplete(this); 178 if (otherListeners != null) { 179 for (IoFutureListener l : otherListeners) { 180 l.operationComplete(this); 181 } 182 } 183 } 184 } 185 } 186 } 187 | Popular Tags |