001package jmri.jmris.simpleserver;
002
003import java.io.File;
004
005import jmri.InstanceManager;
006import jmri.InstanceManagerAutoDefault;
007import jmri.util.FileUtil;
008
009public class SimpleServerManager implements InstanceManagerAutoDefault {
010
011    private SimpleServerPreferences preferences;
012    private SimpleServer server;
013    public SimpleServerManager(){
014        if (InstanceManager.getNullableDefault(SimpleServerPreferences.class) == null) {
015            String fileName = FileUtil.getUserFilesPath() + "networkServices" + File.separator + "SimpleServer.xml";
016            if ((new File(fileName)).exists()) {
017                InstanceManager.store(new SimpleServerPreferences(fileName), SimpleServerPreferences.class); // NOI18N
018            } else {
019                InstanceManager.store(new SimpleServerPreferences(), SimpleServerPreferences.class);
020            }
021        }
022        preferences = InstanceManager.getDefault(SimpleServerPreferences.class);
023    }
024
025    public SimpleServerPreferences getPreferences() {
026        if (preferences == null) {
027            preferences = new SimpleServerPreferences();
028        }
029        return preferences;
030    }
031
032    public static SimpleServerPreferences getSimpleServerPreferences() {
033        return InstanceManager.getDefault(SimpleServerManager.class).getPreferences();
034    }
035
036    public SimpleServer getServer() {
037        if (server == null) {
038            server = new SimpleServer(this.getPreferences().getPort());
039        }
040        return server;
041    }
042
043    public static SimpleServer getSimpleServer() {
044        return InstanceManager.getDefault(SimpleServerManager.class).getServer();
045    }
046}