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       */
82      @Deprecated
83      public static final ChannelOption<Integer> MAX_MESSAGES_PER_READ = valueOf("MAX_MESSAGES_PER_READ");
84      public static final ChannelOption<Integer> WRITE_SPIN_COUNT = valueOf("WRITE_SPIN_COUNT");
85      /**
86       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
87       */
88      @Deprecated
89      public static final ChannelOption<Integer> WRITE_BUFFER_HIGH_WATER_MARK = valueOf("WRITE_BUFFER_HIGH_WATER_MARK");
90      /**
91       * @deprecated Use {@link #WRITE_BUFFER_WATER_MARK}
92       */
93      @Deprecated
94      public static final ChannelOption<Integer> WRITE_BUFFER_LOW_WATER_MARK = valueOf("WRITE_BUFFER_LOW_WATER_MARK");
95      public static final ChannelOption<WriteBufferWaterMark> WRITE_BUFFER_WATER_MARK =
96              valueOf("WRITE_BUFFER_WATER_MARK");
97  
98      public static final ChannelOption<Boolean> ALLOW_HALF_CLOSURE = valueOf("ALLOW_HALF_CLOSURE");
99      public static final ChannelOption<Boolean> AUTO_READ = valueOf("AUTO_READ");
100 
101     /**
102      * If {@code true} then the {@link Channel} is closed automatically and immediately on write failure.
103      * The default value is {@code true}.
104      */
105     public static final ChannelOption<Boolean> AUTO_CLOSE = valueOf("AUTO_CLOSE");
106 
107     public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
108     public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
109     public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");
110     public static final ChannelOption<Integer> SO_RCVBUF = valueOf("SO_RCVBUF");
111     public static final ChannelOption<Boolean> SO_REUSEADDR = valueOf("SO_REUSEADDR");
112     public static final ChannelOption<Integer> SO_LINGER = valueOf("SO_LINGER");
113     public static final ChannelOption<Integer> SO_BACKLOG = valueOf("SO_BACKLOG");
114     public static final ChannelOption<Integer> SO_TIMEOUT = valueOf("SO_TIMEOUT");
115 
116     public static final ChannelOption<Integer> IP_TOS = valueOf("IP_TOS");
117     public static final ChannelOption<InetAddress> IP_MULTICAST_ADDR = valueOf("IP_MULTICAST_ADDR");
118     public static final ChannelOption<NetworkInterface> IP_MULTICAST_IF = valueOf("IP_MULTICAST_IF");
119     public static final ChannelOption<Integer> IP_MULTICAST_TTL = valueOf("IP_MULTICAST_TTL");
120     public static final ChannelOption<Boolean> IP_MULTICAST_LOOP_DISABLED = valueOf("IP_MULTICAST_LOOP_DISABLED");
121 
122     public static final ChannelOption<Boolean> TCP_NODELAY = valueOf("TCP_NODELAY");
123 
124     @Deprecated
125     public static final ChannelOption<Boolean> DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION =
126             valueOf("DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION");
127 
128     public static final ChannelOption<Boolean> SINGLE_EVENTEXECUTOR_PER_GROUP =
129             valueOf("SINGLE_EVENTEXECUTOR_PER_GROUP");
130 
131     /**
132      * Creates a new {@link ChannelOption} with the specified unique {@code name}.
133      */
134     private ChannelOption(int id, String name) {
135         super(id, name);
136     }
137 
138     @Deprecated
139     protected ChannelOption(String name) {
140         this(pool.nextId(), name);
141     }
142 
143     /**
144      * Validate the value which is set for the {@link ChannelOption}. Sub-classes
145      * may override this for special checks.
146      */
147     public void validate(T value) {
148         if (value == null) {
149             throw new NullPointerException("value");
150         }
151     }
152 }