1 16 17 package org.apache.axis.ime.internal.util; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.components.threadpool.ThreadPool; 21 import org.apache.axis.i18n.Messages; 22 import org.apache.commons.logging.Log; 23 24 import java.util.Iterator ; 25 import java.util.Vector ; 26 27 34 public class NonPersistentKeyedBuffer 35 implements KeyedBuffer { 36 37 protected static Log log = 38 LogFactory.getLog(NonPersistentKeyedBuffer.class.getName()); 39 40 private final KeyedQueue messages = new KeyedQueue(); 41 42 private ThreadPool WORKERS; 43 44 public NonPersistentKeyedBuffer( 45 ThreadPool workers) { 46 this.WORKERS = workers; 47 } 48 49 public Object peek() { 50 KeyedNode node = null; 51 synchronized (messages) { 52 node = messages.peek(); 53 } 54 if (node != null) { 55 return node.value; 56 } else { 57 return null; 58 } 59 } 60 61 62 public Object [] peekAll() { 63 if (log.isDebugEnabled()) { 64 log.debug("Enter: KeyedBuffer::selectAll"); 65 } 66 Vector v = new Vector (); 67 KeyedNode node = null; 68 synchronized (messages) { 69 for (Iterator i = messages.iterator(); i.hasNext();) { 70 v.add(i.next()); 71 } 72 } 73 Object [] objects = new 74 Object [v.size()]; 75 v.copyInto(objects); 76 if (log.isDebugEnabled()) { 77 log.debug("Exit: KeyedBuffer::selectAll"); 78 } 79 return objects; 80 } 81 82 83 public void put( 84 Object key, 85 Object object) { 86 if (log.isDebugEnabled()) { 87 log.debug("Enter: KeyedBuffer::put"); 88 } 89 if (key == null || 90 object == null) 91 throw new IllegalArgumentException (Messages.getMessage("illegalArgumentException00")); 92 93 synchronized (messages) { 94 messages.put(new KeyedNode(key, object)); 95 messages.notify(); 96 } 97 if (log.isDebugEnabled()) { 98 log.debug("Exit: KeyedBuffer::put"); 99 } 100 } 101 102 public Object cancel(Object key) { 103 if (log.isDebugEnabled()) { 104 log.debug("Enter: KeyedBuffer::cancel"); 105 } 106 if (key == null) 107 throw new IllegalArgumentException (Messages.getMessage("illegalArgumentException00")); 108 Object object = null; 109 synchronized (messages) { 110 KeyedNode node = messages.select(key); if (node != null) 112 object = node.value; 113 node.key = null; 114 node.value = null; 115 } 116 if (log.isDebugEnabled()) { 117 log.debug("Exit: KeyedBuffer::cancel"); 118 } 119 return object; 120 } 121 122 public Object [] selectAll() { 123 if (log.isDebugEnabled()) { 124 log.debug("Enter: KeyedBuffer::selectAll"); 125 } 126 Vector v = new Vector (); 127 KeyedNode node = null; 128 synchronized (messages) { 129 while ((node = messages.select()) != null) { 130 v.add(node.value); 131 node.key = null; 132 node.value = null; 133 } 134 } 135 Object [] objects = new 136 Object [v.size()]; 137 v.copyInto(objects); 138 if (log.isDebugEnabled()) { 139 log.debug("Exit: KeyedBuffer::selectAll"); 140 } 141 return objects; 142 } 143 144 public Object select() 145 throws InterruptedException { 146 if (log.isDebugEnabled()) { 147 log.debug("Enter: KeyedBuffer::select"); 148 } 149 for (; ;) { 150 if (WORKERS.isShuttingDown()) 151 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 152 KeyedNode node = null; 153 synchronized (messages) { 154 node = messages.select(); 155 } 156 if (node != null) { 157 Object object = node.value; 158 node.key = null; 159 node.value = null; 160 if (log.isDebugEnabled()) { 161 log.debug("Exit: KeyedBuffer::select"); 162 } 163 return object; 164 } else { 165 messages.wait(); 166 } 167 } 168 } 169 170 public Object select(long timeout) 171 throws InterruptedException { 172 if (log.isDebugEnabled()) { 173 log.debug("Enter: KeyedBuffer::select"); 174 } 175 for (; ;) { 176 if (WORKERS.isShuttingDown()) 177 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 178 KeyedNode node = null; 179 synchronized (messages) { 180 node = messages.select(); 181 } 182 if (node != null) { 183 Object object = node.value; 184 node.key = null; 185 node.value = null; 186 if (log.isDebugEnabled()) { 187 log.debug("Exit: KeyedBuffer::select"); 188 } 189 return object; 190 } else { 191 messages.wait(timeout); 192 } 193 } 194 } 195 196 public Object select(Object key) 197 throws InterruptedException { 198 if (log.isDebugEnabled()) { 199 log.debug("Enter: KeyedBuffer::select"); 200 } 201 for (; ;) { 202 if (WORKERS.isShuttingDown()) 203 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 204 KeyedNode node = null; 205 synchronized (messages) { 206 node = messages.select(key); 207 } 208 if (node != null) { 209 Object object = node.value; 210 node.key = null; 211 node.value = null; 212 if (log.isDebugEnabled()) { 213 log.debug("Exit: KeyedBuffer::select"); 214 } 215 return object; 216 } else { 217 messages.wait(); 218 } 219 } 220 } 221 222 public Object select(Object key, long timeout) 223 throws InterruptedException { 224 if (log.isDebugEnabled()) { 225 log.debug("Enter: KeyedBuffer::select"); 226 } 227 for (; ;) { 228 if (WORKERS.isShuttingDown()) 229 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 230 KeyedNode node = null; 231 synchronized (messages) { 232 node = messages.select(key); 233 } 234 if (node != null) { 235 Object object = node.value; 236 node.key = null; 237 node.value = null; 238 if (log.isDebugEnabled()) { 239 log.debug("Exit: KeyedBuffer::select"); 240 } 241 return object; 242 } else { 243 messages.wait(timeout); 244 } 245 } 246 } 247 248 public Object get() { 249 if (log.isDebugEnabled()) { 250 log.debug("Enter: KeyedBuffer::get"); 251 } 252 KeyedNode node = null; 253 Object object = null; 254 synchronized (messages) { 255 node = messages.select(); 256 } 257 if (node != null) { 258 object = node.value; 259 node.key = null; 260 node.value = null; 261 } 262 if (log.isDebugEnabled()) { 263 log.debug("Exit: KeyedBuffer::get"); 264 } 265 return object; 266 } 267 268 public Object get(Object key) { 269 if (log.isDebugEnabled()) { 270 log.debug("Enter: KeyedBuffer::get"); 271 } 272 KeyedNode node = null; 273 Object object = null; 274 synchronized (messages) { 275 node = messages.select(key); 276 } 277 if (node != null) { 278 object = node.value; 279 node.key = null; 280 node.value = null; 281 } 282 if (log.isDebugEnabled()) { 283 log.debug("Exit: KeyedBuffer::get"); 284 } 285 return object; 286 } 287 288 protected static class KeyedNode { 290 public Object key; 291 public Object value; 292 public KeyedNode next; 293 294 public KeyedNode() { 295 } 296 297 public KeyedNode( 298 Object key, 299 Object value) { 300 this.key = key; 301 this.value = value; 302 } 303 304 public KeyedNode( 305 Object key, 306 Object value, 307 KeyedNode next) { 308 this(key, value); 309 this.next = next; 310 } 311 } 312 313 protected static class KeyedQueue { 314 315 protected KeyedNode head; 316 protected KeyedNode last; 317 318 protected void put(KeyedNode node) { 319 if (last == null) { 320 last = head = node; 321 } else { 322 last = last.next = node; 323 } 324 } 325 326 protected KeyedNode select() { 327 KeyedNode node = head; 328 if (node != null && (head = node.next) == null) { 329 last = null; 330 } 331 if (node != null) 332 node.next = null; 333 return node; 334 } 335 336 protected KeyedNode select(Object key) { 337 KeyedNode previous = null; 338 for (KeyedNode node = head; node != null; node = node.next) { 339 if (node.key.equals(key)) { 340 if (previous != null) 341 previous.next = node.next; 342 node.next = null; 343 return node; 344 } 345 previous = node; 346 } 347 return null; 348 } 349 350 protected KeyedNode peek() { 351 KeyedNode node = head; 352 return node; 353 } 354 355 protected Iterator iterator() { 356 return new KeyedQueueIterator(head); 357 } 358 } 359 360 protected static class KeyedQueueIterator 361 implements Iterator { 362 protected KeyedNode current; 363 protected KeyedNode next; 364 public KeyedQueueIterator(KeyedNode node) { 365 this.next = node; 366 } 367 368 public boolean hasNext() { 369 return (next != null); 370 } 371 372 public Object next() { 373 KeyedNode node = null; 374 if (next != null) { 375 node = next.next; 376 } 377 current = next; 378 next = node; 379 return current; 380 } 381 382 public void remove() {} 383 } 384 } 385 | Popular Tags |