@@ -96,6 +96,8 @@ abstract class Base implements Server {
9696
9797 private final AtomicBoolean stopping = new AtomicBoolean ();
9898
99+ private boolean defaultOptions ;
100+
99101 protected void fireStart (@ NonNull List <Jooby > applications , @ NonNull Executor defaultWorker ) {
100102 for (Jooby app : applications ) {
101103 app .setDefaultWorker (defaultWorker ).start (this );
@@ -110,10 +112,8 @@ protected void fireReady(@NonNull List<Jooby> applications) {
110112
111113 protected void fireStop (@ NonNull List <Jooby > applications ) {
112114 if (stopping .compareAndSet (false , true )) {
113- if (applications != null ) {
114- for (Jooby app : applications ) {
115- app .stop ();
116- }
115+ for (Jooby app : applications ) {
116+ app .stop ();
117117 }
118118 }
119119 }
@@ -123,10 +123,42 @@ protected void addShutdownHook() {
123123 Runtime .getRuntime ().addShutdownHook (new Thread (MutedServer .mute (this )::stop ));
124124 }
125125 }
126+
127+ private ServerOptions options ;
128+
129+ @ Override
130+ public final ServerOptions getOptions () {
131+ if (options == null ) {
132+ options = defaultOptions ();
133+ defaultOptions = true ;
134+ }
135+ return options ;
136+ }
137+
138+ @ Override
139+ public Server setOptions (@ NonNull ServerOptions options ) {
140+ if (this .options != null && !defaultOptions ) {
141+ var warn =
142+ Boolean .parseBoolean (System .getProperty (AvailableSettings .SERVER_OPTIONS_WARN , "true" ));
143+ if (warn ) {
144+ LoggerFactory .getLogger (getClass ())
145+ .warn (
146+ "Server options must be called once. To turn off this warning set the: `{}`"
147+ + " system property to `false`" ,
148+ AvailableSettings .SERVER_OPTIONS_WARN );
149+ }
150+ }
151+ this .options = options ;
152+ this .defaultOptions = false ;
153+ return this ;
154+ }
155+
156+ protected abstract ServerOptions defaultOptions ();
126157 }
127158
128159 /**
129- * Set server options.
160+ * Set server options. This method should be called once, calling this method multiple times will
161+ * print a warning.
130162 *
131163 * @param options Server options.
132164 * @return This server.
@@ -156,8 +188,8 @@ protected void addShutdownHook() {
156188 @ NonNull Server start (@ NonNull Jooby ... application );
157189
158190 /**
159- * Utility method to turn off odd logger. This help to ensure same startup log lines across server
160- * implementations.
191+ * Utility method to turn off odd logger. This helps to ensure same startup log lines across
192+ * server implementations.
161193 *
162194 * <p>These logs are silent at application startup time.
163195 *
@@ -234,6 +266,16 @@ static boolean isAddressInUse(@Nullable Throwable cause) {
234266 * @return A server.
235267 */
236268 static Server loadServer () {
269+ return loadServer (null );
270+ }
271+
272+ /**
273+ * Load server from classpath using {@link ServiceLoader}.
274+ *
275+ * @param options Optional server options.
276+ * @return A server.
277+ */
278+ static Server loadServer (@ Nullable ServerOptions options ) {
237279 List <Server > servers =
238280 stream (
239281 spliteratorUnknownSize (
@@ -244,13 +286,18 @@ static Server loadServer() {
244286 throw new StartupException ("Server not found." );
245287 }
246288 if (servers .size () > 1 ) {
247- List < String > names =
289+ var names =
248290 servers .stream ()
249291 .map (it -> it .getClass ().getSimpleName ().toLowerCase ())
250292 .collect (Collectors .toList ());
251293 var log = LoggerFactory .getLogger (servers .get (0 ).getClass ());
252294 log .warn ("Multiple servers found {}. Using: {}" , names , names .get (0 ));
253295 }
254- return servers .get (0 );
296+ var server = servers .get (0 );
297+ if (options != null ) {
298+ options .setServer (server .getName ());
299+ server .setOptions (options );
300+ }
301+ return server ;
255302 }
256303}
0 commit comments