-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathJettyContainerService.java
More file actions
506 lines (438 loc) · 20.8 KB
/
Copy pathJettyContainerService.java
File metadata and controls
506 lines (438 loc) · 20.8 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package com.google.appengine.tools.development;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.log.dev.DevLogHandler;
import com.google.appengine.api.log.dev.LocalLogService;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableList;
import com.google.appengine.tools.development.AbstractContainerService;
import com.google.appengine.tools.development.AppContext;
import com.google.appengine.tools.development.ContainerService;
import com.google.appengine.tools.development.DevAppEngineWebAppContext;
import com.google.appengine.tools.development.DevAppServer;
import com.google.appengine.tools.development.IsolatedAppClassLoader;
import com.google.appengine.tools.development.LocalHttpRequestEnvironment;
import com.google.appengine.tools.development.SerializableObjectsOnlyHashSessionManager;
import com.google.appengine.tools.development.ServiceProvider;
import com.google.appengine.tools.resources.ResourceLoader;
import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.utils.config.AppEngineConfigException;
import com.google.apphosting.utils.config.AppEngineWebXml;
import com.google.apphosting.utils.config.WebModule;
import com.google.apphosting.utils.jetty.JettyLogger;
import com.google.apphosting.utils.jetty.StubSessionManager;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.security.Permissions;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.HandlerWrapper;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.DatastoreSessionManager;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.SessionHandler;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.resource.Resource;
import org.mortbay.util.Scanner;
@ServiceProvider(ContainerService.class)
public class JettyContainerService extends AbstractContainerService {
private static final Logger log = Logger.getLogger(JettyContainerService.class.getName());
public static final String WEB_DEFAULTS_XML = "com/google/appengine/tools/development/webdefault.xml";
private static final int MAX_SIMULTANEOUS_API_CALLS = 100;
private static final Long SOFT_DEADLINE_DELAY_MS = Long.valueOf(60000L);
private static final String[] CONFIG_CLASSES = new String[]{"org.mortbay.jetty.webapp.WebXmlConfiguration", "org.mortbay.jetty.webapp.TagLibConfiguration"};
private static final String WEB_XML_ATTR = "com.google.appengine.tools.development.webXml";
private static final String APPENGINE_WEB_XML_ATTR = "com.google.appengine.tools.development.appEngineWebXml";
private static final int SCAN_INTERVAL_SECONDS = 5;
private WebAppContext context;
private AppContext appContext;
private Server server;
private Scanner scanner;
protected File initContext() throws IOException {
this.context = new DevAppEngineWebAppContext(this.appDir, this.externalResourceDir, this.devAppServerVersion, this.apiProxyLocal, this.devAppServer);
this.appContext = new JettyContainerService.JettyAppContext(null);
this.context.setDescriptor(this.webXmlLocation == null?null:this.webXmlLocation.getAbsolutePath());
this.context.setDefaultsDescriptor("com/google/appengine/tools/development/webdefault.xml");
this.context.setConfigurationClasses(CONFIG_CLASSES);
File appRoot = this.determineAppRoot();
this.installLocalInitializationEnvironment();
URL[] classPath = this.getClassPathForApp(appRoot);
this.context.setClassLoader(new IsolatedAppClassLoader(appRoot, this.externalResourceDir, classPath, JettyContainerService.class.getClassLoader()));
if(Boolean.parseBoolean(System.getProperty("appengine.allowRemoteShutdown"))) {
this.context.addServlet(new ServletHolder(new JettyContainerService.ServerShutdownServlet()), "/_ah/admin/quit");
}
return appRoot;
}
protected void connectContainer() throws Exception {
this.moduleConfigurationHandle.checkEnvironmentVariables();
Thread currentThread = Thread.currentThread();
ClassLoader previousCcl = currentThread.getContextClassLoader();
currentThread.setContextClassLoader((ClassLoader)null);
try {
SelectChannelConnector connector = new SelectChannelConnector();
connector.setHost(this.address);
connector.setPort(this.port);
connector.setSoLingerTime(0);
connector.open();
this.server = new Server();
this.server.addConnector(connector);
this.port = connector.getLocalPort();
} finally {
currentThread.setContextClassLoader(previousCcl);
}
}
protected void startContainer() throws Exception {
this.context.setAttribute("com.google.appengine.tools.development.webXml", this.webXml);
this.context.setAttribute("com.google.appengine.tools.development.appEngineWebXml", this.appEngineWebXml);
Thread currentThread = Thread.currentThread();
ClassLoader previousCcl = currentThread.getContextClassLoader();
currentThread.setContextClassLoader((ClassLoader)null);
try {
JettyContainerService.ApiProxyHandler apiHandler = new JettyContainerService.ApiProxyHandler(this.appEngineWebXml);
apiHandler.setHandler(this.context);
this.server.setHandler(apiHandler);
SessionHandler handler = this.context.getSessionHandler();
if(this.isSessionsEnabled()) {
handler.setSessionManager(new DatastoreSessionManager(DatastoreServiceFactory.getDatastoreService()));
} else {
handler.setSessionManager(new StubSessionManager());
}
this.server.start();
} finally {
currentThread.setContextClassLoader(previousCcl);
}
}
protected void stopContainer() throws Exception {
this.server.stop();
}
protected void startHotDeployScanner() throws Exception {
String fullScanInterval = System.getProperty("appengine.fullscan.seconds");
if(fullScanInterval != null) {
try {
int ex = Integer.parseInt(fullScanInterval);
if(ex < 1) {
log.info("Full scan of the web app for changes is disabled.");
return;
}
log.info("Full scan of the web app in place every " + ex + "s.");
this.fullWebAppScanner(ex);
return;
} catch (NumberFormatException var3) {
log.log(Level.WARNING, "appengine.fullscan.seconds property is not an integer:", var3);
log.log(Level.WARNING, "Using the default scanning method.");
}
}
this.scanner = new Scanner();
this.scanner.setScanInterval(5);
this.scanner.setScanDirs(ImmutableList.of(this.getScanTarget()));
this.scanner.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
try {
return name.equals(JettyContainerService.this.getScanTarget().getName());
} catch (Exception var4) {
return false;
}
}
});
this.scanner.scan();
this.scanner.addListener(new JettyContainerService.ScannerListener(null));
this.scanner.start();
}
protected void stopHotDeployScanner() throws Exception {
if(this.scanner != null) {
this.scanner.stop();
}
this.scanner = null;
}
private File getScanTarget() throws Exception {
return !this.appDir.isFile() && this.context.getWebInf() != null?new File(this.context.getWebInf().getFile().getPath() + File.separator + "appengine-web.xml"):this.appDir;
}
private void fullWebAppScanner(int interval) throws IOException {
String webInf = this.context.getWebInf().getFile().getPath();
ArrayList scanList = new ArrayList();
scanList.add(new File(webInf, "classes"));
scanList.add(new File(webInf, "lib"));
scanList.add(new File(webInf, "web.xml"));
scanList.add(new File(webInf, "appengine-web.xml"));
this.scanner = new Scanner();
this.scanner.setScanInterval(interval);
this.scanner.setScanDirs(scanList);
this.scanner.setReportExistingFilesOnStartup(false);
this.scanner.setRecursive(true);
this.scanner.scan();
this.scanner.addListener(new Scanner.BulkListener() {
public void filesChanged(List changedFiles) throws Exception {
JettyContainerService.log.info("A file has changed, reloading the web application.");
JettyContainerService.this.reloadWebApp();
}
});
this.scanner.start();
}
protected void reloadWebApp() throws Exception {
this.server.getHandler().stop();
this.moduleConfigurationHandle.restoreSystemProperties();
this.moduleConfigurationHandle.readConfiguration();
this.moduleConfigurationHandle.checkEnvironmentVariables();
this.extractFieldsFromWebModule(this.moduleConfigurationHandle.getModule());
Thread currentThread = Thread.currentThread();
ClassLoader previousCcl = currentThread.getContextClassLoader();
currentThread.setContextClassLoader((ClassLoader)null);
try {
File webAppDir = this.initContext();
this.installLoggingServiceHandler();
this.installLocalInitializationEnvironment();
if(!this.isSessionsEnabled()) {
this.context.getSessionHandler().setSessionManager(new StubSessionManager());
}
this.context.setAttribute("com.google.appengine.tools.development.webXml", this.webXml);
this.context.setAttribute("com.google.appengine.tools.development.appEngineWebXml", this.appEngineWebXml);
JettyContainerService.ApiProxyHandler apiHandler = new JettyContainerService.ApiProxyHandler(this.appEngineWebXml);
apiHandler.setHandler(this.context);
this.server.setHandler(apiHandler);
apiHandler.start();
} finally {
currentThread.setContextClassLoader(previousCcl);
}
}
public AppContext getAppContext() {
return this.appContext;
}
// AppScale: Allows dispatcher to access request handler.
public void requestShutdown() {
ApiProxyHandler handler = (ApiProxyHandler) this.server.getHandler();
handler.requestShutdown();
}
public int getRequestCount() {
ApiProxyHandler handler = (ApiProxyHandler) this.server.getHandler();
return handler.getRequestCount();
}
// End AppScale.
public void forwardToServer(HttpServletRequest hrequest, HttpServletResponse hresponse) throws IOException, ServletException {
log.finest("forwarding request to module: " + this.appEngineWebXml.getModule() + "." + this.instance);
RequestDispatcher requestDispatcher = this.context.getServletContext().getRequestDispatcher(hrequest.getRequestURI());
requestDispatcher.forward(hrequest, hresponse);
}
private File determineAppRoot() throws IOException {
Resource webInf = this.context.getWebInf();
if(webInf == null) {
if(this.userCodeClasspathManager.requiresWebInf()) {
throw new AppEngineConfigException("Supplied application has to contain WEB-INF directory.");
} else {
return this.appDir;
}
} else {
return webInf.getFile().getParentFile();
}
}
static {
System.setProperty("org.mortbay.log.class", JettyLogger.class.getName());
}
private class RecordingResponseWrapper extends HttpServletResponseWrapper {
private int status = 200;
RecordingResponseWrapper(HttpServletResponse response) {
super(response);
}
public void setStatus(int sc) {
this.status = sc;
super.setStatus(sc);
}
public int getStatus() {
return this.status;
}
public void sendError(int sc) throws IOException {
this.status = sc;
super.sendError(sc);
}
public void sendError(int sc, String msg) throws IOException {
this.status = sc;
super.sendError(sc, msg);
}
public void sendRedirect(String location) throws IOException {
this.status = 302;
super.sendRedirect(location);
}
public void setStatus(int status, String string) {
super.setStatus(status, string);
this.status = status;
}
public void reset() {
super.reset();
this.status = 200;
}
}
private class ApiProxyHandler extends HandlerWrapper {
private final AppEngineWebXml appEngineWebXml;
// AppScale: Keeps track of active requests in case of shutdown.
private int requestCount;
private boolean sigtermSent;
private ReentrantLock gracefulShutdownLock;
// End AppScale.
public ApiProxyHandler(AppEngineWebXml appEngineWebXml) {
this.appEngineWebXml = appEngineWebXml;
this.requestCount = 0;
this.gracefulShutdownLock = new ReentrantLock();
}
// AppScale: A wrapper for handling requests that keeps track of active requests.
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
this.gracefulShutdownLock.lock();
try {
if (this.sigtermSent) {
response.sendError(503, "This instance is shutting down");
return;
}
this.requestCount++;
} finally {
this.gracefulShutdownLock.unlock();
}
handleImpl(target, request, response, dispatch);
this.gracefulShutdownLock.lock();
try {
this.requestCount--;
} finally {
this.gracefulShutdownLock.unlock();
}
}
// End AppScale.
// AppScale: Allows container to manipulate handler state.
public void requestShutdown() {
this.gracefulShutdownLock.lock();
try {
this.sigtermSent = true;
} finally {
this.gracefulShutdownLock.unlock();
}
}
public int getRequestCount() {
return this.requestCount;
}
// End AppScale.
private void handleImpl(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
if(dispatch == 1) {
long startTimeUsec = System.currentTimeMillis() * 1000L;
Semaphore semaphore = new Semaphore(100);
LocalHttpRequestEnvironment env = new LocalHttpRequestEnvironment(this.appEngineWebXml.getAppId(), WebModule.getModuleName(this.appEngineWebXml), this.appEngineWebXml.getMajorVersionId(), JettyContainerService.this.instance, Integer.valueOf(JettyContainerService.this.getPort()), request, JettyContainerService.SOFT_DEADLINE_DELAY_MS, JettyContainerService.this.modulesFilterHelper);
env.getAttributes().put("com.google.appengine.tools.development.api_call_semaphore", semaphore);
// AppScale: Override default version hostname.
String nginxPort = ResourceLoader.getNginxPort();
String defaultVersionHostname = System.getProperty("NGINX_ADDR") + ":" + nginxPort;
env.getAttributes().put("com.google.appengine.runtime.default_version_hostname", defaultVersionHostname);
// End AppScale
ApiProxy.setEnvironmentForCurrentThread(env);
JettyContainerService.RecordingResponseWrapper wrappedResponse = JettyContainerService.this.new RecordingResponseWrapper(response);
boolean var43 = false;
try {
var43 = true;
super.handle(target, request, wrappedResponse, dispatch);
if(request.getRequestURI().startsWith("/_ah/reloadwebapp")) {
try {
JettyContainerService.this.reloadWebApp();
JettyContainerService.log.info("Reloaded the webapp context: " + request.getParameter("info"));
var43 = false;
} catch (Exception var48) {
JettyContainerService.log.log(Level.WARNING, "Failed to reload the current webapp context.", var48);
var43 = false;
}
} else {
var43 = false;
}
} finally {
if(var43) {
try {
semaphore.acquire(100);
} catch (InterruptedException var45) {
JettyContainerService.log.log(Level.WARNING, "Interrupted while waiting for API calls to complete:", var45);
}
env.callRequestEndListeners();
try {
String appId1 = env.getAppId();
String versionId1 = env.getVersionId();
String requestId1 = DevLogHandler.getRequestId();
long endTimeUsec1 = (new Date()).getTime() * 1000L;
LocalLogService logService1 = (LocalLogService)JettyContainerService.this.apiProxyLocal.getService("logservice");
logService1.addRequestInfo(appId1, versionId1, requestId1, request.getRemoteAddr(), request.getRemoteUser(), startTimeUsec, endTimeUsec1, request.getMethod(), request.getRequestURI(), request.getProtocol(), request.getHeader("User-Agent"), true, Integer.valueOf(wrappedResponse.getStatus()), request.getHeader("Referrer"));
logService1.clearResponseSize();
} finally {
ApiProxy.clearEnvironmentForCurrentThread();
}
}
}
try {
semaphore.acquire(100);
} catch (InterruptedException var47) {
JettyContainerService.log.log(Level.WARNING, "Interrupted while waiting for API calls to complete:", var47);
}
env.callRequestEndListeners();
try {
String appId = env.getAppId();
String versionId = env.getVersionId();
String requestId = DevLogHandler.getRequestId();
long endTimeUsec = (new Date()).getTime() * 1000L;
LocalLogService logService = (LocalLogService)JettyContainerService.this.apiProxyLocal.getService("logservice");
logService.addRequestInfo(appId, versionId, requestId, request.getRemoteAddr(), request.getRemoteUser(), startTimeUsec, endTimeUsec, request.getMethod(), request.getRequestURI(), request.getProtocol(), request.getHeader("User-Agent"), true, Integer.valueOf(wrappedResponse.getStatus()), request.getHeader("Referrer"));
logService.clearResponseSize();
} finally {
ApiProxy.clearEnvironmentForCurrentThread();
}
} else {
super.handle(target, request, response, dispatch);
}
}
}
private class ScannerListener implements Scanner.DiscreteListener {
private ScannerListener() {
}
public void fileAdded(String filename) throws Exception {
this.fileChanged(filename);
}
public void fileChanged(String filename) throws Exception {
JettyContainerService.log.info(filename + " updated, reloading the webapp!");
JettyContainerService.this.reloadWebApp();
}
public void fileRemoved(String filename) throws Exception {
}
// $FF: synthetic method
ScannerListener(Object x1) {
this();
}
}
static class ServerShutdownServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println("Shutting down local server.");
resp.flushBuffer();
DevAppServer server = (DevAppServer)this.getServletContext().getAttribute("com.google.appengine.devappserver.Server");
server.gracefulShutdown();
}
}
private class JettyAppContext implements AppContext {
private JettyAppContext() {
}
public IsolatedAppClassLoader getClassLoader() {
return (IsolatedAppClassLoader)JettyContainerService.this.context.getClassLoader();
}
public Permissions getUserPermissions() {
return JettyContainerService.this.getUserPermissions();
}
public Permissions getApplicationPermissions() {
return this.getClassLoader().getAppPermissions();
}
public Object getContainerContext() {
return JettyContainerService.this.context;
}
// $FF: synthetic method
JettyAppContext(Object x1) {
this();
}
}
}