Skip to content

Instantly share code, notes, and snippets.

@valotas
Created May 30, 2011 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valotas/999051 to your computer and use it in GitHub Desktop.
Save valotas/999051 to your computer and use it in GitHub Desktop.
Grizzly 2 with guice 3
import javax.servlet.ServletContextEvent;
import org.glassfish.grizzly.servlet.ServletHandler;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceServletContextListener;
public class GuiceHandler extends ServletHandler {
private static final Logger logger = Logger.getLogger(GuiceHandler.class.getName());
private final GuiceServletContextListener guiceServletContextListener;
public GuiceHandler(GuiceServletContextListener guiceServletContextListener) {
this.guiceServletContextListener = guiceServletContextListener;
addFilter(new GuiceFilter(), "guiceFilter", null);
}
@Override
public void start() {
if (logger.isLoggable(Level.INFO)) logger.log(Level.INFO, String.format("Starting %s", GuiceHandler.class.getSimpleName()));
guiceServletContextListener.contextInitialized(new ServletContextEvent(getServletCtx()));
super.start();
}
@Override
public void destroy() {
super.destroy();
guiceServletContextListener.contextDestroyed(new ServletContextEvent(getServletCtx()));
if (logger.isLoggable(Level.INFO)) logger.log(Level.INFO, String.format("Destroyed %s", GuiceHandler.class.getSimpleName()));
}
}
import java.io.IOException;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.ServletHandler;
public class WebServer {
public static void main(String[] args) {
HttpServer server = HttpServer.createSimpleServer();
ServletHandler handler = new GuiceHandler(new Live24WebApp());
server.getServerConfiguration().addHttpHandler(handler, "/*");
try {
server.start();
System.in.read();
server.stop();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment