Index: ../../../dev/grizzly2_git/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractSocketConnectorHandler.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP <+>/*\n * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.\n *\n * Copyright (c) 2008-2011 Oracle and/or its affiliates. All rights reserved.\n *\n * The contents of this file are subject to the terms of either the GNU\n * General Public License Version 2 only (\"GPL\") or the Common Development\n * and Distribution License(\"CDDL\") (collectively, the \"License\"). You\n * may not use this file except in compliance with the License. You can\n * obtain a copy of the License at\n * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html\n * or packager/legal/LICENSE.txt. See the License for the specific\n * language governing permissions and limitations under the License.\n *\n * When distributing the software, include this License Header Notice in each\n * file and include the License file at packager/legal/LICENSE.txt.\n *\n * GPL Classpath Exception:\n * Oracle designates this particular file as subject to the \"Classpath\"\n * exception as provided by Oracle in the GPL Version 2 section of the License\n * file that accompanied this code.\n *\n * Modifications:\n * If applicable, add the following below the License Header, with the fields\n * enclosed by brackets [] replaced by your own identifying information:\n * \"Portions Copyright [year] [name of copyright owner]\"\n *\n * Contributor(s):\n * If you wish your version of this file to be governed by only the CDDL or\n * only the GPL Version 2, indicate your decision by adding \"[Contributor]\n * elects to include this software in this distribution under the [CDDL or GPL\n * Version 2] license.\" If you don't indicate a single choice of license, a\n * recipient has the option to distribute your version of this file under\n * either the CDDL, the GPL Version 2 or to extend the choice of license to\n * its licensees as provided above. However, if you add GPL Version 2 code\n * and therefore, elected the GPL Version 2 license, then the option applies\n * only if the new code is made subject to such option by the copyright\n * holder.\n */\npackage org.glassfish.grizzly;\n\nimport java.net.InetSocketAddress;\nimport java.net.SocketAddress;\nimport java.util.LinkedList;\nimport java.util.List;\nimport org.glassfish.grizzly.impl.FutureImpl;\nimport org.glassfish.grizzly.utils.Futures;\n\n/**\n * Abstract class simplifies the implementation of\n * {@link SocketConnectorHandler}\n * interface by pre-implementing some of its methods.\n * \n * @author Alexey Stashok\n */\npublic abstract class AbstractSocketConnectorHandler\n implements SocketConnectorHandler {\n\n protected final Transport transport;\n private Processor processor;\n private ProcessorSelector processorSelector;\n\n protected final List probes =\n new LinkedList();\n\n public AbstractSocketConnectorHandler(Transport transport) {\n this.transport = transport;\n this.processor = transport.getProcessor();\n this.processorSelector = transport.getProcessorSelector();\n }\n\n @Override\n public GrizzlyFuture connect(String host, int port) {\n return connect(new InetSocketAddress(host, port));\n }\n\n @Override\n public GrizzlyFuture connect(SocketAddress remoteAddress) {\n return connect(remoteAddress, (SocketAddress) null);\n }\n\n @Override\n public void connect(SocketAddress remoteAddress,\n CompletionHandler completionHandler) {\n connect(remoteAddress, null, completionHandler);\n }\n\n @Override\n public GrizzlyFuture connect(SocketAddress remoteAddress,\n SocketAddress localAddress) {\n final FutureImpl future =\n Futures.createSafeFuture();\n connect(remoteAddress, localAddress, Futures.toCompletionHandler(future));\n return future;\n }\n\n @Override\n public abstract void connect(SocketAddress remoteAddress,\n SocketAddress localAddress,\n CompletionHandler completionHandler);\n\n /**\n * Get the default {@link Processor} to process {@link IOEvent}, occurring\n * on connection phase.\n *\n * @return the default {@link Processor} to process {@link IOEvent},\n * occurring on connection phase.\n */\n public Processor getProcessor() {\n return processor;\n }\n\n /**\n * Set the default {@link Processor} to process {@link IOEvent}, occurring\n * on connection phase.\n *\n * @param processor the default {@link Processor} to process\n * {@link IOEvent}, occurring on connection phase.\n */\n public void setProcessor(Processor processor) {\n this.processor = processor;\n }\n\n /**\n * Gets the default {@link ProcessorSelector}, which will be used to get\n * {@link Processor} to process I/O events, occurring on connection phase.\n *\n * @return the default {@link ProcessorSelector}, which will be used to get\n * {@link Processor} to process I/O events, occurring on connection phase.\n */\n public ProcessorSelector getProcessorSelector() {\n return processorSelector;\n }\n\n /**\n * Sets the default {@link ProcessorSelector}, which will be used to get\n * {@link Processor} to process I/O events, occurring on connection phase.\n *\n * @param processorSelector the default {@link ProcessorSelector},\n * which will be used to get {@link Processor} to process I/O events,\n * occurring on connection phase.\n */\n public void setProcessorSelector(ProcessorSelector processorSelector) {\n this.processorSelector = processorSelector;\n }\n\n /**\n * Add the {@link ConnectionProbe}, which will be notified about\n * Connection life-cycle events.\n *\n * @param probe the {@link ConnectionProbe}.\n */\n public void addMonitoringProbe(ConnectionProbe probe) {\n probes.add(probe);\n }\n\n /**\n * Remove the {@link ConnectionProbe}.\n *\n * @param probe the {@link ConnectionProbe}.\n */\n public boolean removeMonitoringProbe(ConnectionProbe probe) {\n return probes.remove(probe);\n }\n\n /**\n * Get the {@link ConnectionProbe}, which are registered on the Connection.\n * Please note, it's not appropriate to modify the returned array's content.\n * Please use {@link #addMonitoringProbe(org.glassfish.grizzly.ConnectionProbe)} and\n * {@link #removeMonitoringProbe(org.glassfish.grizzly.ConnectionProbe)} instead.\n *\n * @return the {@link ConnectionProbe}, which are registered on the Connection.\n */\n public ConnectionProbe[] getMonitoringProbes() {\n return probes.toArray(new ConnectionProbe[probes.size()]);\n }\n\n /**\n * Pre-configures {@link Connection} object before actual connecting phase\n * will be started.\n * \n * @param connection {@link Connection} to pre-configure.\n */\n protected void preConfigure(Connection connection) {\n }\n\n /**\n * Builder\n *\n * @param \n */\n @SuppressWarnings(\"unchecked\")\n public abstract static class Builder {\n protected final AbstractSocketConnectorHandler connectorHandler;\n\n public Builder(AbstractSocketConnectorHandler connectorHandler) {\n this.connectorHandler = connectorHandler;\n }\n\n public E processor(final Processor processor) {\n connectorHandler.setProcessor(processor);\n return (E) this;\n }\n\n public E processorSelector(final ProcessorSelector processorSelector) {\n connectorHandler.setProcessorSelector(processorSelector);\n return (E) this;\n }\n\n public E probe(ConnectionProbe connectionProbe) {\n connectorHandler.addMonitoringProbe(connectionProbe);\n return (E) this;\n }\n }\n}\n =================================================================== --- ../../../dev/grizzly2_git/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractSocketConnectorHandler.java (revision 6e932260778720de6d1d0906d4e1d10728176b30) +++ ../../../dev/grizzly2_git/modules/grizzly/src/main/java/org/glassfish/grizzly/AbstractSocketConnectorHandler.java (revision ) @@ -88,9 +88,8 @@ @Override public GrizzlyFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { - final FutureImpl future = - Futures.createSafeFuture(); - connect(remoteAddress, localAddress, Futures.toCompletionHandler(future)); + final FutureImpl future = Futures.createSafeFuture(); + connect(remoteAddress, localAddress, new FutureToCompletionHandler(future)); return future; } @@ -182,6 +181,36 @@ * @param connection {@link Connection} to pre-configure. */ protected void preConfigure(Connection connection) { + } + + private static final class FutureToCompletionHandler + extends EmptyCompletionHandler { + + private final FutureImpl future; + + public FutureToCompletionHandler(FutureImpl future) { + this.future = future; + } + + @Override + public void cancelled() { + future.cancel(false); + } + + @Override + public void completed(final Connection result) { + if (future.isCancelled()) { + result.closeSilently(); + future.result(null); + } else { + future.result(result); + } + } + + @Override + public void failed(final Throwable throwable) { + future.failure(throwable); + } } /**