Author: paperwing
Date: 2012-05-24 16:05:06 -0700 (Thu, 24 May 2012)
New Revision: 29347

Modified:
   
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
   
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
   
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/ui/CurrentlyInstalledAppsPanel.java
Log:
In process of adding synchronization between app manager and the local storage 
directories, so that jar files copied to the directory are automatically 
installed by the app manager, and files moved out of the directory are 
automatically uninstalled

Modified: 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
===================================================================
--- 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
 2012-05-24 21:21:57 UTC (rev 29346)
+++ 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
 2012-05-24 23:05:06 UTC (rev 29347)
@@ -63,7 +63,8 @@
        public enum AppStatus{
                INSTALLED("Installed"),
                TO_BE_UNINSTALLED("Uninstall-on-restart"),
-               UNINSTALLED("Uninstalled");
+               UNINSTALLED("Uninstalled"),
+               UNDEFINED("Undefined");
                
                String readableStatus;
                
@@ -171,9 +172,12 @@
                                
                                // The app registered to the app manager that 
happens to have the same filename
                                App conflictingApp = null;
+                               File registeredAppFile;
                                
                                for (App registeredApp : registeredApps) {
-                                       if 
(registeredApp.getAppFile().getName().equalsIgnoreCase(appFile.getName())) {
+                                       registeredAppFile = 
registeredApp.getAppFile();
+                                       
+                                       if (registeredAppFile != null && 
registeredAppFile.getName().equalsIgnoreCase(appFile.getName())) {
                                                conflictingApp = registeredApp;
                                        }
                                }
@@ -186,9 +190,15 @@
                                        if 
(this.getAppName().equalsIgnoreCase(conflictingApp.getAppName())) {
                                                
                                                // Same filename, same app name 
found
-                                               // Do nothing
-                                               return;
                                                
+                                               // Forgive the collision if we 
are copying from the uninstalled apps directory
+                                               if 
(appFile.getParentFile().getCanonicalPath().equals(uninstalledAppsPath)) {
+                                                       
+                                               } else {
+                                                       // Skip installation, 
suspected that a copy of the app is already installed
+                                                       return;
+                                               }
+                                               
                                        } else {
                                                
                                                // Same filename, different app 
name found
@@ -312,30 +322,35 @@
                        throw new AppUninstallException("App is not installed; 
cannot uninstall.");
                }
                
-               // Check if the app is inside the directory containing 
currently installed apps.
-               // If so, prepare to move it to the uninstalled directory.
-               File appParentDirectory = this.getAppFile().getParentFile();
-               try {
-                       // Obtain the path of the "uninstalled apps" 
subdirectory.
-                       String uninstalledAppsPath = 
appManager.getUninstalledAppsPath();
-                       
-                       if (appParentDirectory.getCanonicalPath().equals(
-                                       appManager.getInstalledAppsPath())) {
+               // For an installed app whose file has been moved or is no 
longer available, do not
+               // perform file moving. Instead, attempt to try to complete the 
uninstallation without 
+               // regard to app's file.
+               if (this.getAppFile() != null) {
+                       // Check if the app is inside the directory containing 
currently installed apps.
+                       // If so, prepare to move it to the uninstalled 
directory.
+                       File appParentDirectory = 
this.getAppFile().getParentFile();
+                       try {
+                               // Obtain the path of the "uninstalled apps" 
subdirectory.
+                               String uninstalledAppsPath = 
appManager.getUninstalledAppsPath();
                                
-                               // Use the Apache commons library to copy over 
the file, overwriting existing files.
-                               try {
-                                       
FileUtils.copyFileToDirectory(this.getAppFile(), new File(uninstalledAppsPath));
-                               } catch (IOException e) {
-                                       throw new AppUninstallException("Unable 
to move file: " + e.getMessage());
+                               if 
(appParentDirectory.getCanonicalPath().equals(
+                                               
appManager.getInstalledAppsPath())) {
+                                       
+                                       // Use the Apache commons library to 
copy over the file, overwriting existing files.
+                                       try {
+                                               
FileUtils.copyFileToDirectory(this.getAppFile(), new File(uninstalledAppsPath));
+                                       } catch (IOException e) {
+                                               throw new 
AppUninstallException("Unable to move file: " + e.getMessage());
+                                       }
+                                       
+                                       // Delete the source file after the 
copy operation
+                                       String fileName = 
this.getAppFile().getName();
+                                       this.getAppFile().delete();
+                                       this.setAppFile(new 
File(uninstalledAppsPath + File.separator + fileName));                         
    
                                }
-                               
-                               // Delete the source file after the copy 
operation
-                               String fileName = this.getAppFile().getName();
-                               this.getAppFile().delete();
-                               this.setAppFile(new File(uninstalledAppsPath + 
File.separator + fileName));                             
+                       } catch (IOException e) {
+                               throw new AppUninstallException("Unable to 
obtain path: " + e.getMessage());
                        }
-               } catch (IOException e) {
-                       throw new AppUninstallException("Unable to obtain path: 
" + e.getMessage());
                }
        }
        

Modified: 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
===================================================================
--- 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
  2012-05-24 21:21:57 UTC (rev 29346)
+++ 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
  2012-05-24 23:05:06 UTC (rev 29347)
@@ -1,6 +1,7 @@
 package org.cytoscape.app.internal.manager;
 
 import java.io.File;
+import java.io.FileFilter;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.Date;
@@ -9,7 +10,9 @@
 import java.util.concurrent.Executors;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOCase;
 import org.apache.commons.io.monitor.FileAlterationListener;
+import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
 import org.apache.commons.io.monitor.FileAlterationMonitor;
 import org.apache.commons.io.monitor.FileAlterationObserver;
 import org.cytoscape.app.AbstractCyApp;
@@ -19,6 +22,7 @@
 import org.cytoscape.app.internal.exception.AppInstallException;
 import org.cytoscape.app.internal.exception.AppParsingException;
 import org.cytoscape.app.internal.exception.AppUninstallException;
+import org.cytoscape.app.internal.manager.App.AppStatus;
 import org.cytoscape.app.internal.net.WebQuerier;
 import org.cytoscape.application.CyApplicationConfiguration;
 
@@ -73,6 +77,31 @@
        
        private FileAlterationMonitor fileAlterationMonitor;
        
+       /**
+        * A {@link FileFilter} that accepts only files in the first depth 
level of a given directory
+        */
+       private class SingleLevelFileFilter implements FileFilter {
+
+               private File parentDirectory;
+               
+               public SingleLevelFileFilter(File parentDirectory) {
+                       this.parentDirectory = parentDirectory;
+                       
+               }
+               
+               @Override
+               public boolean accept(File pathName) {
+                       if (!pathName.getParentFile().equals(parentDirectory)) {
+                               return false;
+                       } else if (pathName.isDirectory()) {
+                               return false;
+                       }
+                       
+                       return true;
+               }
+               
+       }
+       
        public AppManager(CyAppAdapter appAdapter, CyApplicationConfiguration 
applicationConfiguration, final WebQuerier webQuerier) {
                this.applicationConfiguration = applicationConfiguration;
                this.appAdapter = appAdapter;
@@ -96,56 +125,136 @@
                // FileAlterationListener listener;
                fileAlterationMonitor = new FileAlterationMonitor(600);
                
-               FileAlterationObserver fileAlterationObserver = new 
FileAlterationObserver(getInstalledAppsPath());
-               try {
-                       fileAlterationObserver.initialize();
-               } catch (Exception e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
-               fileAlterationObserver.addListener(new FileAlterationListener() 
{
-                       
+               File installedAppsPath = new File(getInstalledAppsPath());
+               File uninstalledAppsPath = new File(getUninstalledAppsPath());
+               
+               FileAlterationObserver installAlterationObserver = new 
FileAlterationObserver(
+                               installedAppsPath, new 
SingleLevelFileFilter(installedAppsPath), IOCase.SYSTEM);
+               FileAlterationObserver uninstallAlterationObserver = new 
FileAlterationObserver(
+                               uninstalledAppsPath, new 
SingleLevelFileFilter(uninstalledAppsPath), IOCase.SYSTEM);
+               
+               installAlterationObserver.addListener(new 
FileAlterationListenerAdaptor() {
                        @Override
-                       public void onStop(FileAlterationObserver observer) {
-                       }
-                       
-                       @Override
-                       public void onStart(FileAlterationObserver observer) {
-                       }
-                       
-                       @Override
                        public void onFileDelete(File file) {
-                               System.out.println("File deleted");
+                               System.out.println("Install directory file 
deleted");
+                               
+                               try {
+                                       String canonicalPath = 
file.getCanonicalPath();
+                                       
+                                       for (App app : apps) {
+                                               File appFile = app.getAppFile();
+                                               
+                                               if (appFile != null 
+                                                               && 
appFile.getCanonicalPath().equals(canonicalPath)) {
+                                                       // 
app.setStatus(AppStatus.TO_BE_UNINSTALLED);
+                                                       
+                                                       // TODO: call 
app.setFile(null), prevent re-installation of this app
+                                                       // as the file has been 
moved
+                                                       
+                                                       app.setAppFile(null);
+                                                       
+                                                       try {
+                                                               
uninstallApp(app);
+                                                       } catch 
(AppUninstallException e) {
+                                                               // TODO 
Auto-generated catch block
+                                                               
e.printStackTrace();
+                                                       }
+                                                       
+                                                       // 
fireAppsChangedEvent();
+                                               }
+                                       }
+                               } catch (IOException e) {
+                                       // TODO Auto-generated catch block
+                                       e.printStackTrace();
+                               }
                        }
                        
                        @Override
                        public void onFileCreate(File file) {
-                               System.out.println("File created");
+                               System.out.println("Install directory file 
created");
+                               
+                               App parsedApp = null;
+                               try {
+                                       parsedApp = appParser.parseApp(file);
+                                       installApp(parsedApp);
+                                       
+                                       System.out.println("Installed: " + 
parsedApp.getAppName());
+                               } catch (AppParsingException e) {
+                                       System.out.println("Failed to parse: " 
+ file.getName());
+                               } catch (AppInstallException e) {
+                                       System.out.println("Failed to install: 
" + parsedApp.getAppName());
+                               }
+                       
                        }
                        
                        @Override
                        public void onFileChange(File file) {
-                               System.out.println("File changed");
+                               System.out.println("Install directory file 
changed");
                        }
-                       
+               });
+               
+               uninstallAlterationObserver.addListener(new 
FileAlterationListenerAdaptor() {
                        @Override
-                       public void onDirectoryDelete(File directory) {
-                               System.out.println("File changed");
+                       public void onFileDelete(File file) {
+                               System.out.println("Uninstall directory file 
deleted");
+                               
+                               try {
+                                       String canonicalPath = 
file.getCanonicalPath();
+                                       
+                                       Set<App> appsToBeRemoved = new 
HashSet<App>();
+                                       
+                                       for (App app : apps) {
+                                               File appFile = app.getAppFile();
+                                               
+                                               // If the app was uninstalled 
and was moved from the uninstalled apps
+                                               // directory, remove it from 
the app manager
+                                               if (appFile != null 
+                                                               && 
appFile.getCanonicalPath().equals(canonicalPath)
+                                                               && 
app.getStatus() == AppStatus.UNINSTALLED) {
+                                                       
appsToBeRemoved.add(app);
+                                               }
+                                               
+                                               // TODO: Currently keeps the 
app registered to the app manager
+                                               // if its state was 
about-to-uninstall, perhaps need to 
+                                               // disable app re-installing as 
the file is no longer there.
+                                               // Possibly do by calling 
app.setFile(null)
+                                       }
+                                       
+                                       for (App appToBeRemoved : 
appsToBeRemoved) {
+                                               removeApp(appToBeRemoved);
+                                       }
+                                       
+                                       if (appsToBeRemoved.size() > 0) {
+                                               fireAppsChangedEvent(); 
+                                       }
+                               } catch (IOException e) {
+                                       // TODO Auto-generated catch block
+                                       e.printStackTrace();
+                               }
                        }
                        
                        @Override
-                       public void onDirectoryCreate(File directory) {
-                               System.out.println("Directory created");
+                       public void onFileCreate(File file) {
+                               // Do nothing if a file is added to the 
uninstalled apps directory
                        }
                        
                        @Override
-                       public void onDirectoryChange(File directory) {
-                               System.out.println("Directory changed");
+                       public void onFileChange(File file) {
+                               System.out.println("Uninstall directory file 
changed");
                        }
                });
                
-               fileAlterationMonitor.addObserver(fileAlterationObserver);
                
+               try {
+                       installAlterationObserver.initialize();
+                       uninstallAlterationObserver.initialize();
+                       
fileAlterationMonitor.addObserver(installAlterationObserver);
+                       
fileAlterationMonitor.addObserver(uninstallAlterationObserver);
+                       fileAlterationMonitor.start();
+               } catch (Exception e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
        }
        
        public CyAppAdapter getAppAdapter() {
@@ -201,10 +310,7 @@
                app.install(this);
                
                // Let the listeners know that an app has been installed
-               for (AppsChangedListener appListener : appListeners) {
-                       AppsChangedEvent appEvent = new AppsChangedEvent(this);
-                       appListener.appsChanged(appEvent);
-               }
+               fireAppsChangedEvent();
        }
        
        /**
@@ -223,12 +329,16 @@
                app.uninstall(this);
                
                // Let the listeners know that an app has been uninstalled
+               fireAppsChangedEvent();
+       }
+       
+       private void fireAppsChangedEvent() {
+               AppsChangedEvent appEvent = new AppsChangedEvent(this);
                for (AppsChangedListener appListener : appListeners) {
-                       AppsChangedEvent appEvent = new AppsChangedEvent(this);
                        appListener.appsChanged(appEvent);
                }
        }
-               
+       
        /**
         * Return the set of all apps registered to this app manager.
         * @return The set of all apps registered to this app manager.

Modified: 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/ui/CurrentlyInstalledAppsPanel.java
===================================================================
--- 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/ui/CurrentlyInstalledAppsPanel.java
      2012-05-24 21:21:57 UTC (rev 29346)
+++ 
csplugins/trunk/toronto/yuedong/app_manager/impl/app-impl/src/main/java/org/cytoscape/app/internal/ui/CurrentlyInstalledAppsPanel.java
      2012-05-24 23:05:06 UTC (rev 29347)
@@ -3,8 +3,10 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import javax.swing.SwingUtilities;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
+import javax.swing.event.TableModelEvent;
 import javax.swing.table.DefaultTableModel;
 
 import org.cytoscape.app.internal.event.AppsChangedEvent;
@@ -193,8 +195,10 @@
        int[] selectedRows = appsAvailableTable.getSelectedRows();
        
         for (int index = 0; index < selectedRows.length; index++) {
-               App app = (App) 
appsAvailableTable.getModel().getValueAt(selectedRows[index], 0);
                
+               App app = (App) appsAvailableTable.getModel().getValueAt(
+                               
appsAvailableTable.convertRowIndexToModel(selectedRows[index]), 0);
+               
                selectedApps.add(app);
         }
        
@@ -210,26 +214,33 @@
                        @Override
                        public void appsChanged(AppsChangedEvent event) {
                                
-                               Set<App> selectedApps = getSelectedApps();
-                               
-                               // Clear table
-                               DefaultTableModel tableModel = 
(DefaultTableModel) appsAvailableTable.getModel();
-                               for (int rowIndex = tableModel.getRowCount() - 
1; rowIndex >= 0; rowIndex--) {
-                                       tableModel.removeRow(rowIndex);
-                               }
-                               
-                               // Re-populate table
-                               populateTable();
+                               SwingUtilities.invokeLater(new Runnable() {
 
-                               // Update labels
-                               updateLabels();
+                                       @Override
+                                       public void run() {
+                                               Set<App> selectedApps = 
getSelectedApps();
+                                               
+                                               // Clear table
+                                               DefaultTableModel tableModel = 
(DefaultTableModel) appsAvailableTable.getModel();
+                                               for (int rowIndex = 
tableModel.getRowCount() - 1; rowIndex >= 0; rowIndex--) {
+                                                       
tableModel.removeRow(rowIndex);
+                                               }
+                                               
+                                               // Re-populate table
+                                               populateTable();
+                                               
+                                               // Update labels
+                                               updateLabels();
 
-                               // Re-select previously selected apps
-                               for (int rowIndex = 0; rowIndex < 
tableModel.getRowCount(); rowIndex++) {
-                                       if 
(selectedApps.contains(tableModel.getValueAt(rowIndex, 0))) {
-                                               
appsAvailableTable.addRowSelectionInterval(rowIndex, rowIndex);
+                                               // Re-select previously 
selected apps
+                                               for (int rowIndex = 0; rowIndex 
< tableModel.getRowCount(); rowIndex++) {
+                                                       if 
(selectedApps.contains(tableModel.getValueAt(rowIndex, 0))) {
+                                                               
appsAvailableTable.addRowSelectionInterval(rowIndex, rowIndex);
+                                                       }
+                                               }
                                        }
-                               }
+                               });
+                               
                        }
        };
        

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to