Search in sources :

Example 1 with CyStartEvent

use of org.cytoscape.application.events.CyStartEvent in project cytoscape-impl by cytoscape.

the class AppManager method initializeApps.

void initializeApps() {
    // Move apps from install-on-restart directory to install directory
    Set<App> installOnRestartApps = obtainAppsFromDirectory(new File(getInstallOnRestartAppsPath()), false);
    for (App app : installOnRestartApps) {
        try {
            app.moveAppFile(this, new File(getInstalledAppsPath()));
            userLogger.info("Moved app '" + app.getAppName() + "' that was marked for install on restart to the 'installed' directory.");
        } catch (IOException e) {
            userLogger.error("Cannot move app '" + app.getAppName() + "' that was marked for install on restart to the 'installed' directory:", e);
        }
    }
    // Remove the install-on-restart directory after apps were moved
    try {
        FileUtils.deleteDirectory(new File(getInstallOnRestartAppsPath()));
    } catch (IOException e) {
        userLogger.warn("Could not delete the directory of apps to install on restart:", e);
    }
    setupAlterationMonitor();
    // Obtain previously disabled, installed apps
    Set<App> disabledApps = obtainAppsFromDirectory(new File(getDisabledAppsPath()), false);
    for (App app : disabledApps) {
        try {
            boolean appRegistered = false;
            for (App regApp : apps) {
                if (regApp.heuristicEquals(app)) {
                    userLogger.warn("Disabled app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' is equal to an already registered app '" + regApp.getAppName() + "' in file '" + (regApp.getAppFile() == null ? "N/A" : regApp.getAppFile().getAbsolutePath()) + "'");
                    appRegistered = true;
                }
            }
            if (!appRegistered) {
                apps.add(app);
                app.setStatus(AppStatus.DISABLED);
            } else {
                // Delete the copy
                userLogger.info("Trying to delete the disabled copy of app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' because another copy exists.");
                FileUtils.deleteQuietly(app.getAppFile());
                app.setAppFile(null);
            }
        } catch (Throwable e) {
            userLogger.error("Error validating status of the disabled app '" + app.getAppName() + "'.", e);
        }
    }
    Set<App> uninstalledApps = obtainAppsFromDirectory(new File(getUninstalledAppsPath()), false);
    for (App app : uninstalledApps) {
        try {
            boolean appRegistered = false;
            for (App regApp : apps) {
                if (regApp.heuristicEquals(app)) {
                    userLogger.warn("Uninstalled app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' is equal to an already registered app '" + regApp.getAppName() + "' in file '" + (regApp.getAppFile() == null ? "N/A" : regApp.getAppFile().getAbsolutePath()) + "'");
                    appRegistered = true;
                }
            }
            if (!appRegistered) {
                apps.add(app);
                app.setStatus(AppStatus.UNINSTALLED);
            } else {
                // Delete the copy
                userLogger.info("Trying to delete the uninstalled copy of app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' because another copy exists.");
                FileUtils.deleteQuietly(app.getAppFile());
                app.setAppFile(null);
            }
        } catch (Throwable e) {
            userLogger.error("Error validating status of the uninstalled app '" + app.getAppName() + "'.", e);
        }
    }
    Set<App> installedApps = obtainAppsFromDirectory(getBundledAppsPath(), true);
    installedApps.addAll(obtainAppsFromDirectory(new File(getInstalledAppsPath()), false));
    Map<String, App> appsToStart = new HashMap<String, App>();
    for (App app : installedApps) {
        boolean appRegistered = false;
        for (App regApp : apps) {
            if (regApp.heuristicEquals(app)) {
                userLogger.warn("Installed app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' is equal to an already registered app '" + regApp.getAppName() + "' in file '" + (regApp.getAppFile() == null ? "N/A" : regApp.getAppFile().getAbsolutePath()) + "'");
                appRegistered = true;
            }
        }
        if (!appRegistered) {
            apps.add(app);
            String appName = app.getAppName().toLowerCase();
            App currentVersion = appsToStart.get(appName);
            if (app.isCompatible(version)) {
                if (currentVersion == null) {
                    appsToStart.put(appName, app);
                } else if (compareApps(currentVersion, app) > 0) {
                    userLogger.warn("Installed app '" + app.getAppName() + "' version  " + app.getVersion() + " will override an older version (" + currentVersion.getVersion() + ") that was registered previously.");
                    appsToStart.put(appName, app);
                } else {
                    userLogger.warn("Installed app '" + app.getAppName() + "' version " + app.getVersion() + " will not be started, because a newer version (" + currentVersion.getVersion() + ") was already registered.");
                }
            } else {
                userLogger.error("Installed app '" + app.getAppName() + "' is not compatible with the running version of Cytoscape (" + version.getVersion() + ").");
            }
        } else {
            // Delete the copy
            userLogger.info("Trying to delete the installed copy of app '" + app.getAppName() + "' in file '" + app.getAppFile().getAbsolutePath() + "' because another copy exists.");
            FileUtils.deleteQuietly(app.getAppFile());
            app.setAppFile(null);
        }
    }
    Set<App> coreAppsToStart = new HashSet<App>();
    App coreAppsMetaApp = appsToStart.get("core apps");
    if (coreAppsMetaApp != null && coreAppsMetaApp.getDependencies() != null) {
        for (App.Dependency dep : coreAppsMetaApp.getDependencies()) {
            String appName = dep.getName().toLowerCase();
            App app = appsToStart.get(appName);
            if (app != null) {
                coreAppsToStart.add(app);
            }
        }
        coreAppsToStart.add(coreAppsMetaApp);
    }
    if (!startApps(coreAppsToStart))
        userLogger.warn("One or more core apps failed to load or start");
    eventHelper.fireEvent(new CyStartEvent(this));
    Set<App> otherAppsToStart = new HashSet<App>(appsToStart.values());
    otherAppsToStart.removeAll(coreAppsToStart);
    if (!startApps(otherAppsToStart))
        userLogger.warn("One or more apps failed to load or start");
    eventHelper.fireEvent(new AppsFinishedStartingEvent(this));
}
Also used : AbstractCyApp(org.cytoscape.app.AbstractCyApp) HashMap(java.util.HashMap) IOException(java.io.IOException) AppsFinishedStartingEvent(org.cytoscape.app.event.AppsFinishedStartingEvent) CyStartEvent(org.cytoscape.application.events.CyStartEvent) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AbstractCyApp (org.cytoscape.app.AbstractCyApp)1 AppsFinishedStartingEvent (org.cytoscape.app.event.AppsFinishedStartingEvent)1 CyStartEvent (org.cytoscape.application.events.CyStartEvent)1