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      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.equals("pipelineFactory")) {
42              setPipelineFactory((ChannelPipelineFactory) value);
43          } else if (key.equals("connectTimeoutMillis")) {
44              setConnectTimeoutMillis(ConversionUtil.toInt(value));
45          } else if (key.equals("bufferFactory")) {
46              setBufferFactory((ChannelBufferFactory) value);
47          } else {
48              return false;
49          }
50          return true;
51      }
52  
53      public int getConnectTimeoutMillis() {
54          return connectTimeoutMillis;
55      }
56  
57      public ChannelBufferFactory getBufferFactory() {
58          return bufferFactory;
59      }
60  
61      public void setBufferFactory(ChannelBufferFactory bufferFactory) {
62          if (bufferFactory == null) {
63              throw new NullPointerException("bufferFactory");
64          }
65          this.bufferFactory = bufferFactory;
66      }
67  
68      public ChannelPipelineFactory getPipelineFactory() {
69          return null;
70      }
71  
72      public void setConnectTimeoutMillis(int connectTimeoutMillis) {
73          if (connectTimeoutMillis < 0) {
74              throw new IllegalArgumentException("connectTimeoutMillis: " + connectTimeoutMillis);
75          }
76          this.connectTimeoutMillis = connectTimeoutMillis;
77      }
78  
79      public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
80          // Unused
81      }
82  }