private void start ( String docsFile , String monitorAddress , boolean useBeerSample ) throws IOException { try { if ( port == 0 ) { ServerSocketChannel ch = ServerSocketChannel . open ( ) ; ch . socket ( ) . bind ( new InetSocketAddress ( 0 ) ) ; port = ch . socket ( ) . getLocalPort ( ) ; if ( monitorAddress == null && debug ) { System . out . println ( "port=" + port ) ; } httpServer . bind ( ch ) ; } else { httpServer . bind ( new InetSocketAddress ( port ) ) ; } } catch ( IOException ex ) { Logger . getLogger ( CouchbaseMock . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; System . exit ( - 1 ) ; } for ( BucketConfiguration config : initialConfigs . values ( ) ) { try { createBucket ( config ) ; } catch ( BucketAlreadyExistsException ex ) { throw new IOException ( ex ) ; } } httpServer . start ( ) ; // See if we need to load documents: if ( docsFile != null ) { DocumentLoader loader = new DocumentLoader ( this , "default" ) ; loader . loadDocuments ( docsFile ) ; } else if ( useBeerSample ) { RestAPIUtil . loadBeerSample ( this ) ; } if ( monitorAddress != null ) { startHarakiriMonitor ( monitorAddress , true ) ; } else if ( debug ) { StringBuilder wireshark = new StringBuilder ( "couchbase && (" ) ; System . out . println ( "\nConnection strings:" ) ; for ( Bucket bucket : getBuckets ( ) . values ( ) ) { System . out . println ( "couchbase://127.0.0.1:" + port + "=http/" + bucket . getName ( ) ) ; StringBuilder connstr = new StringBuilder ( "couchbase://" ) ; for ( MemcachedServer server : bucket . getServers ( ) ) { connstr . append ( server . getHostname ( ) ) . append ( ":" ) . append ( server . getPort ( ) ) . append ( "=mcd," ) ; wireshark . append ( "tcp.port == " ) . append ( server . getPort ( ) ) . append ( " || " ) ; } connstr . replace ( connstr . length ( ) - 1 , connstr . length ( ) , "" ) ; connstr . append ( "/" ) . append ( bucket . getName ( ) ) ; System . out . println ( connstr ) ; } wireshark . replace ( wireshark . length ( ) - 4 , wireshark . length ( ) , "" ) ; wireshark . append ( ")" ) ; System . out . println ( "\nWireshark filters:" ) ; System . out . println ( "http && tcp.port == " + port ) ; System . out . println ( wireshark ) ; } startupLatch . countDown ( ) ; }
Used for the command line this ensures that the CountDownLatch object is only set to 0 when all the command line parameters have been initialized ; so that when the monitor finally sends the port over the socket all the items will have already been initialized .