Newer
Older
gitbucket_jkp / src / main / java / JettyLauncher.java
  1. import org.eclipse.jetty.server.ConnectionFactory;
  2. import org.eclipse.jetty.server.Connector;
  3. import org.eclipse.jetty.server.Handler;
  4. import org.eclipse.jetty.server.HttpConnectionFactory;
  5. import org.eclipse.jetty.server.Server;
  6. import org.eclipse.jetty.server.handler.StatisticsHandler;
  7. import org.eclipse.jetty.webapp.WebAppContext;
  8.  
  9. import java.io.File;
  10. import java.net.URL;
  11. import java.net.InetSocketAddress;
  12. import java.security.ProtectionDomain;
  13.  
  14. public class JettyLauncher {
  15. public static void main(String[] args) throws Exception {
  16. System.setProperty("java.awt.headless", "true");
  17.  
  18. String host = null;
  19. int port = 8080;
  20. InetSocketAddress address = null;
  21. String contextPath = "/";
  22. String tmpDirPath="";
  23. boolean forceHttps = false;
  24.  
  25. for(String arg: args) {
  26. if(arg.startsWith("--") && arg.contains("=")) {
  27. String[] dim = arg.split("=");
  28. if(dim.length >= 2) {
  29. switch (dim[0]) {
  30. case "--host":
  31. host = dim[1];
  32. break;
  33. case "--port":
  34. port = Integer.parseInt(dim[1]);
  35. break;
  36. case "--prefix":
  37. contextPath = dim[1];
  38. if (!contextPath.startsWith("/")) {
  39. contextPath = "/" + contextPath;
  40. }
  41. break;
  42. case "--max_file_size":
  43. System.setProperty("gitbucket.maxFileSize", dim[1]);
  44. break;
  45. case "--gitbucket.home":
  46. System.setProperty("gitbucket.home", dim[1]);
  47. break;
  48. case "--temp_dir":
  49. tmpDirPath = dim[1];
  50. break;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. if(host != null) {
  57. address = new InetSocketAddress(host, port);
  58. } else {
  59. address = new InetSocketAddress(port);
  60. }
  61.  
  62. Server server = new Server(address);
  63.  
  64. // SelectChannelConnector connector = new SelectChannelConnector();
  65. // if(host != null) {
  66. // connector.setHost(host);
  67. // }
  68. // connector.setMaxIdleTime(1000 * 60 * 60);
  69. // connector.setSoLingerTime(-1);
  70. // connector.setPort(port);
  71. // server.addConnector(connector);
  72.  
  73. // Disabling Server header
  74. for (Connector connector : server.getConnectors()) {
  75. for (ConnectionFactory factory : connector.getConnectionFactories()) {
  76. if (factory instanceof HttpConnectionFactory) {
  77. ((HttpConnectionFactory) factory).getHttpConfiguration().setSendServerVersion(false);
  78. }
  79. }
  80. }
  81.  
  82. WebAppContext context = new WebAppContext();
  83.  
  84. File tmpDir;
  85. if(tmpDirPath.equals("")){
  86. tmpDir = new File(getGitBucketHome(), "tmp");
  87. if(!tmpDir.exists()){
  88. tmpDir.mkdirs();
  89. }
  90. } else {
  91. tmpDir = new File(tmpDirPath);
  92. if(!tmpDir.exists()){
  93. throw new java.io.FileNotFoundException(
  94. String.format("temp_dir \"%s\" not found", tmpDirPath));
  95. } else if(!tmpDir.isDirectory()) {
  96. throw new IllegalArgumentException(
  97. String.format("temp_dir \"%s\" is not a directory", tmpDirPath));
  98. }
  99. }
  100. context.setTempDirectory(tmpDir);
  101.  
  102. // Disabling the directory listing feature.
  103. context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
  104.  
  105. ProtectionDomain domain = JettyLauncher.class.getProtectionDomain();
  106. URL location = domain.getCodeSource().getLocation();
  107.  
  108. context.setContextPath(contextPath);
  109. context.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
  110. context.setServer(server);
  111. context.setWar(location.toExternalForm());
  112. if (forceHttps) {
  113. context.setInitParameter("org.scalatra.ForceHttps", "true");
  114. }
  115.  
  116. Handler handler = addStatisticsHandler(context);
  117.  
  118. server.setHandler(handler);
  119. server.setStopAtShutdown(true);
  120. server.setStopTimeout(7_000);
  121. server.start();
  122. server.join();
  123. }
  124.  
  125. private static File getGitBucketHome(){
  126. String home = System.getProperty("gitbucket.home");
  127. if(home != null && home.length() > 0){
  128. return new File(home);
  129. }
  130. home = System.getenv("GITBUCKET_HOME");
  131. if(home != null && home.length() > 0){
  132. return new File(home);
  133. }
  134. return new File(System.getProperty("user.home"), ".gitbucket");
  135. }
  136.  
  137. private static Handler addStatisticsHandler(Handler handler) {
  138. // The graceful shutdown is implemented via the statistics handler.
  139. // See the following: https://bugs.eclipse.org/bugs/show_bug.cgi?id=420142
  140. final StatisticsHandler statisticsHandler = new StatisticsHandler();
  141. statisticsHandler.setHandler(handler);
  142. return statisticsHandler;
  143. }
  144. }