1 16 17 package org.apache.catalina.cluster.util; 18 19 29 30 import java.util.LinkedList ; 31 import java.util.HashMap ; 32 33 public class SmartQueue { 34 35 public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 36 .getLog(SmartQueue.class); 37 38 41 private LinkedList queue = new LinkedList (); 42 43 46 private HashMap queueMap = new HashMap (); 47 48 private Object mutex = new Object (); 49 50 public SmartQueue() { 51 } 52 53 59 public void add(SmartEntry entry) { 60 64 synchronized (mutex) { 65 66 SmartEntry current = (SmartEntry) queueMap.get(entry.getKey()); 67 if (current == null) { 68 69 if (log.isDebugEnabled()) 70 log.debug("[" + Thread.currentThread().getName() 71 + "][SmartQueue] Adding new object=" + entry); 72 queue.addLast(entry); 73 queueMap.put(entry.getKey(), entry); 74 } else { 75 76 if (log.isDebugEnabled()) 77 log.debug("[" + Thread.currentThread().getName() 78 + "][SmartQueue] Replacing old object=" + current); 79 current.setValue(entry.getValue()); 80 if (log.isDebugEnabled()) 81 log.debug("with new object=" + current); 82 } 83 87 mutex.notifyAll(); 88 } 89 } 90 91 public int size() { 92 synchronized (mutex) { 93 return queue.size(); 94 } 95 } 96 97 100 public SmartEntry remove() { 101 return remove(0); 102 } 103 104 public SmartEntry remove(long timeout) { 105 SmartEntry result = null; 106 long startEntry = System.currentTimeMillis(); 107 synchronized (mutex) { 108 while (size() == 0) { 109 try { 110 if (log.isDebugEnabled()) 111 log 112 .debug("[" 113 + Thread.currentThread().getName() 114 + "][SmartQueue] Queue sleeping until object added size=" 115 + size() + "."); 116 if ((timeout != 0) 117 && ((System.currentTimeMillis() - startEntry) > timeout)) { 118 return null; 119 } 120 mutex.wait(timeout); 121 if (log.isDebugEnabled()) 122 log 123 .debug("[" 124 + Thread.currentThread().getName() 125 + "][SmartQueue] Queue woke up or interrupted size=" 126 + size() + "."); 127 } catch (IllegalMonitorStateException ex) { 128 throw ex; 129 } catch (InterruptedException ex) { 130 } } 133 result = (SmartEntry) queue.removeFirst(); 134 queueMap.remove(result.getKey()); 135 if (log.isDebugEnabled()) 136 log.debug("[" + Thread.currentThread().getName() 137 + "][SmartQueue] Returning=" + result); 138 } 139 return result; 140 } 141 142 public static class SmartEntry { 143 protected Object key; 144 145 protected Object value; 146 147 public SmartEntry(Object key, Object value) { 148 if (key == null) 149 throw new IllegalArgumentException ( 150 "SmartEntry key can not be null."); 151 if (value == null) 152 throw new IllegalArgumentException ( 153 "SmartEntry value can not be null."); 154 this.key = key; 155 this.value = value; 156 } 157 158 public Object getKey() { 159 return key; 160 } 161 162 public Object getValue() { 163 return value; 164 } 165 166 public void setValue(Object value) { 167 if (value == null) 168 throw new IllegalArgumentException ( 169 "SmartEntry value can not be null."); 170 this.value = value; 171 } 172 173 public int hashCode() { 174 return key.hashCode(); 175 } 176 177 public boolean equals(Object o) { 178 if (!(o instanceof SmartEntry)) 179 return false; 180 SmartEntry other = (SmartEntry) o; 181 return other.getKey().equals(getKey()); 182 } 183 184 public String toString() { 185 return "[SmartyEntry key=" + key + " value=" + value + "]"; 186 } 187 } 188 189 } | Popular Tags |