public void prepareServer ( ) { try { server = new Server ( 0 ) ; jettyHandler = new AbstractHandler ( ) { public void handle ( String target , Request req , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException { response . setContentType ( "text/plain" ) ; String [ ] operands = request . getRequestURI ( ) . split ( "/" ) ; String name = "" ; String command = "" ; String value = "" ; //operands[0] = "", request starts with a "/" if ( operands . length >= 2 ) { name = operands [ 1 ] ; } if ( operands . length >= 3 ) { command = operands [ 2 ] ; } if ( operands . length >= 4 ) { value = operands [ 3 ] ; } if ( command . equals ( "report" ) ) { //report a number of lines written response . getWriter ( ) . write ( makeReport ( name , value ) ) ; } else if ( command . equals ( "request" ) && value . equals ( "block" ) ) { //request a new block of work response . getWriter ( ) . write ( requestBlock ( name ) ) ; } else if ( command . equals ( "request" ) && value . equals ( "name" ) ) { //request a new name to report with response . getWriter ( ) . write ( requestName ( ) ) ; } else { //non recognized response response . getWriter ( ) . write ( "exit" ) ; } ( ( Request ) request ) . setHandled ( true ) ; } } ; server . setHandler ( jettyHandler ) ; // Select any available port server . start ( ) ; Connector [ ] connectors = server . getConnectors ( ) ; NetworkConnector nc = ( NetworkConnector ) connectors [ 0 ] ; listeningPort = nc . getLocalPort ( ) ; hostName = InetAddress . getLocalHost ( ) . getHostName ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } }
Prepares a Jetty server for communicating with consumers .