|
75 | 75 | * |
76 | 76 | * More documentation at <a href="https://jooby.io">jooby.io</a> |
77 | 77 | * |
78 | | - * @since 2.0.0 |
79 | 78 | * @author edgar |
| 79 | + * @since 2.0.0 |
80 | 80 | */ |
81 | 81 | public class Jooby implements Router, Registry { |
82 | 82 |
|
@@ -122,6 +122,8 @@ public class Jooby implements Router, Registry { |
122 | 122 |
|
123 | 123 | private String version; |
124 | 124 |
|
| 125 | + private Server server; |
| 126 | + |
125 | 127 | /** Creates a new Jooby instance. */ |
126 | 128 | public Jooby() { |
127 | 129 | if (owner == null) { |
@@ -539,6 +541,17 @@ public Jooby encoder(@NonNull MediaType contentType, @NonNull MessageEncoder enc |
539 | 541 | return this; |
540 | 542 | } |
541 | 543 |
|
| 544 | + /** |
| 545 | + * Set server to use. |
| 546 | + * |
| 547 | + * @param server Web Server. |
| 548 | + * @return This application. |
| 549 | + */ |
| 550 | + @NonNull public Jooby install(@NonNull Server server) { |
| 551 | + this.server = server; |
| 552 | + return this; |
| 553 | + } |
| 554 | + |
542 | 555 | @NonNull @Override |
543 | 556 | public Jooby dispatch(@NonNull Runnable body) { |
544 | 557 | router.dispatch(body); |
@@ -838,46 +851,58 @@ public Jooby setStartupSummary(List<StartupSummary> startupSummary) { |
838 | 851 | * @return Server. |
839 | 852 | */ |
840 | 853 | public @NonNull Server start() { |
841 | | - List<Server> servers = |
842 | | - stream( |
843 | | - spliteratorUnknownSize( |
844 | | - ServiceLoader.load(Server.class).iterator(), Spliterator.ORDERED), |
845 | | - false) |
846 | | - .collect(Collectors.toList()); |
847 | | - if (servers.size() == 0) { |
848 | | - throw new IllegalStateException("Server not found."); |
| 854 | + if (server == null) { |
| 855 | + this.server = loadServer(); |
849 | 856 | } |
850 | | - if (servers.size() > 1) { |
851 | | - List<String> names = |
852 | | - servers.stream() |
853 | | - .map(it -> it.getClass().getSimpleName().toLowerCase()) |
854 | | - .collect(Collectors.toList()); |
855 | | - getLog().warn("Multiple servers found {}. Using: {}", names, names.get(0)); |
856 | | - } |
857 | | - Server server = servers.get(0); |
| 857 | + |
858 | 858 | try { |
859 | 859 | if (serverOptions == null) { |
860 | 860 | serverOptions = ServerOptions.from(getEnvironment().getConfig()).orElse(null); |
861 | 861 | } |
862 | 862 | if (serverOptions != null) { |
863 | | - serverOptions.setServer(server.getClass().getSimpleName().toLowerCase()); |
| 863 | + serverOptions.setServer(server.getName()); |
864 | 864 | server.setOptions(serverOptions); |
865 | 865 | } |
866 | 866 |
|
867 | 867 | return server.start(this); |
868 | | - } catch (Throwable x) { |
| 868 | + } catch (Throwable startupError) { |
869 | 869 | Logger log = getLog(); |
870 | | - log.error("Application startup resulted in exception", x); |
| 870 | + log.error("Application startup resulted in exception", startupError); |
871 | 871 | try { |
872 | 872 | server.stop(); |
873 | | - } catch (Throwable stopx) { |
874 | | - log.info("Server stop resulted in exception", stopx); |
| 873 | + } catch (Throwable stopError) { |
| 874 | + log.info("Server stop resulted in exception", stopError); |
875 | 875 | } |
876 | 876 | // rethrow |
877 | | - throw x instanceof StartupException |
878 | | - ? (StartupException) x |
879 | | - : new StartupException("Application startup resulted in exception", x); |
| 877 | + throw startupError instanceof StartupException |
| 878 | + ? (StartupException) startupError |
| 879 | + : new StartupException("Application startup resulted in exception", startupError); |
| 880 | + } |
| 881 | + } |
| 882 | + |
| 883 | + /** |
| 884 | + * Load server from classpath using {@link ServiceLoader}. |
| 885 | + * |
| 886 | + * @return A server. |
| 887 | + */ |
| 888 | + private Server loadServer() { |
| 889 | + List<Server> servers = |
| 890 | + stream( |
| 891 | + spliteratorUnknownSize( |
| 892 | + ServiceLoader.load(Server.class).iterator(), Spliterator.ORDERED), |
| 893 | + false) |
| 894 | + .collect(Collectors.toList()); |
| 895 | + if (servers.size() == 0) { |
| 896 | + throw new StartupException("Server not found."); |
| 897 | + } |
| 898 | + if (servers.size() > 1) { |
| 899 | + List<String> names = |
| 900 | + servers.stream() |
| 901 | + .map(it -> it.getClass().getSimpleName().toLowerCase()) |
| 902 | + .collect(Collectors.toList()); |
| 903 | + getLog().warn("Multiple servers found {}. Using: {}", names, names.get(0)); |
880 | 904 | } |
| 905 | + return servers.get(0); |
881 | 906 | } |
882 | 907 |
|
883 | 908 | /** |
|
0 commit comments