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  
30  /**
31   * Base implementation of {@link IoService}s.
32   * 
33   * @author The Apache Directory Project (mina-dev@directory.apache.org)
34   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
35   */
36  public abstract class BaseIoService implements IoService {
37      /**
38       * Current filter chain builder.
39       */
40      private IoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
41  
42      /**
43       * Maintains the {@link IoServiceListener}s of this service.
44       */
45      private final IoServiceListenerSupport listeners;
46  
47      protected BaseIoService() {
48          this.listeners = new IoServiceListenerSupport();
49      }
50  
51      public IoFilterChainBuilder getFilterChainBuilder() {
52          return filterChainBuilder;
53      }
54  
55      public void setFilterChainBuilder(IoFilterChainBuilder builder) {
56          if (builder == null) {
57              builder = new DefaultIoFilterChainBuilder();
58          }
59          filterChainBuilder = builder;
60      }
61  
62      public DefaultIoFilterChainBuilder getFilterChain() {
63          if (filterChainBuilder instanceof DefaultIoFilterChainBuilder) {
64              return (DefaultIoFilterChainBuilder) filterChainBuilder;
65          } else {
66              throw new IllegalStateException(
67                      "Current filter chain builder is not a DefaultIoFilterChainBuilder.");
68          }
69      }
70  
71      public void addListener(IoServiceListener listener) {
72          getListeners().add(listener);
73      }
74  
75      public void removeListener(IoServiceListener listener) {
76          getListeners().remove(listener);
77      }
78  
79      public Set getManagedServiceAddresses() {
80          return getListeners().getManagedServiceAddresses();
81      }
82  
83      public Set getManagedSessions(SocketAddress serviceAddress) {
84          return getListeners().getManagedSessions(serviceAddress);
85      }
86  
87      public boolean isManaged(SocketAddress serviceAddress) {
88          return getListeners().isManaged(serviceAddress);
89      }
90  
91      protected IoServiceListenerSupport getListeners() {
92          return listeners;
93      }
94  }