SLightly more atomic locations.yml saving to guard against ctrl+c'ing users

This commit is contained in:
md_5 2013-05-22 17:07:46 +10:00
parent 2c225a05e7
commit 98860ffd02

View File

@ -20,7 +20,8 @@ public class YamlReconnectHandler implements ReconnectHandler
{ {
private final Yaml yaml = new Yaml(); private final Yaml yaml = new Yaml();
private final File file = new File( "locations.yml" ); private final File readFile = new File( "locations.yml" );
private final File writeFile = new File( "locations.yml~" );
/*========================================================================*/ /*========================================================================*/
private Map<String, String> data; private Map<String, String> data;
@ -29,8 +30,8 @@ public class YamlReconnectHandler implements ReconnectHandler
{ {
try try
{ {
file.createNewFile(); readFile.createNewFile();
try ( FileReader rd = new FileReader( file ) ) try ( FileReader rd = new FileReader( readFile ) )
{ {
data = yaml.loadAs( rd, Map.class ); data = yaml.loadAs( rd, Map.class );
} }
@ -85,9 +86,11 @@ public class YamlReconnectHandler implements ReconnectHandler
@Override @Override
public synchronized void save() public synchronized void save()
{ {
try ( FileWriter wr = new FileWriter( file ) ) try ( FileWriter wr = new FileWriter( writeFile ) )
{ {
yaml.dump( data, wr ); yaml.dump( data, wr );
readFile.delete();
writeFile.renameTo( readFile );
} catch ( IOException ex ) } catch ( IOException ex )
{ {
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not save reconnect locations", ex ); ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not save reconnect locations", ex );