/
Play.java
822 lines (736 loc) · 27.9 KB
/
Play.java
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
package play;
import java.io.File;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.LineNumberReader;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import play.cache.Cache;
import play.classloading.ApplicationClasses;
import play.classloading.ApplicationClassloader;
import play.exceptions.PlayException;
import play.exceptions.UnexpectedException;
import play.libs.IO;
import play.mvc.Http;
import play.mvc.Router;
import play.plugins.PluginCollection;
import play.templates.TemplateLoader;
import play.utils.OrderSafeProperties;
import play.vfs.VirtualFile;
/**
* Main framework class
*/
public class Play {
/**
* 2 modes
*/
public enum Mode {
/**
* Enable development-specific features, e.g. view the documentation at the URL {@literal "/@documentation"}.
*/
DEV,
/**
* Disable development-specific features.
*/
PROD;
public boolean isDev() {
return this == DEV;
}
public boolean isProd() {
return this == PROD;
}
}
/**
* Is the application initialized
*/
public static boolean initialized = false;
/**
* Is the application started
*/
public static boolean started = false;
/**
* True when the one and only shutdown hook is enabled
*/
private static boolean shutdownHookEnabled = false;
/**
* The framework ID
*/
public static String id;
/**
* The application mode
*/
public static Mode mode;
/**
* The application root
*/
public static File applicationPath = null;
/**
* tmp dir
*/
public static File tmpDir = null;
/**
* tmp dir is readOnly
*/
public static boolean readOnlyTmp = false;
/**
* The framework root
*/
public static File frameworkPath = null;
/**
* All loaded application classes
*/
public static ApplicationClasses classes;
/**
* The application classLoader
*/
public static ApplicationClassloader classloader;
/**
* All paths to search for files
*/
public static List<VirtualFile> roots = new ArrayList<VirtualFile>(16);
/**
* All paths to search for Java files
*/
public static List<VirtualFile> javaPath;
/**
* All paths to search for templates files
*/
public static List<VirtualFile> templatesPath;
/**
* Main routes file
*/
public static VirtualFile routes;
/**
* Plugin routes files
*/
public static Map<String, VirtualFile> modulesRoutes;
/**
* The main application.conf
*/
public static VirtualFile conf;
/**
* The app configuration (already resolved from the framework id)
*/
public static Properties configuration;
/**
* The last time than the application has started
*/
public static long startedAt;
/**
* The list of supported locales
*/
public static List<String> langs = new ArrayList<String>(16);
/**
* The very secret key
*/
public static String secretKey;
/**
* pluginCollection that holds all loaded plugins and all enabled plugins..
*/
public static PluginCollection pluginCollection = new PluginCollection();
/**
* Readonly list containing currently enabled plugins.
* This list is updated from pluginCollection when pluginCollection is modified
* Play plugins
* @deprecated Use pluginCollection instead.
*/
@Deprecated
public static List<PlayPlugin> plugins = pluginCollection.getEnabledPlugins();
/**
* Modules
*/
public static Map<String, VirtualFile> modules = new HashMap<String, VirtualFile>(16);
/**
* Framework version
*/
public static String version = null;
/**
* Context path (when several application are deployed on the same host)
*/
public static String ctxPath = "";
static boolean firstStart = true;
public static boolean usePrecompiled = false;
public static boolean forceProd = false;
/**
* Lazy load the templates on demand
*/
public static boolean lazyLoadTemplates = false;
/**
* This is used as default encoding everywhere related to the web: request, response, WS
*/
public static String defaultWebEncoding = "utf-8";
/**
* This flag indicates if the app is running in a standalone Play server or
* as a WAR in an applicationServer
*/
public static boolean standalonePlayServer = true;
/**
* Init the framework
*
* @param root The application path
* @param id The framework id to use
*/
public static void init(File root, String id) {
// Simple things
Play.id = id;
Play.started = false;
Play.applicationPath = root;
// load all play.static of exists
initStaticStuff();
guessFrameworkPath();
// Read the configuration file
readConfiguration();
Play.classes = new ApplicationClasses();
// Configure logs
Logger.init();
String logLevel = configuration.getProperty("application.log", "INFO");
//only override log-level if Logger was not configured manually
if (!Logger.configuredManually) {
Logger.setUp(logLevel);
}
Logger.recordCaller = Boolean.parseBoolean(configuration.getProperty("application.log.recordCaller", "false"));
Logger.info("Starting %s", root.getAbsolutePath());
if (configuration.getProperty("play.tmp", "tmp").equals("none")) {
tmpDir = null;
Logger.debug("No tmp folder will be used (play.tmp is set to none)");
} else {
tmpDir = new File(configuration.getProperty("play.tmp", "tmp"));
if (!tmpDir.isAbsolute()) {
tmpDir = new File(applicationPath, tmpDir.getPath());
}
if (Logger.isTraceEnabled()) {
Logger.trace("Using %s as tmp dir", Play.tmpDir);
}
if (!tmpDir.exists()) {
try {
if (readOnlyTmp) {
throw new Exception("ReadOnly tmp");
}
tmpDir.mkdirs();
} catch (Throwable e) {
tmpDir = null;
Logger.warn("No tmp folder will be used (cannot create the tmp dir)");
}
}
}
// Mode
mode = Mode.valueOf(configuration.getProperty("application.mode", "DEV").toUpperCase());
if (usePrecompiled || forceProd) {
mode = Mode.PROD;
}
// Context path
ctxPath = configuration.getProperty("http.path", ctxPath);
// Build basic java source path
VirtualFile appRoot = VirtualFile.open(applicationPath);
roots.add(appRoot);
javaPath = new CopyOnWriteArrayList<VirtualFile>();
javaPath.add(appRoot.child("app"));
javaPath.add(appRoot.child("conf"));
// Build basic templates path
if (appRoot.child("app/views").exists()) {
templatesPath = new ArrayList<VirtualFile>(2);
templatesPath.add(appRoot.child("app/views"));
} else {
templatesPath = new ArrayList<VirtualFile>(1);
}
// Main route file
routes = appRoot.child("conf/routes");
// Plugin route files
modulesRoutes = new HashMap<String, VirtualFile>(16);
// Load modules
loadModules();
// Load the templates from the framework after the one from the modules
templatesPath.add(VirtualFile.open(new File(frameworkPath, "framework/templates")));
// Enable a first classloader
classloader = new ApplicationClassloader();
// Fix ctxPath
if ("/".equals(Play.ctxPath)) {
Play.ctxPath = "";
}
// Default cookie domain
Http.Cookie.defaultDomain = configuration.getProperty("application.defaultCookieDomain", null);
if (Http.Cookie.defaultDomain != null) {
Logger.info("Using default cookie domain: " + Http.Cookie.defaultDomain);
}
// Plugins
pluginCollection.loadPlugins();
// Done !
if (mode == Mode.PROD || System.getProperty("precompile") != null) {
mode = Mode.PROD;
if (preCompile() && System.getProperty("precompile") == null) {
start();
} else {
return;
}
} else {
Logger.warn("You're running Play! in DEV mode");
}
// Plugins
pluginCollection.onApplicationReady();
Play.initialized = true;
}
public static void guessFrameworkPath() {
// Guess the framework path
try {
URL versionUrl = Play.class.getResource("/play/version");
// Read the content of the file
Play.version = new LineNumberReader(new InputStreamReader(versionUrl.openStream())).readLine();
// This is used only by the embedded server (Mina, Netty, Jetty etc)
URI uri = new URI(versionUrl.toString().replace(" ", "%20"));
if (frameworkPath == null || !frameworkPath.exists()) {
if (uri.getScheme().equals("jar")) {
String jarPath = uri.getSchemeSpecificPart().substring(5, uri.getSchemeSpecificPart().lastIndexOf("!"));
frameworkPath = new File(jarPath).getParentFile().getParentFile().getAbsoluteFile();
} else if (uri.getScheme().equals("file")) {
frameworkPath = new File(uri).getParentFile().getParentFile().getParentFile().getParentFile();
} else {
throw new UnexpectedException("Cannot find the Play! framework - trying with uri: " + uri + " scheme " + uri.getScheme());
}
}
} catch (Exception e) {
throw new UnexpectedException("Where is the framework ?", e);
}
}
/**
* Read application.conf and resolve overriden key using the play id mechanism.
*/
public static void readConfiguration() {
configuration = readOneConfigurationFile("application.conf", new HashSet<String>());
extractHttpPort();
// Plugins
pluginCollection.onConfigurationRead();
}
private static void extractHttpPort() {
final String javaCommand = System.getProperty("sun.java.command", "");
jregex.Matcher m = new jregex.Pattern(".* --http.port=({port}\\d+)").matcher(javaCommand);
if (m.matches()) {
configuration.setProperty("http.port", m.group("port"));
}
}
private static Properties readOneConfigurationFile(String filename, Set<String> seenFileNames) {
if (seenFileNames.contains(filename)) {
throw new RuntimeException("Detected recursive @include usage. Have seen the file " + filename + " before");
}
seenFileNames.add(filename);
Properties propsFromFile=null;
VirtualFile appRoot = VirtualFile.open(applicationPath);
conf = VirtualFile.open(System.getProperty("config.file", applicationPath + "/conf/" + filename));
Logger.info("Load application config file : %s", conf.getRealFile().getAbsolutePath());
try {
propsFromFile = IO.readUtf8Properties(conf.inputstream());
} catch (RuntimeException e) {
if (e.getCause() instanceof IOException) {
Logger.fatal("Cannot read "+filename);
fatalServerErrorOccurred();
}
}
// OK, check for instance specifics configuration
Properties newConfiguration = new OrderSafeProperties();
Pattern pattern = Pattern.compile("^%([a-zA-Z0-9_\\-]+)\\.(.*)$");
for (Object key : propsFromFile.keySet()) {
Matcher matcher = pattern.matcher(key + "");
if (!matcher.matches()) {
newConfiguration.put(key, propsFromFile.get(key).toString().trim());
}
}
for (Object key : propsFromFile.keySet()) {
Matcher matcher = pattern.matcher(key + "");
if (matcher.matches()) {
String instance = matcher.group(1);
if (instance.equals(id)) {
newConfiguration.put(matcher.group(2), propsFromFile.get(key).toString().trim());
}
}
}
propsFromFile = newConfiguration;
// Resolve ${..}
pattern = Pattern.compile("\\$\\{([^}]+)}");
for (Object key : propsFromFile.keySet()) {
String value = propsFromFile.getProperty(key.toString());
Matcher matcher = pattern.matcher(value);
StringBuffer newValue = new StringBuffer(100);
while (matcher.find()) {
String jp = matcher.group(1);
String r;
if (jp.equals("application.path")) {
r = Play.applicationPath.getAbsolutePath();
} else if (jp.equals("play.path")) {
r = Play.frameworkPath.getAbsolutePath();
} else {
r = System.getProperty(jp);
if (r == null) {
r = System.getenv(jp);
}
if (r == null) {
Logger.warn("Cannot replace %s in configuration (%s=%s)", jp, key, value);
continue;
}
}
matcher.appendReplacement(newValue, r.replaceAll("\\\\", "\\\\\\\\"));
}
matcher.appendTail(newValue);
propsFromFile.setProperty(key.toString(), newValue.toString());
}
// Include
Map<Object, Object> toInclude = new HashMap<Object, Object>(16);
for (Object key : propsFromFile.keySet()) {
if (key.toString().startsWith("@include.")) {
try {
String filenameToInclude = propsFromFile.getProperty(key.toString());
toInclude.putAll( readOneConfigurationFile(filenameToInclude, seenFileNames) );
} catch (Exception ex) {
Logger.warn("Missing include: %s", key);
}
}
}
propsFromFile.putAll(toInclude);
return propsFromFile;
}
/**
* Start the application.
* Recall to restart !
*/
public static synchronized void start() {
try {
if (started) {
stop();
}
if( standalonePlayServer) {
// Can only register shutdown-hook if running as standalone server
if (!shutdownHookEnabled) {
//registers shutdown hook - Now there's a good chance that we can notify
//our plugins that we're going down when some calls ctrl+c or just kills our process..
shutdownHookEnabled = true;
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
Play.stop();
}
});
}
}
if (mode == Mode.DEV) {
// Need a new classloader
classloader = new ApplicationClassloader();
// Reload plugins
pluginCollection.reloadApplicationPlugins();
}
// Reload configuration
readConfiguration();
// Configure logs
String logLevel = configuration.getProperty("application.log", "INFO");
//only override log-level if Logger was not configured manually
if (!Logger.configuredManually) {
Logger.setUp(logLevel);
}
Logger.recordCaller = Boolean.parseBoolean(configuration.getProperty("application.log.recordCaller", "false"));
// Locales
langs = new ArrayList<String>(Arrays.asList(configuration.getProperty("application.langs", "").split(",")));
if (langs.size() == 1 && langs.get(0).trim().length() == 0) {
langs = new ArrayList<String>(16);
}
// Clean templates
TemplateLoader.cleanCompiledCache();
// SecretKey
secretKey = configuration.getProperty("application.secret", "").trim();
if (secretKey.length() == 0) {
Logger.warn("No secret key defined. Sessions will not be encrypted");
}
// Default web encoding
String _defaultWebEncoding = configuration.getProperty("application.web_encoding");
if (_defaultWebEncoding != null) {
Logger.info("Using custom default web encoding: " + _defaultWebEncoding);
defaultWebEncoding = _defaultWebEncoding;
// Must update current response also, since the request/response triggering
// this configuration-loading in dev-mode have already been
// set up with the previous encoding
if (Http.Response.current() != null) {
Http.Response.current().encoding = _defaultWebEncoding;
}
}
// Try to load all classes
Play.classloader.getAllClasses();
// Routes
Router.detectChanges(ctxPath);
// Cache
Cache.init();
// Plugins
try {
pluginCollection.onApplicationStart();
} catch (Exception e) {
if (Play.mode.isProd()) {
Logger.error(e, "Can't start in PROD mode with errors");
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new UnexpectedException(e);
}
if (firstStart) {
Logger.info("Application '%s' is now started !", configuration.getProperty("application.name", ""));
firstStart = false;
}
// We made it
started = true;
startedAt = System.currentTimeMillis();
// Plugins
pluginCollection.afterApplicationStart();
} catch (PlayException e) {
started = false;
try { Cache.stop(); } catch(Exception ignored) {}
throw e;
} catch (Exception e) {
started = false;
try { Cache.stop(); } catch(Exception ignored) {}
throw new UnexpectedException(e);
}
}
/**
* Stop the application
*/
public static synchronized void stop() {
if (started) {
Logger.trace("Stopping the play application");
pluginCollection.onApplicationStop();
started = false;
Cache.stop();
Router.lastLoading = 0L;
}
}
/**
* Force all java source and template compilation.
*
* @return success ?
*/
static boolean preCompile() {
if (usePrecompiled) {
if (Play.getFile("precompiled").exists()) {
classloader.getAllClasses();
Logger.info("Application is precompiled");
return true;
}
Logger.error("Precompiled classes are missing!!");
fatalServerErrorOccurred();
return false;
}
try {
Logger.info("Precompiling ...");
Thread.currentThread().setContextClassLoader(Play.classloader);
long start = System.currentTimeMillis();
classloader.getAllClasses();
if (Logger.isTraceEnabled()) {
Logger.trace("%sms to precompile the Java stuff", System.currentTimeMillis() - start);
}
if (!lazyLoadTemplates) {
start = System.currentTimeMillis();
TemplateLoader.getAllTemplate();
if (Logger.isTraceEnabled()) {
Logger.trace("%sms to precompile the templates", System.currentTimeMillis() - start);
}
}
return true;
} catch (Throwable e) {
Logger.error(e, "Cannot start in PROD mode with errors");
fatalServerErrorOccurred();
return false;
}
}
/**
* Detect sources modifications
*/
public static synchronized void detectChanges() {
if (mode == Mode.PROD) {
return;
}
try {
pluginCollection.beforeDetectingChanges();
if(!pluginCollection.detectClassesChange()) {
classloader.detectChanges();
}
Router.detectChanges(ctxPath);
if (conf.lastModified() > startedAt) {
start();
return;
}
pluginCollection.detectChange();
if (!Play.started) {
throw new RuntimeException("Not started");
}
} catch (PlayException e) {
throw e;
} catch (Exception e) {
// We have to do a clean refresh
start();
}
}
@SuppressWarnings("unchecked")
public static <T> T plugin(Class<T> clazz) {
return (T) pluginCollection.getPluginInstance((Class<? extends PlayPlugin>) clazz);
}
/**
* Allow some code to run very early in Play - Use with caution !
*/
public static void initStaticStuff() {
// Play! plugings
Enumeration<URL> urls = null;
try {
urls = Play.class.getClassLoader().getResources("play.static");
} catch (Exception e) {
}
while (urls != null && urls.hasMoreElements()) {
URL url = urls.nextElement();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
try {
Class.forName(line);
} catch (Exception e) {
Logger.warn("! Cannot init static: " + line);
}
}
} catch (Exception ex) {
Logger.error(ex, "Cannot load %s", url);
}
}
}
/**
* Load all modules.
* You can even specify the list using the MODULES environement variable.
*/
public static void loadModules() {
if (System.getenv("MODULES") != null) {
// Modules path is prepended with a env property
if (System.getenv("MODULES") != null && System.getenv("MODULES").trim().length() > 0) {
for (String m : System.getenv("MODULES").split(System.getProperty("os.name").startsWith("Windows") ? ";" : ":")) {
File modulePath = new File(m);
if (!modulePath.exists() || !modulePath.isDirectory()) {
Logger.error("Module %s will not be loaded because %s does not exist", modulePath.getName(), modulePath.getAbsolutePath());
} else {
final String modulePathName = modulePath.getName();
final String moduleName = modulePathName.contains("-") ?
modulePathName.substring(0, modulePathName.lastIndexOf("-")) :
modulePathName;
addModule(moduleName, modulePath);
}
}
}
}
for (Object key : configuration.keySet()) {
String pName = key.toString();
if (pName.startsWith("module.")) {
Logger.warn("Declaring modules in application.conf is deprecated. Use dependencies.yml instead (%s)", pName);
String moduleName = pName.substring(7);
File modulePath = new File(configuration.getProperty(pName));
if (!modulePath.isAbsolute()) {
modulePath = new File(applicationPath, configuration.getProperty(pName));
}
if (!modulePath.exists() || !modulePath.isDirectory()) {
Logger.error("Module %s will not be loaded because %s does not exist", moduleName, modulePath.getAbsolutePath());
} else {
addModule(moduleName, modulePath);
}
}
}
// Load modules from modules/ directory
File localModules = Play.getFile("modules");
if (localModules.exists() && localModules.isDirectory()) {
for (File module : localModules.listFiles()) {
String moduleName = module.getName();
if (moduleName.startsWith(".")) {
Logger.info("Module %s is ignored, name starts with a dot", moduleName);
continue;
}
if (moduleName.contains("-")) {
moduleName = moduleName.substring(0, moduleName.indexOf("-"));
}
if (module.isDirectory()) {
addModule(moduleName, module);
} else {
File modulePath = new File(IO.readContentAsString(module).trim());
if (!modulePath.exists() || !modulePath.isDirectory()) {
Logger.error("Module %s will not be loaded because %s does not exist", moduleName, modulePath.getAbsolutePath());
} else {
addModule(moduleName, modulePath);
}
}
}
}
// Auto add special modules
if (Play.runningInTestMode()) {
addModule("_testrunner", new File(Play.frameworkPath, "modules/testrunner"));
}
if (Play.mode == Mode.DEV) {
addModule("_docviewer", new File(Play.frameworkPath, "modules/docviewer"));
}
}
/**
* Add a play application (as plugin)
*
* @param path The application path
*/
public static void addModule(String name, File path) {
VirtualFile root = VirtualFile.open(path);
modules.put(name, root);
if (root.child("app").exists()) {
javaPath.add(root.child("app"));
}
if (root.child("app/views").exists()) {
templatesPath.add(root.child("app/views"));
}
if (root.child("conf/routes").exists()) {
modulesRoutes.put(name, root.child("conf/routes"));
}
roots.add(root);
if (!name.startsWith("_")) {
Logger.info("Module %s is available (%s)", name, path.getAbsolutePath());
}
}
/**
* Search a VirtualFile in all loaded applications and plugins
*
* @param path Relative path from the applications root
* @return The virtualFile or null
*/
public static VirtualFile getVirtualFile(String path) {
return VirtualFile.search(roots, path);
}
/**
* Search a File in the current application
*
* @param path Relative path from the application root
* @return The file even if it doesn't exist
*/
public static File getFile(String path) {
return new File(applicationPath, path);
}
/**
* Returns true if application is running in test-mode.
* Test-mode is resolved from the framework id.
*
* Your app is running in test-mode if the framwork id (Play.id)
* is 'test' or 'test-?.*'
* @return true if testmode
*/
public static boolean runningInTestMode() {
return id.matches("test|test-?.*");
}
/**
* Call this method when there has been a fatal error that Play cannot recover from
*/
public static void fatalServerErrorOccurred() {
if (standalonePlayServer) {
// Just quit the process
System.exit(-1);
} else {
// Cannot quit the process while running inside an applicationServer
String msg = "A fatal server error occurred";
Logger.error(msg);
throw new Error(msg);
}
}
}