1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/ServerMgr.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,174 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.transport.http.server; 1.30 + 1.31 +import com.sun.net.httpserver.HttpContext; 1.32 +import com.sun.net.httpserver.HttpServer; 1.33 +import com.sun.xml.internal.ws.server.ServerRtException; 1.34 + 1.35 +import java.net.InetSocketAddress; 1.36 +import java.net.URL; 1.37 +import java.util.HashMap; 1.38 +import java.util.HashSet; 1.39 +import java.util.Map; 1.40 +import java.util.Set; 1.41 +import java.util.concurrent.ExecutorService; 1.42 +import java.util.concurrent.Executors; 1.43 +import java.util.logging.Logger; 1.44 + 1.45 +/** 1.46 + * Manages all the WebService HTTP servers created by JAXWS runtime. 1.47 + * 1.48 + * @author Jitendra Kotamraju 1.49 + */ 1.50 +final class ServerMgr { 1.51 + 1.52 + private static final ServerMgr serverMgr = new ServerMgr(); 1.53 + private static final Logger logger = 1.54 + Logger.getLogger( 1.55 + com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http"); 1.56 + private final Map<InetSocketAddress,ServerState> servers = new HashMap<InetSocketAddress,ServerState>(); 1.57 + 1.58 + private ServerMgr() {} 1.59 + 1.60 + /** 1.61 + * Gets the singleton instance. 1.62 + * @return manager instance 1.63 + */ 1.64 + static ServerMgr getInstance() { 1.65 + return serverMgr; 1.66 + } 1.67 + 1.68 + /* 1.69 + * Creates a HttpContext at the given address. If there is already a server 1.70 + * it uses that server to create a context. Otherwise, it creates a new 1.71 + * HTTP server. This sever is added to servers Map. 1.72 + */ 1.73 + /*package*/ HttpContext createContext(String address) { 1.74 + try { 1.75 + HttpServer server; 1.76 + ServerState state; 1.77 + URL url = new URL(address); 1.78 + int port = url.getPort(); 1.79 + if (port == -1) { 1.80 + port = url.getDefaultPort(); 1.81 + } 1.82 + InetSocketAddress inetAddress = new InetSocketAddress(url.getHost(), 1.83 + port); 1.84 + synchronized(servers) { 1.85 + state = servers.get(inetAddress); 1.86 + if (state == null) { 1.87 + logger.fine("Creating new HTTP Server at "+inetAddress); 1.88 + // Creates server with default socket backlog 1.89 + server = HttpServer.create(inetAddress, 0); 1.90 + server.setExecutor(Executors.newCachedThreadPool()); 1.91 + String path = url.toURI().getPath(); 1.92 + logger.fine("Creating HTTP Context at = "+path); 1.93 + HttpContext context = server.createContext(path); 1.94 + server.start(); 1.95 + 1.96 + // we have to get actual inetAddress from server, which can differ from the original in some cases. 1.97 + // e.g. A port number of zero will let the system pick up an ephemeral port in a bind operation, 1.98 + // or IP: 0.0.0.0 - which is used to monitor network traffic from any valid IP address 1.99 + inetAddress = server.getAddress(); 1.100 + 1.101 + logger.fine("HTTP server started = "+inetAddress); 1.102 + state = new ServerState(server, path); 1.103 + servers.put(inetAddress, state); 1.104 + return context; 1.105 + } 1.106 + } 1.107 + server = state.getServer(); 1.108 + 1.109 + if (state.getPaths().contains(url.getPath())) { 1.110 + String err = "Context with URL path "+url.getPath()+ " already exists on the server "+server.getAddress(); 1.111 + logger.fine(err); 1.112 + throw new IllegalArgumentException(err); 1.113 + } 1.114 + 1.115 + logger.fine("Creating HTTP Context at = "+url.getPath()); 1.116 + HttpContext context = server.createContext(url.getPath()); 1.117 + state.oneMoreContext(url.getPath()); 1.118 + return context; 1.119 + } catch(Exception e) { 1.120 + throw new ServerRtException("server.rt.err",e ); 1.121 + } 1.122 + } 1.123 + 1.124 + /* 1.125 + * Removes a context. If the server doesn't have anymore contexts, it 1.126 + * would stop the server and server is removed from servers Map. 1.127 + */ 1.128 + /*package*/ void removeContext(HttpContext context) { 1.129 + InetSocketAddress inetAddress = context.getServer().getAddress(); 1.130 + synchronized(servers) { 1.131 + ServerState state = servers.get(inetAddress); 1.132 + int instances = state.noOfContexts(); 1.133 + if (instances < 2) { 1.134 + ((ExecutorService)state.getServer().getExecutor()).shutdown(); 1.135 + state.getServer().stop(0); 1.136 + servers.remove(inetAddress); 1.137 + } else { 1.138 + state.getServer().removeContext(context); 1.139 + state.oneLessContext(context.getPath()); 1.140 + } 1.141 + } 1.142 + } 1.143 + 1.144 + private static final class ServerState { 1.145 + private final HttpServer server; 1.146 + private int instances; 1.147 + private Set<String> paths = new HashSet<String>(); 1.148 + 1.149 + ServerState(HttpServer server, String path) { 1.150 + this.server = server; 1.151 + this.instances = 1; 1.152 + paths.add(path); 1.153 + } 1.154 + 1.155 + public HttpServer getServer() { 1.156 + return server; 1.157 + } 1.158 + 1.159 + public void oneMoreContext(String path) { 1.160 + ++instances; 1.161 + paths.add(path); 1.162 + } 1.163 + 1.164 + public void oneLessContext(String path) { 1.165 + --instances; 1.166 + paths.remove(path); 1.167 + } 1.168 + 1.169 + public int noOfContexts() { 1.170 + return instances; 1.171 + } 1.172 + 1.173 + public Set<String> getPaths() { 1.174 + return paths; 1.175 + } 1.176 + } 1.177 +}