Search in sources :

Example 1 with AppStartupException

use of org.cytoscape.app.internal.exception.AppStartupException in project cytoscape-impl by cytoscape.

the class AppManager method setupAlterationMonitor.

private void setupAlterationMonitor() {
    // Set up the FileAlterationMonitor to install/uninstall apps when apps are moved in/out of the
    // installed/uninstalled app directories
    fileAlterationMonitor = new FileAlterationMonitor(2000L);
    File installedAppsPath = new File(getInstalledAppsPath());
    FileAlterationObserver installAlterationObserver = new FileAlterationObserver(installedAppsPath, new AppFileFilter(installedAppsPath), IOCase.SYSTEM);
    final AppManager appManager = this;
    // Listen for events on the "installed apps" folder
    installAlterationObserver.addListener(new FileAlterationListenerAdaptor() {

        @Override
        public void onFileCreate(File file) {
            App parsedApp = null;
            try {
                parsedApp = appParser.parseApp(file);
            } catch (AppParsingException e) {
                userLogger.error("Could not parse app from newly discovered file '" + file.getAbsolutePath() + "' :", e);
                return;
            }
            boolean startApp = parsedApp.isCompatible(version);
            if (!startApp) {
                userLogger.error("Newly discovered app '" + parsedApp.getAppName() + "' is not compatible with the running version of Cytoscape (" + version + ").");
            }
            App registeredApp = null;
            for (App app : apps) {
                if (parsedApp.heuristicEquals(app)) {
                    registeredApp = app;
                    userLogger.warn("Newly discovered app '" + parsedApp.getAppName() + "' in file '" + parsedApp.getAppFile().getAbsolutePath() + "' is equal to an already registered app '" + registeredApp.getAppName() + "' in file '" + (registeredApp.getAppFile() == null ? "N/A" : registeredApp.getAppFile().getAbsolutePath()) + "'");
                    // Delete old file if it was still there
                    File oldFile = registeredApp.getAppFile();
                    if (oldFile != null && oldFile.exists() && !registeredApp.getAppFile().equals(parsedApp.getAppFile())) {
                        userLogger.info("Trying to delete the installed copy of app '" + registeredApp.getAppName() + "' in file '" + registeredApp.getAppFile().getAbsolutePath() + "' because another copy exists.");
                        FileUtils.deleteQuietly(oldFile);
                    }
                    // Update file reference to reflect file having been moved
                    registeredApp.setAppFile(file);
                    registeredApp.setStatus(AppStatus.INACTIVE);
                } else if (parsedApp.isCompatible(version) && parsedApp.getAppName().equals(app.getAppName())) {
                    try {
                        if (!app.isDetached() && app.isCompatible(version)) {
                            if (compareApps(parsedApp, app) > 0) {
                                startApp = false;
                                userLogger.warn("Not starting newly discovered app '" + parsedApp.getAppName() + "' because a newer version is already loaded.");
                            } else {
                                app.unload(AppManager.this);
                                app.setStatus(AppStatus.INACTIVE);
                                userLogger.warn("Unloading app '" + app.getAppName() + "' because the newly discovered app '" + parsedApp.getAppName() + "' is a newer version.");
                            }
                        }
                    } catch (AppUnloadingException e) {
                        // TODO Auto-generated catch block
                        userLogger.warn("Failed to unload app " + app.getAppName(), e);
                    }
                }
            }
            App app = null;
            if (registeredApp == null) {
                app = parsedApp;
                apps.add(app);
            } else {
                app = registeredApp;
            }
            try {
                if (startApp) {
                    app.load(appManager);
                    app.start(appManager);
                    app.setStatus(AppStatus.INSTALLED);
                    userLogger.info("Started newly discovered app '" + app.getAppName() + "'.");
                }
            } catch (AppLoadingException e) {
                app.setStatus(AppStatus.FAILED_TO_LOAD);
                userLogger.error("Failed to load app " + app.getAppName(), e);
            } catch (AppStartupException e) {
                app.setStatus(AppStatus.FAILED_TO_START);
                userLogger.error("Failed to start app " + app.getAppName(), e);
            }
            fireAppsChangedEvent();
        }

        @Override
        public void onFileChange(File file) {
            // Can treat file replacements/changes as old file deleted, new file added
            this.onFileDelete(file);
            this.onFileCreate(file);
            fireAppsChangedEvent();
        }

        @Override
        public void onFileDelete(File file) {
            // System.out.println(file + " on delete");
            DebugHelper.print(this + " installObserverDelete", file.getAbsolutePath() + " deleted.");
            App registeredApp = null;
            for (App app : apps) {
                if (file.equals(app.getAppFile())) {
                    app.setAppFile(null);
                    registeredApp = app;
                    break;
                }
            }
            if (registeredApp == null)
                return;
            try {
                registeredApp.unload(appManager);
                registeredApp.setStatus(AppStatus.FILE_MOVED);
                userLogger.info("Unloaded app '" + registeredApp.getAppName() + "', because its file is no longer available.");
            } catch (AppUnloadingException e) {
                userLogger.warn("Failed to unload app " + registeredApp.getAppName(), e);
            }
            // Do this so that we don't reload an old app when responding to change events
            if (file.exists()) {
                App parsedApp = null;
                try {
                    parsedApp = appParser.parseApp(file);
                } catch (AppParsingException e) {
                    return;
                }
                if (parsedApp.isCompatible(version) && registeredApp.getAppName().equalsIgnoreCase(parsedApp.getAppName()))
                    return;
            }
            App appToStart = null;
            for (App app : apps) {
                if (!app.isDetached() && app.isCompatible(version) && app.getAppName().equalsIgnoreCase(registeredApp.getAppName())) {
                    if (appToStart == null || compareApps(appToStart, app) > 0)
                        appToStart = app;
                }
            }
            if (appToStart != null) {
                try {
                    appToStart.load(appManager);
                    appToStart.start(appManager);
                    appToStart.setStatus(AppStatus.INSTALLED);
                    userLogger.info("Started app " + appToStart.getAppName() + " because a different version has been unloaded.");
                } catch (AppLoadingException e) {
                    appToStart.setStatus(AppStatus.FAILED_TO_LOAD);
                    userLogger.error("Failed to load app " + appToStart.getAppName(), e);
                } catch (AppStartupException e) {
                    appToStart.setStatus(AppStatus.FAILED_TO_START);
                    userLogger.error("Failed to start app " + appToStart.getAppName(), e);
                }
            }
            fireAppsChangedEvent();
        }
    });
    FileAlterationObserver disableAlterationObserver = new FileAlterationObserver(getDisabledAppsPath(), new AppFileFilter(new File(getDisabledAppsPath())), IOCase.SYSTEM);
    // Listen for events on the "disabled apps" folder
    disableAlterationObserver.addListener(new FileAlterationListenerAdaptor() {

        @Override
        public void onFileCreate(File file) {
            App parsedApp = null;
            try {
                parsedApp = appParser.parseApp(file);
            } catch (AppParsingException e) {
                return;
            }
            DebugHelper.print(this + " disableObserver Create", parsedApp.getAppName() + " parsed");
            App registeredApp = null;
            for (App app : apps) {
                if (parsedApp.heuristicEquals(app)) {
                    registeredApp = app;
                    // Delete old file if it was still there
                    // TODO: Possible rename from filename-2 to filename?
                    File oldFile = registeredApp.getAppFile();
                    if (oldFile != null && oldFile.exists() && !registeredApp.getAppFile().equals(parsedApp.getAppFile())) {
                        DebugHelper.print(this + " disableObserverCreate", registeredApp.getAppName() + " moved from " + registeredApp.getAppFile().getAbsolutePath() + " to " + parsedApp.getAppFile().getAbsolutePath() + ". deleting: " + oldFile);
                        FileUtils.deleteQuietly(oldFile);
                    }
                    // Update file reference to reflect file having been moved
                    registeredApp.setAppFile(file);
                }
            }
            App app = null;
            if (registeredApp == null) {
                app = parsedApp;
                apps.add(app);
            } else {
                app = registeredApp;
            }
            app.setStatus(AppStatus.DISABLED);
            fireAppsChangedEvent();
        // System.out.println(file + " on create");
        }

        @Override
        public void onFileChange(File file) {
            // Can treat file replacements/changes as old file deleted, new file added
            this.onFileDelete(file);
            this.onFileCreate(file);
            fireAppsChangedEvent();
        }

        @Override
        public void onFileDelete(File file) {
            // System.out.println(file + " on delete");
            DebugHelper.print(this + " disableObserverDelete", file.getAbsolutePath() + " deleted.");
            for (App app : apps) {
                // System.out.println("checking " + app.getAppFile().getAbsolutePath());
                if (file.equals(app.getAppFile())) {
                    app.setAppFile(null);
                    app.setStatus(AppStatus.FILE_MOVED);
                    break;
                }
            }
            fireAppsChangedEvent();
        }
    });
    FileAlterationObserver uninstallAlterationObserver = new FileAlterationObserver(getUninstalledAppsPath(), new AppFileFilter(new File(getUninstalledAppsPath())), IOCase.SYSTEM);
    // Listen for events on the "uninstalled apps" folder
    uninstallAlterationObserver.addListener(new FileAlterationListenerAdaptor() {

        @Override
        public void onFileCreate(File file) {
            App parsedApp = null;
            try {
                parsedApp = appParser.parseApp(file);
            } catch (AppParsingException e) {
                return;
            }
            DebugHelper.print(this + " uninstallObserverCreate", parsedApp.getAppName() + " parsed");
            App registeredApp = null;
            for (App app : apps) {
                if (parsedApp.heuristicEquals(app)) {
                    registeredApp = app;
                    // Delete old file if it was still there
                    // TODO: Possible rename from filename-2 to filename?
                    File oldFile = registeredApp.getAppFile();
                    if (oldFile != null && oldFile.exists() && !registeredApp.getAppFile().equals(parsedApp.getAppFile())) {
                        DebugHelper.print(this + " uninstallObserverCreate", registeredApp.getAppName() + " moved from " + registeredApp.getAppFile().getAbsolutePath() + " to " + parsedApp.getAppFile().getAbsolutePath() + ". deleting: " + oldFile);
                        FileUtils.deleteQuietly(oldFile);
                    }
                    // Update file reference to reflect file having been moved
                    registeredApp.setAppFile(file);
                }
            }
            App app = null;
            if (registeredApp == null) {
                app = parsedApp;
                apps.add(app);
            } else {
                app = registeredApp;
            }
            app.setStatus(AppStatus.UNINSTALLED);
            fireAppsChangedEvent();
        // System.out.println(file + " on create");
        }

        @Override
        public void onFileChange(File file) {
            // Can treat file replacements/changes as old file deleted, new file added
            this.onFileDelete(file);
            this.onFileCreate(file);
            fireAppsChangedEvent();
        }

        @Override
        public void onFileDelete(File file) {
            // System.out.println(file + " on delete");
            DebugHelper.print(this + " uninstallObserverDelete", file.getAbsolutePath() + " deleted.");
            for (App app : apps) {
                // System.out.println("checking " + app.getAppFile().getAbsolutePath());
                if (file.equals(app.getAppFile())) {
                    app.setAppFile(null);
                    app.setStatus(AppStatus.FILE_MOVED);
                    break;
                }
            }
            fireAppsChangedEvent();
        }
    });
    try {
        installAlterationObserver.initialize();
        fileAlterationMonitor.addObserver(installAlterationObserver);
        disableAlterationObserver.initialize();
        fileAlterationMonitor.addObserver(disableAlterationObserver);
        uninstallAlterationObserver.initialize();
        fileAlterationMonitor.addObserver(uninstallAlterationObserver);
        fileAlterationMonitor.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : AbstractCyApp(org.cytoscape.app.AbstractCyApp) FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) AppLoadingException(org.cytoscape.app.internal.exception.AppLoadingException) AppParsingException(org.cytoscape.app.internal.exception.AppParsingException) AppUnloadingException(org.cytoscape.app.internal.exception.AppUnloadingException) AppStartupException(org.cytoscape.app.internal.exception.AppStartupException) AppInstallException(org.cytoscape.app.internal.exception.AppInstallException) AppUninstallException(org.cytoscape.app.internal.exception.AppUninstallException) AppLoadingException(org.cytoscape.app.internal.exception.AppLoadingException) IOException(java.io.IOException) AppDisableException(org.cytoscape.app.internal.exception.AppDisableException) AppParsingException(org.cytoscape.app.internal.exception.AppParsingException) FileAlterationObserver(org.apache.commons.io.monitor.FileAlterationObserver) FileAlterationListenerAdaptor(org.apache.commons.io.monitor.FileAlterationListenerAdaptor) AppStartupException(org.cytoscape.app.internal.exception.AppStartupException) AppUnloadingException(org.cytoscape.app.internal.exception.AppUnloadingException) File(java.io.File)

Example 2 with AppStartupException

use of org.cytoscape.app.internal.exception.AppStartupException in project cytoscape-impl by cytoscape.

the class AppManager method startApps.

private boolean startApps(Collection<App> apps) {
    boolean success = true;
    for (Iterator<App> i = apps.iterator(); i.hasNext(); ) {
        App app = i.next();
        try {
            app.load(this);
            userLogger.info("Loaded app " + app.getAppName());
        } catch (AppLoadingException e) {
            i.remove();
            success = false;
            app.setStatus(AppStatus.FAILED_TO_LOAD);
            userLogger.error("Failed to load app " + app.getAppName(), e);
        }
    }
    for (App app : apps) {
        try {
            app.start(this);
            app.setStatus(AppStatus.INSTALLED);
            userLogger.info("Started app " + app.getAppName());
        } catch (AppStartupException e) {
            success = false;
            app.setStatus(AppStatus.FAILED_TO_START);
            userLogger.error("Failed to start app " + app.getAppName(), e);
        }
    }
    return success;
}
Also used : AbstractCyApp(org.cytoscape.app.AbstractCyApp) AppLoadingException(org.cytoscape.app.internal.exception.AppLoadingException) AppStartupException(org.cytoscape.app.internal.exception.AppStartupException)

Aggregations

AbstractCyApp (org.cytoscape.app.AbstractCyApp)2 AppLoadingException (org.cytoscape.app.internal.exception.AppLoadingException)2 AppStartupException (org.cytoscape.app.internal.exception.AppStartupException)2 File (java.io.File)1 IOException (java.io.IOException)1 FileAlterationListenerAdaptor (org.apache.commons.io.monitor.FileAlterationListenerAdaptor)1 FileAlterationMonitor (org.apache.commons.io.monitor.FileAlterationMonitor)1 FileAlterationObserver (org.apache.commons.io.monitor.FileAlterationObserver)1 AppDisableException (org.cytoscape.app.internal.exception.AppDisableException)1 AppInstallException (org.cytoscape.app.internal.exception.AppInstallException)1 AppParsingException (org.cytoscape.app.internal.exception.AppParsingException)1 AppUninstallException (org.cytoscape.app.internal.exception.AppUninstallException)1 AppUnloadingException (org.cytoscape.app.internal.exception.AppUnloadingException)1