查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.lang.reflect.Method;
23  
24  import org.apache.mina.common.DefaultIoFilterChainBuilder;
25  import org.apache.mina.common.ExecutorThreadModel;
26  import org.apache.mina.common.IoFilterChainBuilder;
27  import org.apache.mina.common.IoServiceConfig;
28  import org.apache.mina.common.ThreadModel;
29  
30  /**
31   * A base implementation of {@link IoServiceConfig}.
32   *
33   * @author The Apache Directory Project (mina-dev@directory.apache.org)
34   * @version $Rev: 629332 $, $Date: 2008-02-20 12:33:32 +0900 (Wed, 20 Feb 2008) $
35   */
36  public abstract class BaseIoServiceConfig implements IoServiceConfig, Cloneable {
37      /**
38       * Current filter chain builder.
39       */
40      private IoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
41  
42      /**
43       * The default thread model (initialized lazily).
44       */
45      private ThreadModel defaultThreadModel;
46  
47      /**
48       * Current thread model.
49       */
50      private ThreadModel threadModel;
51  
52      public BaseIoServiceConfig() {
53          super();
54      }
55  
56      public IoFilterChainBuilder getFilterChainBuilder() {
57          return filterChainBuilder;
58      }
59  
60      public void setFilterChainBuilder(IoFilterChainBuilder builder) {
61          if (builder == null) {
62              builder = new DefaultIoFilterChainBuilder();
63          }
64          filterChainBuilder = builder;
65      }
66  
67      public DefaultIoFilterChainBuilder getFilterChain() {
68          if (filterChainBuilder instanceof DefaultIoFilterChainBuilder) {
69              return (DefaultIoFilterChainBuilder) filterChainBuilder;
70          } else {
71              throw new IllegalStateException(
72                      "Current filter chain builder is not a DefaultIoFilterChainBuilder.");
73          }
74      }
75  
76      public ThreadModel getThreadModel() {
77          if (threadModel == null) {
78              threadModel = getDefaultThreadModel();
79          }
80          return threadModel;
81      }
82  
83      public void setThreadModel(ThreadModel threadModel) {
84          if (threadModel == null) {
85              // We reuse the previous default model to prevent too much
86              // daemon threads are created.
87              threadModel = getDefaultThreadModel();
88          }
89          this.threadModel = threadModel;
90      }
91  
92      private synchronized ThreadModel getDefaultThreadModel() {
93          if (defaultThreadModel == null) {
94              defaultThreadModel = ExecutorThreadModel.getInstance("AnonymousIoService");
95          }
96          return defaultThreadModel;
97      }
98      @Override
99      public Object clone() {
100         BaseIoServiceConfig ret;
101         try {
102             ret = (BaseIoServiceConfig) super.clone();
103         } catch (CloneNotSupportedException e) {
104             throw (InternalError) new InternalError().initCause(e);
105         }
106 
107         // Try to clone the chain builder.
108         try {
109             Method cloneMethod = this.filterChainBuilder.getClass().getMethod(
110                     "clone");
111             if (cloneMethod.isAccessible()) {
112                 ret.filterChainBuilder = (IoFilterChainBuilder) cloneMethod
113                         .invoke(this.filterChainBuilder);
114             }
115         } catch (Exception e) {
116             // uncloneable
117         }
118 
119         return ret;
120     }
121 }