src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/ServerMgr.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
child 1307
9b94b2a51e16
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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) {
ohair@286 84 logger.fine("Creating new HTTP Server at "+inetAddress);
ohair@286 85 // Creates server with default socket backlog
ohair@286 86 server = HttpServer.create(inetAddress, 0);
ohair@286 87 server.setExecutor(Executors.newCachedThreadPool());
ohair@286 88 String path = url.toURI().getPath();
ohair@286 89 logger.fine("Creating HTTP Context at = "+path);
ohair@286 90 HttpContext context = server.createContext(path);
ohair@286 91 server.start();
ohair@286 92
ohair@286 93 // we have to get actual inetAddress from server, which can differ from the original in some cases.
ohair@286 94 // e.g. A port number of zero will let the system pick up an ephemeral port in a bind operation,
ohair@286 95 // or IP: 0.0.0.0 - which is used to monitor network traffic from any valid IP address
ohair@286 96 inetAddress = server.getAddress();
ohair@286 97
ohair@286 98 logger.fine("HTTP server started = "+inetAddress);
ohair@286 99 state = new ServerState(server, path);
ohair@286 100 servers.put(inetAddress, state);
ohair@286 101 return context;
ohair@286 102 }
ohair@286 103 }
ohair@286 104 server = state.getServer();
ohair@286 105
ohair@286 106 if (state.getPaths().contains(url.getPath())) {
ohair@286 107 String err = "Context with URL path "+url.getPath()+ " already exists on the server "+server.getAddress();
ohair@286 108 logger.fine(err);
ohair@286 109 throw new IllegalArgumentException(err);
ohair@286 110 }
ohair@286 111
ohair@286 112 logger.fine("Creating HTTP Context at = "+url.getPath());
ohair@286 113 HttpContext context = server.createContext(url.getPath());
ohair@286 114 state.oneMoreContext(url.getPath());
ohair@286 115 return context;
ohair@286 116 } catch(Exception e) {
ohair@286 117 throw new ServerRtException("server.rt.err",e );
ohair@286 118 }
ohair@286 119 }
ohair@286 120
ohair@286 121 /*
ohair@286 122 * Removes a context. If the server doesn't have anymore contexts, it
ohair@286 123 * would stop the server and server is removed from servers Map.
ohair@286 124 */
ohair@286 125 /*package*/ void removeContext(HttpContext context) {
ohair@286 126 InetSocketAddress inetAddress = context.getServer().getAddress();
ohair@286 127 synchronized(servers) {
ohair@286 128 ServerState state = servers.get(inetAddress);
ohair@286 129 int instances = state.noOfContexts();
ohair@286 130 if (instances < 2) {
ohair@286 131 ((ExecutorService)state.getServer().getExecutor()).shutdown();
ohair@286 132 state.getServer().stop(0);
ohair@286 133 servers.remove(inetAddress);
ohair@286 134 } else {
ohair@286 135 state.getServer().removeContext(context);
ohair@286 136 state.oneLessContext(context.getPath());
ohair@286 137 }
ohair@286 138 }
ohair@286 139 }
ohair@286 140
ohair@286 141 private static final class ServerState {
ohair@286 142 private final HttpServer server;
ohair@286 143 private int instances;
ohair@286 144 private Set<String> paths = new HashSet<String>();
ohair@286 145
ohair@286 146 ServerState(HttpServer server, String path) {
ohair@286 147 this.server = server;
ohair@286 148 this.instances = 1;
ohair@286 149 paths.add(path);
ohair@286 150 }
ohair@286 151
ohair@286 152 public HttpServer getServer() {
ohair@286 153 return server;
ohair@286 154 }
ohair@286 155
ohair@286 156 public void oneMoreContext(String path) {
ohair@286 157 ++instances;
ohair@286 158 paths.add(path);
ohair@286 159 }
ohair@286 160
ohair@286 161 public void oneLessContext(String path) {
ohair@286 162 --instances;
ohair@286 163 paths.remove(path);
ohair@286 164 }
ohair@286 165
ohair@286 166 public int noOfContexts() {
ohair@286 167 return instances;
ohair@286 168 }
ohair@286 169
ohair@286 170 public Set<String> getPaths() {
ohair@286 171 return paths;
ohair@286 172 }
ohair@286 173 }
ohair@286 174 }

mercurial