View Javadoc
1   /*
2    * Copyright 2016 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.netty5.bootstrap;
17  
18  import io.netty5.channel.Channel;
19  import io.netty5.channel.ChannelFactory;
20  import io.netty5.channel.ChannelHandler;
21  import io.netty5.channel.ChannelOption;
22  import io.netty5.channel.EventLoopGroup;
23  import io.netty5.channel.ServerChannelFactory;
24  import io.netty5.util.AttributeKey;
25  import io.netty5.util.internal.StringUtil;
26  
27  import java.net.SocketAddress;
28  import java.util.Map;
29  
30  import static java.util.Objects.requireNonNull;
31  
32  /**
33   * Exposes the configuration of an {@link AbstractBootstrap}.
34   */
35  public abstract class AbstractBootstrapConfig<B extends AbstractBootstrap<B, C, F>, C extends Channel, F> {
36  
37      protected final B bootstrap;
38  
39      AbstractBootstrapConfig(B bootstrap) {
40          this.bootstrap = requireNonNull(bootstrap, "bootstrap");
41      }
42  
43      /**
44       * Returns the configured local address or {@code null} if non is configured yet.
45       */
46      public final SocketAddress localAddress() {
47          return bootstrap.localAddress();
48      }
49  
50      /**
51       * Returns the configured {@link ChannelFactory} / {@link ServerChannelFactory} or {@code null}
52       * if non is configured yet.
53       */
54      @SuppressWarnings("deprecation")
55      public abstract F channelFactory();
56  
57      /**
58       * Returns the configured {@link ChannelHandler} or {@code null} if non is configured yet.
59       */
60      public final ChannelHandler handler() {
61          return bootstrap.handler();
62      }
63  
64      /**
65       * Returns a copy of the configured options.
66       */
67      public final Map<ChannelOption<?>, Object> options() {
68          return bootstrap.options();
69      }
70  
71      /**
72       * Returns a copy of the configured attributes.
73       */
74      public final Map<AttributeKey<?>, Object> attrs() {
75          return bootstrap.attrs();
76      }
77  
78      /**
79       * Returns the configured {@link EventLoopGroup} or {@code null} if non is configured yet.
80       */
81      @SuppressWarnings("deprecation")
82      public final EventLoopGroup group() {
83          return bootstrap.group();
84      }
85  
86      @Override
87      public String toString() {
88          StringBuilder buf = new StringBuilder()
89                  .append(StringUtil.simpleClassName(this))
90                  .append('(');
91          EventLoopGroup group = group();
92          if (group != null) {
93              buf.append("group: ")
94                      .append(StringUtil.simpleClassName(group))
95                      .append(", ");
96          }
97          @SuppressWarnings("deprecation")
98          F factory = channelFactory();
99          if (factory != null) {
100             buf.append("channelFactory: ")
101                     .append(factory)
102                     .append(", ");
103         }
104         SocketAddress localAddress = localAddress();
105         if (localAddress != null) {
106             buf.append("localAddress: ")
107                     .append(localAddress)
108                     .append(", ");
109         }
110 
111         Map<ChannelOption<?>, Object> options = options();
112         if (!options.isEmpty()) {
113             buf.append("options: ")
114                     .append(options)
115                     .append(", ");
116         }
117         Map<AttributeKey<?>, Object> attrs = attrs();
118         if (!attrs.isEmpty()) {
119             buf.append("attrs: ")
120                     .append(attrs)
121                     .append(", ");
122         }
123         ChannelHandler handler = handler();
124         if (handler != null) {
125             buf.append("handler: ")
126                     .append(handler)
127                     .append(", ");
128         }
129         if (buf.charAt(buf.length() - 1) == '(') {
130             buf.append(')');
131         } else {
132             buf.setCharAt(buf.length() - 2, ')');
133             buf.setLength(buf.length() - 1);
134         }
135         return buf.toString();
136     }
137 }