Skip to content

Package: AbstractSocketConnectorHandler$1

AbstractSocketConnectorHandler$1

nameinstructionbranchcomplexitylinemethod
onComplete()
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2008, 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.net.InetSocketAddress;
21: import java.net.SocketAddress;
22: import java.util.LinkedList;
23: import java.util.List;
24:
25: import org.glassfish.grizzly.impl.FutureImpl;
26: import org.glassfish.grizzly.impl.SafeFutureImpl;
27:
28: /**
29: * Abstract class simplifies the implementation of {@link SocketConnectorHandler} interface by pre-implementing some of
30: * its methods.
31: *
32: * @author Alexey Stashok
33: */
34: public abstract class AbstractSocketConnectorHandler implements SocketConnectorHandler {
35:
36: protected final Transport transport;
37: private Processor processor;
38: private ProcessorSelector processorSelector;
39:
40: protected final List<ConnectionProbe> probes = new LinkedList<>();
41:
42: public AbstractSocketConnectorHandler(Transport transport) {
43: this.transport = transport;
44: this.processor = transport.getProcessor();
45: this.processorSelector = transport.getProcessorSelector();
46: }
47:
48: @Override
49: public GrizzlyFuture<Connection> connect(String host, int port) {
50: return connect(new InetSocketAddress(host, port));
51: }
52:
53: @Override
54: public GrizzlyFuture<Connection> connect(SocketAddress remoteAddress) {
55: return connect(remoteAddress, (SocketAddress) null);
56: }
57:
58: @Override
59: public void connect(SocketAddress remoteAddress, CompletionHandler<Connection> completionHandler) {
60: connect(remoteAddress, null, completionHandler);
61: }
62:
63: @Override
64: public GrizzlyFuture<Connection> connect(SocketAddress remoteAddress, SocketAddress localAddress) {
65: return connectAsync(remoteAddress, localAddress, null, true);
66: }
67:
68: @Override
69: public void connect(SocketAddress remoteAddress, SocketAddress localAddress, CompletionHandler<Connection> completionHandler) {
70: connectAsync(remoteAddress, localAddress, completionHandler, false);
71: }
72:
73: protected abstract FutureImpl<Connection> connectAsync(final SocketAddress remoteAddress, final SocketAddress localAddress,
74: final CompletionHandler<Connection> completionHandler, final boolean needFuture);
75:
76: /**
77: * Get the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
78: *
79: * @return the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
80: */
81: public Processor getProcessor() {
82: return processor;
83: }
84:
85: /**
86: * Set the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
87: *
88: * @param processor the default {@link Processor} to process {@link IOEvent}, occurring on connection phase.
89: */
90: public void setProcessor(Processor processor) {
91: this.processor = processor;
92: }
93:
94: /**
95: * Gets the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
96: * occurring on connection phase.
97: *
98: * @return the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
99: * occurring on connection phase.
100: */
101: public ProcessorSelector getProcessorSelector() {
102: return processorSelector;
103: }
104:
105: /**
106: * Sets the default {@link ProcessorSelector}, which will be used to get {@link Processor} to process I/O events,
107: * occurring on connection phase.
108: *
109: * @param processorSelector the default {@link ProcessorSelector}, which will be used to get {@link Processor} to
110: * process I/O events, occurring on connection phase.
111: */
112: public void setProcessorSelector(ProcessorSelector processorSelector) {
113: this.processorSelector = processorSelector;
114: }
115:
116: /**
117: * Add the {@link ConnectionProbe}, which will be notified about <tt>Connection</tt> life-cycle events.
118: *
119: * @param probe the {@link ConnectionProbe}.
120: */
121: public void addMonitoringProbe(ConnectionProbe probe) {
122: probes.add(probe);
123: }
124:
125: /**
126: * Remove the {@link ConnectionProbe}.
127: *
128: * @param probe the {@link ConnectionProbe}.
129: * @return true if probe was in the list and is now removed
130: */
131: public boolean removeMonitoringProbe(ConnectionProbe probe) {
132: return probes.remove(probe);
133: }
134:
135: /**
136: * Get the {@link ConnectionProbe}, which are registered on the <tt>Connection</tt>. Please note, it's not appropriate
137: * to modify the returned array's content. Please use {@link #addMonitoringProbe(org.glassfish.grizzly.ConnectionProbe)}
138: * and {@link #removeMonitoringProbe(org.glassfish.grizzly.ConnectionProbe)} instead.
139: *
140: * @return the {@link ConnectionProbe}, which are registered on the <tt>Connection</tt>.
141: */
142: public ConnectionProbe[] getMonitoringProbes() {
143: return probes.toArray(new ConnectionProbe[probes.size()]);
144: }
145:
146: /**
147: * Pre-configures {@link Connection} object before actual connecting phase will be started.
148: *
149: * @param connection {@link Connection} to pre-configure.
150: */
151: protected void preConfigure(Connection connection) {
152: }
153:
154: protected FutureImpl<Connection> makeCancellableFuture(final Connection connection) {
155: return new SafeFutureImpl<Connection>() {
156:
157: @Override
158: protected void onComplete() {
159: try {
160:• if (!isCancelled()) {
161: get();
162: return;
163: }
164: } catch (Throwable ignored) {
165: }
166:
167: try {
168: connection.closeSilently();
169: } catch (Exception ignored) {
170: }
171: }
172: };
173: }
174:
175: /**
176: * Builder
177: *
178: * @param <E> itself
179: */
180: @SuppressWarnings("unchecked")
181: public abstract static class Builder<E extends Builder> {
182:
183: protected Processor processor;
184: protected ProcessorSelector processorSelector;
185: protected ConnectionProbe connectionProbe;
186:
187: public E processor(final Processor processor) {
188: this.processor = processor;
189: return (E) this;
190: }
191:
192: public E processorSelector(final ProcessorSelector processorSelector) {
193: this.processorSelector = processorSelector;
194: return (E) this;
195: }
196:
197: public E probe(ConnectionProbe connectionProbe) {
198: this.connectionProbe = connectionProbe;
199: return (E) this;
200: }
201:
202: public AbstractSocketConnectorHandler build() {
203: AbstractSocketConnectorHandler handler = create();
204: if (processor != null) {
205: handler.setProcessor(processor);
206: }
207: if (processorSelector != null) {
208: handler.setProcessorSelector(processorSelector);
209: }
210: if (connectionProbe != null) {
211: handler.addMonitoringProbe(connectionProbe);
212: }
213: return handler;
214: }
215:
216: protected abstract AbstractSocketConnectorHandler create();
217: }
218: }