1   package eu.fbk.dh.tint.runner;
2   
3   import eu.fbk.utils.core.CommandLine;
4   import org.glassfish.grizzly.http.server.HttpServer;
5   import org.glassfish.grizzly.http.server.NetworkListener;
6   import org.slf4j.LoggerFactory;
7   
8   import javax.annotation.Nullable;
9   import java.io.File;
10  import java.io.StringReader;
11  import java.nio.charset.Charset;
12  import java.util.Date;
13  import java.util.List;
14  import java.util.Properties;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: alessio
19   * Date: 21/07/14
20   * Time: 15:26
21   * To change this template use File | Settings | File Templates.
22   */
23  
24  public class TintServer {
25  
26      private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TintServer.class);
27  
28      public static final String DEFAULT_HOST = "0.0.0.0";
29      public static final Integer DEFAULT_PORT = 8012;
30  
31      public TintServer(String host, Integer port) {
32          this(host, port, null, null);
33      }
34  
35      public TintServer(String host, Integer port, @Nullable File configFile) {
36          this(host, port, configFile, null);
37      }
38  
39      public TintServer(String host, Integer port, @Nullable File configFile,
40              @Nullable Properties additionalProperties) {
41          LOGGER.info("starting " + host + "\t" + port + " (" + new Date() + ")...");
42  
43          int timeoutInSeconds = -1;
44  
45          try {
46              // Load the pipeline
47              TintPipeline pipeline = new TintPipeline();
48              pipeline.loadDefaultProperties();
49              pipeline.loadPropertiesFromFile(configFile);
50              pipeline.addProperties(additionalProperties);
51  
52              // todo: parametrize this!
53  //            pipeline.loadSerializers();
54  
55              pipeline.load();
56  
57              LOGGER.info("Pipeline loaded");
58  
59              final HttpServer httpServer = new HttpServer();
60              NetworkListener nl = new NetworkListener("tint-server", host, port);
61              httpServer.addListener(nl);
62  
63              TintHandler tintHandler = new TintHandler(pipeline);
64              tintHandler.setRequestURIEncoding(Charset.forName("UTF-8"));
65  
66              httpServer.getServerConfiguration().setSessionTimeoutSeconds(timeoutInSeconds);
67              httpServer.getServerConfiguration().setMaxPostSize(4194304);
68              httpServer.getServerConfiguration().addHttpHandler(tintHandler, "/tint");
69  
70              httpServer.start();
71              Thread.currentThread().join();
72          } catch (Exception e) {
73              LOGGER.error("error running " + host + ":" + port);
74              e.printStackTrace();
75          }
76      }
77  
78      public static void main(String[] args) {
79  
80          try {
81              final CommandLine cmd = CommandLine
82                      .parser()
83                      .withName("./tint-server")
84                      .withHeader("Run the Tint Server")
85                      .withOption("c", "config", "Configuration file", "FILE", CommandLine.Type.FILE_EXISTING, true,
86                              false, false)
87                      .withOption("p", "port", String.format("Host port (default %d)", DEFAULT_PORT), "NUM",
88                              CommandLine.Type.INTEGER, true, false, false)
89                      .withOption("h", "host", String.format("Host address (default %s)", DEFAULT_HOST), "NUM",
90                              CommandLine.Type.STRING, true, false, false)
91  //                    .withOption(null, "properties", "Additional properties", "PROPS", CommandLine.Type.STRING, true,
92  //                            true, false)
93                      .withLogger(LoggerFactory.getLogger("eu.fbk")).parse(args);
94  
95              String host = cmd.getOptionValue("host", String.class, DEFAULT_HOST);
96              Integer port = cmd.getOptionValue("port", Integer.class, DEFAULT_PORT);
97              File configFile = cmd.getOptionValue("config", File.class);
98  
99  //            List<String> addProperties = cmd.getOptionValues("properties", String.class);
100 
101             Properties additionalProps = new Properties();
102 //            for (String property : addProperties) {
103 //                try {
104 //                    additionalProps.load(new StringReader(property));
105 //                } catch (Exception e) {
106 //                    LOGGER.warn(e.getMessage());
107 //                }
108 //            }
109 
110             TintServer server = new TintServer(host, port, configFile, additionalProps);
111 
112         } catch (Exception e) {
113             CommandLine.fail(e);
114         }
115 
116     }
117 }