PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/src/play/server/Server.java

https://github.com/Frostman/play
Java | 166 lines | 140 code | 26 blank | 0 comment | 44 complexity | 9642fab6eeab8a06291636375a3fa154 MD5 | raw file
  1. package play.server;
  2. import java.io.File;
  3. import java.lang.management.ManagementFactory;
  4. import java.net.InetAddress;
  5. import java.net.InetSocketAddress;
  6. import java.util.Properties;
  7. import java.util.concurrent.Executors;
  8. import org.jboss.netty.bootstrap.ServerBootstrap;
  9. import org.jboss.netty.channel.ChannelException;
  10. import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
  11. import play.Logger;
  12. import play.Play;
  13. import play.Play.Mode;
  14. import play.libs.IO;
  15. import play.server.ssl.SslHttpServerPipelineFactory;
  16. import play.vfs.VirtualFile;
  17. public class Server {
  18. public static int httpPort;
  19. public static int httpsPort;
  20. public final static String PID_FILE = "server.pid";
  21. public Server(String[] args) {
  22. System.setProperty("file.encoding", "utf-8");
  23. final Properties p = Play.configuration;
  24. httpPort = Integer.parseInt(getOpt(args, "http.port", p.getProperty("http.port", "-1")));
  25. httpsPort = Integer.parseInt(getOpt(args, "https.port", p.getProperty("https.port", "-1")));
  26. if (httpPort == -1 && httpsPort == -1) {
  27. httpPort = 9000;
  28. }
  29. if (httpPort == httpsPort) {
  30. Logger.error("Could not bind on https and http on the same port " + httpPort);
  31. Play.fatalServerErrorOccurred();
  32. }
  33. InetAddress address = null;
  34. InetAddress secureAddress = null;
  35. try {
  36. if (p.getProperty("http.address") != null) {
  37. address = InetAddress.getByName(p.getProperty("http.address"));
  38. } else if (System.getProperties().containsKey("http.address")) {
  39. address = InetAddress.getByName(System.getProperty("http.address"));
  40. }
  41. } catch (Exception e) {
  42. Logger.error(e, "Could not understand http.address");
  43. Play.fatalServerErrorOccurred();
  44. }
  45. try {
  46. if (p.getProperty("https.address") != null) {
  47. secureAddress = InetAddress.getByName(p.getProperty("https.address"));
  48. } else if (System.getProperties().containsKey("https.address")) {
  49. secureAddress = InetAddress.getByName(System.getProperty("https.address"));
  50. }
  51. } catch (Exception e) {
  52. Logger.error(e, "Could not understand https.address");
  53. Play.fatalServerErrorOccurred();
  54. }
  55. ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
  56. Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
  57. );
  58. try {
  59. if (httpPort != -1) {
  60. bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
  61. bootstrap.bind(new InetSocketAddress(address, httpPort));
  62. bootstrap.setOption("child.tcpNoDelay", true);
  63. if (Play.mode == Mode.DEV) {
  64. if (address == null) {
  65. Logger.info("Listening for HTTP on port %s (Waiting a first request to start) ...", httpPort);
  66. } else {
  67. Logger.info("Listening for HTTP at %2$s:%1$s (Waiting a first request to start) ...", httpPort, address);
  68. }
  69. } else {
  70. if (address == null) {
  71. Logger.info("Listening for HTTP on port %s ...", httpPort);
  72. } else {
  73. Logger.info("Listening for HTTP at %2$s:%1$s ...", httpPort, address);
  74. }
  75. }
  76. }
  77. } catch (ChannelException e) {
  78. Logger.error("Could not bind on port " + httpPort, e);
  79. Play.fatalServerErrorOccurred();
  80. }
  81. bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
  82. Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
  83. );
  84. try {
  85. if (httpsPort != -1) {
  86. bootstrap.setPipelineFactory(new SslHttpServerPipelineFactory());
  87. bootstrap.bind(new InetSocketAddress(secureAddress, httpsPort));
  88. bootstrap.setOption("child.tcpNoDelay", true);
  89. if (Play.mode == Mode.DEV) {
  90. if (secureAddress == null) {
  91. Logger.info("Listening for HTTPS on port %s (Waiting a first request to start) ...", httpsPort);
  92. } else {
  93. Logger.info("Listening for HTTPS at %2$s:%1$s (Waiting a first request to start) ...", httpsPort, secureAddress);
  94. }
  95. } else {
  96. if (secureAddress == null) {
  97. Logger.info("Listening for HTTPS on port %s ...", httpsPort);
  98. } else {
  99. Logger.info("Listening for HTTPS at %2$s:%1$s ...", httpsPort, secureAddress);
  100. }
  101. }
  102. }
  103. } catch (ChannelException e) {
  104. Logger.error("Could not bind on port " + httpsPort, e);
  105. Play.fatalServerErrorOccurred();
  106. }
  107. }
  108. private String getOpt(String[] args, String arg, String defaultValue) {
  109. String s = "--" + arg + "=";
  110. for (String a : args) {
  111. if (a.startsWith(s)) {
  112. return a.substring(s.length());
  113. }
  114. }
  115. return defaultValue;
  116. }
  117. private static void writePID(File root) {
  118. String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
  119. File pidfile = new File(root, PID_FILE);
  120. if (pidfile.exists()) {
  121. throw new RuntimeException("The " + PID_FILE + " already exists. Is the server already running?");
  122. }
  123. IO.write(pid.getBytes(), pidfile);
  124. }
  125. public static void main(String[] args) throws Exception {
  126. File root = new File(System.getProperty("application.path"));
  127. if (System.getProperty("precompiled", "false").equals("true")) {
  128. Play.usePrecompiled = true;
  129. }
  130. if (System.getProperty("writepid", "false").equals("true")) {
  131. writePID(root);
  132. }
  133. Play.init(root, System.getProperty("play.id", ""));
  134. if (System.getProperty("precompile") == null) {
  135. new Server(args);
  136. } else {
  137. Logger.info("Done.");
  138. }
  139. }
  140. }