1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* Copyright 2011 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.launcher;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import com.vaadin.launcher.util.BrowserLauncher;
/**
* Class for running Jetty servlet container within Eclipse project.
*
*/
public class DevelopmentServerLauncher {
private static final String KEYSTORE = "src/com/vaadin/launcher/keystore";
private final static int serverPort = 8888;
/**
* Main function for running Jetty.
*
* Command line Arguments are passed through to Jetty, see runServer method
* for options.
*
* @param args
* @throws Exception
*/
public static void main(String[] args) {
System.setProperty("java.awt.headless", "true");
// Pass-through of arguments for Jetty
final Map<String, String> serverArgs = parseArguments(args);
if (serverArgs.containsKey("shutdownPort")) {
int port = Integer.parseInt(serverArgs.get("shutdownPort"));
try {
// Try to notify another instance that it's time to close
Socket socket = new Socket((String) null, port);
// Wait until the other instance says it has closed
socket.getInputStream().read();
// Then tidy up
socket.close();
} catch (IOException e) {
// Ignore if port is not open
}
}
// Start Jetty
System.out.println("Starting Jetty servlet container.");
String url;
try {
url = runServer(serverArgs, "Development Server Mode");
// Start Browser
if (serverArgs.containsKey("gui") && url != null) {
System.out.println("Starting Web Browser.");
// Open browser into application URL
BrowserLauncher.openBrowser(url);
}
} catch (Exception e) {
// NOP exception already on console by jetty
}
}
/**
* Run the server with specified arguments.
*
* @param serverArgs
* @return
* @throws Exception
* @throws Exception
*/
protected static String runServer(Map<String, String> serverArgs,
String mode) throws Exception {
// Assign default values for some arguments
assignDefault(serverArgs, "webroot", "WebContent");
assignDefault(serverArgs, "httpPort", "" + serverPort);
assignDefault(serverArgs, "context", "");
int port = serverPort;
try {
port = Integer.parseInt(serverArgs.get("httpPort"));
} catch (NumberFormatException e) {
// keep default value for port
}
// Add help for System.out
System.out
.println("-------------------------------------------------\n"
+ "Starting Vaadin in "
+ mode
+ ".\n"
+ "Running in http://localhost:"
+ port
+ "\n-------------------------------------------------\n");
final Server server = new Server();
final Connector connector = new SelectChannelConnector();
connector.setPort(port);
if (serverArgs.containsKey("withssl")) {
final SslSocketConnector sslConnector = new SslSocketConnector();
sslConnector.setPort(8444);
sslConnector.setTruststore(KEYSTORE);
sslConnector.setTrustPassword("password");
sslConnector.setKeystore(KEYSTORE);
sslConnector.setKeyPassword("password");
sslConnector.setPassword("password");
server.setConnectors(new Connector[] { connector, sslConnector });
} else {
server.setConnectors(new Connector[] { connector });
}
final WebAppContext webappcontext = new WebAppContext();
String path = DevelopmentServerLauncher.class.getPackage().getName()
.replace(".", File.separator);
webappcontext.setDefaultsDescriptor(path + File.separator
+ "jetty-webdefault.xml");
webappcontext.setContextPath(serverArgs.get("context"));
webappcontext.setWar(serverArgs.get("webroot"));
server.setHandler(webappcontext);
try {
server.start();
if (serverArgs.containsKey("shutdownPort")) {
int shutdownPort = Integer.parseInt(serverArgs
.get("shutdownPort"));
final ServerSocket serverSocket = new ServerSocket(
shutdownPort, 1, InetAddress.getByName("127.0.0.1"));
new Thread() {
@Override
public void run() {
try {
System.out
.println("Waiting for shutdown signal on port "
+ serverSocket.getLocalPort());
// Start waiting for a close signal
Socket accept = serverSocket.accept();
// First stop listening to the port
serverSocket.close();
// Then stop the jetty server
server.stop();
// Send a byte to tell the other process that it can
// start jetty
OutputStream outputStream = accept
.getOutputStream();
outputStream.write(0);
outputStream.flush();
// Finally close the socket
accept.close();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
} catch (Exception e) {
server.stop();
throw e;
}
return "http://localhost:" + port + serverArgs.get("context");
}
/**
* Assign default value for given key.
*
* @param map
* @param key
* @param value
*/
private static void assignDefault(Map<String, String> map, String key,
String value) {
if (!map.containsKey(key)) {
map.put(key, value);
}
}
/**
* Parse all command line arguments into a map.
*
* Arguments format "key=value" are put into map.
*
* @param args
* @return map of arguments key value pairs.
*/
protected static Map<String, String> parseArguments(String[] args) {
final Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < args.length; i++) {
final int d = args[i].indexOf("=");
if (d > 0 && d < args[i].length() && args[i].startsWith("--")) {
final String name = args[i].substring(2, d);
final String value = args[i].substring(d + 1);
map.put(name, value);
}
}
return map;
}
}
|