JerseyWebServer.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package hello;
  2. import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
  3. import com.sun.jersey.api.core.PackagesResourceConfig;
  4. import com.sun.jersey.api.core.ResourceConfig;
  5. import java.net.URI;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.logging.Level;
  9. import javax.ws.rs.core.UriBuilder;
  10. import org.apache.commons.cli.BasicParser;
  11. import org.apache.commons.cli.CommandLine;
  12. import org.apache.commons.cli.CommandLineParser;
  13. import org.apache.commons.cli.Options;
  14. import org.glassfish.grizzly.Grizzly;
  15. import org.glassfish.grizzly.http.server.HttpHandler;
  16. import org.glassfish.grizzly.http.server.HttpServer;
  17. public class JerseyWebServer {
  18. public static void main(String[] args) throws Exception {
  19. CommandLineParser parser = new BasicParser();
  20. CommandLine cmd = parser.parse(options(), args);
  21. int port = Integer.parseInt(cmd.getOptionValue("port", "8080"));
  22. String dbHost = cmd.getOptionValue("dbhost", "localhost");
  23. int dbPort = Integer.parseInt(cmd.getOptionValue("dbport", "3306"));
  24. new JerseyWebServer(port,dbHost, dbPort).run();
  25. }
  26. private final int port;
  27. private final String dbHost;
  28. private final int dbPort;
  29. public JerseyWebServer(int port, String dbHost, int dbPort) {
  30. this.port = port;
  31. this.dbHost = dbHost;
  32. this.dbPort = dbPort;
  33. }
  34. public void run() throws Exception {
  35. URI baseUri = getBaseUrl(port);
  36. ResourceConfig rc = new PackagesResourceConfig("hello");
  37. rc.setPropertiesAndFeatures(properties());
  38. rc.getContainerResponseFilters().add(new ServerHeaderFilter());
  39. HttpServer server = GrizzlyServerFactory.createHttpServer(baseUri, rc);
  40. // There will be *a lot* of broken connections during the plaintext test.
  41. // That's not a good thing, but what would make matters even worse would be
  42. // to log the full stack trace of an IOException for each broken connection.
  43. // That's what Grizzly does by default, and it logs those messages at the
  44. // WARNING level, so setting the threshold to SEVERE hides those messages.
  45. Grizzly.logger(HttpHandler.class).setLevel(Level.SEVERE);
  46. try {
  47. server.start();
  48. System.err.print("Server started.\n");
  49. synchronized (JerseyWebServer.class) {
  50. JerseyWebServer.class.wait();
  51. }
  52. } finally {
  53. server.stop();
  54. }
  55. }
  56. private Map<String, Object> properties() {
  57. Map<String, Object> properties = new HashMap<>();
  58. properties.put("dbhost", dbHost);
  59. properties.put("dbport", dbPort);
  60. return properties;
  61. }
  62. private static URI getBaseUrl(int port) {
  63. return UriBuilder.fromUri("http://0.0.0.0/").port(port).build();
  64. }
  65. private static Options options() {
  66. Options options = new Options();
  67. options.addOption("port", true, "server port");
  68. options.addOption("dbhost", true, "database host");
  69. options.addOption("dbport", true, "database port");
  70. return options;
  71. }
  72. }