View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.common.support;
    21
    22  import java.net.SocketAddress;
    23  import java.util.Set;
    24
    25  import org.apache.mina.common.DefaultIoFilterChainBuilder;
    26  import org.apache.mina.common.IoFilterChainBuilder;
    27  import org.apache.mina.common.IoService;
    28  import org.apache.mina.common.IoServiceListener;
    29  import org.apache.mina.common.IoSession;
    30
    31  /**
    32   * Base implementation of {@link IoService}s.
    33   *
    34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    35   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    36   */
    37  public abstract class BaseIoService implements IoService {
    38      /**
    39       * Current filter chain builder.
    40       */
    41      private IoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
    42
    43      /**
    44       * Maintains the {@link IoServiceListener}s of this service.
    45       */
    46      private final IoServiceListenerSupport listeners;
    47
    48      protected BaseIoService() {
    49          this.listeners = new IoServiceListenerSupport();
    50      }
    51
    52      public IoFilterChainBuilder getFilterChainBuilder() {
    53          return filterChainBuilder;
    54      }
    55
    56      public void setFilterChainBuilder(IoFilterChainBuilder builder) {
    57          if (builder == null) {
    58              builder = new DefaultIoFilterChainBuilder();
    59          }
    60          filterChainBuilder = builder;
    61      }
    62
    63      public DefaultIoFilterChainBuilder getFilterChain() {
    64          if (filterChainBuilder instanceof DefaultIoFilterChainBuilder) {
    65              return (DefaultIoFilterChainBuilder) filterChainBuilder;
    66          } else {
    67              throw new IllegalStateException(
    68                      "Current filter chain builder is not a DefaultIoFilterChainBuilder.");
    69          }
    70      }
    71
    72      public void addListener(IoServiceListener listener) {
    73          getListeners().add(listener);
    74      }
    75
    76      public void removeListener(IoServiceListener listener) {
    77          getListeners().remove(listener);
    78      }
    79
    80      public Set<SocketAddress> getManagedServiceAddresses() {
    81          return getListeners().getManagedServiceAddresses();
    82      }
    83
    84      public Set<IoSession> getManagedSessions(SocketAddress serviceAddress) {
    85          return getListeners().getManagedSessions(serviceAddress);
    86      }
    87
    88      public boolean isManaged(SocketAddress serviceAddress) {
    89          return getListeners().isManaged(serviceAddress);
    90      }
    91
    92      protected IoServiceListenerSupport getListeners() {
    93          return listeners;
    94      }
    95  }