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 io.netty.channel;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.util.AbstractConstant;
20  import io.netty.util.ConstantPool;
21  
22  import java.net.InetAddress;
23  import java.net.NetworkInterface;
24  
25  /**
26   * A {@link ChannelOption} allows to configure a {@link ChannelConfig} in a type-safe
27   * way. Which {@link ChannelOption} is supported depends on the actual implementation
28   * of {@link ChannelConfig} and may depend on the nature of the transport it belongs
29   * to.
30   *
31   * @param <T>   the type of the value which is valid for the {@link ChannelOption}
32   */
33  public class ChannelOption<T> extends AbstractConstant<ChannelOption<T>> {
34  
35      private static final ConstantPool<ChannelOption<Object>> pool = new ConstantPool<ChannelOption<Object>>() {
36          @Override
37          protected ChannelOption<Object> newConstant(int id, String name) {
38              return new ChannelOption<Object>(id, name);
39          }
40      };
41  
42      /**
43       * Returns the {@link ChannelOption} of the specified name.
44       */
45      @SuppressWarnings("unchecked")
46      public static <T> ChannelOption<T> valueOf(String name) {
47          return (ChannelOption<T>) pool.valueOf(name);
48      }
49  
50      /**
51       * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
52       */
53      @SuppressWarnings("unchecked")
54      public static <T> ChannelOption<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
55          return (ChannelOption<T>) pool.valueOf(firstNameComponent, secondNameComponent);
56      }
57  
58      /**
59       * Returns {@code true} if a {@link ChannelOption} exists for the given {@code name}.
60       */
61      public static boolean exists(String name) {
62          return pool.exists(name);
63      }
64  
65      /**
66       * Creates a new {@link ChannelOption} for the given {@code name} or fail with an
67       * {@link IllegalArgumentException} if a {@link ChannelOption} for the given {@code name} exists.
68       */
69      @SuppressWarnings("unchecked")
70      public static <T> ChannelOption<T> newInstance(String name) {
71          return (ChannelOption<T>) pool.newInstance(name);
72      }
73  
74      public static final ChannelOption<ByteBufAllocator> ALLOCATOR = valueOf("ALLOCATOR");
75      public static final ChannelOption<RecvByteBufAllocator> RCVBUF_ALLOCATOR = valueOf("RCVBUF_ALLOCATOR");
76      public static final ChannelOption<MessageSizeEstimator> MESSAGE_SIZE_ESTIMATOR = valueOf("MESSAGE_SIZE_ESTIMATOR");
77  
78      public static final ChannelOption<Integer> CONNECT_TIMEOUT_MILLIS = valueOf("CONNECT_TIMEOUT_MILLIS");
79      /**
80       * @deprecated Use {@link MaxMessagesRecvByteBufAllocator}
81       * and {@link MaxMessagesRecvByteBufAllocator#maxMessagesPerRead(int)}.
82       */
83      @Deprecated
84      public static final ChannelOption<Integer> MAX_MESSAGES_PER_READ = valueOf("MAX_MESSAGES_PER_READ");
85      public static final ChannelOption<Integer> WRITE_SPIN_COUNT = valueOf("WRITE_SPIN_COUNT");
86      /**
87       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
88       */
89      @Deprecated
90      public static final ChannelOption<Integer> WRITE_BUFFER_HIGH_WATER_MARK = valueOf("WRITE_BUFFER_HIGH_WATER_MARK");
91      /**
92       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
93       */
94      @Deprecated
95      public static final ChannelOption<Integer> WRITE_BUFFER_LOW_WATER_MARK = valueOf("WRITE_BUFFER_LOW_WATER_MARK");
96      public static final ChannelOption<WriteBufferWaterMark> WRITE_BUFFER_WATER_MARK =
97              valueOf("WRITE_BUFFER_WATER_MARK");
98  
99      public static final ChannelOption<Boolean> ALLOW_HALF_CLOSURE = valueOf("ALLOW_HALF_CLOSURE");
100     public static final ChannelOption<Boolean> AUTO_READ = valueOf("AUTO_READ");
101 
102     /**
103      * If {@code true} then the {@link Channel} is closed automatically and immediately on write failure.
104      * The default value is {@code true}.
105      */
106     public static final ChannelOption<Boolean> AUTO_CLOSE = valueOf("AUTO_CLOSE");
107 
108     public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
109     public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
110     public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");
111     public static final ChannelOption<Integer> SO_RCVBUF = valueOf("SO_RCVBUF");
112     public static final ChannelOption<Boolean> SO_REUSEADDR = valueOf("SO_REUSEADDR");
113     public static final ChannelOption<Integer> SO_LINGER = valueOf("SO_LINGER");
114     public static final ChannelOption<Integer> SO_BACKLOG = valueOf("SO_BACKLOG");
115     public static final ChannelOption<Integer> SO_TIMEOUT = valueOf("SO_TIMEOUT");
116 
117     public static final ChannelOption<Integer> IP_TOS = valueOf("IP_TOS");
118     public static final ChannelOption<InetAddress> IP_MULTICAST_ADDR = valueOf("IP_MULTICAST_ADDR");
119     public static final ChannelOption<NetworkInterface> IP_MULTICAST_IF = valueOf("IP_MULTICAST_IF");
120     public static final ChannelOption<Integer> IP_MULTICAST_TTL = valueOf("IP_MULTICAST_TTL");
121     public static final ChannelOption<Boolean> IP_MULTICAST_LOOP_DISABLED = valueOf("IP_MULTICAST_LOOP_DISABLED");
122 
123     public static final ChannelOption<Boolean> TCP_NODELAY = valueOf("TCP_NODELAY");
124 
125     @Deprecated
126     public static final ChannelOption<Boolean> DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION =
127             valueOf("DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION");
128 
129     public static final ChannelOption<Boolean> SINGLE_EVENTEXECUTOR_PER_GROUP =
130             valueOf("SINGLE_EVENTEXECUTOR_PER_GROUP");
131 
132     /**
133      * Creates a new {@link ChannelOption} with the specified unique {@code name}.
134      */
135     private ChannelOption(int id, String name) {
136         super(id, name);
137     }
138 
139     @Deprecated
140     protected ChannelOption(String name) {
141         this(pool.nextId(), name);
142     }
143 
144     /**
145      * Validate the value which is set for the {@link ChannelOption}. Sub-classes
146      * may override this for special checks.
147      */
148     public void validate(T value) {
149         if (value == null) {
150             throw new NullPointerException("value");
151         }
152     }
153 }