pablomelo / subethasmtp

Automatically exported from code.google.com/p/subethasmtp
Other
0 stars 0 forks source link

Add support to update the reported value from getPort() when port is 0 #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We have shared continuous integration servers and we want to use subetha smtp 
as mock mail server. IN order to not clash with ports in other projects we use 
port 0 for all our ports in tests so that they JVM will bind to any free port 
and then we pass a reference to the resolver port. For example in Spring I 
would like to be able to do:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="#{mockSMTPServer.server.hostName}" />
        <property name="port" value="#{mockSMTPServer.server.port}" />
    </bean>

    <bean id="mockSMTPServer" class="org.subethamail.wiser.Wiser" init-method="start">
       <property name="port" value="0" />
    </bean>

Unfortunately #{mockSMTPServer.server.port} will report 0 back to me still 
because it doesn't take into account the port the JVM resolved to. This could 
be solved in one of two ways:

1. Expose the ServerSocket via a getter in SMTPServer, then the above config I 
showed would look like:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="#{mockSMTPServer.server.hostName}" />
        <property name="port" value="#{mockSMTPServer.server.serverSocket.localPort}" />
    </bean>

    <bean id="mockSMTPServer" class="org.subethamail.wiser.Wiser" init-method="start">
       <property name="port" value="0" />
    </bean>

2. Change the implementation of SMTPServer.createServerSocket to be:

    protected ServerSocket createServerSocket() throws IOException {
        InetSocketAddress isa;

        if (this.bindAddress == null) {
            isa = new InetSocketAddress(this.port);
        } else {
            isa = new InetSocketAddress(this.bindAddress, this.port);
        }

        ServerSocket serverSocket = new ServerSocket();
        // http://java.sun.com/j2se/1.5.0/docs/api/java/net/ServerSocket.html#setReuseAddress(boolean)
        serverSocket.setReuseAddress(true);
        serverSocket.bind(isa, this.backlog);

        if (this.port == 0) {
            this.port = serverSocket.getLocalPort();
        }

        return serverSocket;
    }

Original issue reported on code.google.com by mattgoldspink1@gmail.com on 3 Mar 2011 at 6:27

GoogleCodeExporter commented 9 years ago
I slightly prefer the second solution, because it is similar to the existing 
behavior of ServerSocket (it returns the actual port number, not the 0 
originally specified).

Original comment by hontvari@flyordie.com on 4 Mar 2011 at 10:58

GoogleCodeExporter commented 9 years ago

Original comment by hontvari@flyordie.com on 4 Mar 2011 at 10:59

GoogleCodeExporter commented 9 years ago

Original comment by hontvari@flyordie.com on 8 Mar 2011 at 1:38

GoogleCodeExporter commented 9 years ago
Thanks for the quick turnaround on adding this. Much appreciated

Original comment by mattgoldspink1@gmail.com on 8 Mar 2011 at 6:21