WebServer.java

  1. /*
  2.  * @copyright defined in LICENSE.txt
  3.  */

  4. package ship.build;

  5. import static hera.DefaultConstants.DEFAULT_ENDPOINT;
  6. import static hera.util.StringUtils.nvl;

  7. import hera.server.ServerStatus;
  8. import hera.server.ThreadServer;
  9. import hera.util.ThreadUtils;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.logging.LogManager;
  13. import lombok.NoArgsConstructor;
  14. import lombok.Setter;
  15. import org.springframework.boot.Banner.Mode;
  16. import org.springframework.boot.builder.SpringApplicationBuilder;
  17. import org.springframework.context.ConfigurableApplicationContext;
  18. import ship.ProjectFile;
  19. import ship.build.web.SpringWebLauncher;
  20. import ship.build.web.service.BuildService;

  21. @NoArgsConstructor
  22. public class WebServer extends ThreadServer {

  23.   protected int port = -1;

  24.   @Setter
  25.   protected ProjectFile projectFile;

  26.   protected ConfigurableApplicationContext applicationContext;

  27.   public WebServer(final int port) {
  28.     this.port = port;
  29.   }

  30.   /**
  31.    * Get server port.
  32.    * <p>
  33.    * Return 8080 as default port if not specified.
  34.    * </p>
  35.    * @return server port
  36.    */
  37.   public int getPort() {
  38.     if (port < 0) {
  39.       return 8080;
  40.     } else {
  41.       return port;
  42.     }
  43.   }

  44.   /**
  45.    * Get build service from server.
  46.    *
  47.    * @return build service
  48.    */
  49.   public BuildService getBuildService() {
  50.     if (null == applicationContext) {
  51.       return null;
  52.     }
  53.     return applicationContext.getBean(BuildService.class);
  54.   }

  55.   /**
  56.    * Set server port.
  57.    * <p>
  58.    * Specify negative value if you want default port.
  59.    * </p>
  60.    *
  61.    * @param port server port
  62.    */
  63.   public void setPort(final int port) {
  64.     if (isStatus(ServerStatus.TERMINATED)) {
  65.       this.port = port;
  66.     } else {
  67.       throw new IllegalStateException("Server already started");
  68.     }
  69.   }

  70.   @Override
  71.   protected void initialize() throws Exception {
  72.     super.initialize();
  73.     final SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringWebLauncher.class);
  74.     builder.bannerMode(Mode.OFF);
  75.     builder.logStartupInfo(false);

  76.     final Map<String, Object> properties = new HashMap<>();
  77.     if (0 <= port) {
  78.       properties.put("server.port", port);
  79.     }
  80.     properties.put("project.endpoint", nvl(projectFile.getEndpoint(), DEFAULT_ENDPOINT));
  81.     if (!properties.isEmpty()) {
  82.       builder.properties(properties);
  83.     }
  84.     LogManager.getLogManager().reset();
  85.     applicationContext = builder.run();
  86.   }

  87.   @Override
  88.   protected void process() throws Exception {
  89.     super.process();
  90.     ThreadUtils.trySleep(2000);
  91.   }

  92.   @Override
  93.   protected void terminate() {
  94.     try {
  95.       applicationContext.close();;
  96.     } catch (Throwable ex) {
  97.       this.exception = ex;
  98.     }
  99.     super.terminate();
  100.   }
  101. }