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