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  * @run main/othervm WSTest
29  */
30 import javax.jws.WebMethod;
31 import javax.jws.WebService;
32 import javax.xml.ws.Endpoint;
33 import java.net.ServerSocket;
34 
35 public class WSTest {
36 
37     @WebService(targetNamespace = "test")
38     public static class Method1 {
39         @WebMethod
getMethod1Value()40         public String getMethod1Value() {
41             return "from Method1";
42         }
43     }
44 
45     @WebService(targetNamespace = "test")
46     public static class Method2 {
47         @WebMethod
getMethod2Value()48         public String getMethod2Value() {
49             return "from Method2";
50         }
51     }
52 
main(String[] args)53     public static void main(String[] args) throws Exception {
54 
55         // find a free port
56         ServerSocket ss = new ServerSocket(0);
57         int port = ss.getLocalPort();
58         ss.close();
59 
60         Endpoint endPoint1 = null;
61         Endpoint endPoint2 = null;
62         try {
63             endPoint1 = Endpoint.publish("http://0.0.0.0:" + port + "/method1",
64                     new Method1());
65             endPoint2 = Endpoint.publish("http://0.0.0.0:" + port + "/method2",
66                     new Method2());
67 
68             System.out.println("Sleep 3 secs...");
69 
70             Thread.sleep(3000);
71         } finally {
72             stop(endPoint2);
73             stop(endPoint1);
74         }
75     }
76 
stop(Endpoint endPoint)77     private static void stop(Endpoint endPoint) {
78         if (endPoint == null) return;
79 
80         try {
81             endPoint.stop();
82         } catch (Throwable ignored) {
83         }
84     }
85 
86 }
87