FilterAsynchChannelServer xref

View Javadoc

    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  import java.net.URI;
    21
    22
    23  /***
    24   * A AsynchChannelFilter can be used as a filter between a {@see org.activeio.AsynchChannel}
    25   * and it's {@see org.activeio.ChannelConsumer}.  Most {@see org.activeio.AsynchChannel}
    26   * that are not directly accessing the network will extends the AsynchChannelFilter since they act as a
    27   * filter between the client and the network.  O
    28   *
    29   * @version $Revision$
    30   */
    31  public class FilterAsynchChannelServer implements AsynchChannelServer, AcceptListener {
    32
    33      final protected AsynchChannelServer next;
    34      protected AcceptListener acceptListener;
    35
    36      public FilterAsynchChannelServer(AsynchChannelServer next) {
    37          this.next = next;
    38          if( next == null )
    39              throw new IllegalArgumentException("The next AsynchChannelServer cannot be null.");
    40      }
    41
    42      public void setAcceptListener(AcceptListener acceptListener) {
    43          this.acceptListener = acceptListener;
    44          if (acceptListener == null)
    45              next.setAcceptListener(null);
    46          else
    47              next.setAcceptListener(this);
    48
    49      }
    50
    51      /***
    52       * @see org.activeio.Disposable#dispose()
    53       */
    54      public void dispose() {
    55          next.dispose();
    56      }
    57
    58      /***
    59       * @see org.activeio.Service#start()
    60       * @throws IOException if the next channel has not been set.
    61       */
    62      public void start() throws IOException {
    63          if( acceptListener ==null )
    64              throw new IOException("The AcceptListener has not been set.");
    65          next.start();
    66      }
    67
    68      /***
    69       * @see org.activeio.Service#stop(long)
    70       */
    71      public void stop(long timeout) throws IOException {
    72          next.stop(timeout);
    73      }
    74
    75      public void onAccept(Channel channel) {
    76          acceptListener.onAccept(channel);
    77      }
    78
    79      public void onAcceptError(IOException error) {
    80          acceptListener.onAcceptError(error);
    81      }
    82
    83      public URI getBindURI() {
    84          return next.getBindURI();
    85      }
    86
    87      public URI getConnectURI() {
    88          return next.getConnectURI();
    89      }
    90
    91      public Object narrow(Class target) {
    92          if( target.isAssignableFrom(getClass()) ) {
    93              return this;
    94          }
    95          return next.narrow(target);
    96      }
    97
    98      public String toString() {
    99          return next.toString();
    100     }
    101  }