@ -2,8 +2,11 @@ package app.services.io;
import app.entities.server.Server ;
import app.entities.server.Server ;
import app.entities.Stats ;
import app.entities.Stats ;
import app.updates.PlayersUpdater ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
@ -18,21 +21,23 @@ public class ServersReader {
Stats stats ;
Stats stats ;
ObjectMapper objectMapper ;
ObjectMapper objectMapper ;
private final Logger logger = LoggerFactory . getLogger ( ServersReader . class ) ;
@Autowired
@Autowired
public ServersReader ( Stats stats , @Value ( "${backend.servers_file}" ) String servers_path ) {
public ServersReader ( Stats stats , @Value ( "${backend.servers_file}" ) String servers_path ) {
this . stats = stats ;
this . stats = stats ;
this . objectMapper = new ObjectMapper ( ) ;
this . objectMapper = new ObjectMapper ( ) ;
try {
try {
System . out . printf ( "Read from: %s\n ", servers_path ) ;
logger . info ( "Read from: {} ", servers_path ) ;
JsonNode node = this . objectMapper . readTree ( new File ( servers_path ) ) ;
JsonNode node = this . objectMapper . readTree ( new File ( servers_path ) ) ;
Iterator < Map . Entry < String , JsonNode > > iterator = node . fields ( ) ;
Iterator < Map . Entry < String , JsonNode > > iterator = node . fields ( ) ;
while ( iterator . hasNext ( ) ) {
while ( iterator . hasNext ( ) ) {
Map . Entry < String , JsonNode > server = iterator . next ( ) ;
Map . Entry < String , JsonNode > server = iterator . next ( ) ;
stats . getServers ( ) . put ( server . getKey ( ) , this . objectMapper . treeToValue ( server . getValue ( ) , Server . class ) ) ;
stats . getServers ( ) . put ( server . getKey ( ) , this . objectMapper . treeToValue ( server . getValue ( ) , Server . class ) ) ;
logger . info ( "{}\n{}" , server . getKey ( ) , stats . getServers ( ) . get ( server . getKey ( ) ) ) ;
}
}
} catch ( IOException err ) {
} catch ( IOException err ) {
System . out . printf ( "Cannot read servers file: %s\n ", servers_path ) ;
logger . error ( "Cannot read servers file: {} ", servers_path ) ;
}
}
}
}
}
}