use of jmri.util.prefs.InitializationException in project JMRI by JMRI.
the class FileLocationsPreferences method initialize.
@Override
public void initialize(Profile profile) throws InitializationException {
if (!this.isInitialized(profile)) {
Preferences shared = ProfileUtils.getPreferences(profile, this.getClass(), true);
Preferences perNode = ProfileUtils.getPreferences(profile, this.getClass(), false);
String userFiles = shared.get(USER_FILES, FileUtil.PROFILE);
if (!userFiles.startsWith(FileUtil.PROFILE)) {
userFiles = perNode.get(USER_FILES, userFiles);
}
FileUtil.setUserFilesPath(FileUtil.getAbsoluteFilename(userFiles));
String scripts = shared.get(SCRIPTS, FileUtil.PROFILE);
if (!scripts.startsWith(FileUtil.PROFILE) && !scripts.startsWith(FileUtil.PROGRAM)) {
scripts = perNode.get(SCRIPTS, scripts);
}
FileUtil.setScriptsPath(FileUtil.getAbsoluteFilename(scripts));
this.setInitialized(profile, true);
try {
if (!FileUtil.getFile(userFiles).isDirectory()) {
// NOI18N
String message = "UserFilesIsNotDir";
userFiles = FileUtil.getAbsoluteFilename(userFiles);
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, userFiles), Bundle.getMessage(message, userFiles));
}
} catch (FileNotFoundException ex) {
// NOI18N
String message = "UserFilesDoesNotExist";
userFiles = FileUtil.getAbsoluteFilename(userFiles);
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, userFiles), Bundle.getMessage(message, userFiles));
}
try {
if (!FileUtil.getFile(scripts).isDirectory()) {
// NOI18N
String message = "ScriptsIsNotDir";
scripts = FileUtil.getAbsoluteFilename(scripts);
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, scripts), Bundle.getMessage(message, scripts));
}
} catch (FileNotFoundException ex) {
// NOI18N
String message = "ScriptsDoesNotExist";
scripts = FileUtil.getAbsoluteFilename(scripts);
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, scripts), Bundle.getMessage(message, scripts));
}
}
}
use of jmri.util.prefs.InitializationException in project JMRI by JMRI.
the class RosterConfigManager method initialize.
@Override
public void initialize(Profile profile) throws InitializationException {
if (!this.isInitialized(profile)) {
Preferences preferences = ProfileUtils.getPreferences(profile, this.getClass(), true);
this.setDefaultOwner(preferences.get(DEFAULT_OWNER, this.getDefaultOwner()));
try {
this.setDirectory(preferences.get(DIRECTORY, this.getDirectory()));
} catch (IllegalArgumentException ex) {
this.setInitialized(profile, true);
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, "IllegalRosterLocation", preferences.get(DIRECTORY, this.getDirectory())), ex.getMessage(), ex);
}
Roster.getDefault().setRosterLocation(this.getDirectory());
this.setInitialized(profile, true);
}
}
use of jmri.util.prefs.InitializationException in project JMRI by JMRI.
the class ConnectionConfigManager method initialize.
@Override
public void initialize(Profile profile) throws InitializationException {
if (!isInitialized(profile)) {
log.debug("Initializing...");
Element sharedConnections = null;
Element perNodeConnections = null;
try {
sharedConnections = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(CONNECTIONS, NAMESPACE, true));
} catch (NullPointerException ex) {
// Normal if this is a new profile
log.info("No connections configured.");
log.debug("Null pointer thrown reading shared configuration.", ex);
}
if (sharedConnections != null) {
try {
perNodeConnections = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(CONNECTIONS, NAMESPACE, false));
} catch (NullPointerException ex) {
// Normal if the profile has not been used on this computer
log.info("No local configuration found.");
log.debug("Null pointer thrown reading local configuration.", ex);
// TODO: notify user
}
for (Element shared : sharedConnections.getChildren(CONNECTION)) {
Element perNode = shared;
String className = shared.getAttributeValue(CLASS);
// NOI18N
String userName = shared.getAttributeValue(USER_NAME, "");
// NOI18N
String systemName = shared.getAttributeValue(SYSTEM_NAME, "");
// NOI18N
String manufacturer = shared.getAttributeValue(MANUFACTURER, "");
log.debug("Read shared connection {}:{} ({}) class {}", userName, systemName, manufacturer, className);
if (perNodeConnections != null) {
for (Element e : perNodeConnections.getChildren(CONNECTION)) {
if (systemName.equals(e.getAttributeValue(SYSTEM_NAME))) {
perNode = e;
className = perNode.getAttributeValue(CLASS);
// NOI18N
userName = perNode.getAttributeValue(USER_NAME, "");
// NOI18N
manufacturer = perNode.getAttributeValue(MANUFACTURER, "");
log.debug("Read perNode connection {}:{} ({}) class {}", userName, systemName, manufacturer, className);
}
}
}
try {
log.debug("Creating connection {}:{} ({}) class {}", userName, systemName, manufacturer, className);
XmlAdapter adapter = (XmlAdapter) Class.forName(className).newInstance();
ConnectionConfigManagerErrorHandler handler = new ConnectionConfigManagerErrorHandler();
adapter.setExceptionHandler(handler);
if (!adapter.load(shared, perNode)) {
log.error("Unable to create {} for {}, load returned false", className, shared);
// NOI18N
String english = Bundle.getMessage(Locale.ENGLISH, "ErrorSingleConnection", userName, systemName);
// NOI18N
String localized = Bundle.getMessage("ErrorSingleConnection", userName, systemName);
this.addInitializationException(profile, new InitializationException(english, localized));
}
handler.exceptions.forEach((exception) -> {
this.addInitializationException(profile, exception);
});
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
log.error("Unable to create {} for {}", className, shared, ex);
// NOI18N
String english = Bundle.getMessage(Locale.ENGLISH, "ErrorSingleConnection", userName, systemName);
// NOI18N
String localized = Bundle.getMessage("ErrorSingleConnection", userName, systemName);
this.addInitializationException(profile, new InitializationException(english, localized, ex));
} catch (Exception ex) {
log.error("Unable to load {} into {}", shared, className, ex);
// NOI18N
String english = Bundle.getMessage(Locale.ENGLISH, "ErrorSingleConnection", userName, systemName);
// NOI18N
String localized = Bundle.getMessage("ErrorSingleConnection", userName, systemName);
this.addInitializationException(profile, new InitializationException(english, localized, ex));
}
}
}
setInitialized(profile, true);
List<Exception> exceptions = this.getInitializationExceptions(profile);
if (exceptions.size() == 1) {
if (exceptions.get(0) instanceof InitializationException) {
throw (InitializationException) exceptions.get(0);
} else {
throw new InitializationException(exceptions.get(0));
}
} else if (exceptions.size() > 1) {
// NOI18N
String english = Bundle.getMessage(Locale.ENGLISH, "ErrorMultipleConnections");
// NOI18N
String localized = Bundle.getMessage("ErrorMultipleConnections");
throw new InitializationException(english, localized);
}
log.debug("Initialized...");
}
}
use of jmri.util.prefs.InitializationException in project JMRI by JMRI.
the class ConnectionConfigManagerTest method testInitialize_EmptyProfile.
/**
* Test of initialize method, of class ConnectionConfigManager.
*
* @throws java.io.IOException if untested condition (most likely inability
* to write to temp space) fails
*/
@Test
public void testInitialize_EmptyProfile() throws IOException {
String id = Long.toString((new Date()).getTime());
Profile profile = new Profile(this.getClass().getSimpleName(), id, new File(this.workspace.toFile(), id));
ConnectionConfigManager instance = new ConnectionConfigManager();
try {
instance.initialize(profile);
} catch (InitializationException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.util.prefs.InitializationException in project JMRI by JMRI.
the class StartupActionsManager method initialize.
/**
* {@inheritDoc}
*
* Loads the startup action preferences and, if all required managers have
* initialized without exceptions, performs those actions. Startup actions
* are only performed if {@link apps.startup.StartupModel#isValid()} is true
* for the action. It is assumed that the action has retained an Exception
* that can be used to explain why isValid() is false.
*/
@Override
public void initialize(Profile profile) throws InitializationException {
if (!this.isInitialized(profile)) {
boolean perform = true;
try {
this.requiresNoInitializedWithExceptions(profile, Bundle.getMessage("StartupActionsManager.RefusalToInitialize"));
} catch (InitializationException ex) {
perform = false;
}
try {
Element startup;
try {
startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE, true));
} catch (NullPointerException ex) {
log.debug("Reading element from version 2.9.6 namespace...");
startup = JDOMUtil.toJDOMElement(ProfileUtils.getAuxiliaryConfiguration(profile).getConfigurationFragment(STARTUP, NAMESPACE_OLD, true));
}
for (Element action : startup.getChildren()) {
// NOI18N
String adapter = action.getAttributeValue("class");
// NOI18N
String name = action.getAttributeValue("name");
String override = StartupActionModelUtil.getDefault().getOverride(name);
if (override != null) {
action.setAttribute("name", override);
log.info("Overridding statup action class {} with {}", name, override);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override), Bundle.getMessage(Locale.ENGLISH, "StartupActionsOverriddenClasses", name, override)));
// after logging difference and creating error message
name = override;
}
// NOI18N
String type = action.getAttributeValue("type");
log.debug("Read {} {} adapter {}", type, name, adapter);
try {
log.debug("Creating {} {} adapter {}...", type, name, adapter);
// no perNode preferences
((XmlAdapter) Class.forName(adapter).newInstance()).load(action, null);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
log.error("Unable to create {} for {}", adapter, action, ex);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsCreationError", adapter, name), // NOI18N
Bundle.getMessage("StartupActionsCreationError", adapter, name)));
} catch (Exception ex) {
log.error("Unable to load {} into {}", action, adapter, ex);
this.addInitializationException(profile, new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsLoadError", adapter, name), // NOI18N
Bundle.getMessage("StartupActionsLoadError", adapter, name)));
}
}
} catch (NullPointerException ex) {
// ignore - this indicates migration has not occured
log.debug("No element to read");
}
if (perform) {
this.actions.stream().filter((action) -> (action.isValid())).forEachOrdered((action) -> {
try {
action.performAction();
} catch (JmriException ex) {
this.addInitializationException(profile, ex);
}
});
}
this.isDirty = false;
this.restartRequired = false;
this.setInitialized(profile, true);
List<Exception> exceptions = this.getInitializationExceptions(profile);
if (exceptions.size() == 1) {
throw new InitializationException(exceptions.get(0));
} else if (exceptions.size() > 1) {
throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, "StartupActionsMultipleErrors"), // NOI18N
Bundle.getMessage("StartupActionsMultipleErrors"));
}
}
}
Aggregations