Skip to content

Package: AbstractBindingHandler$Builder

AbstractBindingHandler$Builder

nameinstructionbranchcomplexitylinemethod
AbstractBindingHandler.Builder()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
build()
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
processor(Processor)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
processorSelector(ProcessorSelector)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
3: * Copyright (c) 2018 Payara Services Ltd.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package org.glassfish.grizzly;
19:
20: import java.io.IOException;
21: import java.net.BindException;
22: import java.net.InetSocketAddress;
23: import java.nio.channels.Channel;
24: import java.util.Random;
25:
26: import org.glassfish.grizzly.nio.NIOTransport;
27:
28: /**
29: * @since 2.2.19
30: */
31: public abstract class AbstractBindingHandler implements SocketBinder {
32: protected static final Random RANDOM = new Random();
33: protected final NIOTransport transport;
34: protected Processor processor;
35: protected ProcessorSelector processorSelector;
36:
37: // ------------------------------------------------------------ Constructors
38:
39: public AbstractBindingHandler(final NIOTransport transport) {
40: this.transport = transport;
41: this.processor = transport.getProcessor();
42: this.processorSelector = transport.getProcessorSelector();
43: }
44:
45: // ---------------------------------------------------------- Public Methods
46:
47: /**
48: * Get the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
49: *
50: * @return the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
51: */
52: public Processor getProcessor() {
53: return processor;
54: }
55:
56: /**
57: * Set the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
58: *
59: * @param processor the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
60: */
61: public void setProcessor(Processor processor) {
62: this.processor = processor;
63: }
64:
65: /**
66: * Gets the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
67: * occurring on connection phase.
68: *
69: * @return the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
70: * occurring on connection phase.
71: */
72: public ProcessorSelector getProcessorSelector() {
73: return processorSelector;
74: }
75:
76: /**
77: * Sets the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
78: * occurring on connection phase.
79: *
80: * @param processorSelector the default {@link ProcessorSelector}, which will be used to get {@link Processor} to
81: * process I/O events, occurring on connection phase.
82: */
83: public void setProcessorSelector(final ProcessorSelector processorSelector) {
84: this.processorSelector = processorSelector;
85: }
86:
87: // ----------------------------------------------- Methods from SocketBinder
88:
89: /**
90: * {@inheritDoc}
91: */
92: @Override
93: public Connection<?> bind(final int port) throws IOException {
94: return bind(new InetSocketAddress(port));
95: }
96:
97: /**
98: * {@inheritDoc}
99: */
100: @Override
101: public Connection<?> bind(final String host, final int port) throws IOException {
102: return bind(new InetSocketAddress(host, port));
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: @Override
109: public Connection<?> bind(final String host, final int port, final int backlog) throws IOException {
110: return bind(new InetSocketAddress(host, port), backlog);
111: }
112:
113: /**
114: * {@inheritDoc}
115: */
116: @Override
117: public Connection<?> bind(final String host, final PortRange portRange, final int backlog) throws IOException {
118: return bind(host, portRange, true, backlog);
119: }
120:
121: @Override
122: public Connection<?> bind(final String host, final PortRange portRange, boolean randomStartPort, final int backlog) throws IOException {
123: // Get the initial range parameters
124: final int lower = portRange.getLower();
125: final int range = portRange.getUpper() - lower + 1;
126:
127: // Select a start point in the range
128: final int initialOffset;
129: if (randomStartPort) {
130: initialOffset = RANDOM.nextInt(range);
131: } else {
132: initialOffset = 0;
133: }
134:
135: // Loop the offset through all ports in the range and attempt
136: // to bind to each
137: int offset = initialOffset;
138: do {
139: final int port = lower + offset;
140: try {
141: return bind(host, port, backlog);
142: } catch (IOException caught) {
143: // Swallow exceptions until the end
144: }
145: offset = (offset + 1) % range;
146: } while (offset != initialOffset);
147:
148: // If a port can't be bound, throw the exception
149: throw new BindException(String.format("Couldn't bind to any port in the range `%s`.", portRange.toString()));
150: }
151:
152: /**
153: * This operation is not supported by implementations of {@link AbstractBindingHandler}.
154: *
155: * @throws UnsupportedOperationException by default
156: */
157: @Override
158: public final void unbindAll() {
159: throw new UnsupportedOperationException();
160: }
161:
162: // ------------------------------------------------------- Protected Methods
163:
164: @SuppressWarnings("unchecked")
165: protected <T> T getSystemInheritedChannel(final Class<?> channelType) throws IOException {
166: final Channel inheritedChannel = System.inheritedChannel();
167:
168: if (inheritedChannel == null) {
169: throw new IOException("Inherited channel is not set");
170: }
171: if (!channelType.isInstance(inheritedChannel)) {
172: throw new IOException("Inherited channel is not " + channelType.getName() + ", but " + inheritedChannel.getClass().getName());
173: }
174: return (T) inheritedChannel;
175: }
176:
177: // ----------------------------------------------------------- Inner Classes
178:
179: /**
180: * Builder
181: *
182: * @param <E>
183: */
184: @SuppressWarnings("unchecked")
185: public abstract static class Builder<E extends Builder> {
186:
187: protected Processor processor;
188: protected ProcessorSelector processorSelector;
189:
190: public E processor(final Processor processor) {
191: this.processor = processor;
192: return (E) this;
193: }
194:
195: public E processorSelector(final ProcessorSelector processorSelector) {
196: this.processorSelector = processorSelector;
197: return (E) this;
198: }
199:
200: public AbstractBindingHandler build() {
201: AbstractBindingHandler bindingHandler = create();
202:• if (processor != null) {
203: bindingHandler.setProcessor(processor);
204: }
205:• if (processorSelector != null) {
206: bindingHandler.setProcessorSelector(processorSelector);
207: }
208: return bindingHandler;
209: }
210:
211: protected abstract AbstractBindingHandler create();
212:
213: }
214: }