Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package restx.server.simple.simple;
  
 
 
 import static restx.common.MoreIO.checkCanOpenSocket;

User: xavierhanin Date: 2/16/13 Time: 1:34 PM
 
 public abstract class SimpleWebServer implements WebServer {
     private static final AtomicLong SERVER_ID = new AtomicLong();
     public static class SimpleWebServerBuilder {
 
         private int port;
         private String routerPath = "/api";
         private String appBase = null;
         private String serverId;
         private RestxMainRouter router;
         public SimpleWebServerBuilder setPort(int port) {
             this. = port;
             return this;
         }
 
         public SimpleWebServerBuilder setRouterPath(String routerPath) {
             this. = routerPath;
             return this;
         }
 
         public SimpleWebServerBuilder setAppBase(String appBase) {
             this. = appBase;
             return this;
         }
 
         public SimpleWebServerBuilder setServerId(String serverId) {
             this. = serverId;
             return this;
         }
 
         public SimpleWebServerBuilder setRouter(RestxMainRouter router) {
             this. = router;
             return this;
         }
 
         public SimpleWebServer build() {
             if ( == null) {
                  = "SimpleWebServer#" + .incrementAndGet();
             }
 
             if ( == null) {
                 return new SimpleWebServer() {
                     @Override
                     protected RestxMainRouter setupRouter() {
                         return RestxMainRouterFactory.newInstance(
                                 ,
                                 Optional.of(WebServers.baseUri("0.0.0.0")));
                     }
 
                     @Override
                     public synchronized void stop() throws Exception {
                         super.stop();
 
                         RestxMainRouterFactory.clear();
                     }
                 };
             } else {
                 return new SimpleWebServer() {
                     @Override
                     protected RestxMainRouter setupRouter() {
                         return ;
                     }
                 };
             }
         }
    }
    public static SimpleWebServerBuilder builder() {
        return new SimpleWebServerBuilder();
    }
    private static final Logger logger = LoggerFactory.getLogger(SimpleWebServer.class);
    private final String serverId;
    private final String routerPath;
    private final HttpSettings httpSettings;
    private final String appBase;
    private final int port;
    private RestxMainRouter router;
    private Connection connection;
    private SimpleWebServer(String serverIdString routerPathString appBaseint port) {
        this. = serverId;
        this. = routerPath;
        this. = appBase;
        this. = port;
        this. = Factory.getInstance().getComponent(HttpSettings.class);
    }
    public String getServerId() {
        return ;
    }
    @Override
    public String getServerType() {
        return "SimpleFramework " + Version.getVersion("org.simpleframework""simple") + ", embedded";
    }
    public RestxMainRouter getRouter() {
        return ;
    }
    @Override
    public synchronized void start() throws Exception {
        checkCanOpenSocket();
        .debug("starting web server");
        WebServers.register(this);
         = setupRouter();
        Container container = new Container() {
            @Override
            public void handle(Request requestResponse response) {
                try {
                    if (request.getTarget().startsWith()) {
                        .route(
                                new SimpleRestxRequest(request), new SimpleRestxResponse(response));
                    } else {
                        response.getPrintStream().print("Not found...");
                        response.getPrintStream().close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        Server server = new ContainerServer(container);
         = new SocketConnection(server);
        SocketAddress address = new InetSocketAddress();
        .connect(address);
    }
    protected abstract RestxMainRouter setupRouter();
    @Override
    public void startAndAwait() throws Exception {
        start();
        // this doesn't await but since the simple thread is not a daemon thread it will keep the jvm alive
    }
    @Override
    public void await() throws Exception {
        // does nothing, simple is started in daemon mode which keeps jvm alive
    }
    @Override
    public synchronized void stop() throws Exception {
        if ( instanceof AutoCloseable) {
            ((AutoCloseable).close();
        }
        .close();
         = null;
        WebServers.unregister();
    }
    @Override
    public synchronized boolean isStarted() {
        return  != null;
    }
    @Override
    public String baseUrl() {
        return String.format("http://localhost:%s");
    }
    @Override
    public int getPort() {
        return ;
    }
    public static WebServerSupplier simpleWebServerSupplier() {
        return new WebServerSupplier() {
            @Override
            public WebServer newWebServer(int port) {
                return SimpleWebServer.builder().setPort(port).build();
            }
        };
    }
New to GrepCode? Check out our FAQ X