-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathDevAppServerMain.java
More file actions
495 lines (453 loc) · 18.3 KB
/
Copy pathDevAppServerMain.java
File metadata and controls
495 lines (453 loc) · 18.3 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
package com.google.appengine.tools.development;
import com.google.appengine.repackaged.com.google.common.annotations.VisibleForTesting;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableList;
import com.google.appengine.tools.info.SdkInfo;
import com.google.appengine.tools.info.UpdateCheck;
import com.google.appengine.tools.plugins.SDKPluginManager;
import com.google.appengine.tools.plugins.SDKRuntimePlugin;
import com.google.appengine.tools.plugins.SDKRuntimePlugin.ApplicationDirectories;
import com.google.appengine.tools.util.Action;
import com.google.appengine.tools.util.Logging;
import com.google.appengine.tools.util.Option;
import com.google.appengine.tools.util.Parser;
import com.google.appengine.tools.util.Parser.ParseResult;
import java.awt.Toolkit;
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
public class DevAppServerMain
{
public static final String EXTERNAL_RESOURCE_DIR_ARG = "external_resource_dir";
public static final String GENERATE_WAR_ARG = "generate_war";
public static final String GENERATED_WAR_DIR_ARG = "generated_war_dir";
private static final String DEFAULT_RDBMS_PROPERTIES_FILE = ".local.rdbms.properties";
private static final String RDBMS_PROPERTIES_FILE_SYSTEM_PROPERTY = "rdbms.properties.file";
private static String originalTimeZone;
private final Action ACTION = new StartAction();
private String server = SdkInfo.getDefaultServer();
private String address = "127.0.0.1";
private int port = 8080;
private boolean disableUpdateCheck;
private boolean disableRestrictedCheck = false;
private String externalResourceDir = null;
private List<String> propertyOptions = null;
private String generatedDirectory = null;
// add for AppScale
private String db_location;
private String login_server;
private String cookie;
private String appscale_version;
private String admin_console_version;
private final List<Option> PARSERS = buildOptions(this);
private static final String PREFIX = "When generating a war directory,";
private static List<Option> getBuiltInOptions( DevAppServerMain main )
{
return Arrays.asList(new Option[] { new Option("h", "help", true)
{
public void apply()
{
DevAppServerMain.printHelp(System.err);
System.exit(0);
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --help, -h Show this help message and exit.");
}
}, new DevAppServerOption(main, "s", "server", false)
{
public void apply()
{
this.main.server = getValue();
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --server=SERVER The server to use to determine the latest", " -s SERVER SDK version.");
}
}, new DevAppServerOption(main, "a", "address", false)
{
public void apply()
{
this.main.address = getValue();
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --address=ADDRESS The address of the interface on the local machine", " -a ADDRESS to bind to (or 0.0.0.0 for all interfaces).");
}
}, new DevAppServerOption(main, "p", "port", false)
{
public void apply()
{
this.main.port = Integer.valueOf(getValue()).intValue();
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --port=PORT The port number to bind to on the local machine.", " -p PORT");
}
}, new DevAppServerOption(main, null, "sdk_root", false)
{
public void apply()
{
System.setProperty("appengine.sdk.root", getValue());
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --sdk_root=DIR Overrides where the SDK is located.");
}
}, new DevAppServerOption(main, null, "disable_update_check", true)
{
public void apply()
{
this.main.disableUpdateCheck = true;
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --disable_update_check Disable the check for newer SDK versions.");
}
}, new DevAppServerOption(main, null, "generated_dir", false)
{
public void apply()
{
this.main.generatedDirectory = getValue();
}
public List<String> getHelpLines()
{
return ImmutableList.of(" --generated_dir=DIR Set the directory where generated files are created.");
}
}, new DevAppServerOption(main, null, "disable_restricted_check", true)
{
public void apply()
{
this.main.disableRestrictedCheck = true;
}
}, new DevAppServerOption(main, null, "external_resource_dir", false)
{
public void apply()
{
this.main.externalResourceDir = getValue();
}
}, new DevAppServerOption(main, null, "property", false)
{
public void apply()
{
this.main.propertyOptions = getValues();
}
/*
* AppScale added all of the below to end of list
*/
}, new DevAppServerOption(main, null, "datastore_path", false)
{
public void apply()
{
this.main.db_location = getValue();
System.setProperty("DB_LOCATION", this.main.db_location);
}
}, new DevAppServerOption(main, null, "login_server", false)
{
public void apply()
{
this.main.login_server = getValue();
System.setProperty("LOGIN_SERVER", this.main.login_server);
}
}, new DevAppServerOption(main, null, "cookie_secret", false)
{
public void apply()
{
this.main.cookie = getValue();
System.setProperty("COOKIE_SECRET", this.main.cookie);
}
}, new DevAppServerOption(main, null, "appscale_version", false)
{
public void apply()
{
this.main.appscale_version = getValue();
System.setProperty("APP_SCALE_VERSION", this.main.appscale_version);
}
}, new DevAppServerOption(main, null, "admin_console_version", false)
{ // changed from admin_console_server
public void apply()
{
this.main.admin_console_version = getValue();
System.setProperty("ADMIN_CONSOLE_VERSION", this.main.admin_console_version);
}
}, new DevAppServerOption(main, null, "NGINX_ADDRESS", false)
{
public void apply()
{
System.setProperty("NGINX_ADDR", getValue());
}
}, new DevAppServerOption(main, null, "NGINX_PORT", false)
{
public void apply()
{
System.setProperty("NGINX_PORT", getValue());
}
} });
}
private static List<Option> buildOptions( DevAppServerMain main )
{
List options = getBuiltInOptions(main);
for (SDKRuntimePlugin runtimePlugin : SDKPluginManager.findAllRuntimePlugins())
{
options = runtimePlugin.customizeDevAppServerOptions(options);
}
return options;
}
public static void main( String[] args ) throws Exception
{
recordTimeZone();
Logging.initializeLogging();
if (System.getProperty("os.name").equalsIgnoreCase("Mac OS X"))
{
Toolkit.getDefaultToolkit();
}
new DevAppServerMain(args);
}
private static void recordTimeZone()
{
originalTimeZone = System.getProperty("user.timezone");
}
public DevAppServerMain( String[] args ) throws Exception
{
Parser parser = new Parser();
Parser.ParseResult result = parser.parseArgs(this.ACTION, this.PARSERS, args);
result.applyArgs();
}
public static void printHelp( PrintStream out )
{
out.println("Usage: <dev-appserver> [options] <app directory>");
out.println("");
out.println("Options:");
for (Option option : buildOptions(null))
{
for (String helpString : option.getHelpLines())
{
out.println(helpString);
}
}
out.println(" --jvm_flag=FLAG Pass FLAG as a JVM argument. May be repeated to");
out.println(" supply multiple flags.");
}
public static void validateWarPath( File war )
{
if (!war.exists())
{
System.out.println("Unable to find the webapp directory " + war);
printHelp(System.err);
System.exit(1);
}
else if (!war.isDirectory())
{
System.out.println("dev_appserver only accepts webapp directories, not war files.");
printHelp(System.err);
System.exit(1);
}
}
private void validateArgsForWarGeneration( List<String> args )
{
ifNotWarGenConditionExit(this.externalResourceDir != null, "--external_resource_dir must also be specified.");
File appYamlFile = new File(this.externalResourceDir, "app.yaml");
ifNotWarGenConditionExit(appYamlFile.isFile(), "the external resource directory must contain a file named app.yaml.");
ifNotWarGenConditionExit(args.size() == 0, "the command line should not include a war directory argument.");
}
@VisibleForTesting
static Map<String, String> parsePropertiesList( List<String> properties )
{
Map parsedProperties = new HashMap();
if (properties != null)
{
for (String property : properties)
{
String[] propertyKeyValue = property.split("=", 2);
if (propertyKeyValue.length == 2)
parsedProperties.put(propertyKeyValue[0], propertyKeyValue[1]);
else if (propertyKeyValue[0].startsWith("no"))
parsedProperties.put(propertyKeyValue[0].substring(2), "false");
else
{
parsedProperties.put(propertyKeyValue[0], "true");
}
}
}
return parsedProperties;
}
private static void ifNotWarGenConditionExit( boolean condition, String suffix )
{
if (!condition)
{
System.err.println("When generating a war directory, " + suffix);
System.exit(1);
}
}
class StartAction extends Action
{
StartAction()
{
super();
}
public void apply()
{
List args = getArgs();
try
{
File externalResourceDir = getExternalResourceDir();
if (args.size() != 1)
{
DevAppServerMain.printHelp(System.err);
System.exit(1);
}
File appDir = new File((String)args.get(0)).getCanonicalFile();
DevAppServerMain.validateWarPath(appDir);
SDKRuntimePlugin runtimePlugin = SDKPluginManager.findRuntimePlugin(appDir);
if (runtimePlugin != null)
{
SDKRuntimePlugin.ApplicationDirectories appDirs = runtimePlugin.generateApplicationDirectories(appDir);
appDir = appDirs.getWarDir();
externalResourceDir = appDirs.getExternalResourceDir();
}
UpdateCheck updateCheck = new UpdateCheck(DevAppServerMain.this.server, appDir, true);
if ((updateCheck.allowedToCheckForUpdates()) && (!DevAppServerMain.this.disableUpdateCheck))
{
updateCheck.maybePrintNagScreen(System.err);
}
updateCheck.checkJavaVersion(System.err);
DevAppServer server = new DevAppServerFactory().createDevAppServer(appDir, externalResourceDir, DevAppServerMain.this.address, DevAppServerMain.this.port);
Map properties = System.getProperties();
Map stringProperties = properties;
setTimeZone(stringProperties);
setGeneratedDirectory(stringProperties);
if (DevAppServerMain.this.disableRestrictedCheck)
{
stringProperties.put("appengine.disableRestrictedCheck", "");
}
setRdbmsPropertiesFile(stringProperties, appDir, externalResourceDir);
stringProperties.putAll(DevAppServerMain.parsePropertiesList(DevAppServerMain.this.propertyOptions));
server.setServiceProperties(stringProperties);
server.start();
try
{
while (true)
{
Thread.sleep(3600000L);
}
}
catch (InterruptedException e)
{
System.out.println("Shutting down.");
System.exit(0);
}
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
private void setTimeZone( Map<String, String> serviceProperties )
{
String timeZone = (String)serviceProperties.get("appengine.user.timezone");
if (timeZone != null)
TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
else
{
timeZone = DevAppServerMain.originalTimeZone;
}
serviceProperties.put("appengine.user.timezone.impl", timeZone);
}
private void setGeneratedDirectory( Map<String, String> stringProperties )
{
if (DevAppServerMain.this.generatedDirectory != null)
{
File dir = new File(DevAppServerMain.this.generatedDirectory);
String error = null;
if (dir.exists())
{
if (!dir.isDirectory())
error = DevAppServerMain.this.generatedDirectory + " is not a directory.";
else if (!dir.canWrite()) error = DevAppServerMain.this.generatedDirectory + " is not writable.";
}
else if (!dir.mkdirs())
{
error = "Could not make " + DevAppServerMain.this.generatedDirectory;
}
if (error != null)
{
System.err.println(error);
System.exit(1);
}
stringProperties.put("appengine.generated.dir", DevAppServerMain.this.generatedDirectory);
}
}
private void setRdbmsPropertiesFile( Map<String, String> stringProperties, File appDir, File externalResourceDir )
{
if (stringProperties.get("rdbms.properties.file") != null)
{
return;
}
File file = findRdbmsPropertiesFile(externalResourceDir);
if (file == null)
{
file = findRdbmsPropertiesFile(appDir);
}
if (file != null)
{
String path = file.getPath();
System.out.println("Reading local rdbms properties from " + path);
stringProperties.put("rdbms.properties.file", path);
}
}
private File findRdbmsPropertiesFile( File dir )
{
File candidate = new File(dir, ".local.rdbms.properties");
if ((candidate.isFile()) && (candidate.canRead()))
{
return candidate;
}
return null;
}
private File getExternalResourceDir()
{
if (DevAppServerMain.this.externalResourceDir == null)
{
return null;
}
DevAppServerMain.this.externalResourceDir = DevAppServerMain.this.externalResourceDir.trim();
String error = null;
File dir = null;
if (DevAppServerMain.this.externalResourceDir.isEmpty())
{
error = "The empty string was specified for external_resource_dir";
}
else
{
dir = new File(DevAppServerMain.this.externalResourceDir);
if (dir.exists())
{
if (!dir.isDirectory()) error = DevAppServerMain.this.externalResourceDir + " is not a directory.";
}
else
{
error = "No such directory: " + DevAppServerMain.this.externalResourceDir;
}
}
if (error != null)
{
System.err.println(error);
System.exit(1);
}
return dir;
}
}
private static abstract class DevAppServerOption extends Option
{
protected DevAppServerMain main;
DevAppServerOption( DevAppServerMain main, String shortName, String longName, boolean isFlag )
{
super(shortName, longName, isFlag);
this.main = main;
}
}
}