Tue, 20 Dec 2016 14:07:28 +0300
8146086: Publishing two webservices on same port fails with "java.net.BindException: Address already in use"
Reviewed-by: coffeys
ohair@286 | 1 | /* |
aefimov@1307 | 2 | * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.transport.http.server; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.net.httpserver.HttpContext; |
ohair@286 | 29 | import com.sun.net.httpserver.HttpServer; |
ohair@286 | 30 | import com.sun.xml.internal.ws.server.ServerRtException; |
ohair@286 | 31 | |
ohair@286 | 32 | import java.net.InetSocketAddress; |
ohair@286 | 33 | import java.net.URL; |
ohair@286 | 34 | import java.util.HashMap; |
ohair@286 | 35 | import java.util.HashSet; |
ohair@286 | 36 | import java.util.Map; |
ohair@286 | 37 | import java.util.Set; |
ohair@286 | 38 | import java.util.concurrent.ExecutorService; |
ohair@286 | 39 | import java.util.concurrent.Executors; |
ohair@286 | 40 | import java.util.logging.Logger; |
ohair@286 | 41 | |
ohair@286 | 42 | /** |
ohair@286 | 43 | * Manages all the WebService HTTP servers created by JAXWS runtime. |
ohair@286 | 44 | * |
ohair@286 | 45 | * @author Jitendra Kotamraju |
ohair@286 | 46 | */ |
ohair@286 | 47 | final class ServerMgr { |
ohair@286 | 48 | |
ohair@286 | 49 | private static final ServerMgr serverMgr = new ServerMgr(); |
ohair@286 | 50 | private static final Logger logger = |
ohair@286 | 51 | Logger.getLogger( |
ohair@286 | 52 | com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http"); |
ohair@286 | 53 | private final Map<InetSocketAddress,ServerState> servers = new HashMap<InetSocketAddress,ServerState>(); |
ohair@286 | 54 | |
ohair@286 | 55 | private ServerMgr() {} |
ohair@286 | 56 | |
ohair@286 | 57 | /** |
ohair@286 | 58 | * Gets the singleton instance. |
ohair@286 | 59 | * @return manager instance |
ohair@286 | 60 | */ |
ohair@286 | 61 | static ServerMgr getInstance() { |
ohair@286 | 62 | return serverMgr; |
ohair@286 | 63 | } |
ohair@286 | 64 | |
ohair@286 | 65 | /* |
ohair@286 | 66 | * Creates a HttpContext at the given address. If there is already a server |
ohair@286 | 67 | * it uses that server to create a context. Otherwise, it creates a new |
ohair@286 | 68 | * HTTP server. This sever is added to servers Map. |
ohair@286 | 69 | */ |
ohair@286 | 70 | /*package*/ HttpContext createContext(String address) { |
ohair@286 | 71 | try { |
ohair@286 | 72 | HttpServer server; |
ohair@286 | 73 | ServerState state; |
ohair@286 | 74 | URL url = new URL(address); |
ohair@286 | 75 | int port = url.getPort(); |
ohair@286 | 76 | if (port == -1) { |
ohair@286 | 77 | port = url.getDefaultPort(); |
ohair@286 | 78 | } |
ohair@286 | 79 | InetSocketAddress inetAddress = new InetSocketAddress(url.getHost(), |
ohair@286 | 80 | port); |
ohair@286 | 81 | synchronized(servers) { |
ohair@286 | 82 | state = servers.get(inetAddress); |
ohair@286 | 83 | if (state == null) { |
aefimov@1307 | 84 | final int finalPortNum = port; |
aefimov@1307 | 85 | for (ServerState s: servers.values()) { |
aefimov@1307 | 86 | if (s.getServer() |
aefimov@1307 | 87 | .getAddress() |
aefimov@1307 | 88 | .getPort() == finalPortNum) { |
aefimov@1307 | 89 | state = s; |
aefimov@1307 | 90 | break; |
aefimov@1307 | 91 | } |
aefimov@1307 | 92 | } |
ohair@286 | 93 | |
aefimov@1307 | 94 | if (!inetAddress.getAddress().isAnyLocalAddress() || |
aefimov@1307 | 95 | state == null) { |
aefimov@1307 | 96 | logger.fine("Creating new HTTP Server at "+inetAddress); |
aefimov@1307 | 97 | // Creates server with default socket backlog |
aefimov@1307 | 98 | server = HttpServer.create(inetAddress, 0); |
aefimov@1307 | 99 | server.setExecutor(Executors.newCachedThreadPool()); |
aefimov@1307 | 100 | String path = url.toURI().getPath(); |
aefimov@1307 | 101 | logger.fine("Creating HTTP Context at = "+path); |
aefimov@1307 | 102 | HttpContext context = server.createContext(path); |
aefimov@1307 | 103 | server.start(); |
ohair@286 | 104 | |
aefimov@1307 | 105 | // we have to get actual inetAddress from server, which can differ from the original in some cases. |
aefimov@1307 | 106 | // e.g. A port number of zero will let the system pick up an ephemeral port in a bind operation, |
aefimov@1307 | 107 | // or IP: 0.0.0.0 - which is used to monitor network traffic from any valid IP address |
aefimov@1307 | 108 | inetAddress = server.getAddress(); |
aefimov@1307 | 109 | |
aefimov@1307 | 110 | logger.fine("HTTP server started = "+inetAddress); |
aefimov@1307 | 111 | state = new ServerState(server, path); |
aefimov@1307 | 112 | servers.put(inetAddress, state); |
aefimov@1307 | 113 | return context; |
aefimov@1307 | 114 | } |
ohair@286 | 115 | } |
ohair@286 | 116 | } |
ohair@286 | 117 | server = state.getServer(); |
ohair@286 | 118 | |
ohair@286 | 119 | if (state.getPaths().contains(url.getPath())) { |
ohair@286 | 120 | String err = "Context with URL path "+url.getPath()+ " already exists on the server "+server.getAddress(); |
ohair@286 | 121 | logger.fine(err); |
ohair@286 | 122 | throw new IllegalArgumentException(err); |
ohair@286 | 123 | } |
ohair@286 | 124 | |
ohair@286 | 125 | logger.fine("Creating HTTP Context at = "+url.getPath()); |
ohair@286 | 126 | HttpContext context = server.createContext(url.getPath()); |
ohair@286 | 127 | state.oneMoreContext(url.getPath()); |
ohair@286 | 128 | return context; |
ohair@286 | 129 | } catch(Exception e) { |
ohair@286 | 130 | throw new ServerRtException("server.rt.err",e ); |
ohair@286 | 131 | } |
ohair@286 | 132 | } |
ohair@286 | 133 | |
ohair@286 | 134 | /* |
ohair@286 | 135 | * Removes a context. If the server doesn't have anymore contexts, it |
ohair@286 | 136 | * would stop the server and server is removed from servers Map. |
ohair@286 | 137 | */ |
ohair@286 | 138 | /*package*/ void removeContext(HttpContext context) { |
ohair@286 | 139 | InetSocketAddress inetAddress = context.getServer().getAddress(); |
ohair@286 | 140 | synchronized(servers) { |
ohair@286 | 141 | ServerState state = servers.get(inetAddress); |
ohair@286 | 142 | int instances = state.noOfContexts(); |
ohair@286 | 143 | if (instances < 2) { |
ohair@286 | 144 | ((ExecutorService)state.getServer().getExecutor()).shutdown(); |
ohair@286 | 145 | state.getServer().stop(0); |
ohair@286 | 146 | servers.remove(inetAddress); |
ohair@286 | 147 | } else { |
ohair@286 | 148 | state.getServer().removeContext(context); |
ohair@286 | 149 | state.oneLessContext(context.getPath()); |
ohair@286 | 150 | } |
ohair@286 | 151 | } |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | private static final class ServerState { |
ohair@286 | 155 | private final HttpServer server; |
ohair@286 | 156 | private int instances; |
ohair@286 | 157 | private Set<String> paths = new HashSet<String>(); |
ohair@286 | 158 | |
ohair@286 | 159 | ServerState(HttpServer server, String path) { |
ohair@286 | 160 | this.server = server; |
ohair@286 | 161 | this.instances = 1; |
ohair@286 | 162 | paths.add(path); |
ohair@286 | 163 | } |
ohair@286 | 164 | |
ohair@286 | 165 | public HttpServer getServer() { |
ohair@286 | 166 | return server; |
ohair@286 | 167 | } |
ohair@286 | 168 | |
ohair@286 | 169 | public void oneMoreContext(String path) { |
ohair@286 | 170 | ++instances; |
ohair@286 | 171 | paths.add(path); |
ohair@286 | 172 | } |
ohair@286 | 173 | |
ohair@286 | 174 | public void oneLessContext(String path) { |
ohair@286 | 175 | --instances; |
ohair@286 | 176 | paths.remove(path); |
ohair@286 | 177 | } |
ohair@286 | 178 | |
ohair@286 | 179 | public int noOfContexts() { |
ohair@286 | 180 | return instances; |
ohair@286 | 181 | } |
ohair@286 | 182 | |
ohair@286 | 183 | public Set<String> getPaths() { |
ohair@286 | 184 | return paths; |
ohair@286 | 185 | } |
ohair@286 | 186 | } |
ohair@286 | 187 | } |