KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > event > impl > MultiCastSink


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.event.impl;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
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 /**
29  * This is a {@link org.apache.excalibur.event.seda.event.Sink}
30  * implementation that multicasts enqueue operations to the
31  * contained and concrete sink objects. The multi cast sink
32  * will try to enqueue and only succeeds if no element was
33  * rejected from any sink. The sink can be configured to
34  * enqueue into one sink alone or all sinks.
35  * If a sink array in the collection of sinks contains more
36  * than one sink the multicast sink will try to enqueue the
37  * element always to <b>only one</b> of these sinks.
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version $Revision: 1.4 $
41  */

42 public class MultiCastSink implements Sink
43 {
44     /** A collection of sink arrays representing the sinks to enqueue to. */
45     private final Collection JavaDoc m_sinks;
46
47     /** The size of the sink. */
48     private final int m_size;
49
50     /** Boolean value describing if one or all operations must succeed. */
51     private final boolean m_single;
52
53     //---------------------- LossyMultiCastSink constructors
54
/**
55      * This constructor creates a failure in-tolerant multicast
56      * sink based on the collection of sink arrays. The delivery
57      * must succeed for all sinks in the collection or it will
58      * fail entirely.
59      * @since May 16, 2002
60      *
61      * @param sinks
62      * A collection of sink arrays for each stage.
63      */

64     public MultiCastSink(Collection JavaDoc sinks)
65     {
66         this(sinks, false);
67     }
68
69     /**
70      * This constructor creates a failure in-tolerant multicast
71      * sink based on the collection of sink arrays.
72      * @since May 16, 2002
73      *
74      * @param sinks
75      * A collection of sink arrays for each stage.
76      * @param single
77      * <m_code>true</m_code> if just one operation must succeed.
78      * <m_code>false</m_code> if all operations must succeed.
79      */

80     public MultiCastSink(Collection JavaDoc sinks, boolean single)
81     {
82         m_sinks = sinks;
83         m_size = -1;
84         m_single = single;
85     }
86
87     //---------------------- Sink implementation
88
/**
89      * @see Sink#canAccept()
90      */

91     public int canAccept()
92     {
93         return 0;
94     }
95
96     /**
97      * @see Sink#isFull()
98      */

99     public boolean isFull()
100     {
101         return false;
102     }
103
104     /**
105      * @see Sink#maxSize()
106      */

107     public int maxSize()
108     {
109         return 0;
110     }
111
112     /**
113      * @see Sink#enqueue(Object)
114      */

115     public void enqueue(Object JavaDoc element) throws SinkException
116     {
117         final PreparedEnqueue prepared;
118         prepared = prepareEnqueue(new Object JavaDoc[] { element });
119         prepared.commit();
120     }
121
122     /**
123      * @see Sink#enqueue(Object[])
124      */

125     public void enqueue(Object JavaDoc[] elements) throws SinkException
126     {
127         final PreparedEnqueue prepared = prepareEnqueue(elements);
128         prepared.commit();
129     }
130
131     /**
132      * @see Sink#tryEnqueue(Object)
133      */

134     public boolean tryEnqueue(Object JavaDoc element)
135     {
136         try
137         {
138             enqueue(element);
139             return true;
140         }
141         catch (SinkException e)
142         {
143             return false;
144         }
145     }
146
147     /**
148      * @see Sink#prepareEnqueue(Object[])
149      */

150     public PreparedEnqueue prepareEnqueue(Object JavaDoc[] elements)
151         throws SinkException
152     {
153
154         //checkEnqueuePredicate(elements);
155

156         final DefaultPreparedEnqueue prepares = new DefaultPreparedEnqueue();
157         int successful = 0;
158
159         final Iterator JavaDoc sinks = m_sinks.iterator();
160
161         // iterate through the sinks and try to enqueue
162
while (sinks.hasNext())
163         {
164             final Sink sink = (Sink) sinks.next();
165
166             try
167             {
168                 prepares.addPreparedEnqueue(sink.prepareEnqueue(elements));
169             }
170             catch (SinkFullException e)
171             {
172                 continue;
173             }
174
175             // if enqueue successful return here or just break and continue
176
if (m_single)
177             {
178                 return prepares;
179             }
180             successful++;
181             break;
182
183         }
184         if (successful < m_sinks.size())
185         {
186             // rollback all enqueues.
187
prepares.abort();
188
189             throw new SinkFullException("Could not deliver elements.");
190         }
191
192         return prepares;
193     }
194
195     /**
196      * @see Sink#size()
197      */

198     public int size()
199     {
200         return m_size;
201     }
202
203     //------------------------- LossyMultiCastSink inner classes
204
/**
205      * A prepared enqueue object that holds other prepared
206      * enqueue objects and allows to perform a commit / abort
207      * on all of these objects.
208      * @since May 16, 2002
209      *
210      * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
211      */

212     private static final class DefaultPreparedEnqueue
213         implements PreparedEnqueue
214     {
215         /**
216          * A collection of prepared enqueue objects
217          */

218         private final Collection JavaDoc m_preparedEnqueues = new LinkedList JavaDoc();
219
220         //------------------------ PreparedEnqueue implementation
221
/**
222          * @see PreparedEnqueue#abort()
223          */

224         public void abort()
225         {
226             final Iterator JavaDoc iter = m_preparedEnqueues.iterator();
227
228             while (iter.hasNext())
229             {
230                 ((PreparedEnqueue) iter.next()).abort();
231             }
232         }
233
234         /**
235          * @see PreparedEnqueue#commit()
236          */

237         public void commit()
238         {
239             final Iterator JavaDoc iter = m_preparedEnqueues.iterator();
240
241             while (iter.hasNext())
242             {
243                 ((PreparedEnqueue) iter.next()).commit();
244             }
245         }
246
247         //------------------------ DefaultPreparedEnqueue specific implementation
248
/**
249          * Adds a prepared enqueue object to the list
250          * of prepared enqueues.
251          * @since May 16, 2002
252          *
253          * @param preparedEnqueue
254          * The prepared enqueue object to be added.
255          */

256         public void addPreparedEnqueue(PreparedEnqueue preparedEnqueue)
257         {
258             m_preparedEnqueues.add(preparedEnqueue);
259         }
260     } //-- end DefaultPreparedEnqueue inner class
261
}
262
Popular Tags