001package jmri.jmris.srcp;
002
003import java.io.File;
004
005import jmri.InstanceManager;
006import jmri.util.FileUtil;
007
008public class JmriSRCPServerManager {
009
010    private static JmriSRCPServerManager instance = null;
011    private JmriSRCPServerPreferences preferences;
012    private JmriSRCPServer server;
013
014    private JmriSRCPServerManager() {
015        if (InstanceManager.getNullableDefault(JmriSRCPServerPreferences.class) == null) {
016            String fileName = FileUtil.getUserFilesPath() + "networkServices" + File.separator + "JmriSRCPServerPreferences.xml";
017            if ((new File(fileName)).exists()) {
018                InstanceManager.store(new JmriSRCPServerPreferences(fileName), JmriSRCPServerPreferences.class); // NOI18N
019            } else {
020                InstanceManager.store(new JmriSRCPServerPreferences(), JmriSRCPServerPreferences.class); // NOI18N
021            }
022        }
023    }
024
025    public static synchronized JmriSRCPServerManager getInstance() {
026        if (instance == null) {
027            instance = new JmriSRCPServerManager();
028        }
029        return instance;
030    }
031
032    public JmriSRCPServerPreferences getPreferences() {
033        if (preferences == null) {
034            preferences = new JmriSRCPServerPreferences();
035        }
036        return preferences;
037    }
038
039    public static JmriSRCPServerPreferences getJmriSRCPServerPreferences() {
040        return getInstance().getPreferences();
041    }
042
043    public JmriSRCPServer getServer() {
044        if (server == null) {
045            server = new JmriSRCPServer(this.getPreferences().getPort());
046        }
047        return server;
048    }
049
050    public static JmriSRCPServer getJmriSRCPServer() {
051        return getInstance().getServer();
052    }
053}