325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.transport.http.server;
325N/A
325N/Aimport com.sun.net.httpserver.HttpContext;
325N/Aimport com.sun.net.httpserver.HttpServer;
325N/Aimport com.sun.xml.internal.ws.server.ServerRtException;
325N/A
325N/Aimport java.net.InetSocketAddress;
325N/Aimport java.net.URL;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/Aimport java.util.concurrent.ExecutorService;
325N/Aimport java.util.concurrent.Executors;
325N/Aimport java.util.logging.Logger;
325N/A
325N/A/**
325N/A * Manages all the WebService HTTP servers created by JAXWS runtime.
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Afinal class ServerMgr {
325N/A
325N/A private static final ServerMgr serverMgr = new ServerMgr();
325N/A private static final Logger logger =
325N/A Logger.getLogger(
325N/A com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.http");
325N/A private final Map<InetSocketAddress,ServerState> servers = new HashMap<InetSocketAddress,ServerState>();
325N/A
325N/A private ServerMgr() {}
325N/A
325N/A /**
325N/A * Gets the singleton instance.
325N/A * @return manager instance
325N/A */
325N/A static ServerMgr getInstance() {
325N/A return serverMgr;
325N/A }
325N/A
325N/A /*
325N/A * Creates a HttpContext at the given address. If there is already a server
325N/A * it uses that server to create a context. Otherwise, it creates a new
325N/A * HTTP server. This sever is added to servers Map.
325N/A */
325N/A /*package*/ HttpContext createContext(String address) {
325N/A try {
325N/A HttpServer server;
325N/A ServerState state;
325N/A URL url = new URL(address);
325N/A int port = url.getPort();
325N/A if (port == -1) {
325N/A port = url.getDefaultPort();
325N/A }
325N/A InetSocketAddress inetAddress = new InetSocketAddress(url.getHost(),
325N/A port);
325N/A synchronized(servers) {
325N/A state = servers.get(inetAddress);
325N/A if (state == null) {
325N/A logger.fine("Creating new HTTP Server at "+inetAddress);
325N/A // Creates server with default socket backlog
325N/A server = HttpServer.create(inetAddress, 0);
325N/A server.setExecutor(Executors.newCachedThreadPool());
325N/A String path = url.toURI().getPath();
325N/A logger.fine("Creating HTTP Context at = "+path);
325N/A HttpContext context = server.createContext(path);
325N/A server.start();
325N/A logger.fine("HTTP server started = "+inetAddress);
325N/A state = new ServerState(server);
325N/A servers.put(inetAddress, state);
325N/A return context;
325N/A }
325N/A }
325N/A server = state.getServer();
325N/A logger.fine("Creating HTTP Context at = "+url.getPath());
325N/A HttpContext context = server.createContext(url.getPath());
325N/A state.oneMoreContext();
325N/A return context;
325N/A } catch(Exception e) {
325N/A throw new ServerRtException("server.rt.err",e );
325N/A }
325N/A }
325N/A
325N/A /*
325N/A * Removes a context. If the server doesn't have anymore contexts, it
325N/A * would stop the server and server is removed from servers Map.
325N/A */
325N/A /*package*/ void removeContext(HttpContext context) {
325N/A InetSocketAddress inetAddress = context.getServer().getAddress();
325N/A synchronized(servers) {
325N/A ServerState state = servers.get(inetAddress);
325N/A int instances = state.noOfContexts();
325N/A if (instances < 2) {
325N/A ((ExecutorService)state.getServer().getExecutor()).shutdown();
325N/A state.getServer().stop(0);
325N/A servers.remove(inetAddress);
325N/A } else {
325N/A state.getServer().removeContext(context);
325N/A state.oneLessContext();
325N/A }
325N/A }
325N/A }
325N/A
325N/A private static final class ServerState {
325N/A private final HttpServer server;
325N/A private int instances;
325N/A
325N/A ServerState(HttpServer server) {
325N/A this.server = server;
325N/A this.instances = 1;
325N/A }
325N/A
325N/A public HttpServer getServer() {
325N/A return server;
325N/A }
325N/A
325N/A public void oneMoreContext() {
325N/A ++instances;
325N/A }
325N/A
325N/A public void oneLessContext() {
325N/A --instances;
325N/A }
325N/A
325N/A public int noOfContexts() {
325N/A return instances;
325N/A }
325N/A }
325N/A}