View Javadoc
1   package com.github.searls.jasmine.server;
2   
3   import org.eclipse.jetty.server.Connector;
4   import org.eclipse.jetty.server.Server;
5   
6   public class ServerManager {
7   
8     private static final int ANY_PORT = 0;
9   
10    private final Server server;
11    private final Connector connector;
12    private final ResourceHandlerConfigurator configurator;
13  
14    public ServerManager(Server server,
15                         Connector connector,
16                         ResourceHandlerConfigurator configurator) {
17      this.server = server;
18      this.connector = connector;
19      this.configurator = configurator;
20    }
21  
22    public int start() throws Exception {
23      return this.startServer(ANY_PORT);
24    }
25  
26    public void start(int port) throws Exception {
27      this.startServer(port);
28    }
29  
30    private int startServer(int port) throws Exception {
31      connector.setPort(port);
32  
33      this.server.setHandler(this.configurator.createHandler());
34      this.server.addConnector(connector);
35  
36      this.server.start();
37  
38      return connector.getLocalPort();
39    }
40  
41    public void stop() throws Exception {
42      this.server.stop();
43    }
44  
45    public void join() throws Exception {
46      this.server.join();
47    }
48  }