View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the 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  public class DefaultChannelConfig implements ChannelConfig {
    30
    31      private volatile ChannelBufferFactory bufferFactory = HeapChannelBufferFactory.getInstance();
    32      private volatile int connectTimeoutMillis = 10000; // 10 seconds
    33
    34      /**
    35       * Creates a new instance.
    36       */
    37      public DefaultChannelConfig() {
    38          super();
    39      }
    40
    41      public void setOptions(Map<String, Object> options) {
    42          for (Entry<String, Object> e: options.entrySet()) {
    43              setOption(e.getKey(), e.getValue());
    44          }
    45      }
    46
    47      public boolean setOption(String key, Object value) {
    48          if (key.equals("pipelineFactory")) {
    49              setPipelineFactory((ChannelPipelineFactory) value);
    50          } else if (key.equals("connectTimeoutMillis")) {
    51              setConnectTimeoutMillis(ConversionUtil.toInt(value));
    52          } else if (key.equals("bufferFactory")) {
    53              setBufferFactory((ChannelBufferFactory) value);
    54          } else {
    55              return false;
    56          }
    57          return true;
    58      }
    59
    60      public int getConnectTimeoutMillis() {
    61          return connectTimeoutMillis;
    62      }
    63
    64      public ChannelBufferFactory getBufferFactory() {
    65          return bufferFactory;
    66      }
    67
    68      public void setBufferFactory(ChannelBufferFactory bufferFactory) {
    69          if (bufferFactory == null) {
    70              throw new NullPointerException("bufferFactory");
    71          }
    72          this.bufferFactory = bufferFactory;
    73      }
    74
    75      public ChannelPipelineFactory getPipelineFactory() {
    76          return null;
    77      }
    78
    79      public void setConnectTimeoutMillis(int connectTimeoutMillis) {
    80          if (connectTimeoutMillis < 0) {
    81              throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
    82          }
    83          this.connectTimeoutMillis = connectTimeoutMillis;
    84      }
    85
    86      public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
    87          // Unused
    88      }
    89  }