-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathDevAppServerImpl.java
More file actions
253 lines (222 loc) · 9.39 KB
/
Copy pathDevAppServerImpl.java
File metadata and controls
253 lines (222 loc) · 9.39 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
package com.google.appengine.tools.development;
import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.api.ApiProxy.Environment;
import com.google.apphosting.utils.config.AppEngineWebXml;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.BindException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
class DevAppServerImpl implements DevAppServer
{
private final LocalServerEnvironment environment;
private Map<String, String> serviceProperties = new HashMap();
private Logger logger = Logger.getLogger(DevAppServerImpl.class.getName());
private ServerState serverState = ServerState.INITIALIZING;
private ContainerService mainContainer = null;
private final BackendContainer backendContainer;
private ApiProxyLocal apiProxyLocal;
public DevAppServerImpl( File appDir, File externalResourceDir, File webXmlLocation, File appEngineWebXmlLocation, String address, int port, boolean useCustomStreamHandler, Map<String, Object> containerConfigProperties )
{
String serverInfo = ContainerUtils.getServerInfo();
if (useCustomStreamHandler)
{
StreamHandlerFactory.install();
}
DevSocketImplFactory.install();
this.mainContainer = ContainerUtils.loadContainer();
this.environment = this.mainContainer.configure(serverInfo, appDir, externalResourceDir, webXmlLocation, appEngineWebXmlLocation, address, port, containerConfigProperties, this);
this.backendContainer = BackendServers.getInstance();
this.backendContainer.init(appDir, externalResourceDir, webXmlLocation, appEngineWebXmlLocation, address, containerConfigProperties, this);
}
public void setServiceProperties( Map<String, String> properties )
{
if (this.serverState != ServerState.INITIALIZING)
{
String msg = "Cannot set service properties after the server has been started.";
throw new IllegalStateException(msg);
}
this.serviceProperties = new HashMap(properties);
this.backendContainer.setServiceProperties(properties);
}
public void start() throws Exception
{
if (this.serverState != ServerState.INITIALIZING)
{
throw new IllegalStateException("Cannot start a server that has already been started.");
}
initializeLogging();
ApiProxyLocalFactory factory = new ApiProxyLocalFactory();
this.apiProxyLocal = factory.create(this.environment);
this.apiProxyLocal.setProperties(this.serviceProperties);
ApiProxy.setDelegate(this.apiProxyLocal);
TimeZone currentTimeZone = null;
try
{
currentTimeZone = setServerTimeZone();
this.mainContainer.startup();
ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
env.getAttributes().put("com.google.appengine.runtime.default_version_hostname", "localhost:" + getPort());
this.serviceProperties.putAll(this.mainContainer.getServiceProperties());
this.apiProxyLocal.appendProperties(this.mainContainer.getServiceProperties());
// add for AppScale
AppEngineWebXml config = this.mainContainer.getAppEngineWebXmlConfig();
if (config == null)
{
throw new RuntimeException("applciation context config is null");
}
else
{
System.setProperty("APPLICATION_ID", config.getAppId());
}
this.backendContainer.startupAll(this.mainContainer.getBackendsXml(), this.apiProxyLocal);
}
catch (BindException ex)
{
System.err.println();
System.err.println("************************************************");
System.err.println("Could not open the requested socket: " + ex.getMessage());
System.err.println("Try overriding --address and/or --port.");
System.exit(2);
}
finally
{
ApiProxy.clearEnvironmentForCurrentThread();
restoreLocalTimeZone(currentTimeZone);
}
this.serverState = ServerState.RUNNING;
String prettyAddress = this.mainContainer.getAddress();
if ((prettyAddress.equals("0.0.0.0")) || (prettyAddress.equals("127.0.0.1")))
{
prettyAddress = "localhost";
}
String listeningHostAndPort = prettyAddress + ":" + this.mainContainer.getPort();
this.logger.info("The server is running at http://" + listeningHostAndPort + "/");
this.logger.info("The admin console is running at http://" + listeningHostAndPort + "/_ah/admin");
}
private TimeZone setServerTimeZone()
{
String sysTimeZone = (String)this.serviceProperties.get("appengine.user.timezone.impl");
if ((sysTimeZone != null) && (sysTimeZone.trim().length() > 0))
{
return null;
}
TimeZone utc = TimeZone.getTimeZone("UTC");
assert (utc.getID().equals("UTC")) : "Unable to retrieve the UTC TimeZone";
try
{
Field f = TimeZone.class.getDeclaredField("defaultZoneTL");
f.setAccessible(true);
ThreadLocal tl = (ThreadLocal)f.get(null);
Method getZone = ThreadLocal.class.getMethod("get", new Class[0]);
TimeZone previousZone = (TimeZone)getZone.invoke(tl, new Object[0]);
Method setZone = ThreadLocal.class.getMethod("set", new Class[] { Object.class });
setZone.invoke(tl, new Object[] { utc });
return previousZone;
}
catch (Exception e)
{
try
{
Method getZone = TimeZone.class.getDeclaredMethod("getDefaultInAppContext", new Class[0]);
getZone.setAccessible(true);
TimeZone previousZone = (TimeZone)getZone.invoke(null, new Object[0]);
Method setZone = TimeZone.class.getDeclaredMethod("setDefaultInAppContext", new Class[] { TimeZone.class });
setZone.setAccessible(true);
setZone.invoke(null, new Object[] { utc });
return previousZone;
}
catch (Exception ex)
{
throw new RuntimeException("Unable to set the TimeZone to UTC", ex);
}
}
}
private void restoreLocalTimeZone( TimeZone timeZone )
{
String sysTimeZone = (String)this.serviceProperties.get("appengine.user.timezone.impl");
if ((sysTimeZone != null) && (sysTimeZone.trim().length() > 0))
{
return;
}
try
{
Field f = TimeZone.class.getDeclaredField("defaultZoneTL");
f.setAccessible(true);
ThreadLocal tl = (ThreadLocal)f.get(null);
Method setZone = ThreadLocal.class.getMethod("set", new Class[] { Object.class });
setZone.invoke(tl, new Object[] { timeZone });
}
catch (Exception e)
{
try
{
Method setZone = TimeZone.class.getDeclaredMethod("setDefaultInAppContext", new Class[] { TimeZone.class });
setZone.setAccessible(true);
setZone.invoke(null, new Object[] { timeZone });
}
catch (Exception ex)
{
throw new RuntimeException("Unable to restore the previous TimeZone", ex);
}
}
}
public void restart() throws Exception
{
if (this.serverState != ServerState.RUNNING)
{
throw new IllegalStateException("Cannot restart a server that is not currently running.");
}
this.mainContainer.shutdown();
this.backendContainer.shutdownAll();
this.mainContainer.startup();
this.backendContainer.startupAll(this.mainContainer.getBackendsXml(), this.apiProxyLocal);
}
public void shutdown() throws Exception
{
if (this.serverState != ServerState.RUNNING)
{
throw new IllegalStateException("Cannot shutdown a server that is not currently running.");
}
this.mainContainer.shutdown();
this.backendContainer.shutdownAll();
ApiProxy.setDelegate(null);
this.apiProxyLocal = null;
this.serverState = ServerState.SHUTDOWN;
}
public int getPort()
{
return this.mainContainer.getPort();
}
public AppContext getAppContext()
{
return this.mainContainer.getAppContext();
}
public void setThrowOnEnvironmentVariableMismatch( boolean throwOnMismatch )
{
this.mainContainer.setEnvironmentVariableMismatchSeverity(throwOnMismatch ? ContainerService.EnvironmentVariableMismatchSeverity.ERROR : ContainerService.EnvironmentVariableMismatchSeverity.WARNING);
}
private void initializeLogging()
{
for (Handler handler : Logger.getLogger("").getHandlers())
if ((handler instanceof ConsoleHandler)) handler.setLevel(Level.FINEST);
}
ServerState getServerState()
{
return this.serverState;
}
static enum ServerState
{
INITIALIZING,
RUNNING,
STOPPING,
SHUTDOWN;
}
}