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.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: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
    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.
    44       */
    45      private final ThreadModel defaultThreadModel = ExecutorThreadModel
    46              .getInstance("AnonymousIoService");
    47
    48      /**
    49       * Current thread model.
    50       */
    51      private ThreadModel threadModel = defaultThreadModel;
    52
    53      public BaseIoServiceConfig() {
    54          super();
    55      }
    56
    57      public IoFilterChainBuilder getFilterChainBuilder() {
    58          return filterChainBuilder;
    59      }
    60
    61      public void setFilterChainBuilder(IoFilterChainBuilder builder) {
    62          if (builder == null) {
    63              builder = new DefaultIoFilterChainBuilder();
    64          }
    65          filterChainBuilder = builder;
    66      }
    67
    68      public DefaultIoFilterChainBuilder getFilterChain() {
    69          if (filterChainBuilder instanceof DefaultIoFilterChainBuilder) {
    70              return (DefaultIoFilterChainBuilder) filterChainBuilder;
    71          } else {
    72              throw new IllegalStateException(
    73                      "Current filter chain builder is not a DefaultIoFilterChainBuilder.");
    74          }
    75      }
    76
    77      public ThreadModel getThreadModel() {
    78          return threadModel;
    79      }
    80
    81      public void setThreadModel(ThreadModel threadModel) {
    82          if (threadModel == null) {
    83              // We reuse the previous default model to prevent too much
    84              // daemon threads are created.
    85              threadModel = defaultThreadModel;
    86          }
    87          this.threadModel = threadModel;
    88      }
    89
    90      @Override
    91      public Object clone() {
    92          BaseIoServiceConfig ret;
    93          try {
    94              ret = (BaseIoServiceConfig) super.clone();
    95          } catch (CloneNotSupportedException e) {
    96              throw (InternalError) new InternalError().initCause(e);
    97          }
    98
    99          // Try to clone the chain builder.
    100         try {
    101             Method cloneMethod = this.filterChainBuilder.getClass().getMethod(
    102                     "clone");
    103             if (cloneMethod.isAccessible()) {
    104                 ret.filterChainBuilder = (IoFilterChainBuilder) cloneMethod
    105                         .invoke(this.filterChainBuilder);
    106             }
    107         } catch (Exception e) {
    108             // uncloneable
    109         }
    110
    111         return ret;
    112     }
    113 }