001package jmri.server.json; 002 003import java.util.prefs.BackingStoreException; 004import java.util.prefs.Preferences; 005import jmri.InstanceManagerAutoDefault; 006import jmri.beans.Bean; 007import jmri.profile.Profile; 008import jmri.profile.ProfileManager; 009import jmri.profile.ProfileUtils; 010import jmri.util.FileUtil; 011import jmri.util.prefs.JmriPreferencesProvider; 012 013import org.slf4j.Logger; 014import org.slf4j.LoggerFactory; 015 016public class JsonServerPreferences extends Bean implements InstanceManagerAutoDefault { 017 018 public static final int DEFAULT_PORT = 2056; 019 static final String XML_PREFS_ELEMENT = "JSONServerPreferences"; // NOI18N 020 static final String HEARTBEAT_INTERVAL = "heartbeatInterval"; // NOI18N 021 static final String PORT = "port"; // NOI18N 022 static final String VALIDATE_CLIENT = "validateClientMessages"; // NOI18N 023 static final String VALIDATE_SERVER = "validateServerMessages"; // NOI18N 024 // initial defaults if preferences not found 025 private int heartbeatInterval = 15000; 026 private int portNumber = DEFAULT_PORT; 027 private boolean validateClientMessages = false; 028 private boolean validateServerMessages = false; 029 // as loaded preferences 030 private int asLoadedHeartbeatInterval = 15000; 031 private int asLoadedPort = DEFAULT_PORT; 032 private static final Logger log = LoggerFactory.getLogger(JsonServerPreferences.class); 033 034 public JsonServerPreferences(String fileName) { 035 boolean migrate = false; 036 Profile activeProfile = ProfileManager.getDefault().getActiveProfile(); 037 Preferences sharedPreferences = ProfileUtils.getPreferences(activeProfile, getClass(), true); 038 migrate = sharedPreferences.get(PORT, null) == null; 039 if (migrate) { 040 // method is deprecated to discourage use, not for removal 041 // using to allow migration from old package to new package 042 Preferences oldPreferences = JmriPreferencesProvider.getPreferences(activeProfile, "jmri.jmris.json", true); 043 readPreferences(oldPreferences); 044 } 045 readPreferences(sharedPreferences); 046 if (migrate) { 047 try { 048 log.info("Migrating from old JsonServer preferences in {} to new format in {}.", fileName, FileUtil.getAbsoluteFilename("profile:profile")); 049 sharedPreferences.sync(); 050 } catch (BackingStoreException ex) { 051 log.error("Unable to write JsonServer preferences.", ex); 052 } 053 } 054 } 055 056 public JsonServerPreferences() { 057 Preferences sharedPreferences = ProfileUtils.getPreferences(ProfileManager.getDefault().getActiveProfile(), getClass(), true); 058 readPreferences(sharedPreferences); 059 } 060 061 private void readPreferences(Preferences sharedPreferences) { 062 setHeartbeatInterval(sharedPreferences.getInt(HEARTBEAT_INTERVAL, getHeartbeatInterval())); 063 setPort(sharedPreferences.getInt(PORT, getPort())); 064 setValidateClientMessages(sharedPreferences.getBoolean(VALIDATE_CLIENT, getValidateClientMessages())); 065 setValidateServerMessages(sharedPreferences.getBoolean(VALIDATE_SERVER, getValidateServerMessages())); 066 asLoadedHeartbeatInterval = getHeartbeatInterval(); 067 asLoadedPort = getPort(); 068 } 069 070 public boolean compareValuesDifferent(JsonServerPreferences prefs) { 071 if (getHeartbeatInterval() != prefs.getHeartbeatInterval()) { 072 return true; 073 } 074 return getPort() != prefs.getPort(); 075 } 076 077 public void apply(JsonServerPreferences prefs) { 078 setHeartbeatInterval(prefs.getHeartbeatInterval()); 079 setPort(prefs.getPort()); 080 } 081 082 public void save() { 083 Preferences sharedPreferences = ProfileUtils.getPreferences(ProfileManager.getDefault().getActiveProfile(), getClass(), true); 084 sharedPreferences.putInt(HEARTBEAT_INTERVAL, heartbeatInterval); 085 sharedPreferences.putInt(PORT, portNumber); 086 sharedPreferences.putBoolean(VALIDATE_CLIENT, validateClientMessages); 087 sharedPreferences.putBoolean(VALIDATE_SERVER, validateServerMessages); 088 } 089 090 public boolean isDirty() { 091 return (getHeartbeatInterval() != asLoadedHeartbeatInterval 092 || getPort() != asLoadedPort); 093 } 094 095 public boolean isRestartRequired() { 096 // Once the JsonServer heartbeat interval can be updated, return true 097 // only if the server port changes. 098 return (getHeartbeatInterval() != asLoadedHeartbeatInterval 099 || getPort() != asLoadedPort); 100 } 101 102 public int getHeartbeatInterval() { 103 return heartbeatInterval; 104 } 105 106 public void setHeartbeatInterval(int value) { 107 heartbeatInterval = value; 108 } 109 110 public int getPort() { 111 return portNumber; 112 } 113 114 public void setPort(int value) { 115 portNumber = value; 116 } 117 118 /** 119 * Validate that messages from clients are schema valid. 120 * 121 * @return true if client messages should be validated; false otherwise 122 */ 123 public boolean getValidateClientMessages() { 124 return validateClientMessages; 125 } 126 127 public void setValidateClientMessages(boolean validate) { 128 validateClientMessages = validate; 129 } 130 131 /** 132 * Validate that messages from the server are schema valid. 133 * 134 * @return true if server messages should be validated; false otherwise 135 */ 136 public boolean getValidateServerMessages() { 137 return validateServerMessages; 138 } 139 140 public void setValidateServerMessages(boolean validate) { 141 validateServerMessages = validate; 142 } 143}