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