From 1342904faecb9a2eaa6374c7f337ac669173fcd7 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 9 Oct 2015 09:03:48 +0200 Subject: [PATCH] KEYCLOAK-1935 Stop KeycloakServer if Undertow fails to start --- .../keycloak/testsuite/KeycloakServer.java | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/KeycloakServer.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/KeycloakServer.java index 9f7530db7e..985ae9c146 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/KeycloakServer.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/KeycloakServer.java @@ -46,6 +46,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.BindException; import java.util.Properties; /** @@ -289,37 +290,43 @@ public class KeycloakServer { .setWorkerThreads(config.getWorkerThreads()) .setIoThreads(config.getWorkerThreads() / 8); - server = new UndertowJaxrsServer().start(builder); + server = new UndertowJaxrsServer(); + try { + server.start(builder); - DeploymentInfo di = server.undertowDeployment(deployment, ""); - di.setClassLoader(getClass().getClassLoader()); - di.setContextPath("/auth"); - di.setDeploymentName("Keycloak"); - di.setDefaultEncoding("UTF-8"); + DeploymentInfo di = server.undertowDeployment(deployment, ""); + di.setClassLoader(getClass().getClassLoader()); + di.setContextPath("/auth"); + di.setDeploymentName("Keycloak"); + di.setDefaultEncoding("UTF-8"); - di.setDefaultServletConfig(new DefaultServletConfig(true)); - di.addWelcomePage("theme/keycloak/welcome/resources/index.html"); + di.setDefaultServletConfig(new DefaultServletConfig(true)); + di.addWelcomePage("theme/keycloak/welcome/resources/index.html"); - FilterInfo filter = Servlets.filter("SessionFilter", KeycloakSessionServletFilter.class); - di.addFilter(filter); - di.addFilterUrlMapping("SessionFilter", "/*", DispatcherType.REQUEST); + FilterInfo filter = Servlets.filter("SessionFilter", KeycloakSessionServletFilter.class); + di.addFilter(filter); + di.addFilterUrlMapping("SessionFilter", "/*", DispatcherType.REQUEST); - FilterInfo connectionFilter = Servlets.filter("ClientConnectionFilter", ClientConnectionFilter.class); - di.addFilter(connectionFilter); - di.addFilterUrlMapping("ClientConnectionFilter", "/*", DispatcherType.REQUEST); + FilterInfo connectionFilter = Servlets.filter("ClientConnectionFilter", ClientConnectionFilter.class); + di.addFilter(connectionFilter); + di.addFilterUrlMapping("ClientConnectionFilter", "/*", DispatcherType.REQUEST); - server.deploy(di); + server.deploy(di); - sessionFactory = ((KeycloakApplication) deployment.getApplication()).getSessionFactory(); + sessionFactory = ((KeycloakApplication) deployment.getApplication()).getSessionFactory(); - setupDevConfig(); + setupDevConfig(); - if (config.getResourcesHome() != null) { - info("Loading resources from " + config.getResourcesHome()); + if (config.getResourcesHome() != null) { + info("Loading resources from " + config.getResourcesHome()); + } + + info("Started Keycloak (http://" + config.getHost() + ":" + config.getPort() + "/auth) in " + + (System.currentTimeMillis() - start) + " ms\n"); + } catch (RuntimeException e) { + server.stop(); + throw e; } - - info("Started Keycloak (http://" + config.getHost() + ":" + config.getPort() + "/auth) in " - + (System.currentTimeMillis() - start) + " ms\n"); } private void info(String message) {