1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8146086
27 * @summary Publishing two webservices on same port fails with "java.net.BindException: Address already in use"
28 * @modules java.xml.ws
29 * @run main/othervm WSTest
30 */
31import javax.jws.WebMethod;
32import javax.jws.WebService;
33import javax.xml.ws.Endpoint;
34import java.net.ServerSocket;
35
36public class WSTest {
37
38    @WebService(targetNamespace = "test")
39    public static class Method1 {
40        @WebMethod
41        public String getMethod1Value() {
42            return "from Method1";
43        }
44    }
45
46    @WebService(targetNamespace = "test")
47    public static class Method2 {
48        @WebMethod
49        public String getMethod2Value() {
50            return "from Method2";
51        }
52    }
53
54    public static void main(String[] args) throws Exception {
55
56        // find a free port
57        ServerSocket ss = new ServerSocket(0);
58        int port = ss.getLocalPort();
59        ss.close();
60
61        Endpoint endPoint1 = null;
62        Endpoint endPoint2 = null;
63        try {
64            endPoint1 = Endpoint.publish("http://0.0.0.0:" + port + "/method1",
65                    new Method1());
66            endPoint2 = Endpoint.publish("http://0.0.0.0:" + port + "/method2",
67                    new Method2());
68
69            System.out.println("Sleep 3 secs...");
70
71            Thread.sleep(3000);
72        } finally {
73            stop(endPoint2);
74            stop(endPoint1);
75        }
76    }
77
78    private static void stop(Endpoint endPoint) {
79        if (endPoint == null) return;
80
81        try {
82            endPoint.stop();
83        } catch (Throwable ignored) {
84        }
85    }
86
87}
88