View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.channel;
    17
    18  import java.util.Map;
    19  import java.util.Map.Entry;
    20
    21  import org.jboss.netty.buffer.ChannelBufferFactory;
    22  import org.jboss.netty.buffer.HeapChannelBufferFactory;
    23  import org.jboss.netty.channel.socket.SocketChannelConfig;
    24  import org.jboss.netty.util.internal.ConversionUtil;
    25
    26  /**
    27   * The default {@link SocketChannelConfig} implementation.
    28   *
    29   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    30   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    31   *
    32   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    33   *
    34   */
    35  public class DefaultChannelConfig implements ChannelConfig {
    36
    37      private volatile ChannelBufferFactory bufferFactory = HeapChannelBufferFactory.getInstance();
    38      private volatile int connectTimeoutMillis = 10000; // 10 seconds
    39
    40      /**
    41       * Creates a new instance.
    42       */
    43      public DefaultChannelConfig() {
    44          super();
    45      }
    46
    47      public void setOptions(Map<String, Object> options) {
    48          for (Entry<String, Object> e: options.entrySet()) {
    49              setOption(e.getKey(), e.getValue());
    50          }
    51      }
    52
    53      public boolean setOption(String key, Object value) {
    54          if (key.equals("pipelineFactory")) {
    55              setPipelineFactory((ChannelPipelineFactory) value);
    56          } else if (key.equals("connectTimeoutMillis")) {
    57              setConnectTimeoutMillis(ConversionUtil.toInt(value));
    58          } else if (key.equals("bufferFactory")) {
    59              setBufferFactory((ChannelBufferFactory) value);
    60          } else {
    61              return false;
    62          }
    63          return true;
    64      }
    65
    66      public int getConnectTimeoutMillis() {
    67          return connectTimeoutMillis;
    68      }
    69
    70      public ChannelBufferFactory getBufferFactory() {
    71          return bufferFactory;
    72      }
    73
    74      public void setBufferFactory(ChannelBufferFactory bufferFactory) {
    75          if (bufferFactory == null) {
    76              throw new NullPointerException("bufferFactory");
    77          }
    78          this.bufferFactory = bufferFactory;
    79      }
    80
    81      public ChannelPipelineFactory getPipelineFactory() {
    82          return null;
    83      }
    84
    85      public void setConnectTimeoutMillis(int connectTimeoutMillis) {
    86          if (connectTimeoutMillis < 0) {
    87              throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
    88          }
    89          this.connectTimeoutMillis = connectTimeoutMillis;
    90      }
    91
    92      public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
    93          // Unused
    94      }
    95  }