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