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.util.concurrent.Executor;
23  import java.util.concurrent.Future;
24  
25  import org.eclipse.jetty.io.ByteBufferPool;
26  import org.eclipse.jetty.util.annotation.ManagedAttribute;
27  import org.eclipse.jetty.util.annotation.ManagedObject;
28  import org.eclipse.jetty.util.thread.Scheduler;
29  
30  /**
31   * An abstract Network Connector.
32   * <p>
33   * Extends the {@link AbstractConnector} support for the {@link NetworkConnector} interface.
34   */
35  @ManagedObject("AbstractNetworkConnector")
36  public abstract class AbstractNetworkConnector extends AbstractConnector implements NetworkConnector
37  {
38  
39      private volatile String _host;
40      private volatile int _port = 0;
41  
42      public AbstractNetworkConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, ConnectionFactory... factories)
43      {
44          super(server,executor,scheduler,pool,acceptors,factories);
45      }
46  
47      public void setHost(String host)
48      {
49          _host = host;
50      }
51  
52      @Override
53      @ManagedAttribute("The network interface this connector binds to as an IP address or a hostname.  If null or 0.0.0.0, then bind to all interfaces.")
54      public String getHost()
55      {
56          return _host;
57      }
58  
59      public void setPort(int port)
60      {
61          _port = port;
62      }
63  
64      @Override
65      @ManagedAttribute("Port this connector listens on. If set the 0 a random port is assigned which may be obtained with getLocalPort()")
66      public int getPort()
67      {
68          return _port;
69      }
70  
71      @Override
72      public int getLocalPort()
73      {
74          return -1;
75      }
76  
77      @Override
78      protected void doStart() throws Exception
79      {
80          open();
81          super.doStart();
82      }
83  
84      @Override
85      protected void doStop() throws Exception
86      {
87          close();
88          super.doStop();
89      }
90  
91      @Override
92      public void open() throws IOException
93      {
94      }
95  
96      @Override
97      public void close()
98      {
99          // Interrupting is often sufficient to close the channel
100         interruptAcceptors();
101     }
102     
103 
104     @Override
105     public Future<Void> shutdown()
106     {
107         close();
108         return super.shutdown();
109     }
110 
111     @Override
112     protected boolean isAccepting()
113     {
114         return super.isAccepting() && isOpen();
115     }
116 
117     @Override
118     public String toString()
119     {
120         return String.format("%s{%s:%d}",
121                 super.toString(),
122                 getHost() == null ? "0.0.0.0" : getHost(),
123                 getLocalPort() <= 0 ? getPort() : getLocalPort());
124     }
125 }