1 /***
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.activeio;
18
19 import java.io.IOException;
20
21
22 /***
23 * A AsynchChannelFilter can be used as a filter between a {@see org.activeio.AsynchChannel}
24 * and it's {@see org.activeio.ChannelConsumer}. Most {@see org.activeio.AsynchChannel}
25 * that are not directly accessing the network will extends the AsynchChannelFilter since they act as a
26 * filter between the client and the network. O
27 *
28 * @version $Revision$
29 */
30 public class FilterAsynchChannel implements AsynchChannel, AsynchChannelListener {
31
32 final protected AsynchChannel next;
33 protected AsynchChannelListener channelListener;
34
35 public FilterAsynchChannel(AsynchChannel next) {
36 this.next = next;
37 }
38
39 /***
40 */
41 public void setAsynchChannelListener(AsynchChannelListener channelListener) {
42 this.channelListener = channelListener;
43 if (channelListener == null)
44 next.setAsynchChannelListener(null);
45 else
46 next.setAsynchChannelListener(this);
47 }
48
49 /***
50 * @see org.activeio.Channel#write(org.activeio.channel.Packet)
51 */
52 public void write(Packet packet) throws IOException {
53 next.write(packet);
54 }
55
56 /***
57 * @see org.activeio.Channel#flush()
58 */
59 public void flush() throws IOException {
60 next.flush();
61 }
62
63 /***
64 * @see org.activeio.Disposable#dispose()
65 */
66 public void dispose() {
67 next.dispose();
68 }
69
70 /***
71 * @see org.activeio.Service#start()
72 * @throws IOException if the next channel has not been set.
73 */
74 public void start() throws IOException {
75 if( next == null )
76 throw new IOException("The next channel has not been set.");
77 if( channelListener ==null )
78 throw new IOException("The UpPacketListener has not been set.");
79 next.start();
80 }
81
82 /***
83 * @see org.activeio.Service#stop(long)
84 */
85 public void stop(long timeout) throws IOException {
86 next.stop(timeout);
87 }
88
89 /***
90 * @see org.activeio.AsynchChannelListener#onPacket(org.activeio.channel.Packet)
91 */
92 public void onPacket(Packet packet) {
93 channelListener.onPacket(packet);
94 }
95
96 /***
97 * @see org.activeio.AsynchChannelListener#onPacketError(org.activeio.channel.ChannelException)
98 */
99 public void onPacketError(IOException error) {
100 channelListener.onPacketError(error);
101 }
102
103 /***
104 * @return Returns the next.
105 */
106 public AsynchChannel getNext() {
107 return next;
108 }
109
110 /***
111 * @return Returns the packetListener.
112 */
113 public AsynchChannelListener getAsynchChannelListener() {
114 return channelListener;
115 }
116
117 public Object narrow(Class target) {
118 if( target.isAssignableFrom(getClass()) ) {
119 return this;
120 }
121 return next.narrow(target);
122 }
123
124 public String toString() {
125 return next.toString();
126 }
127 }