Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.rmannibucau.featuredmock.http;
  
  
 
     private static final Logger LOGGER = Logger.getLogger(FeaturedHttpServer.class.getName());
 
     private final String host;
     private final int port;
     private final int threads;
     private final boolean ssl;
     private final String protocol;
     private final SecureRandom secureRandom;
     private final TrustManager[] trustManagers;
     private final KeyManager[] keyManagers;
 
     private NioEventLoopGroup workerGroup;
 
     public DefaultFeaturedHttpServer(final String hostfinal int portfinal int threads,
                                      final boolean sslfinal String protocolfinal SecureRandom secureRandom,
                                      final TrustManager[] trustManagersfinal KeyManager[] keyManagers) {
         this. = host;
         if (port <= 0) { // generate a port
             this. = findNextAvailablePort();
         } else {
             this. = port;
         }
         this. = Math.max(threads, 1);
         this. = ssl;
         this. = protocol;
         this. = secureRandom;
         this. = trustManagers;
         this. = keyManagers;
     }
 
     @Override
     public int getPort() {
         return ;
     }
 
     private static int findNextAvailablePort() {
         ServerSocket serverSocket = null;
         try {
             serverSocket = new ServerSocket(0);
             return serverSocket.getLocalPort();
         } catch (final IOException e) {
             .log(.e.getMessage(), e);
         } finally {
             if (serverSocket != null) {
                 try {
                     serverSocket.close();
                 } catch (final IOException e) {
                     // no-op
                 }
             }
         }
         return 0;
     }
 
     @Override
     public FeaturedHttpServer start() {
          = new NioEventLoopGroup(new FeaturedThreadFactory());
 
         try {
             final ServerBootstrap bootstrap = new ServerBootstrap();
             bootstrap
                 .option(.true)
                 .option(., 1024)
                 .option(.true)
                 .group()
                 .channel(NioServerSocketChannel.class)
                 .bind().addListener(new ChannelFutureListener() {
                 @Override
                 public void operationComplete(final ChannelFuture futurethrows Exception {
                     if (!future.isSuccess()) {
                         .severe("Can't start HTTP server");
                     } else {
                         .info(String.format("Server started on http://%s:%s"));
                     }
                 }
             }).sync();
         } catch (final InterruptedException e) {
             .log(.e.getMessage(), e);
         }
 
        return this;
    }
    @Override
    public void stop() {
        if ( != null) {
            .shutdownGracefully();
            .info(String.format("Server http://%s:%s stopped"));
        }
    }
New to GrepCode? Check out our FAQ X