use of org.cytoscape.app.internal.exception.AppLoadingException 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();
}
}
use of org.cytoscape.app.internal.exception.AppLoadingException 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;
}
use of org.cytoscape.app.internal.exception.AppLoadingException in project cytoscape-impl by cytoscape.
the class SimpleApp method load.
@Override
public void load(AppManager appManager) throws AppLoadingException {
if (appConstructor != null)
return;
// Make a copy used to create app instance
LinkedList<String> uniqueNameDirectory = new LinkedList<String>();
uniqueNameDirectory.add(appManager.getTemporaryInstallPath());
try {
if (appTemporaryInstallFile == null) {
File targetFile = new File(appManager.getTemporaryInstallPath() + File.separator + suggestFileName(uniqueNameDirectory, this.getAppFile().getName()));
FileUtils.copyFile(this.getAppFile(), targetFile);
appTemporaryInstallFile = targetFile;
}
} catch (IOException e) {
throw new AppLoadingException("Unable to make copy of app jar for instancing", e);
}
File installFile = appTemporaryInstallFile;
if (installFile == null) {
throw new AppLoadingException("No copy of app jar for instancing was found");
}
URL appURL = null;
try {
appURL = installFile.toURI().toURL();
} catch (MalformedURLException e) {
throw new AppLoadingException("Unable to obtain URL for file: " + installFile, e);
}
// TODO: Currently uses the CyAppAdapter's loader to load apps' classes. Is there reason to use a different one?
ClassLoader appClassLoader = new URLClassLoader(new URL[] { appURL }, appManager.getSwingAppAdapter().getClass().getClassLoader());
// Attempt to load the class
try {
appEntryClass = appClassLoader.loadClass(this.getEntryClassName());
} catch (ClassNotFoundException e) {
throw new AppLoadingException("Class " + this.getEntryClassName() + " not found in URL: " + appURL, e);
}
// Attempt to obtain the constructor
try {
try {
appConstructor = appEntryClass.getConstructor(CyAppAdapter.class);
} catch (SecurityException e) {
throw new AppLoadingException("Access to the constructor for " + appEntryClass + " denied.", e);
} catch (NoSuchMethodException e) {
throw new AppLoadingException("Unable to find a constructor for " + appEntryClass + " that takes a CyAppAdapter as its argument.", e);
}
} catch (AppLoadingException e) {
try {
appConstructor = appEntryClass.getConstructor(CySwingAppAdapter.class);
} catch (SecurityException e2) {
throw new AppLoadingException("Access to the constructor for " + appEntryClass + " taking a CySwingAppAdapter as its argument denied.", e2);
} catch (NoSuchMethodException e2) {
throw new AppLoadingException("Unable to find an accessible constructor that takes either" + " a CyAppAdapter or a CySwingAppAdapter as its argument.", e2);
}
}
}
Aggregations