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 org.jboss.netty.buffer.ChannelBufferFactory;
19  import org.jboss.netty.buffer.HeapChannelBufferFactory;
20  import org.jboss.netty.channel.socket.SocketChannelConfig;
21  import org.jboss.netty.util.internal.ConversionUtil;
22  
23  import java.util.Map;
24  import java.util.Map.Entry;
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      public void setOptions(Map<String, Object> options) {
35          for (Entry<String, Object> e: options.entrySet()) {
36              setOption(e.getKey(), e.getValue());
37          }
38      }
39  
40      public boolean setOption(String key, Object value) {
41          if (key == null) {
42              throw new NullPointerException("key");
43          }
44  
45          if ("pipelineFactory".equals(key)) {
46              setPipelineFactory((ChannelPipelineFactory) value);
47          } else if ("connectTimeoutMillis".equals(key)) {
48              setConnectTimeoutMillis(ConversionUtil.toInt(value));
49          } else if ("bufferFactory".equals(key)) {
50              setBufferFactory((ChannelBufferFactory) value);
51          } else {
52              return false;
53          }
54          return true;
55      }
56  
57      public int getConnectTimeoutMillis() {
58          return connectTimeoutMillis;
59      }
60  
61      public ChannelBufferFactory getBufferFactory() {
62          return bufferFactory;
63      }
64  
65      public void setBufferFactory(ChannelBufferFactory bufferFactory) {
66          if (bufferFactory == null) {
67              throw new NullPointerException("bufferFactory");
68          }
69          this.bufferFactory = bufferFactory;
70      }
71  
72      public ChannelPipelineFactory getPipelineFactory() {
73          return null;
74      }
75  
76      public void setConnectTimeoutMillis(int connectTimeoutMillis) {
77          if (connectTimeoutMillis < 0) {
78              throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
79          }
80          this.connectTimeoutMillis = connectTimeoutMillis;
81      }
82  
83      public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
84          // Unused
85      }
86  }