View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.util.Map;
23  
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  
27  /**
28   * Factory for client-side {@link Connection} instances.
29   */
30  public interface ClientConnectionFactory
31  {
32      /**
33       *
34       * @param endPoint the {@link org.eclipse.jetty.io.EndPoint} to link the newly created connection to
35       * @param context the context data to create the connection
36       * @return a new {@link Connection}
37       * @throws IOException if the connection cannot be created
38       */
39      public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException;
40  
41      public static class Helper
42      {
43          private static Logger LOG = Log.getLogger(Helper.class);
44  
45          private Helper()
46          {
47          }
48  
49          /**
50           * Replaces the given {@code oldConnection} with the given {@code newConnection} on the
51           * {@link EndPoint} associated with {@code oldConnection}, performing connection lifecycle management.
52           * <p />
53           * The {@code oldConnection} will be closed by invoking {@link org.eclipse.jetty.io.Connection#onClose()}
54           * and the {@code newConnection} will be opened by invoking {@link org.eclipse.jetty.io.Connection#onOpen()}.
55           * @param oldConnection the old connection to replace
56           * @param newConnection the new connection replacement
57           */
58          public static void replaceConnection(Connection oldConnection, Connection newConnection)
59          {
60              close(oldConnection);
61              oldConnection.getEndPoint().setConnection(newConnection);
62              open(newConnection);
63          }
64  
65          private static void open(Connection connection)
66          {
67              try
68              {
69                  connection.onOpen();
70              }
71              catch (Throwable x)
72              {
73                  LOG.debug(x);
74              }
75          }
76  
77          private static void close(Connection connection)
78          {
79              try
80              {
81                  connection.onClose();
82              }
83              catch (Throwable x)
84              {
85                  LOG.debug(x);
86              }
87          }
88      }
89  }