View Javadoc
1   /*
2    * Copyright 2013 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  
17  package io.netty.channel;
18  
19  import io.netty.util.internal.InternalThreadLocalMap;
20  
21  import java.util.Map;
22  
23  /**
24   * Skeleton implementation of a {@link ChannelHandler}.
25   */
26  public abstract class ChannelHandlerAdapter implements ChannelHandler {
27  
28      // Not using volatile because it's used only for a sanity check.
29      boolean added;
30  
31      /**
32       * Throws {@link IllegalStateException} if {@link ChannelHandlerAdapter#isSharable()} returns {@code true}
33       */
34      protected void ensureNotSharable() {
35          if (isSharable()) {
36              throw new IllegalStateException("ChannelHandler " + getClass().getName() + " is not allowed to be shared");
37          }
38      }
39  
40      /**
41       * Return {@code true} if the implementation is {@link Sharable} and so can be added
42       * to different {@link ChannelPipeline}s.
43       */
44      public boolean isSharable() {
45          /**
46           * Cache the result of {@link Sharable} annotation detection to workaround a condition. We use a
47           * {@link ThreadLocal} and {@link WeakHashMap} to eliminate the volatile write/reads. Using different
48           * {@link WeakHashMap} instances per {@link Thread} is good enough for us and the number of
49           * {@link Thread}s are quite limited anyway.
50           *
51           * See <a href="https://github.com/netty/netty/issues/2289">#2289</a>.
52           */
53          Class<?> clazz = getClass();
54          Map<Class<?>, Boolean> cache = InternalThreadLocalMap.get().handlerSharableCache();
55          Boolean sharable = cache.get(clazz);
56          if (sharable == null) {
57              sharable = clazz.isAnnotationPresent(Sharable.class);
58              cache.put(clazz, sharable);
59          }
60          return sharable;
61      }
62  
63      /**
64       * Do nothing by default, sub-classes may override this method.
65       */
66      @Override
67      public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
68          // NOOP
69      }
70  
71      /**
72       * Do nothing by default, sub-classes may override this method.
73       */
74      @Override
75      public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
76          // NOOP
77      }
78  
79      /**
80       * Calls {@link ChannelHandlerContext#fireExceptionCaught(Throwable)} to forward
81       * to the next {@link ChannelHandler} in the {@link ChannelPipeline}.
82       *
83       * Sub-classes may override this method to change behavior.
84       */
85      @Override
86      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
87          ctx.fireExceptionCaught(cause);
88      }
89  }