1 17 package org.apache.excalibur.event.impl; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 23 import org.apache.excalibur.event.PreparedEnqueue; 24 import org.apache.excalibur.event.Sink; 25 import org.apache.excalibur.event.SinkException; 26 import org.apache.excalibur.event.SinkFullException; 27 28 40 public class LossyMultiCastSink implements Sink 41 { 42 46 private final Collection m_sinks; 47 48 51 private final int m_size; 52 53 56 private final boolean m_oneSuccess; 57 58 68 public LossyMultiCastSink(Collection sinks) 69 { 70 this(sinks, false); 71 } 72 73 83 public LossyMultiCastSink(Collection sinks, boolean oneSuccess) 84 { 85 m_sinks = sinks; 86 m_size = -1; 87 m_oneSuccess = oneSuccess; 88 } 89 90 94 public int canAccept() 95 { 96 return 0; 97 } 98 99 102 public boolean isFull() 103 { 104 return false; 105 } 106 107 110 public int maxSize() 111 { 112 return 0; 113 } 114 115 118 public void enqueue(Object element) throws SinkException 119 { 120 final Iterator sinks = m_sinks.iterator(); 121 122 int successful = 0; 123 124 126 while (sinks.hasNext()) 128 { 129 final Sink sink = (Sink) sinks.next(); 130 131 final boolean enqueued = sink.tryEnqueue(element); 132 133 if (enqueued) 135 { 136 successful++; 137 break; 138 } 139 } 140 141 if (successful == 0 && m_oneSuccess) 142 { 143 throw new SinkFullException("Could not deliver one single element."); 144 } 145 } 146 147 150 public void enqueue(Object [] elements) throws SinkException 151 { 152 final Iterator sinks = m_sinks.iterator(); 153 154 int successful = 0; 155 156 158 while (sinks.hasNext()) 160 { 161 final Sink sink = (Sink) sinks.next(); 162 163 try 164 { 165 sink.enqueue(elements); 166 } 167 catch (SinkFullException e) 168 { 169 continue; 170 } 171 172 successful++; 174 break; 175 } 176 177 if (successful == 0 && m_oneSuccess) 178 { 179 throw new SinkFullException("Could not deliver one single elements."); 180 } 181 } 182 183 186 public boolean tryEnqueue(Object element) 187 { 188 try 189 { 190 enqueue(element); 191 return true; 192 } 193 catch (SinkException e) 194 { 195 return !m_oneSuccess; 196 } 197 } 198 199 202 public PreparedEnqueue prepareEnqueue(Object [] elements) 203 throws SinkException 204 { 205 final Iterator sinks = m_sinks.iterator(); 206 final DefaultPreparedEnqueue prepares = new DefaultPreparedEnqueue(); 207 208 int successful = 0; 209 210 212 while (sinks.hasNext()) 214 { 215 final Sink sink = (Sink) sinks.next(); 216 217 try 218 { 219 prepares.addPreparedEnqueue(sink.prepareEnqueue(elements)); 220 } 221 catch (SinkFullException e) 222 { 223 continue; 224 } 225 226 successful++; 228 break; 229 } 230 if (successful == 0 && m_oneSuccess) 231 { 232 throw new SinkFullException("Could not deliver elements at all."); 233 } 234 235 return prepares; 236 } 237 238 241 public int size() 242 { 243 return m_size; 244 } 245 246 255 private static final class DefaultPreparedEnqueue 256 implements PreparedEnqueue 257 { 258 261 private final Collection m_preparedEnqueues = new LinkedList (); 262 263 267 public void abort() 268 { 269 final Iterator iter = m_preparedEnqueues.iterator(); 270 271 while (iter.hasNext()) 272 { 273 ((PreparedEnqueue) iter.next()).abort(); 274 } 275 } 276 277 280 public void commit() 281 { 282 final Iterator iter = m_preparedEnqueues.iterator(); 283 284 while (iter.hasNext()) 285 { 286 ((PreparedEnqueue) iter.next()).commit(); 287 } 288 } 289 290 299 public void addPreparedEnqueue(PreparedEnqueue preparedEnqueue) 300 { 301 m_preparedEnqueues.add(preparedEnqueue); 302 } 303 } } 305 | Popular Tags |