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