01: /* ====================================================================
02: The Jicarilla Software License
03:
04: Copyright (c) 2003 Leo Simons.
05: All rights reserved.
06:
07: Permission is hereby granted, free of charge, to any person obtaining
08: a copy of this software and associated documentation files (the
09: "Software"), to deal in the Software without restriction, including
10: without limitation the rights to use, copy, modify, merge, publish,
11: distribute, sublicense, and/or sell copies of the Software, and to
12: permit persons to whom the Software is furnished to do so, subject to
13: the following conditions:
14:
15: The above copyright notice and this permission notice shall be
16: included in all copies or substantial portions of the Software.
17:
18: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25: ==================================================================== */
26: package org.jicarilla.plumbing;
27:
28: import EDU.oswego.cs.dl.util.concurrent.Executor;
29: import org.jicarilla.plumbing.Sink;
30: import org.jicarilla.plumbing.Source;
31: import org.jicarilla.lang.AbstractExecutable;
32: import org.jicarilla.lang.Assert;
33:
34: /**
35: * <p>Sets up a connection between a source and a sink that will do nothing but
36: * continuously move messages from source to sink. In other words, use a
37: * <code>Connector</code> to creates a zero-length pipe/channel between a
38: * source and a sink. This movement is done by a supplied {@link Executor},
39: * so will happen asynchronously.</p>
40: *
41: * <p>Note that this class is {@link org.jicarilla.lang.Active}, and extends from
42: * {@link org.jicarilla.lang.AbstractExecutable}. As such, you should not override the
43: * {@link #initialize()} nor the {@link #dispose()} methods, but only ever
44: * override {@link #doInitialize()} and/or {@link #doDispose()}. When you do,
45: * make sure to call the overridden methods as well.</p>
46: *
47: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
48: * @version $Id: Connector.java,v 1.2 2004/03/23 13:37:58 lsimons Exp $
49: */
50: public class Connector extends AbstractExecutable {
51: /** The source to retrieve messages from. */
52: protected final Source m_source;
53: /** The sink to send messages to. */
54: protected final Sink m_sink;
55:
56: /**
57: * Create a new instance.
58: *
59: * @param source the source to retrieve messages from
60: * @param sink the sink to send messages to
61: * @param executor the executor to pass on to {@link AbstractExecutable}
62: */
63: public Connector(final Source source, final Sink sink,
64: final Executor executor) {
65: super (executor);
66:
67: Assert.assertNotNull(source);
68: Assert.assertNotNull(sink);
69:
70: m_source = source;
71: m_sink = sink;
72: }
73:
74: /**
75: * Get a message from the source and send it to the sink.
76: *
77: * @throws InterruptedException if the current thread has been
78: * {@link java.lang.Thread#interrupt()}ed
79: */
80: protected void work() throws InterruptedException {
81: final Object msg = m_source.take();
82: m_sink.put(msg);
83: }
84: }
|