Author: paperwing
Date: 2012-08-03 16:02:32 -0700 (Fri, 03 Aug 2012)
New Revision: 30099

Modified:
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebApp.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebQuerier.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/ui/InstallFromStorePanel.java
Log:
refs #1272 Download site combo box keeps track of entered URLs until Cytoscape 
exits, can now switch between the 2 current app store sites: 
http://apps3.nrnb.org/ and http://apps.cytoscape.org, though they have 
identical app databases. Added method to compute SHA-512 digest for 
vertification before install and update, started method to check a given app 
store url for app updates.

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
 2012-08-03 22:54:29 UTC (rev 30098)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/App.java
 2012-08-03 23:02:32 UTC (rev 30099)
@@ -72,6 +72,11 @@
         */
        private boolean officialNameObtained;
        
+       /**
+        * The SHA-512 checksum of the app file, in format sha512:0a516c..
+        */
+       private String sha512Checksum;
+       
        private AppStatus status;
        
        /**
@@ -551,6 +556,10 @@
                return compatibleVersions;
        }
        
+       public String getSha512Checksum() {
+               return sha512Checksum;
+       }
+       
        public URL getAppStoreUrl() {
                return appStoreUrl;
        }
@@ -603,6 +612,10 @@
                this.compatibleVersions = compatibleVersions;
        }
        
+       public void setSha512Checksum(String sha512Checksum) {
+               this.sha512Checksum = sha512Checksum;
+       }
+       
        public void setAppStoreUrl(URL appStoreURL) {
                this.appStoreUrl = appStoreURL;
        }

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
  2012-08-03 22:54:29 UTC (rev 30098)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/manager/AppManager.java
  2012-08-03 23:02:32 UTC (rev 30099)
@@ -2,7 +2,14 @@
 
 import java.io.File;
 import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
@@ -520,6 +527,12 @@
        public void installApp(App app) throws AppInstallException {
                
                try {
+                       System.out.println(getChecksum(app.getAppFile()));
+               } catch (ChecksumException e) {
+                       System.out.println(e.getMessage());
+               }
+               
+               try {
                        app.moveAppFile(this, new File(getInstalledAppsPath()));
                } catch (IOException e) {
                        throw new AppInstallException("Unable to move app file, 
" + e.getMessage());
@@ -883,14 +896,67 @@
                appListeners.add(appListener);
        }
        
-       public  void removeAppListener(AppsChangedListener appListener) {
+       public void removeAppListener(AppsChangedListener appListener) {
                appListeners.remove(appListener);
        }
        
+       public class ChecksumException extends Exception {
+               private static final long serialVersionUID = 
7022699404764909882L;
+               
+               public ChecksumException(String text) {
+                       super(text);
+               }
+       }
+       
        /**
-        * Install apps from the local storage directory containing previously 
installed apps.
+        * Obtain the SHA-512 checksum of a file, in the following format: 
sha512:3c1c..
+        * @param file The file to find the checksum
+        * @return The SHA-512 checksum, in format: sha512:e1..
+        * @throws ChecksumException If unable to obtain SHA-512 algorithm 
implementation,
+        * file does not exist, or IO error while reading
         */
-       public void installAppsFromDirectory() {
-               installAppsInDirectory(new File(getInstalledAppsPath()), false);
+       public String getChecksum(File file) throws ChecksumException {
+               MessageDigest messageDigest;
+                
+               try {
+                       messageDigest = MessageDigest.getInstance("SHA-512");
+               } catch (NoSuchAlgorithmException e) {
+                       throw new ChecksumException("Unable to obtain SHA-512 
algorithm implementation");
+               }
+
+               InputStream inputStream;
+               try {
+                       inputStream = new FileInputStream(file);
+               } catch (FileNotFoundException e) {
+                       throw new ChecksumException("File " + 
file.getAbsolutePath() + " does not exist.");
+               }
+                
+               try {
+                       inputStream = new DigestInputStream(inputStream, 
messageDigest);
+                       
+                       byte[] byteBuffer = new byte[128];
+                       
+                       while (inputStream.available() != 0) {
+                               inputStream.read(byteBuffer);
+                       }
+               } catch (IOException e) {
+                       throw new ChecksumException("Error reading from file " 
+ file + ", " + e.getMessage());
+               } finally {
+                       try {
+                               inputStream.close();
+                       } catch (IOException e) {
+                               logger.warn("Failed to close input stream on 
file " + file.getAbsolutePath() + ", " + e.getMessage());
+                       }
+               }
+               
+               byte[] digest = messageDigest.digest();
+               
+               String result = "";
+               for (int i = 0; i < digest.length; i++) {
+                       // Convert each byte to a 2-digit hex number, adding 
0x100 to obtain the 0 if the byte is 0E
+                       result += Integer.toString((digest[i] & 0xff) + 0x100, 
16).substring(1);
+               }
+               
+           return "sha512:" + result;
        }
 }

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebApp.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebApp.java
  2012-08-03 22:54:29 UTC (rev 30098)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebApp.java
  2012-08-03 23:02:32 UTC (rev 30099)
@@ -56,6 +56,7 @@
                private String releaseDate;
                private String releaseVersion;
                private String compatibleCytoscapeVersions;
+               private String sha512Checksum;
                
                @Override
                public int compareTo(Release other) {
@@ -78,6 +79,9 @@
                        return compatibleCytoscapeVersions;
                }
                
+               public String getSha512Checksum() {
+                       return sha512Checksum;
+               }
                
                public void setRelativeUrl(String relativeUrl) {
                        this.relativeUrl = relativeUrl;
@@ -94,6 +98,10 @@
                public void setCompatibleCytoscapeVersions(String 
compatibleCytoscapeVersions) {
                        this.compatibleCytoscapeVersions = 
compatibleCytoscapeVersions;
                }
+               
+               public void setSha512Checksum(String checksum) {
+                       this.sha512Checksum = checksum;
+               }
 
        }
        

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebQuerier.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebQuerier.java
      2012-08-03 22:54:29 UTC (rev 30098)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/WebQuerier.java
      2012-08-03 23:02:32 UTC (rev 30099)
@@ -21,6 +21,7 @@
 
 import org.apache.commons.io.IOUtils;
 import org.cytoscape.app.internal.exception.AppDownloadException;
+import org.cytoscape.app.internal.manager.App;
 import org.cytoscape.app.internal.manager.AppManager;
 import org.cytoscape.app.internal.net.WebQuerier.AppTag;
 import org.cytoscape.app.internal.util.DebugHelper;
@@ -61,7 +62,7 @@
        // private Map<String, AppTag> appTags;
        
        /**
-        * A reference to the result obtained by the last successful query for 
all available apps 
+        * A reference to the result obtained by the last successful query for 
all available apps
         * to this app store URL.
         */
        private Map<String, Set<WebApp>> appsByUrl;
@@ -192,7 +193,7 @@
                return DEFAULT_APP_STORE_URL;
        }
        
-       public String getCurrentAppStoreUrl(String url) {
+       public String getCurrentAppStoreUrl() {
                return currentAppStoreUrl;
        }
        
@@ -259,7 +260,7 @@
                String jsonResult = null;
                try {
                        // Obtain information about the app from the website
-                       jsonResult = query(DEFAULT_APP_STORE_URL + 
"backend/all_apps");
+                       jsonResult = query(currentAppStoreUrl + 
"backend/all_apps");
                        
                        // Parse the JSON result
                        JSONArray jsonArray = new JSONArray(jsonResult);
@@ -331,9 +332,10 @@
                                                        
                                                        WebApp.Release release 
= new WebApp.Release();
                                                        
-                                                       
release.setRelativeUrl(jsonRelease.get("release_download_url").toString());
-                                                       
release.setReleaseDate(jsonRelease.get("created_iso").toString());
-                                                       
release.setReleaseVersion(jsonRelease.get("version").toString());
+                                                       
release.setRelativeUrl(jsonRelease.optString("release_download_url"));
+                                                       
release.setReleaseDate(jsonRelease.optString("created_iso"));
+                                                       
release.setReleaseVersion(jsonRelease.optString("version"));                    
                                
+                                                       
release.setSha512Checksum(jsonRelease.optString("hexchecksum"));
                                                        
                                                        keyName = "works_with";
                                                        if 
(jsonRelease.has(keyName)) {
@@ -466,7 +468,7 @@
                        
                        URL downloadUrl = null;
                        try {
-                               downloadUrl = new URL(DEFAULT_APP_STORE_URL + 
releaseToDownload.getRelativeUrl());
+                               downloadUrl = new URL(currentAppStoreUrl + 
releaseToDownload.getRelativeUrl());
                        } catch (MalformedURLException e) {
                                throw new AppDownloadException("Unable to 
obtain URL for version " + version 
                                                + " of the release for " + 
webApp.getFullName());
@@ -524,11 +526,30 @@
                return compatibleReleases;
        }
        
-       
        public Set<WebApp> getAppsByTag(String tagName) {
                // Query for apps (which includes tag information) if not done 
so
                Set<WebApp> webApps = getAllApps();
                
                return appsByTagNameByUrl.get(currentAppStoreUrl).get(tagName);
        }
+       
+       public Set<Update> checkForUpdates(Set<App> apps) {
+               Set<Update> updates = new HashSet<Update>();
+               
+               for (App app : apps) {
+                       
+               }
+               
+               return updates;
+       }
+       
+       private Update checkForUpdate(App app, String url) {
+               Set<WebApp> urlApps = appsByUrl.get(url);
+               
+               if (urlApps != null) {
+                       
+               }
+               
+               return null;
+       }
 }

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/ui/InstallFromStorePanel.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/ui/InstallFromStorePanel.java
    2012-08-03 22:54:29 UTC (rev 30098)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/ui/InstallFromStorePanel.java
    2012-08-03 23:02:32 UTC (rev 30099)
@@ -4,6 +4,9 @@
 import java.awt.Container;
 import java.awt.Desktop;
 import java.awt.Font;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
@@ -15,6 +18,8 @@
 import java.util.List;
 import java.util.Set;
 
+import javax.swing.ComboBoxEditor;
+import javax.swing.DefaultComboBoxModel;
 import javax.swing.JFileChooser;
 import javax.swing.JTree;
 import javax.swing.SwingUtilities;
@@ -106,15 +111,20 @@
                
                setupTextFieldListener();
        
-               taskManager.execute(new TaskIterator(new Task() {
+               queryForApps();
+    }
+    
+    // Queries the currently set app store url for available apps.
+    private void queryForApps() {
+       taskManager.execute(new TaskIterator(new Task() {
                        
                        // Obtain information for all available apps, then 
append tag information
                        @Override
                        public void run(TaskMonitor taskMonitor) throws 
Exception {
-                               taskMonitor.setTitle("Obtaining Apps from App 
Store");
-                               
                                WebQuerier webQuerier = 
appManager.getWebQuerier();
                        
+                               taskMonitor.setTitle("Obtaining apps from: " + 
webQuerier.getCurrentAppStoreUrl());
+                               
                                taskMonitor.setStatusMessage("Getting available 
apps");
                                Set<WebApp> availableApps = 
webQuerier.getAllApps();
                        
@@ -233,7 +243,22 @@
         downloadSiteLabel.setText("Download Site:");
 
         downloadSiteComboBox.setEditable(true);
-        downloadSiteComboBox.setModel(new javax.swing.DefaultComboBoxModel(new 
String[] { "http://apps3.nrnb.org/"; }));
+        downloadSiteComboBox.setModel(new javax.swing.DefaultComboBoxModel(new 
String[] { "http://apps3.nrnb.org/";, "http://apps.cytoscape.org/"; }));
+        downloadSiteComboBox.addItemListener(new java.awt.event.ItemListener() 
{
+            public void itemStateChanged(java.awt.event.ItemEvent evt) {
+                downloadSiteComboBoxItemStateChanged(evt);
+            }
+        });
+        downloadSiteComboBox.addActionListener(new 
java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                downloadSiteComboBoxActionPerformed(evt);
+            }
+        });
+        downloadSiteComboBox.addKeyListener(new java.awt.event.KeyAdapter() {
+            public void keyPressed(java.awt.event.KeyEvent evt) {
+                downloadSiteComboBoxKeyPressed(evt);
+            }
+        });
 
         closeButton.setText("Close");
         closeButton.addActionListener(new java.awt.event.ActionListener() {
@@ -293,6 +318,57 @@
                 .addContainerGap())
         );
 
+        // Add a key listener to the download site combo box to listen for the 
enter key event
+        final WebQuerier webQuerier = this.appManager.getWebQuerier();
+        
+        
downloadSiteComboBox.getEditor().getEditorComponent().addKeyListener(new 
KeyAdapter() {
+                       
+                       @Override
+                       public void keyPressed(KeyEvent e) {
+                               final ComboBoxEditor editor = 
downloadSiteComboBox.getEditor();
+                       final Object selectedValue = editor.getItem();
+                               
+                               if (e.isActionKey() || e.getKeyCode() == 
KeyEvent.VK_ENTER) {
+                                       
+                               if (downloadSiteComboBox.getModel() instanceof 
DefaultComboBoxModel
+                                               && selectedValue != null) {
+                                       final DefaultComboBoxModel 
comboBoxModel = (DefaultComboBoxModel) downloadSiteComboBox.getModel();
+                               
+                                       SwingUtilities.invokeLater(new 
Runnable() {
+                                               
+                                               @Override
+                                               public void run() {
+                                                       boolean 
selectedAlreadyInList = false;
+                                               
+                                                       for (int i = 0; i < 
comboBoxModel.getSize(); i++) {
+                                                               Object 
listElement = comboBoxModel.getElementAt(i);
+                                                               
+                                                               if 
(listElement.equals(selectedValue)) {
+                                                                       
selectedAlreadyInList = true;
+                                                                       
+                                                                       break;  
+                                                               }
+                                                       }
+                                                       
+                                                       if 
(!selectedAlreadyInList) {
+                                                               
comboBoxModel.insertElementAt(selectedValue, 1);
+                                                               
+                                                               
editor.setItem(selectedValue);
+                                                       }
+                                               }
+                                               
+                                       });
+                               }
+                               
+                               if (webQuerier.getCurrentAppStoreUrl() != 
selectedValue.toString()) {
+                                       
webQuerier.setCurrentAppStoreUrl(selectedValue.toString());
+                                       
+                                       queryForApps();
+                               }
+                               }
+                       }
+               });
+        
         // Make the JTextPane render HTML using the default UI font
         Font font = UIManager.getFont("Label.font");
         String bodyRule = "body { font-family: " + font.getFamily() + "; " +
@@ -623,4 +699,57 @@
     private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        ((javax.swing.JDialog)InstallFromStorePanel.this.parent).dispose();
     }
+    
+    private void downloadSiteComboBoxItemStateChanged(java.awt.event.ItemEvent 
evt) {
+    }
+
+    private void 
downloadSiteComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
+       
+       final Object selected = downloadSiteComboBox.getSelectedItem();
+       
+       if (downloadSiteComboBox.getModel() instanceof DefaultComboBoxModel
+                       && selected != null) {
+               final DefaultComboBoxModel comboBoxModel = 
(DefaultComboBoxModel) downloadSiteComboBox.getModel();
+       
+               SwingUtilities.invokeLater(new Runnable() {
+                       
+                       @Override
+                       public void run() {
+                               boolean selectedAlreadyInList = false;
+                       
+                               for (int i = 0; i < comboBoxModel.getSize(); 
i++) {
+                                       Object listElement = 
comboBoxModel.getElementAt(i);
+                                       
+                                       if (listElement.equals(selected)) {
+                                               selectedAlreadyInList = true;
+                                               
+                                               if (i > 0) {
+                                                       // 
comboBoxModel.removeElementAt(i);
+                                                       // 
comboBoxModel.insertElementAt(listElement, 1);
+                                               }
+                                               
+                                               break;  
+                                       }
+                               }
+                               
+                               if (!selectedAlreadyInList) {
+                                       comboBoxModel.insertElementAt(selected, 
1);
+                               }
+                       }
+                       
+               });
+               
+               if (appManager.getWebQuerier().getCurrentAppStoreUrl() != 
selected.toString()) {
+                       
appManager.getWebQuerier().setCurrentAppStoreUrl(selected.toString());
+                       
+                       queryForApps();
+               }
+       }
+       
+       
+   
+    }
+    
+    private void downloadSiteComboBoxKeyPressed(java.awt.event.KeyEvent evt) {
+    }
 }

-- 
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