1
18
19 package org.xnio;
20
21 import org.xnio.channels.CloseListenerSettable;
22 import org.xnio.conduits.ConduitStreamSinkChannel;
23 import org.xnio.conduits.ConduitStreamSourceChannel;
24 import org.xnio.conduits.StreamSinkConduit;
25 import org.xnio.conduits.StreamSourceConduit;
26
27 import static org.xnio._private.Messages.msg;
28
29
34 public abstract class StreamConnection extends Connection implements CloseListenerSettable<StreamConnection> {
35
36 private ConduitStreamSourceChannel sourceChannel;
37 private ConduitStreamSinkChannel sinkChannel;
38 private ChannelListener<? super StreamConnection> closeListener;
39
40
45 protected StreamConnection(final XnioIoThread thread) {
46 super(thread);
47 }
48
49 public void setCloseListener(final ChannelListener<? super StreamConnection> listener) {
50 this.closeListener = listener;
51 }
52
53 public ChannelListener<? super StreamConnection> getCloseListener() {
54 return closeListener;
55 }
56
57 public ChannelListener.Setter<? extends StreamConnection> getCloseSetter() {
58 return new Setter<StreamConnection>(this);
59 }
60
61
66 protected void setSourceConduit(StreamSourceConduit conduit) {
67 this.sourceChannel = conduit == null ? null : new ConduitStreamSourceChannel(this, conduit);
68 }
69
70
75 protected void setSinkConduit(StreamSinkConduit conduit) {
76 this.sinkChannel = conduit == null ? null : new ConduitStreamSinkChannel(this, conduit);
77 }
78
79 void invokeCloseListener() {
80 ChannelListeners.invokeChannelListener(this, closeListener);
81 }
82
83 private static <T> T notNull(T orig) throws IllegalStateException {
84 if (orig == null) {
85 throw msg.channelNotAvailable();
86 }
87 return orig;
88 }
89
90
95 public ConduitStreamSourceChannel getSourceChannel() {
96 return notNull(sourceChannel);
97 }
98
99
104 public ConduitStreamSinkChannel getSinkChannel() {
105 return notNull(sinkChannel);
106 }
107 }
108