View Javadoc
1   /*
2    * Copyright 2023 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    *   https://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.bootstrap;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ServerChannel;
20  
21  /**
22   * A channel initializer extension make it possible to enforce rules and apply modifications across multiple,
23   * disconnected uses of Netty within the same JVM process.
24   * <p>
25   * For instance, application-level firewall rules can be injected into all uses of Netty within an application,
26   * without making changes to such uses that are otherwise outside the purview of the application code,
27   * such as 3rd-party libraries.
28   * <p>
29   * Channel initializer extensions are <em>not</em> enabled by default, because of their power to influence Netty
30   * pipelines across libraries, frameworks, and use-cases.
31   * Extensions must be explicitly enabled by setting the {@value #EXTENSIONS_SYSTEM_PROPERTY} to {@code serviceload}.
32   * <p>
33   * All channel initializer extensions that are available on the classpath will be
34   * {@linkplain java.util.ServiceLoader#load(Class) service-loaded} and used by all {@link AbstractBootstrap} subclasses.
35   * <p>
36   * Note that this feature will not work for Netty uses that are shaded <em>and relocated</em> into other libraries.
37   * The classes in a relocated Netty library are technically distinct and incompatible types. This means the
38   * service-loader in non-relocated Netty will not see types from a relocated Netty, and vice versa.
39   */
40  public abstract class ChannelInitializerExtension {
41      /**
42       * The name of the system property that control initializer extensions.
43       * <p>
44       * These extensions can potentially be a security liability, so they are disabled by default.
45       * <p>
46       * To enable the extensions, application operators can explicitly opt in by setting this system property to the
47       * value {@code serviceload}. This will enable all the extensions that are available through the service loader
48       * mechanism.
49       * <p>
50       * To load and log (at INFO level) all available extensions without actually running them, set this system property
51       * to the value {@code log}.
52       */
53      public static final String EXTENSIONS_SYSTEM_PROPERTY = "io.netty.bootstrap.extensions";
54  
55      /**
56       * Get the "priority" of this extension. If multiple extensions are avilable, then they will be called in their
57       * priority order, from lowest to highest.
58       * <p>
59       * Implementers are encouraged to pick a number between {@code -100.0} and {@code 100.0}, where extensions that have
60       * no particular opinion on their priority are encouraged to return {@code 0.0}.
61       * <p>
62       * Extensions with lower priority will get called first, while extensions with greater priority may be able to
63       * observe the effects of extensions with lesser priority.
64       * <p>
65       * Note that if multiple extensions have the same priority, then their relative order will be unpredictable.
66       * As such, implementations should always take into consideration that other extensions might be called before
67       * or after them.
68       * <p>
69       * Override this method to specify your own priority.
70       * The default implementation just returns {@code 0}.
71       *
72       * @return The priority.
73       */
74      public double priority() {
75          return 0;
76      }
77  
78      /**
79       * Called by {@link Bootstrap} after the initialization of the given client channel.
80       * <p>
81       * The method is allowed to modify the handlers in the pipeline, the channel attributes, or the channel options.
82       * The method must refrain from doing any I/O, or from closing the channel.
83       * <p>
84       * Override this method to add your own callback logic.
85       * The default implementation does nothing.
86       *
87       * @param channel The channel that was initialized.
88       */
89      public void postInitializeClientChannel(Channel channel) {
90      }
91  
92      /**
93       * Called by {@link ServerBootstrap} after the initialization of the given server listener channel.
94       * The listener channel is responsible for invoking the {@code accept(2)} system call,
95       * and for producing child channels.
96       * <p>
97       * The method is allowed to modify the handlers in the pipeline, the channel attributes, or the channel options.
98       * The method must refrain from doing any I/O, or from closing the channel.
99       * <p>
100      * Override this method to add your own callback logic.
101      * The default implementation does nothing.
102      *
103      * @param channel The channel that was initialized.
104      */
105     public void postInitializeServerListenerChannel(ServerChannel channel) {
106     }
107 
108     /**
109      * Called by {@link ServerBootstrap} after the initialization of the given child channel.
110      * A child channel is a newly established connection from a client to the server.
111      * <p>
112      * The method is allowed to modify the handlers in the pipeline, the channel attributes, or the channel options.
113      * The method must refrain from doing any I/O, or from closing the channel.
114      * <p>
115      * Override this method to add your own callback logic.
116      * The default implementation does nothing.
117      *
118      * @param channel The channel that was initialized.
119      */
120     public void postInitializeServerChildChannel(Channel channel) {
121     }
122 }