public static StartupSettings fromJSONFile ( File jsonFile ) throws JSONException , FileNotFoundException , IOException { // Read the file to a String. StringBuffer buffer = new StringBuffer ( ) ; try ( BufferedReader br = new BufferedReader ( new FileReader ( jsonFile ) ) ) { String line ; while ( ( line = br . readLine ( ) ) != null ) { buffer . append ( line . trim ( ) ) ; } } JSONObject jsonObject = new JSONArray ( buffer . toString ( ) ) . getJSONObject ( 0 ) ; int port = jsonObject . getInt ( "port" ) ; String id = jsonObject . getString ( "id" ) ; int gossipInterval = jsonObject . getInt ( "gossip_interval" ) ; int cleanupInterval = jsonObject . getInt ( "cleanup_interval" ) ; String cluster = jsonObject . getString ( "cluster" ) ; if ( cluster == null ) { throw new IllegalArgumentException ( "cluster was null. It is required" ) ; } StartupSettings settings = new StartupSettings ( id , port , new GossipSettings ( gossipInterval , cleanupInterval ) , cluster ) ; // Now iterate over the members from the config file and add them to the settings. String configMembersDetails = "Config-members [" ; JSONArray membersJSON = jsonObject . getJSONArray ( "members" ) ; for ( int i = 0 ; i < membersJSON . length ( ) ; i ++ ) { JSONObject memberJSON = membersJSON . getJSONObject ( i ) ; RemoteGossipMember member = new RemoteGossipMember ( memberJSON . getString ( "cluster" ) , memberJSON . getString ( "host" ) , memberJSON . getInt ( "port" ) , "" ) ; settings . addGossipMember ( member ) ; configMembersDetails += member . getAddress ( ) ; if ( i < ( membersJSON . length ( ) - 1 ) ) configMembersDetails += ", " ; } log . info ( configMembersDetails + "]" ) ; // Return the created settings object. return settings ; }
Parse the settings for the gossip service from a JSON file .