View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.server;
20  
21  import java.io.IOException;
22  import java.nio.channels.SelectionKey;
23  import java.nio.channels.SocketChannel;
24  import java.util.List;
25  import java.util.concurrent.CopyOnWriteArrayList;
26  import java.util.concurrent.Executor;
27  
28  import org.eclipse.jetty.io.ByteBufferPool;
29  import org.eclipse.jetty.io.ManagedSelector;
30  import org.eclipse.jetty.io.NetworkTrafficListener;
31  import org.eclipse.jetty.io.NetworkTrafficSelectChannelEndPoint;
32  import org.eclipse.jetty.io.SelectChannelEndPoint;
33  import org.eclipse.jetty.util.ssl.SslContextFactory;
34  import org.eclipse.jetty.util.thread.Scheduler;
35  
36  /**
37   * <p>A specialized version of {@link ServerConnector} that supports {@link NetworkTrafficListener}s.</p>
38   * <p>{@link NetworkTrafficListener}s can be added and removed dynamically before and after this connector has
39   * been started without causing {@link java.util.ConcurrentModificationException}s.</p>
40   */
41  public class NetworkTrafficServerConnector extends ServerConnector
42  {
43      private final List<NetworkTrafficListener> listeners = new CopyOnWriteArrayList<>();
44  
45      public NetworkTrafficServerConnector(Server server)
46      {
47          this(server, null, null, null, 0, 0, new HttpConnectionFactory());
48      }
49  
50      public NetworkTrafficServerConnector(Server server, ConnectionFactory connectionFactory, SslContextFactory sslContextFactory)
51      {
52          super(server, sslContextFactory, connectionFactory);
53      }
54  
55      public NetworkTrafficServerConnector(Server server, ConnectionFactory connectionFactory)
56      {
57          super(server, connectionFactory);
58      }
59  
60      public NetworkTrafficServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, int selectors, ConnectionFactory... factories)
61      {
62          super(server, executor, scheduler, pool, acceptors, selectors, factories);
63      }
64  
65      public NetworkTrafficServerConnector(Server server, SslContextFactory sslContextFactory)
66      {
67          super(server, sslContextFactory);
68      }
69  
70      /**
71       * @param listener the listener to add
72       */
73      public void addNetworkTrafficListener(NetworkTrafficListener listener)
74      {
75          listeners.add(listener);
76      }
77  
78      /**
79       * @param listener the listener to remove
80       */
81      public void removeNetworkTrafficListener(NetworkTrafficListener listener)
82      {
83          listeners.remove(listener);
84      }
85  
86      @Override
87      protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
88      {
89          NetworkTrafficSelectChannelEndPoint endPoint = new NetworkTrafficSelectChannelEndPoint(channel, selectSet, key, getScheduler(), getIdleTimeout(), listeners);
90          return endPoint;
91      }
92  }