View | Details | Raw Unified | Return to bug 52028
Collapse All | Expand All

(-)a/java/org/apache/catalina/connector/Connector.java (-3 / +15 lines)
Lines 114-120 public class Connector extends LifecycleMBeanBase { Link Here
114
    /**
114
    /**
115
     * The port number on which we listen for requests.
115
     * The port number on which we listen for requests.
116
     */
116
     */
117
    protected int port = 0;
117
    protected int port = -1;
118
118
119
119
120
    /**
120
    /**
Lines 497-503 public class Connector extends LifecycleMBeanBase { Link Here
497
    }
497
    }
498
498
499
    /**
499
    /**
500
     * Return the port number on which we listen for requests.
500
     * Return the port number on which this connector is configured to listen
501
     * for requests. The special value of 0 means select a random free port
502
     * when the socket is bound.
501
     */
503
     */
502
    public int getPort() {
504
    public int getPort() {
503
505
Lines 520-525 public class Connector extends LifecycleMBeanBase { Link Here
520
522
521
523
522
    /**
524
    /**
525
     * Return the port number on which this connector is listening to requests.
526
     * If the special value for {@link #port} of zero is used then this method
527
     * will report the actual port bound.
528
     */
529
    public int getLocalPort() {
530
        return ((Integer) getProperty("localPort")).intValue();
531
    }
532
533
534
    /**
523
     * Return the Coyote protocol handler in use.
535
     * Return the Coyote protocol handler in use.
524
     */
536
     */
525
    public String getProtocol() {
537
    public String getProtocol() {
Lines 938-944 public class Connector extends LifecycleMBeanBase { Link Here
938
    protected void startInternal() throws LifecycleException {
950
    protected void startInternal() throws LifecycleException {
939
951
940
        // Validate settings before starting
952
        // Validate settings before starting
941
        if (getPort() < 1) {
953
        if (getPort() < 0) {
942
            throw new LifecycleException(sm.getString(
954
            throw new LifecycleException(sm.getString(
943
                    "coyoteConnector.invalidPort", Integer.valueOf(getPort())));
955
                    "coyoteConnector.invalidPort", Integer.valueOf(getPort())));
944
        }
956
        }
(-)a/java/org/apache/catalina/connector/mbeans-descriptors.xml (-1 / +5 lines)
Lines 108-114 Link Here
108
                 type="int"/>
108
                 type="int"/>
109
109
110
    <attribute   name="port"
110
    <attribute   name="port"
111
          description="The port number on which we listen for ajp13 requests"
111
          description="The port number on which this connector is configured to listen for requests. The special value of 0 means select a random free port when the socket is bound."
112
                type="int"/>
113
114
    <attribute   name="localPort"
115
          description="The port number on which this connector is listening to requests. If the special value for port of zero is used then this method will report the actual port bound."
112
                type="int"/>
116
                type="int"/>
113
117
114
    <!-- Common -->
118
    <!-- Common -->
(-)a/java/org/apache/coyote/AbstractProtocol.java (+2 lines)
Lines 194-199 public abstract class AbstractProtocol implements ProtocolHandler, Link Here
194
    }
194
    }
195
195
196
196
197
    public int getLocalPort() { return endpoint.getLocalPort(); }
198
197
    /*
199
    /*
198
     * When Tomcat expects data from the client, this is the time Tomcat will
200
     * When Tomcat expects data from the client, this is the time Tomcat will
199
     * wait for that data to arrive before closing the connection.
201
     * wait for that data to arrive before closing the connection.
(-)a/java/org/apache/tomcat/util/net/AbstractEndpoint.java (+1 lines)
Lines 141-146 public abstract class AbstractEndpoint { Link Here
141
    public int getPort() { return port; }
141
    public int getPort() { return port; }
142
    public void setPort(int port ) { this.port=port; }
142
    public void setPort(int port ) { this.port=port; }
143
143
144
    public abstract int getLocalPort();
144
145
145
    /**
146
    /**
146
     * Address for the server socket.
147
     * Address for the server socket.
(-)a/java/org/apache/tomcat/util/net/AprEndpoint.java (-1 / +23 lines)
Lines 37-42 import org.apache.tomcat.jni.Pool; Link Here
37
import org.apache.tomcat.jni.SSL;
37
import org.apache.tomcat.jni.SSL;
38
import org.apache.tomcat.jni.SSLContext;
38
import org.apache.tomcat.jni.SSLContext;
39
import org.apache.tomcat.jni.SSLSocket;
39
import org.apache.tomcat.jni.SSLSocket;
40
import org.apache.tomcat.jni.Sockaddr;
40
import org.apache.tomcat.jni.Socket;
41
import org.apache.tomcat.jni.Socket;
41
import org.apache.tomcat.jni.Status;
42
import org.apache.tomcat.jni.Status;
42
import org.apache.tomcat.util.ExceptionUtils;
43
import org.apache.tomcat.util.ExceptionUtils;
Lines 326-333 public class AprEndpoint extends AbstractEndpoint { Link Here
326
    public void setSSLInsecureRenegotiation(boolean SSLInsecureRenegotiation) { this.SSLInsecureRenegotiation = SSLInsecureRenegotiation; }
327
    public void setSSLInsecureRenegotiation(boolean SSLInsecureRenegotiation) { this.SSLInsecureRenegotiation = SSLInsecureRenegotiation; }
327
    public boolean getSSLInsecureRenegotiation() { return SSLInsecureRenegotiation; }
328
    public boolean getSSLInsecureRenegotiation() { return SSLInsecureRenegotiation; }
328
329
329
    // --------------------------------------------------------- Public Methods
330
330
331
    /**
332
     * Port in use.
333
     */
334
    @Override
335
    public int getLocalPort() {
336
        long s = serverSock;
337
        if (s == 0) {
338
            return -1;
339
        } else {
340
            long sa;
341
            try {
342
                sa = Address.get(Socket.APR_LOCAL, s);
343
                Sockaddr addr = Address.getInfo(sa);
344
                return addr.port;
345
            } catch (Exception e) {
346
                return -1;
347
            }
348
        }
349
    }
350
351
352
    // --------------------------------------------------------- Public Methods
331
353
332
    /**
354
    /**
333
     * Number of keepalive sockets.
355
     * Number of keepalive sockets.
(-)a/java/org/apache/tomcat/util/net/JIoEndpoint.java (+12 lines)
Lines 96-101 public class JIoEndpoint extends AbstractEndpoint { Link Here
96
    public void setServerSocketFactory(ServerSocketFactory factory) { this.serverSocketFactory = factory; }
96
    public void setServerSocketFactory(ServerSocketFactory factory) { this.serverSocketFactory = factory; }
97
    public ServerSocketFactory getServerSocketFactory() { return serverSocketFactory; }
97
    public ServerSocketFactory getServerSocketFactory() { return serverSocketFactory; }
98
98
99
    /**
100
     * Port in use.
101
     */
102
    @Override
103
    public int getLocalPort() {
104
        ServerSocket s = serverSocket;
105
        if (s == null) {
106
            return -1;
107
        } else {
108
            return s.getLocalPort();
109
        }
110
    }
99
111
100
    /*
112
    /*
101
     * Optional feature support.
113
     * Optional feature support.
(-)a/java/org/apache/tomcat/util/net/NioEndpoint.java (+21 lines)
Lines 21-26 import java.io.File; Link Here
21
import java.io.FileInputStream;
21
import java.io.FileInputStream;
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.net.InetSocketAddress;
23
import java.net.InetSocketAddress;
24
import java.net.ServerSocket;
24
import java.net.Socket;
25
import java.net.Socket;
25
import java.net.SocketTimeoutException;
26
import java.net.SocketTimeoutException;
26
import java.nio.ByteBuffer;
27
import java.nio.ByteBuffer;
Lines 402-407 public class NioEndpoint extends AbstractEndpoint { Link Here
402
    public SSLContext getSSLContext() { return sslContext;}
403
    public SSLContext getSSLContext() { return sslContext;}
403
    public void setSSLContext(SSLContext c) { sslContext = c;}
404
    public void setSSLContext(SSLContext c) { sslContext = c;}
404
405
406
407
    /**
408
     * Port in use.
409
     */
410
    @Override
411
    public int getLocalPort() {
412
        ServerSocketChannel ssc = serverSock;
413
        if (ssc == null) {
414
            return -1;
415
        } else {
416
            ServerSocket s = ssc.socket();
417
            if (s == null) {
418
                return -1;
419
            } else {
420
                return s.getLocalPort();
421
            }
422
        }
423
    }
424
425
405
    // --------------------------------------------------------- OOM Parachute Methods
426
    // --------------------------------------------------------- OOM Parachute Methods
406
427
407
    protected void checkParachute() {
428
    protected void checkParachute() {
(-)a/test/org/apache/catalina/connector/TestConnector.java (+23 lines)
Lines 19-24 package org.apache.catalina.connector; Link Here
19
import java.net.SocketTimeoutException;
19
import java.net.SocketTimeoutException;
20
20
21
import static org.junit.Assert.assertEquals;
21
import static org.junit.Assert.assertEquals;
22
import static org.junit.Assert.assertTrue;
22
23
23
import org.junit.Test;
24
import org.junit.Test;
24
25
Lines 69-72 public class TestConnector extends TomcatBaseTest { Link Here
69
        }
70
        }
70
        assertEquals(503, rc);
71
        assertEquals(503, rc);
71
    }
72
    }
73
74
75
    @Test
76
    public void testPort() throws Exception {
77
        Tomcat tomcat = getTomcatInstance();
78
79
        Connector connector1 = tomcat.getConnector();
80
        connector1.setPort(0);
81
82
        Connector connector2 = new Connector();
83
        connector2.setPort(0);
84
85
        tomcat.getService().addConnector(connector2);
86
87
        tomcat.start();
88
89
        int localPort1 = connector1.getLocalPort();
90
        int localPort2 = connector2.getLocalPort();
91
92
        assertTrue(localPort1 > 0);
93
        assertTrue(localPort2 > 0);
94
    }
72
}
95
}

Return to bug 52028