001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.transport.http.server;
026:
027: import com.sun.net.httpserver.HttpContext;
028: import com.sun.net.httpserver.HttpServer;
029: import com.sun.net.httpserver.HttpsConfigurator;
030: import com.sun.net.httpserver.HttpsServer;
031: import com.sun.xml.internal.ws.server.ServerRtException;
032: import java.net.InetSocketAddress;
033: import java.net.URL;
034: import java.util.HashMap;
035: import java.util.Map;
036: import java.util.concurrent.ExecutorService;
037: import java.util.concurrent.Executors;
038: import java.util.logging.Logger;
039: import javax.net.ssl.SSLContext;
040:
041: /**
042: * Manages all the WebService HTTP servers created by JAXWS runtime.
043: *
044: * @author WS Development Team
045: */
046: public class ServerMgr {
047:
048: private static final ServerMgr serverMgr = new ServerMgr();
049: private static final Logger logger = Logger
050: .getLogger(com.sun.xml.internal.ws.util.Constants.LoggingDomain
051: + ".server.http");
052: private Map<InetSocketAddress, ServerState> servers = new HashMap();
053:
054: protected ServerMgr() {
055: }
056:
057: public static ServerMgr getInstance() {
058: return serverMgr;
059: }
060:
061: /*
062: * Creates a HttpContext at the given address. If there is already a server
063: * it uses that server to create a context. Otherwise, it creates a new
064: * HTTP server. This sever is added to servers Map.
065: */
066: public HttpContext createContext(String address) {
067: try {
068: HttpServer server = null;
069: ServerState state = null;
070: boolean started = false;
071: URL url = new URL(address);
072: int port = url.getPort();
073: if (port == -1) {
074: port = url.getDefaultPort();
075: }
076: InetSocketAddress inetAddress = new InetSocketAddress(url
077: .getHost(), url.getPort());
078: synchronized (servers) {
079: state = servers.get(inetAddress);
080: if (state == null) {
081: logger.fine("Creating new HTTP Server at "
082: + inetAddress);
083: server = HttpServer.create(inetAddress, 5);
084: server.setExecutor(Executors.newFixedThreadPool(5));
085: logger.fine("Creating HTTP Context at = "
086: + url.getPath());
087: HttpContext context = server.createContext(url
088: .getPath());
089: server.start();
090: logger.fine("HTTP server started = " + inetAddress);
091: state = new ServerState(server);
092: servers.put(inetAddress, state);
093: return context;
094: }
095: }
096: server = state.getServer();
097: logger.fine("Creating HTTP Context at = " + url.getPath());
098: HttpContext context = server.createContext(url.getPath());
099: state.oneMoreContext();
100: return context;
101: } catch (Exception e) {
102: throw new ServerRtException("server.rt.err", e);
103: }
104: }
105:
106: /*
107: * Removes a context. If the server doesn't have anymore contexts, it
108: * would stop the server and server is removed from servers Map.
109: */
110: public void removeContext(HttpContext context) {
111: InetSocketAddress inetAddress = context.getServer()
112: .getAddress();
113: synchronized (servers) {
114: ServerState state = servers.get(inetAddress);
115: int instances = state.noOfContexts();
116: if (instances < 2) {
117: ((ExecutorService) state.getServer().getExecutor())
118: .shutdown();
119: state.getServer().stop(0);
120: servers.remove(inetAddress);
121: } else {
122: state.getServer().removeContext(context);
123: state.oneLessContext();
124: }
125: }
126: }
127:
128: private static class ServerState {
129: private HttpServer server;
130: private int instances;
131:
132: ServerState(HttpServer server) {
133: this .server = server;
134: this .instances = 1;
135: }
136:
137: public HttpServer getServer() {
138: return server;
139: }
140:
141: public void oneMoreContext() {
142: ++instances;
143: }
144:
145: public void oneLessContext() {
146: --instances;
147: }
148:
149: public int noOfContexts() {
150: return instances;
151: }
152: }
153: }
|