Search in sources :

Example 16 with Profile

use of jmri.profile.Profile 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)

Example 17 with Profile

use of jmri.profile.Profile in project JMRI by JMRI.

the class JmriPreferencesProviderTest method testFindProvider.

/**
     * Test of findProvider method, of class JmriPreferencesProvider.
     * @throws java.io.IOException
     */
public void testFindProvider() throws IOException {
    String id = Long.toString((new Date()).getTime());
    Profile p = new Profile(this.getName(), id, new File(this.workspace.toFile(), id));
    JmriPreferencesProvider shared = JmriPreferencesProvider.findProvider(p.getPath(), true);
    JmriPreferencesProvider privat = JmriPreferencesProvider.findProvider(p.getPath(), false);
    assertNotNull(shared);
    assertNotNull(privat);
    assertNotSame(shared, privat);
    FileUtil.delete(p.getPath());
}
Also used : File(java.io.File) Date(java.util.Date) Profile(jmri.profile.Profile)

Example 18 with Profile

use of jmri.profile.Profile in project JMRI by JMRI.

the class JmriConfigurationProviderTest method testFindProvider.

/**
     * Test of findProvider method, of class JmriConfigurationProvider.
     *
     * @throws java.io.IOException
     */
public void testFindProvider() throws IOException {
    String id = Long.toString((new Date()).getTime());
    Profile p = new Profile(this.getName(), id, new File(this.workspace.toFile(), id));
    JmriConfigurationProvider config = JmriConfigurationProvider.findProvider(p);
    assertNotNull(config);
    FileUtil.delete(p.getPath());
}
Also used : File(java.io.File) Date(java.util.Date) Profile(jmri.profile.Profile)

Example 19 with Profile

use of jmri.profile.Profile in project JMRI by JMRI.

the class JmriConfigurationProviderTest method testGetConfiguration.

/**
     * Test of getConfiguration method, of class JmriConfigurationProvider.
     *
     * @throws java.io.IOException
     */
public void testGetConfiguration() throws IOException {
    String id = Long.toString((new Date()).getTime());
    Profile project = new Profile(this.getName(), id, new File(this.workspace.toFile(), id));
    AuxiliaryConfiguration config = JmriConfigurationProvider.getConfiguration(project);
    assertNotNull(config);
    FileUtil.delete(project.getPath());
}
Also used : AuxiliaryConfiguration(jmri.profile.AuxiliaryConfiguration) File(java.io.File) Date(java.util.Date) Profile(jmri.profile.Profile)

Example 20 with Profile

use of jmri.profile.Profile in project JMRI by JMRI.

the class WebAppManager method lifeCycleStarted.

private void lifeCycleStarted(LifeCycle lc, Profile profile) {
    // register watcher to watch web/app directories everywhere
    if (this.watcher.get(profile) != null) {
        FileUtil.findFiles("web", ".").stream().filter((file) -> (file.isDirectory())).forEachOrdered((file) -> {
            try {
                Path path = file.toPath();
                WebAppManager.this.watchPaths.put(path.register(this.watcher.get(profile), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY), path);
            } catch (IOException ex) {
                log.error("Unable to watch {} for changes.", file);
            }
            (new Thread() {

                @Override
                public void run() {
                    while (WebAppManager.this.watcher.get(profile) != null) {
                        WatchKey key;
                        try {
                            key = WebAppManager.this.watcher.get(profile).take();
                        } catch (InterruptedException ex) {
                            return;
                        }
                        key.pollEvents().stream().filter((event) -> (event.kind() != OVERFLOW)).forEachOrdered((event) -> {
                            WebAppManager.this.savePreferences(profile);
                        });
                        if (!key.reset()) {
                            WebAppManager.this.watcher.remove(profile);
                        }
                    }
                }
            }).start();
        });
    }
}
Also used : OVERFLOW(java.nio.file.StandardWatchEventKinds.OVERFLOW) LifeCycle(org.eclipse.jetty.util.component.LifeCycle) URL(java.net.URL) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) AngularRoute(jmri.server.web.spi.AngularRoute) WatchKey(java.nio.file.WatchKey) HashSet(java.util.HashSet) WebServer(jmri.web.server.WebServer) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Map(java.util.Map) Profile(jmri.profile.Profile) Path(java.nio.file.Path) PropertyChangeEvent(java.beans.PropertyChangeEvent) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) WebMenuItem(jmri.server.web.spi.WebMenuItem) File(java.io.File) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) WatchService(java.nio.file.WatchService) List(java.util.List) WebManifest(jmri.server.web.spi.WebManifest) WebServerPreferences(jmri.web.server.WebServerPreferences) FileUtil(jmri.util.FileUtil) StringJoiner(java.util.StringJoiner) FileSystems(java.nio.file.FileSystems) Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Aggregations

Profile (jmri.profile.Profile)23 File (java.io.File)14 Date (java.util.Date)7 ArrayList (java.util.ArrayList)6 InitializationException (jmri.util.prefs.InitializationException)6 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 IOException (java.io.IOException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)3 HashSet (java.util.HashSet)3 Locale (java.util.Locale)3 Map (java.util.Map)3 ServiceLoader (java.util.ServiceLoader)3 ProfileUtils (jmri.profile.ProfileUtils)3 AbstractPreferencesManager (jmri.util.prefs.AbstractPreferencesManager)3 Test (org.junit.Test)3