Skip to content

Commit

Permalink
Fix refresh in undertow launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienlauer committed Nov 30, 2017
1 parent 759be30 commit 715429a
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 3.4.1 (2017-11-29)

* [fix] Undertow-based applications would not refresh after a startup failure.

# Version 3.4.0 (2017-11-28)

* [new] Validation exceptions on REST resources are automatically mapped to a detailed response.
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion rest/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-rest</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-rest-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion rest/jersey2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-rest</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-rest-jersey2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-rest</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion rest/specs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-rest</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-rest-specs</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion security/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-security</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-security-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion security/specs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-security</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-security-specs</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion specs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-specs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface SeedLauncher {
*/
void shutdown() throws Exception;

default void refresh() {
default void refresh() throws Exception {
throw new UnsupportedOperationException("Refresh is not supported by this launcher");
}
}
2 changes: 1 addition & 1 deletion testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-testing</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion web/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-web</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-web-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-web</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion web/security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-web</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-web-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion web/specs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-web</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-web-specs</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion web/undertow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-web</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.1-SNAPSHOT</version>
</parent>

<artifactId>seed-web-undertow</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import io.nuun.kernel.api.Plugin;
import io.undertow.Undertow;
import io.undertow.servlet.api.DeploymentManager;
import javax.servlet.ServletException;
import org.seedstack.seed.SeedException;
import org.seedstack.seed.core.Seed;
import org.seedstack.seed.spi.SeedLauncher;
import org.seedstack.seed.undertow.UndertowConfig;
import org.seedstack.seed.web.WebConfig;
import org.seedstack.seed.web.internal.ServletContextUtils;
import org.seedstack.shed.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,7 +37,6 @@ public void launch(String[] args) throws Exception {

// Start the HTTP server
start(serverConfig, undertowConfig);

}

@Override
Expand All @@ -47,60 +46,68 @@ public void shutdown() throws Exception {
}

@Override
public void refresh() {
public void refresh() throws Exception {
LOGGER.info("Refreshing Web application");
if (undertow != null && deploymentManager != null) {
stop();
undeploy();
stop();
undeploy();

Seed.refresh();
Seed.refresh();

WebConfig.ServerConfig serverConfig = Seed.baseConfiguration().get(WebConfig.ServerConfig.class);
UndertowConfig undertowConfig = Seed.baseConfiguration().get(UndertowConfig.class);
WebConfig.ServerConfig serverConfig = Seed.baseConfiguration().get(WebConfig.ServerConfig.class);
UndertowConfig undertowConfig = Seed.baseConfiguration().get(UndertowConfig.class);

deploy(serverConfig);
start(serverConfig, undertowConfig);
}
deploy(serverConfig);
start(serverConfig, undertowConfig);
}

private void deploy(WebConfig.ServerConfig serverConfig) {
DeploymentManagerFactory factory = new DeploymentManagerFactory(serverConfig);
deploymentManager = factory.createDeploymentManager();
deploymentManager.deploy();
private void deploy(WebConfig.ServerConfig serverConfig) throws Exception {
try {
DeploymentManagerFactory factory = new DeploymentManagerFactory(serverConfig);
deploymentManager = factory.createDeploymentManager();
deploymentManager.deploy();
deploymentManager.start();
} catch (ServletException e) {
LOGGER.error("An error occurred when trying to start the servlet context", e);
} catch (Exception e) {
handleUndertowException(e);
}
}

private void undeploy() {
private void undeploy() throws Exception {
if (deploymentManager != null) {
try {
deploymentManager.stop();
} catch (ServletException e) {
LOGGER.error("An error occurred when trying to stop the servlet context", e);
deploymentManager.undeploy();
} catch (Exception e) {
handleUndertowException(e);
} finally {
deploymentManager = null;
}
deploymentManager.undeploy();
deploymentManager = null;
}
}

private void start(WebConfig.ServerConfig serverConfig, UndertowConfig undertowConfig) {
UndertowPlugin undertowPlugin = getUndertowPlugin(deploymentManager);
undertow = new ServerFactory(serverConfig, undertowConfig).createServer(
deploymentManager,
undertowPlugin.getSslProvider()
);
undertow.start();
LOGGER.info("Undertow Web server listening on {}:{}", serverConfig.getHost(), serverConfig.getPort());
private void start(WebConfig.ServerConfig serverConfig, UndertowConfig undertowConfig) throws Exception {
try {
UndertowPlugin undertowPlugin = getUndertowPlugin(deploymentManager);
undertow = new ServerFactory(serverConfig, undertowConfig).createServer(
deploymentManager,
undertowPlugin.getSslProvider()
);
undertow.start();
LOGGER.info("Undertow Web server listening on {}:{}", serverConfig.getHost(), serverConfig.getPort());
} catch (Exception e) {
handleUndertowException(e);
}
}

private void stop() {
private void stop() throws Exception {
if (undertow != null) {
undertow.stop();
LOGGER.info("Undertow Web server stopped");
undertow = null;
try {
undertow.stop();
LOGGER.info("Undertow Web server stopped");
} catch (Exception e) {
handleUndertowException(e);
} finally {
undertow = null;
}
}
}

Expand All @@ -114,4 +121,13 @@ private UndertowPlugin getUndertowPlugin(DeploymentManager deploymentManager) {
}
throw SeedException.createNew(UndertowErrorCode.MISSING_UNDERTOW_PLUGIN);
}

private void handleUndertowException(Exception e) throws Exception {
// Undertow always wraps exception in a RuntimeException
if (e instanceof RuntimeException && e.getCause() instanceof BaseException) {
throw (BaseException) e.getCause();
} else {
throw e;
}
}
}

0 comments on commit 715429a

Please sign in to comment.