1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.io; 20 21 import java.io.IOException; 22 import java.nio.ByteBuffer; 23 import java.util.Map; 24 25 import org.eclipse.jetty.util.log.Log; 26 import org.eclipse.jetty.util.log.Logger; 27 28 /** 29 * Factory for client-side {@link Connection} instances. 30 */ 31 public interface ClientConnectionFactory 32 { 33 /** 34 * 35 * @param endPoint the {@link org.eclipse.jetty.io.EndPoint} to link the newly created connection to 36 * @param context the context data to create the connection 37 * @return a new {@link Connection} 38 * @throws IOException if the connection cannot be created 39 */ 40 public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException; 41 42 public static class Helper 43 { 44 private static Logger LOG = Log.getLogger(Helper.class); 45 46 private Helper() 47 { 48 } 49 50 /** 51 * Replaces the given {@code oldConnection} with the given {@code newConnection} on the 52 * {@link EndPoint} associated with {@code oldConnection}, performing connection lifecycle management. 53 * <p> 54 * The {@code oldConnection} will be closed by invoking {@link org.eclipse.jetty.io.Connection#onClose()} 55 * and the {@code newConnection} will be opened by invoking {@link org.eclipse.jetty.io.Connection#onOpen()}. 56 * @param oldConnection the old connection to replace 57 * @param newConnection the new connection replacement 58 */ 59 public static void replaceConnection(Connection oldConnection, Connection newConnection) 60 { 61 close(oldConnection); 62 oldConnection.getEndPoint().setConnection(newConnection); 63 open(newConnection); 64 } 65 66 private static void open(Connection connection) 67 { 68 try 69 { 70 connection.onOpen(); 71 } 72 catch (Throwable x) 73 { 74 LOG.debug(x); 75 } 76 } 77 78 private static void close(Connection connection) 79 { 80 try 81 { 82 connection.onClose(); 83 } 84 catch (Throwable x) 85 { 86 LOG.debug(x); 87 } 88 } 89 } 90 }