Last active
January 24, 2017 19:31
-
-
Save ljnelson/2821f6e0fa27c40758c348c9526fb7b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2016–2017 Laird Nelson. Released under the terms of the MIT license: https://opensource.org/licenses/MIT | |
*/ | |
package com.serverco; | |
import java.io.IOException; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import java.util.Set; | |
import java.util.concurrent.ExecutionException; | |
import javax.enterprise.context.spi.CreationalContext; | |
import javax.enterprise.inject.event.Observes; | |
import javax.enterprise.inject.spi.AfterDeploymentValidation; | |
import javax.enterprise.inject.spi.Bean; | |
import javax.enterprise.inject.spi.BeanManager; | |
import javax.enterprise.inject.spi.BeforeShutdown; | |
import javax.enterprise.inject.spi.Extension; | |
import org.glassfish.grizzly.http.server.HttpServer; | |
public class HttpServerStartingExtension implements Extension { | |
private volatile HttpServer server; | |
private void startHttpServer(@Observes final AfterDeploymentValidation event, final BeanManager beanManager) { | |
if (beanManager != null && this.server == null) { | |
final HttpServer server = get(beanManager, HttpServer.class); | |
if (server != null) { | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public final void run() { | |
shutdown(server); | |
} | |
}); | |
if (!server.isStarted()) { | |
try { | |
server.start(); | |
} catch (final IOException ioException) { | |
event.addDeploymentProblem(ioException); | |
} | |
} | |
this.server = server; | |
} | |
} | |
} | |
private void shutdownHttpServer(@Observes final BeforeShutdown event, final BeanManager beanManager) { | |
if (this.server != null) { | |
try { | |
// You should really run this thing on another thread; | |
// join()ing here just for expediency | |
Thread.currentThread().join(); | |
} catch (final InterruptedException interruptedException) { | |
Thread.currentThread().interrupt(); | |
} | |
shutdown(server); | |
} | |
} | |
private static final <T> T get(final BeanManager beanManager, final Type c, final Annotation... qualifiers) { | |
T returnValue = null; | |
if (beanManager != null && c != null) { | |
final Set<Bean<?>> beans = beanManager.getBeans(c, qualifiers); | |
if (beans != null && !beans.isEmpty()) { | |
final Bean<?> bean = beanManager.resolve(beans); | |
assert bean != null; | |
final CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); | |
assert creationalContext != null; | |
@SuppressWarnings("unchecked") | |
final T reference = (T)beanManager.getReference(bean, c, creationalContext); | |
returnValue = reference; | |
} | |
} | |
return returnValue; | |
} | |
private static final void shutdown(final HttpServer server) { | |
if (server != null && server.isStarted()) { | |
try { | |
server.shutdown().get(); | |
} catch (final ExecutionException ignore) { | |
} catch (final InterruptedException interruptedException) { | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment