Search in sources :

Example 1 with InitializationException

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));
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Preferences(java.util.prefs.Preferences) InitializationException(jmri.util.prefs.InitializationException)

Example 2 with InitializationException

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);
    }
}
Also used : FileLocationsPreferences(jmri.implementation.FileLocationsPreferences) Preferences(java.util.prefs.Preferences) InitializationException(jmri.util.prefs.InitializationException)

Example 3 with InitializationException

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...");
    }
}
Also used : Element(org.jdom2.Element) InitializationException(jmri.util.prefs.InitializationException) XmlAdapter(jmri.configurexml.XmlAdapter) InitializationException(jmri.util.prefs.InitializationException) JDOMException(org.jdom2.JDOMException)

Example 4 with InitializationException

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());
    }
}
Also used : InitializationException(jmri.util.prefs.InitializationException) File(java.io.File) Date(java.util.Date) Profile(jmri.profile.Profile) Test(org.junit.Test)

Example 5 with InitializationException

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"));
        }
    }
}
Also used : ConfigXmlManager(jmri.configurexml.ConfigXmlManager) StartupModel(apps.startup.StartupModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) RosterConfigManager(jmri.jmrit.roster.RosterConfigManager) JDOMUtil(jmri.util.jdom.JDOMUtil) ProgrammerConfigManager(jmri.jmrit.symbolicprog.ProgrammerConfigManager) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Profile(jmri.profile.Profile) XmlAdapter(jmri.configurexml.XmlAdapter) Logger(org.slf4j.Logger) FileLocationsPreferences(jmri.implementation.FileLocationsPreferences) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) JmriException(jmri.JmriException) WarrantPreferences(jmri.jmrit.logix.WarrantPreferences) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StartupActionModelUtil(apps.startup.StartupActionModelUtil) List(java.util.List) ManagerDefaultSelector(jmri.managers.ManagerDefaultSelector) PreferencesManager(jmri.spi.PreferencesManager) StartupModelFactory(apps.startup.StartupModelFactory) Element(org.jdom2.Element) Element(org.jdom2.Element) JmriException(jmri.JmriException) InitializationException(jmri.util.prefs.InitializationException) InitializationException(jmri.util.prefs.InitializationException) JDOMException(org.jdom2.JDOMException) JmriException(jmri.JmriException) XmlAdapter(jmri.configurexml.XmlAdapter)

Aggregations

InitializationException (jmri.util.prefs.InitializationException)8 Preferences (java.util.prefs.Preferences)3 Element (org.jdom2.Element)3 FileNotFoundException (java.io.FileNotFoundException)2 XmlAdapter (jmri.configurexml.XmlAdapter)2 FileLocationsPreferences (jmri.implementation.FileLocationsPreferences)2 Profile (jmri.profile.Profile)2 JDOMException (org.jdom2.JDOMException)2 StartupActionsManager (apps.StartupActionsManager)1 GuiLafPreferencesManager (apps.gui.GuiLafPreferencesManager)1 ScriptButtonModel (apps.startup.ScriptButtonModel)1 StartupActionModelUtil (apps.startup.StartupActionModelUtil)1 StartupModel (apps.startup.StartupModel)1 StartupModelFactory (apps.startup.StartupModelFactory)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1