2626import java .nio .file .Path ;
2727import java .nio .file .Paths ;
2828import java .util .ArrayList ;
29- import java .util .HashMap ;
29+ import java .util .Arrays ;
3030import java .util .Iterator ;
3131import java .util .LinkedList ;
3232import java .util .List ;
33- import java .util .Map ;
3433import java .util .NoSuchElementException ;
3534import java .util .ServiceLoader ;
3635import java .util .Spliterator ;
4746
4847public class Jooby implements Router , Registry {
4948
49+ private static final String ENV_KEY = "application.env" ;
50+
5051 private RouterImpl router ;
5152
5253 private ExecutionMode mode ;
@@ -61,27 +62,12 @@ public class Jooby implements Router, Registry {
6162
6263 private LinkedList <Throwing .Runnable > stopCallbacks ;
6364
64- /**
65- * Not ideal but useful. We want to have access to environment properties from instance
66- * initializer. So external method before creating a new Jooby instance does a call to
67- * {@link Jooby#setEnv(Env)} this makes available the env instance in any other Jooby instance
68- * created.
69- */
70- private static ThreadLocal <Env > ENV = new ThreadLocal <>();
71-
72- protected Env environment ;
65+ private Env environment ;
7366
7467 private Registry registry ;
7568
7669 public Jooby () {
7770 router = new RouterImpl (new RouteAnalyzer (getClass ().getClassLoader (), false ));
78- environment = ENV .get ();
79- if (environment == null ) {
80- // TODO: fallback for now, but probably better if we throws a specific error
81- environment = Env .defaultEnvironment ();
82- }
83- tmpdir = Paths .get (environment .get ("application.tmpdir" ).value ()).toAbsolutePath ();
84-
8571 }
8672
8773 public @ Nonnull Env environment () {
@@ -340,14 +326,20 @@ private <T> T require(AttributeKey<T> key) {
340326 if (serverConfigurer != null ) {
341327 serverConfigurer .accept (server );
342328 }
329+ if (environment == null ) {
330+ environment = Env .defaultEnvironment (getClass ().getClassLoader ());
331+ }
332+ if (tmpdir == null ) {
333+ tmpdir = Paths .get (environment .get ("application.tmpdir" ).value ()).toAbsolutePath ();
334+ }
343335 return server .start (this );
344336 }
345337
346338 public @ Nonnull Jooby start (@ Nonnull Server server ) {
347339 /** Start router: */
348340 ensureTmpdir (tmpdir );
349- Logger log = log ();
350- log .debug ("environment:\n {}" , environment );
341+
342+ log () .debug ("environment:\n {}" , environment );
351343
352344 if (mode == null ) {
353345 mode = ExecutionMode .DEFAULT ;
@@ -399,55 +391,51 @@ private <T> T require(AttributeKey<T> key) {
399391 return router .toString ();
400392 }
401393
402- public static void setEnv (@ Nonnull Env environment ) {
403- ENV .set (environment );
404- }
405-
406394 public static void run (@ Nonnull Supplier <Jooby > provider , String ... args ) {
407395 run (provider , ExecutionMode .DEFAULT , args );
408396 }
409397
410398 public static void run (@ Nonnull Supplier <Jooby > provider , @ Nonnull ExecutionMode mode ,
411399 String ... args ) {
412- Server server ;
413- try {
414- Env environment = Env .defaultEnvironment (args );
415- setEnv (environment );
416400
417- logback (environment );
401+ /** Dump command line as system properties. */
402+ Env .parse (args ).properties ().forEach (System ::setProperty );
418403
419- Jooby app = provider .get ();
420- if (app .mode == null ) {
421- app .mode = mode ;
422- }
423- server = app .start ();
424- } finally {
425- // clear env
426- setEnv (null );
404+ /** Fin application.env: */
405+ String env = System .getProperty (ENV_KEY , System .getenv ().getOrDefault (ENV_KEY , "dev" ))
406+ .toLowerCase ();
407+
408+ logback (env );
409+
410+ Jooby app = provider .get ();
411+ if (app .mode == null ) {
412+ app .mode = mode ;
427413 }
414+ Server server = app .start ();
428415 server .join ();
429416 }
430417
431- public static void logback (@ Nonnull Env env ) {
432- String setfile = env .get ("logback.configurationFile" ).value ((String ) null );
433- if (setfile != null ) {
434- System .setProperty ("logback.configurationFile" , setfile );
435- return ;
418+ public static void logback (@ Nonnull String env ) {
419+ String logfile = System
420+ .getProperty ("logback.configurationFile" , System .getenv ().get ("logback.configurationFile" ));
421+ if (logfile != null ) {
422+ System .setProperty ("logback.configurationFile" , logfile );
423+ } else {
424+ Path userdir = Paths .get (System .getProperty ("user.dir" ));
425+ Path conf = userdir .resolve ("conf" );
426+ String logbackenv = "logback." + env + ".xml" ;
427+ String fallback = "logback.xml" ;
428+ Stream .of (
429+ /** Env specific inside conf or userdir: */
430+ conf .resolve (logbackenv ), userdir .resolve (logbackenv ),
431+ /** Fallback inside conf or userdir: */
432+ conf .resolve (fallback ), userdir .resolve (fallback )
433+ ).filter (Files ::exists )
434+ .findFirst ()
435+ .map (Path ::toAbsolutePath )
436+ .ifPresent (
437+ logback -> System .setProperty ("logback.configurationFile" , logback .toString ()));
436438 }
437- String name = env .name ();
438- Path userdir = Paths .get (System .getProperty ("user.dir" ));
439- Path conf = userdir .resolve ("conf" );
440- String logbackenv = "logback." + name + ".xml" ;
441- String fallback = "logback.xml" ;
442- Stream .of (
443- /** Env specific inside conf or userdir: */
444- conf .resolve (logbackenv ), userdir .resolve (logbackenv ),
445- /** Fallback inside conf or userdir: */
446- conf .resolve (fallback ), userdir .resolve (fallback )
447- ).filter (Files ::exists )
448- .findFirst ()
449- .map (Path ::toAbsolutePath )
450- .ifPresent (logback -> System .setProperty ("logback.configurationFile" , logback .toString ()));
451439 }
452440
453441 private static void ensureTmpdir (Path tmpdir ) {
0 commit comments