Author: noelr
Date: 2009-08-13 03:01:38 -0700 (Thu, 13 Aug 2009)
New Revision: 17805

Added:
   cytoscape/trunk/tests/cytoscape/data/readers/CytoscapeSessionReaderTest.java
Modified:
   cytoscape/trunk/src/cytoscape/data/ImportHandler.java
   cytoscape/trunk/src/cytoscape/data/servers/BioDataServer.java
   cytoscape/trunk/src/cytoscape/data/writers/CytoscapeSessionWriter.java
   cytoscape/trunk/src/cytoscape/plugin/InstallablePlugin.java
   cytoscape/trunk/src/cytoscape/plugin/PluginManager.java
   cytoscape/trunk/src/cytoscape/util/URLUtil.java
   cytoscape/trunk/src/cytoscape/util/ZipUtil.java
   cytoscape/trunk/src/cytoscape/visual/CalculatorIO.java
Log:
Third batch of fixes for Bug 1988
These are the files that required both trivial and non-trivial alteration


Modified: cytoscape/trunk/src/cytoscape/data/ImportHandler.java
===================================================================
--- cytoscape/trunk/src/cytoscape/data/ImportHandler.java       2009-08-13 
04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/data/ImportHandler.java       2009-08-13 
10:01:38 UTC (rev 17805)
@@ -475,42 +475,51 @@
                BufferedReader in = null;
 
                out = new BufferedWriter(new FileWriter(tmpFile));
-               in = new BufferedReader(new 
InputStreamReader(conn.getInputStream()));
+        try {
+            in = new BufferedReader(new 
InputStreamReader(conn.getInputStream()));
+            try {
+                String inputLine = null;
+                double percent = 0.0d;
 
-               String inputLine = null;
-               double percent = 0.0d;
+                while ((inputLine = in.readLine()) != null) {
+                    progressCount += inputLine.length();
 
-               while ((inputLine = in.readLine()) != null) {
-                       progressCount += inputLine.length();
+                    //  Report on Progress
+                    if (taskMonitor != null) {
+                        percent = ((double) progressCount / maxCount) * 100.0;
 
-                       //  Report on Progress
-                       if (taskMonitor != null) {
-                               percent = ((double) progressCount / maxCount) * 
100.0;
+                        if (maxCount == -1) { // file size unknown
+                            percent = -1;
+                        }
 
-                               if (maxCount == -1) { // file size unknown
-                                       percent = -1;
-                               }
+                        JTask jTask = (JTask) taskMonitor;
 
-                               JTask jTask = (JTask) taskMonitor;
+                        if (jTask.haltRequested()) { //abort
+                            tmpFile = null;
+                            taskMonitor.setStatus("Canceling the download task 
...");
+                            taskMonitor.setPercentCompleted(100);
 
-                               if (jTask.haltRequested()) { //abort
-                                       tmpFile = null;
-                                       taskMonitor.setStatus("Canceling the 
download task ...");
-                                       taskMonitor.setPercentCompleted(100);
+                            break;
+                        }
 
-                                       break;
-                               }
-
-                               taskMonitor.setPercentCompleted((int) percent);
-                       }
-
-                       out.write(inputLine);
-                       out.newLine();
+                        taskMonitor.setPercentCompleted((int) percent);
+                    }
+                    out.write(inputLine);
+                    out.newLine();
+                }
+            }
+            finally {
+                if (in != null) {
+                    in.close();
+                }
+            }
                }
+        finally {
+            if (out != null) {
+                out.close();
+            }
+        }
 
-               in.close();
-               out.close();
-
                return tmpFile;
        } // End of downloadFromURL()
 }

Modified: cytoscape/trunk/src/cytoscape/data/servers/BioDataServer.java
===================================================================
--- cytoscape/trunk/src/cytoscape/data/servers/BioDataServer.java       
2009-08-13 04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/data/servers/BioDataServer.java       
2009-08-13 10:01:38 UTC (rev 17805)
@@ -219,18 +219,21 @@
        //
        protected boolean checkFileType(final BufferedReader br) throws 
IOException {
                String curLine = null;
+        boolean rv;
 
-               while (null != (curLine = br.readLine())) {
-                       if (curLine.startsWith(OBO_FILE) || 
curLine.startsWith(GENE_ASSOCIATION_FILE)) {
-                               br.close();
+        rv = false;
+        try {
+               while (!rv && (null != (curLine = br.readLine()))) {
+                       if (curLine.startsWith(OBO_FILE) || 
curLine.startsWith(GENE_ASSOCIATION_FILE)) {
+                       rv = true;
+                }
+            }
+        }
+        finally {
+            br.close();
+        }
 
-                               return true;
-                       }
-               }
-
-               br.close();
-
-               return false;
+               return rv;
        }
 
        // 
----------------------------------------------------------------------------------------
@@ -390,30 +393,37 @@
                        // Reader for the ".obo" file
                        BufferedReader oboReader = new BufferedReader(new 
OboOntologyReader(new FileReader(filename)));
 
-                       OntologyFlatFileReader reader = new 
OntologyFlatFileReader(oboReader);
-                       list.add(reader.getOntology());
-                       oboReader.close();
+            try {
+                OntologyFlatFileReader reader = new 
OntologyFlatFileReader(oboReader);
+                list.add(reader.getOntology());
+            }
+            finally {
+                oboReader.close();
+            }
 
                        BufferedReader oboReader2 = new BufferedReader(new 
OboOntologyReader2(new FileReader(filename)));
-                       String line;
-                       String[] parts = null;
+            try {
+                String line;
+                String[] parts = null;
 
-                       while ((line = oboReader2.readLine()) != null) {
-                               parts = line.split("=");
+                while ((line = oboReader2.readLine()) != null) {
+                    parts = line.split("=");
 
-                               if (parts.length == 2) {
-                                       //logger.info("ID = " + parts[0] + ", " 
+ parts[1]);
-                                       if 
(parts[1].equals("biological_process")) {
-                                               ontologyTypeMap.put(parts[0], 
"P");
-                                       } else if 
(parts[1].equals("molecular_function")) {
-                                               ontologyTypeMap.put(parts[0], 
"F");
-                                       } else if 
(parts[1].equals("cellular_component")) {
-                                               ontologyTypeMap.put(parts[0], 
"C");
-                                       }
-                               }
-                       }
-
-                       oboReader2.close();
+                    if (parts.length == 2) {
+                        //logger.info("ID = " + parts[0] + ", " + parts[1]);
+                        if (parts[1].equals("biological_process")) {
+                            ontologyTypeMap.put(parts[0], "P");
+                        } else if (parts[1].equals("molecular_function")) {
+                            ontologyTypeMap.put(parts[0], "F");
+                        } else if (parts[1].equals("cellular_component")) {
+                            ontologyTypeMap.put(parts[0], "C");
+                        }
+                    }
+                }
+            }
+            finally {
+                oboReader2.close();
+            }
                }
 
                return (Ontology[]) list.toArray(new Ontology[0]);
@@ -488,33 +498,43 @@
                                                                                
                       ontologyTypeMap,
                                                                                
                       new FileReader(filename)));
 
-                       BufferedReader ccRd = new BufferedReader(new 
CellularComponentAnnotationReader(taxonName,
-                                                                               
                       ontologyTypeMap,
-                                                                               
                       new FileReader(filename)));
+            try {
+                BufferedReader ccRd = new BufferedReader(new 
CellularComponentAnnotationReader(taxonName,
+                                                                               
                ontologyTypeMap,
+                                                                               
                new FileReader(filename)));
+                try {
+                    BufferedReader mfRd = new BufferedReader(new 
MolecularFunctionAnnotationReader(taxonName,
+                                                                               
                    ontologyTypeMap,
+                                                                               
                    new FileReader(filename)));
 
-                       BufferedReader mfRd = new BufferedReader(new 
MolecularFunctionAnnotationReader(taxonName,
-                                                                               
                       ontologyTypeMap,
-                                                                               
                       new FileReader(filename)));
+                    try {
+                        AnnotationFlatFileReader bpReader = new 
AnnotationFlatFileReader(bpRd, thesaurus, flip);
+                        AnnotationFlatFileReader ccReader = new 
AnnotationFlatFileReader(ccRd, thesaurus, flip);
+                        AnnotationFlatFileReader mfReader = new 
AnnotationFlatFileReader(mfRd, thesaurus, flip);
 
-                       AnnotationFlatFileReader bpReader = new 
AnnotationFlatFileReader(bpRd, thesaurus, flip);
-                       AnnotationFlatFileReader ccReader = new 
AnnotationFlatFileReader(ccRd, thesaurus, flip);
-                       AnnotationFlatFileReader mfReader = new 
AnnotationFlatFileReader(mfRd, thesaurus, flip);
+                        bpAnnotation = bpReader.getAnnotation();
+                        ccAnnotation = ccReader.getAnnotation();
+                        mfAnnotation = mfReader.getAnnotation();
 
-                       bpAnnotation = bpReader.getAnnotation();
-                       ccAnnotation = ccReader.getAnnotation();
-                       mfAnnotation = mfReader.getAnnotation();
+                        bpAnnotation.setOntology(pickOntology(ontologies, 
bpAnnotation));
+                        ccAnnotation.setOntology(pickOntology(ontologies, 
ccAnnotation));
+                        mfAnnotation.setOntology(pickOntology(ontologies, 
mfAnnotation));
 
-                       bpAnnotation.setOntology(pickOntology(ontologies, 
bpAnnotation));
-                       ccAnnotation.setOntology(pickOntology(ontologies, 
ccAnnotation));
-                       mfAnnotation.setOntology(pickOntology(ontologies, 
mfAnnotation));
-
-                       server.addAnnotation(bpAnnotation);
-                       server.addAnnotation(ccAnnotation);
-                       server.addAnnotation(mfAnnotation);
-
-                       bpRd.close();
-                       ccRd.close();
-                       mfRd.close();
+                        server.addAnnotation(bpAnnotation);
+                        server.addAnnotation(ccAnnotation);
+                        server.addAnnotation(mfAnnotation);
+                    }
+                    finally {
+                        mfRd.close();
+                    }
+                }
+                finally {
+                    ccRd.close();
+                }
+            }
+            finally {
+                bpRd.close();
+            }
                }
        } // loadAnnotationFiles2
 
@@ -587,12 +607,16 @@
                        BufferedReader thRd = new BufferedReader(new 
SynonymReader(taxonName,
                                                                                
   new FileReader(filename)));
 
-                       ThesaurusFlatFileReader reader = new 
ThesaurusFlatFileReader(thRd, flip);
-                       thesaurus = reader.getThesaurus();
+            try {
+                ThesaurusFlatFileReader reader = new 
ThesaurusFlatFileReader(thRd, flip);
+                thesaurus = reader.getThesaurus();
 
-                       //thesaurus.dump();
-                       server.addThesaurus(thesaurus.getSpecies(), thesaurus);
-                       thRd.close();
+                //thesaurus.dump();
+                server.addThesaurus(thesaurus.getSpecies(), thesaurus);
+            }
+            finally {
+                thRd.close();
+            }
                }
        } // loadThesaurusFiles
 

Modified: cytoscape/trunk/src/cytoscape/data/writers/CytoscapeSessionWriter.java
===================================================================
--- cytoscape/trunk/src/cytoscape/data/writers/CytoscapeSessionWriter.java      
2009-08-13 04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/data/writers/CytoscapeSessionWriter.java      
2009-08-13 10:01:38 UTC (rev 17805)
@@ -244,16 +244,21 @@
                
                zos = new ZipOutputStream(new 
FileOutputStream(sessionFileName)); 
 
-               for (CyNetwork network : networks)
-                       zipNetwork(network);
-               zipCySession();
-               zipVizmapProps();
-               zipCytoscapeProps();
-               zipBookmarks();
-               zipFileListMap();
+        try {
+            for (CyNetwork network : networks)
+                zipNetwork(network);
+            zipCySession();
+            zipVizmapProps();
+            zipCytoscapeProps();
+            zipBookmarks();
+            zipFileListMap();
+        }
+        finally {
+            if (zos != null) {
+                zos.close();
+            }
+        }
 
-               zos.close();
-
                Cytoscape.firePropertyChange(Cytoscape.SESSION_SAVED, null, 
null);
        }
 
@@ -336,10 +341,15 @@
 
                zos.putNextEntry(new ZipEntry(sessionDir + VIZMAP_FILE) );
 
-               Writer writer = new OutputStreamWriter( zos );
-               CalculatorIO.storeCatalog(catalog, writer);
-
-               zos.closeEntry();
+        try {
+            Writer writer = new OutputStreamWriter( zos );
+            CalculatorIO.storeCatalog(catalog, writer);
+        }
+        finally {
+            if (zos != null) {
+                zos.closeEntry();
+            }
+        }
        }
 
        /**
@@ -349,9 +359,14 @@
        
                zos.putNextEntry(new ZipEntry(sessionDir + CYPROP_FILE) );
 
-               CytoscapeInit.getProperties().store(zos, "Cytoscape Property 
File");
-
-               zos.closeEntry();
+        try {
+            CytoscapeInit.getProperties().store(zos, "Cytoscape Property 
File");
+        }
+        finally {
+            if (zos != null) {
+                zos.closeEntry();
+            }
+        }
        }
 
        /**
@@ -361,10 +376,15 @@
 
                zos.putNextEntry(new ZipEntry(sessionDir + BOOKMARKS_FILE) );
 
-               bookmarks = Cytoscape.getBookmarks();
-               BookmarksUtil.saveBookmark(bookmarks, zos);
-
-               zos.closeEntry();
+        try {
+            bookmarks = Cytoscape.getBookmarks();
+            BookmarksUtil.saveBookmark(bookmarks, zos);
+        }
+        finally {
+            if (zos != null) {
+                zos.closeEntry();
+            }
+        }
        }
 
        /**
@@ -380,18 +400,19 @@
                String xgmmlFile = getValidFileName( network.getTitle() + 
XGMML_EXT );
                CyNetworkView view = 
Cytoscape.getNetworkView(network.getIdentifier());
 
-               zos.putNextEntry(new ZipEntry(sessionDir + xgmmlFile) );
-               Writer writer = new OutputStreamWriter(zos, "UTF-8");
-
-               // Write the XGMML file *without* our graphics attributes
-               // We'll let the Vizmapper handle those
-               XGMMLWriter xgmmlWriter = new XGMMLWriter(network, view, true);
-               xgmmlWriter.write(writer);
-
-               zos.closeEntry();
-
-               writer = null;
-               xgmmlWriter = null;
+               zos.putNextEntry(new ZipEntry(sessionDir + xgmmlFile));
+        Writer writer = new OutputStreamWriter(zos, "UTF-8");
+        try {
+            // Write the XGMML file *without* our graphics attributes
+            // We'll let the Vizmapper handle those
+            XGMMLWriter xgmmlWriter = new XGMMLWriter(network, view, true);
+            xgmmlWriter.write(writer);
+        }
+        finally {
+            if (zos != null) {
+                zos.closeEntry();
+            }
+        }
        }
 
        /**
@@ -418,10 +439,14 @@
 
                zos.putNextEntry(new ZipEntry(sessionDir + CYSESSION_FILE_NAME) 
);
 
-               m.marshal(session, zos);
-
-               zos.closeEntry();
-               m = null;
+        try {
+            m.marshal(session, zos);
+        }
+        finally {
+            if (zos != null) {
+                zos.closeEntry();
+            }
+        }
                session = null;
        }
 
@@ -454,14 +479,25 @@
                                        zos.putNextEntry(new ZipEntry( 
sessionDir + "plugins/" + pluginName + 
                                                                       "/" + 
theFile.getName() ) );
 
-                                       // copy the file contents to the zip 
output stream
-                                       FileInputStream fileIS = new 
FileInputStream(theFile);
-                                       int numRead = 0;
-                               while ((numRead = fileIS.read(buf)) > -1)
-                               zos.write(buf, 0, numRead);
-                                       fileIS.close();
-
-                                       zos.closeEntry();
+                    try {
+                        // copy the file contents to the zip output stream
+                        FileInputStream fileIS = new FileInputStream(theFile);
+                        try {
+                            int numRead = 0;
+                            while ((numRead = fileIS.read(buf)) > -1)
+                                zos.write(buf, 0, numRead);
+                        }
+                        finally {
+                            if (fileIS != null) {
+                                fileIS.close();
+                            }
+                        }
+                    }
+                    finally {
+                        if (zos != null) {
+                            zos.closeEntry();
+                        }
+                    }
                                }
                        }
                }

Modified: cytoscape/trunk/src/cytoscape/plugin/InstallablePlugin.java
===================================================================
--- cytoscape/trunk/src/cytoscape/plugin/InstallablePlugin.java 2009-08-13 
04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/plugin/InstallablePlugin.java 2009-08-13 
10:01:38 UTC (rev 17805)
@@ -19,6 +19,7 @@
 import cytoscape.util.URLUtil;
 import cytoscape.util.ZipUtil;
 import cytoscape.logger.CyLogger;
+import java.util.zip.ZipFile;
 
 /**
  * @author skillcoy
@@ -270,8 +271,14 @@
                switch (Type) {
                case JAR:
                        JarFile Jar = new JarFile(FileName);
-                       PluginClassName = 
getManifestAttribute(Jar.getManifest());
-                       Jar.close();
+            try {
+                PluginClassName = getManifestAttribute(Jar.getManifest());
+            }
+            finally {
+                if (Jar != null) {
+                    Jar.close();
+                }
+            }
                        break;
 
                case ZIP:
@@ -285,18 +292,38 @@
                                                                + " does not 
contain any jar files or is not a zip file.");
                        }
 
-                       for (ZipEntry Entry : Entries) {
-                               String EntryName = Entry.getName();
+            ZipFile zf = null;
 
-                               InputStream is = ZipUtil.readFile(FileName, 
EntryName);
-                               JarInputStream jis = new JarInputStream(is);
-                               PluginClassName = 
getManifestAttribute(jis.getManifest());
-                               jis.close();
-                               is.close();
-                       }
+            zf = new ZipFile(FileName);
+            try {
+                for (ZipEntry Entry : Entries) {
+                    String EntryName = Entry.getName();
+
+                    InputStream is = ZipUtil.readFile(zf, EntryName);
+                    try {
+                        JarInputStream jis = new JarInputStream(is);
+                        try {
+                            PluginClassName = 
getManifestAttribute(jis.getManifest());
+                        }
+                        finally {
+                            if (jis != null) {
+                                jis.close();
+                            }
+                        }
+                    }
+                    finally {
+                        if (is != null) {
+                            is.close();
+                        }
+                    }
+                }
+            }
+            finally {
+                zf.close();
+            }
                }
-               ;
-               return PluginClassName;
+
+        return PluginClassName;
        }
 
        /*

Modified: cytoscape/trunk/src/cytoscape/plugin/PluginManager.java
===================================================================
--- cytoscape/trunk/src/cytoscape/plugin/PluginManager.java     2009-08-13 
04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/plugin/PluginManager.java     2009-08-13 
10:01:38 UTC (rev 17805)
@@ -64,6 +64,7 @@
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 /**
  * @author skillcoy
@@ -997,8 +998,14 @@
                switch (Type) {
                case JAR:
                        JarFile Jar = new JarFile(FileName);
-                       PluginClassName = 
getManifestAttribute(Jar.getManifest());
-                       Jar.close();
+            try {
+                PluginClassName = getManifestAttribute(Jar.getManifest());
+            }
+            finally {
+                if (Jar != null) {
+                    Jar.close();
+                }
+            }
                        break;
 
                case ZIP:
@@ -1012,15 +1019,36 @@
                                                                + " does not 
contain any jar files or is not a zip file.");
                        }
 
-                       for (ZipEntry Entry : Entries) {
-                               String EntryName = Entry.getName();
+            ZipFile zf = null;
+            zf = new ZipFile(FileName);
+            try {
+                for (ZipEntry Entry : Entries) {
+                    String EntryName = Entry.getName();
 
-                               InputStream is = ZipUtil.readFile(FileName, 
EntryName);
-                               JarInputStream jis = new JarInputStream(is);
-                               PluginClassName = 
getManifestAttribute(jis.getManifest());
-                               jis.close();
-                               is.close();
+                    InputStream is = ZipUtil.readFile(zf, EntryName);
+                    try {
+                        JarInputStream jis = new JarInputStream(is);
+                        try {
+                            PluginClassName = 
getManifestAttribute(jis.getManifest());
+                        }
+                        finally {
+                            if (jis != null) {
+                                jis.close();
+                            }
+                        }
+                    }
+                    finally {
+                        if (is != null) {
+                            is.close();
+                        }
+                    }
+                }
                        }
+            finally {
+                if (zf != null) {
+                    zf.close();
+                }
+            }
                }
                return PluginClassName;
        }

Modified: cytoscape/trunk/src/cytoscape/util/URLUtil.java
===================================================================
--- cytoscape/trunk/src/cytoscape/util/URLUtil.java     2009-08-13 04:16:54 UTC 
(rev 17804)
+++ cytoscape/trunk/src/cytoscape/util/URLUtil.java     2009-08-13 10:01:38 UTC 
(rev 17805)
@@ -51,10 +51,6 @@
        private static final String JAR = ".jar";
 
        private static int msConnectionTimeout = 2000;
-       /**
-        * 
-        */
-       public static boolean STOP = false;
 
        /**
         * Gets the an input stream given a URL.
@@ -156,57 +152,71 @@
         */
        public static void download(String urlString, File downloadFile,
                        TaskMonitor taskMonitor) throws IOException {
-               URL url = new URL(urlString);
-               InputStream is = null;
-//             Proxy CytoProxyHandler = ProxyHandler.getProxyServer();
-               int maxCount = 0; // -1 if unknown
-               int progressCount = 0;
-//             if (CytoProxyHandler == null) {
-                       URLConnection conn = getURLConnection(url);
-                       //URLConnection conn = url.openConnection();
-                       // Ensure we are reading the real content from url,
-                       // and not some out-of-date cached content:
-                       //conn.setUseCaches(false);
-                       maxCount = conn.getContentLength();
-                       is = conn.getInputStream();
-//             } else {
-//                     URLConnection conn = 
url.openConnection(CytoProxyHandler);
-//                     // Ensure we are reading the real content from url,
-//                     // and not some out-of-date cached content:
-//                     conn.setUseCaches(false);
-//                     conn.setConnectTimeout(msConnectionTimeout);
+        boolean stop = false;
+
+        try {
+               URL url = new URL(urlString);
+               InputStream is = null;
+//          Proxy CytoProxyHandler = ProxyHandler.getProxyServer();
+               int maxCount = 0; // -1 if unknown
+               int progressCount = 0;
+//          if (CytoProxyHandler == null) {
+                URLConnection conn = getURLConnection(url);
+                //URLConnection conn = url.openConnection();
+                // Ensure we are reading the real content from url,
+                // and not some out-of-date cached content:
+                //conn.setUseCaches(false);
+                maxCount = conn.getContentLength();
+                is = conn.getInputStream();
+//             } else {
+//                     URLConnection conn = 
url.openConnection(CytoProxyHandler);
+//             // Ensure we are reading the real content from url,
+//              // and not some out-of-date cached content:
+//                     conn.setUseCaches(false);
+//             conn.setConnectTimeout(msConnectionTimeout);
 //
-//                     maxCount = conn.getContentLength();
-//                     is = conn.getInputStream();
-//             }
-               FileOutputStream os = new FileOutputStream(downloadFile);
-               double percent = 0.0d;
-               byte[] buffer = new byte[1];
-               while (((is.read(buffer)) != -1) && !STOP) {
-                       progressCount += buffer.length;
-                       // Report on Progress
-                       if (taskMonitor != null) {
-                               percent = ((double) progressCount / maxCount) * 
100.0;
-                               if (maxCount == -1) { // file size unknown
-                                       percent = -1;
-                               }
-                               JTask jTask = (JTask) taskMonitor;
-                               if (jTask.haltRequested()) { // abort
-                                       downloadFile = null;
-                                       taskMonitor.setStatus("Canceling the 
download ...");
-                                       taskMonitor.setPercentCompleted(100);
-                                       break;
-                               }
-                               taskMonitor.setPercentCompleted((int) percent);
-                       }
-                       os.write(buffer);
-               }
-               os.flush();
-               os.close();
-               is.close();
-               if (STOP) {
-                       downloadFile.delete();
-               }
+//              maxCount = conn.getContentLength();
+//                     is = conn.getInputStream();
+//             }
+            FileOutputStream os = new FileOutputStream(downloadFile);
+            try {
+                double percent = 0.0d;
+                byte[] buffer = new byte[1];
+                while (((is.read(buffer)) != -1) && !stop) {
+                    progressCount += buffer.length;
+                    // Report on Progress
+                    if (taskMonitor != null) {
+                        percent = ((double) progressCount / maxCount) * 100.0;
+                        if (maxCount == -1) { // file size unknown
+                            percent = -1;
+                        }
+                        JTask jTask = (JTask) taskMonitor;
+                        if (jTask.haltRequested()) { // abort
+                            stop = true;
+                            taskMonitor.setStatus("Canceling the download 
...");
+                            taskMonitor.setPercentCompleted(100);
+                            break;
+                        }
+                        taskMonitor.setPercentCompleted((int) percent);
+                    }
+                    os.write(buffer);
+                }
+                os.flush();
+            }
+            finally {
+                if (os != null) {
+                    os.close();
+                }
+                if (is != null) {
+                    is.close();
+                }
+            }
+        }
+        finally {
+            if (stop) {
+                downloadFile.delete();
+            }
+        }
        }
 
        /**
@@ -220,10 +230,18 @@
                InputStream is = getInputStream(source);
                StringBuffer buffer = new StringBuffer();
                int c;
-               while ((c = is.read()) != -1) {
-                       buffer.append((char) c);
-               }
-               is.close();
+
+        try {
+            while ((c = is.read()) != -1) {
+                buffer.append((char) c);
+            }
+        }
+        finally {
+            if (is != null) {
+                is.close();
+            }
+        }
+
                return buffer.toString();
        }
 }

Modified: cytoscape/trunk/src/cytoscape/util/ZipUtil.java
===================================================================
--- cytoscape/trunk/src/cytoscape/util/ZipUtil.java     2009-08-13 04:16:54 UTC 
(rev 17804)
+++ cytoscape/trunk/src/cytoscape/util/ZipUtil.java     2009-08-13 10:01:38 UTC 
(rev 17805)
@@ -162,45 +162,50 @@
                final byte[] rgb = new byte[5000];
                final ZipOutputStream zipOS = new ZipOutputStream(new 
BufferedOutputStream(new FileOutputStream(zipArchiveName)));
 
-               // Tuning performance
-               zipOS.setMethod(ZipOutputStream.DEFLATED);
+        try {
+            // Tuning performance
+            zipOS.setMethod(ZipOutputStream.DEFLATED);
 
-               if ((compressionLevel >= 0) && (compressionLevel <= 9)) {
-                       zipOS.setLevel(compressionLevel);
-               } else {
-                       zipOS.setLevel(DEF_COMPRESSION_LEVEL);
-               }
+            if ((compressionLevel >= 0) && (compressionLevel <= 9)) {
+                zipOS.setLevel(compressionLevel);
+            } else {
+                zipOS.setLevel(DEF_COMPRESSION_LEVEL);
+            }
 
-               String targetName = "";
+            String targetName = "";
 
-               for (int i = 0; i < fileCount; i++) {
-                       final File file = new File(inputFileDir + 
inputFiles[i]);
-                       targetName = sessionDirName + FS + inputFiles[i];
-                       addEntryToZip(file, targetName, zipOS, crc32, rgb);
-               }
+            for (int i = 0; i < fileCount; i++) {
+                final File file = new File(inputFileDir + inputFiles[i]);
+                targetName = sessionDirName + FS + inputFiles[i];
+                addEntryToZip(file, targetName, zipOS, crc32, rgb);
+            }
 
-               if ((pluginFileMap != null) && (pluginFileMap.size() > 0)) {
-                       Set<String> pluginSet = pluginFileMap.keySet();
+            if ((pluginFileMap != null) && (pluginFileMap.size() > 0)) {
+                Set<String> pluginSet = pluginFileMap.keySet();
 
-                       for (String pluginName : pluginSet) {
-                               List<File> theFileList = (List<File>) 
pluginFileMap.get(pluginName);
+                for (String pluginName : pluginSet) {
+                    List<File> theFileList = (List<File>) 
pluginFileMap.get(pluginName);
 
-                               if ((theFileList == null) || 
(theFileList.size() == 0))
-                                       continue;
+                    if ((theFileList == null) || (theFileList.size() == 0))
+                        continue;
 
-                               for (File theFile : theFileList) {
-                                       if ((theFile == null) || 
(!theFile.exists()))
-                                               continue;
+                    for (File theFile : theFileList) {
+                        if ((theFile == null) || (!theFile.exists()))
+                            continue;
 
-                                       targetName = sessionDirName + FS + 
"plugins" + FS + pluginName + FS
-                                                    + theFile.getName();
-                                       addEntryToZip(theFile, targetName, 
zipOS, crc32, rgb);
-                               }
-                       }
-               }
+                        targetName = sessionDirName + FS + "plugins" + FS + 
pluginName + FS
+                                     + theFile.getName();
+                        addEntryToZip(theFile, targetName, zipOS, crc32, rgb);
+                    }
+                }
+            }
+        }
+        finally {
+            if (zipOS != null) {
+                zipOS.close();
+            }
+        }
 
-               zipOS.close();
-
                // final double stop = System.currentTimeMillis();
                // final double diff = stop - start;
                // CyLogger.getLogger().info("Compression time 3 = " + diff / 
1000 + " sec.");
@@ -225,27 +230,43 @@
                // Set CRC
                FileInputStream fileIS = new FileInputStream(srcFile);
 
-               while ((numRead = fileIS.read(rgb)) > -1) {
-                       crc32.update(rgb, 0, numRead);
-               }
+        try {
+            while ((numRead = fileIS.read(rgb)) > -1) {
+                crc32.update(rgb, 0, numRead);
+            }
+        }
+        finally {
+            if (fileIS != null) {
+                fileIS.close();
+            }
+        }
 
-               fileIS.close();
-
                final ZipEntry zipEntry = new ZipEntry(targetName);
                zipEntry.setSize(srcFile.length());
                zipEntry.setTime(srcFile.lastModified());
                zipEntry.setCrc(crc32.getValue());
                zipOS.putNextEntry(zipEntry);
 
-               // Write the file
-               fileIS = new FileInputStream(srcFile);
+        try {
+            // Write the file
+            fileIS = new FileInputStream(srcFile);
 
-               while ((numRead = fileIS.read(rgb)) > -1) {
-                       zipOS.write(rgb, 0, numRead);
-               }
-
-               fileIS.close();
-               zipOS.closeEntry();
+            try {
+                while ((numRead = fileIS.read(rgb)) > -1) {
+                    zipOS.write(rgb, 0, numRead);
+                }
+            }
+            finally {
+                if (fileIS != null) {
+                    fileIS.close();
+                }
+            }
+        }
+        finally {
+            if (zipOS != null) {
+                zipOS.closeEntry();
+            }
+        }
        }
 
        /**
@@ -265,24 +286,89 @@
         *            clutter from the zip file.
         * @return An InputStream of the zip entry identified by the regular
         *         expression or null if nothing matches.
+     * @throws IOexception
+     * @deprecated
         */
+    @Deprecated
        public static InputStream readFile(String zipName, String fileNameRegEx)
            throws IOException {
                final ZipFile sessionZipFile = new ZipFile(zipName);
-               final Enumeration zipEntries = sessionZipFile.entries();
+        final Enumeration zipEntries = sessionZipFile.entries();
 
-               while (zipEntries.hasMoreElements()) {
-                       final ZipEntry zent = (ZipEntry) 
zipEntries.nextElement();
+        try {
+            while (zipEntries.hasMoreElements()) {
+                final ZipEntry zent = (ZipEntry) zipEntries.nextElement();
 
-                       if (zent.getName().matches(fileNameRegEx)) {
-                               return sessionZipFile.getInputStream(zent);
-                       }
-               }
-               sessionZipFile.close();
+                if (zent.getName().matches(fileNameRegEx)) {
+                    return sessionZipFile.getInputStream(zent);
+                }
+            }
+        }
+        catch (Exception e) {
+            // This is not an ideal way to address bug 1988 for this method,
+            // as sessionZipFile must remain open for the stream on the entry
+            // to be usable.
+            // Therefore sessionZipFile can only be closed explicitly when an
+            // exception has occured.
+            // More importantly sessionZipFile cannot otherwise be closed and
+            // so will remain open until the finalize is called on it.
+            // This is the reason for deprecating this method for one that
+            // allows the enclosing method to timely close the ZipFile object
+            // and otherwise properly manage it's lifetime.
+            if (sessionZipFile != null) {
+                sessionZipFile.close();
+            }
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException)e;
+            }
+            else if (e instanceof IOException) {
+                throw (IOException)e;
+            }
+            else {
+                throw new RuntimeException(e);
+            }
+        }
+
                return null;
        }
        
        /**
+        * Reads a file contained within a zip file and returns an InputStream 
for the
+        * specific file you want from within the zip file.
+        *
+        * @param sessionZipFile
+        *            The ZipFile in which to look for an entry matching
+     *            fileNameRegEx.
+        * @param fileNameRegEx
+        *            A regular expression that identifies the file to be read. 
In
+        *            general this should just be the file name you're looking 
for.
+        *            If more than one file matches the regular expression, 
only the
+        *            first will be returned. If you're looking for a specific 
file
+        *            remeber to build your regular expression correctly. For
+        *            example, if you're looking for the file 'vizmap.props', 
make
+        *            your regular expression '.*vizmap.props' to accomodate any
+        *            clutter from the zip file.
+        * @return An InputStream of the zip entry identified by the regular
+        *         expression or null if nothing matches.
+     * @throws IOexception
+        */
+       public static InputStream readFile(ZipFile sessionZipFile, String 
fileNameRegEx)
+           throws IOException {
+               //final ZipFile sessionZipFile = new ZipFile(zipName);
+        final Enumeration zipEntries = sessionZipFile.entries();
+
+        while (zipEntries.hasMoreElements()) {
+            final ZipEntry zent = (ZipEntry) zipEntries.nextElement();
+
+            if (zent.getName().matches(fileNameRegEx)) {
+                return sessionZipFile.getInputStream(zent);
+            }
+        }
+
+               return null;
+       }
+
+       /**
         * Reads zip file, returns a list of all entries that match the given 
         * regular expression
         * @param zipName
@@ -294,16 +380,23 @@
                List<ZipEntry> Matching = new ArrayList<ZipEntry>();
                
                ZipFile Zip = new ZipFile(zipName);
-               Enumeration Entries = Zip.entries();
-               
-               while (Entries.hasMoreElements()) {
-                       ZipEntry CurrentEntry = (ZipEntry) 
Entries.nextElement();
-                       if (CurrentEntry.getName().matches(fileNameRegEx)) {
-                               Matching.add(CurrentEntry);
-                       }
-               }
-       Zip.close();
-       return Matching;
+        try {
+            Enumeration Entries = Zip.entries();
+
+            while (Entries.hasMoreElements()) {
+                ZipEntry CurrentEntry = (ZipEntry) Entries.nextElement();
+                if (CurrentEntry.getName().matches(fileNameRegEx)) {
+                    Matching.add(CurrentEntry);
+                }
+            }
+        }
+        finally {
+            if (Zip != null) {
+                Zip.close();
+            }
+        }
+
+        return Matching;
        }
 
 
@@ -327,64 +420,90 @@
                double percent = 0.0d;
                
                ZipFile Zip = new ZipFile(zipName);
-               Enumeration Entries = Zip.entries();
-               int BUFFER = 2048;
-               
-               while(Entries.hasMoreElements()) {
-                       ZipEntry CurrentEntry = (ZipEntry)Entries.nextElement();
-                       File ZipFile = new File(unzipDir + 
CurrentEntry.getName());
-                       if (!CurrentEntry.isDirectory()) {
-                               if (ZipFile.getParent() != null) {
-                                       File ParentDirs = new 
File(ZipFile.getParent());
-                                       
-                                       if (!ParentDirs.exists()) {
-                                               ParentDirs.mkdirs();
-                                       }
-                               }
-                       } else { // entry is directory, create and move on
-                               if (!ZipFile.exists()) {
-                                       ZipFile.mkdirs();
-                               }
-                               continue;
-                       }
+        try {
+            Enumeration Entries = Zip.entries();
+            int BUFFER = 2048;
 
-                       InputStream zis = Zip.getInputStream(CurrentEntry);
-                       maxCount = zis.available();
-                       
-                       FileOutputStream fos = new FileOutputStream(ZipFile);
-                       BufferedOutputStream dest = new 
BufferedOutputStream(fos, BUFFER);
+            while(Entries.hasMoreElements()) {
+                ZipEntry CurrentEntry = (ZipEntry)Entries.nextElement();
+                File ZipFile = new File(unzipDir + CurrentEntry.getName());
+                if (!CurrentEntry.isDirectory()) {
+                    if (ZipFile.getParent() != null) {
+                        File ParentDirs = new File(ZipFile.getParent());
 
-                       // write the files to the disk
-                       int count;
-                       byte[] data = new byte[BUFFER];
+                        if (!ParentDirs.exists()) {
+                            ParentDirs.mkdirs();
+                        }
+                    }
+                } else { // entry is directory, create and move on
+                    if (!ZipFile.exists()) {
+                        ZipFile.mkdirs();
+                    }
+                    continue;
+                }
 
-                       while ((count = zis.read(data, 0, BUFFER)) != -1) {
-                               dest.write(data, 0, count);
-                               //  Report on Progress
-                               if (taskMonitor != null) {
-                                       percent = ((double) progressCount / 
maxCount) * 100.0;
-                                       if (maxCount == -1) { // file size 
unknown
-                                               percent = -1;
-                                       }
+                InputStream zis = Zip.getInputStream(CurrentEntry);
+                try {
+                    maxCount = zis.available();
 
-                                       JTask jTask = (JTask) taskMonitor;
-                                       // TODO erm...how?
-                                       if (jTask.haltRequested()) { //abort
-                                               
taskMonitor.setStatus("Canceling the unzip ...");
-                                               
taskMonitor.setPercentCompleted(100);
-                                               break;
-                                       }
-                                       taskMonitor.setPercentCompleted((int) 
percent);
-                               }
-                       }
+                    FileOutputStream fos = new FileOutputStream(ZipFile);
+                    try {
+                        BufferedOutputStream dest = new 
BufferedOutputStream(fos, BUFFER);
 
-                       dest.flush();
-                       dest.close();
-                       zis.close();
-                       
-                       UnzippedFiles.add(ZipFile.getAbsolutePath());
-               }
-               Zip.close();
+                        try {
+                            // write the files to the disk
+                            int count;
+                            byte[] data = new byte[BUFFER];
+
+                            while ((count = zis.read(data, 0, BUFFER)) != -1) {
+                                dest.write(data, 0, count);
+                                //  Report on Progress
+                                if (taskMonitor != null) {
+                                    percent = ((double) progressCount / 
maxCount) * 100.0;
+                                    if (maxCount == -1) { // file size unknown
+                                        percent = -1;
+                                    }
+
+                                    JTask jTask = (JTask) taskMonitor;
+                                    // TODO erm...how?
+                                    if (jTask.haltRequested()) { //abort
+                                        taskMonitor.setStatus("Canceling the 
unzip ...");
+                                        taskMonitor.setPercentCompleted(100);
+                                        break;
+                                    }
+                                    taskMonitor.setPercentCompleted((int) 
percent);
+                                }
+                            }
+
+                            dest.flush();
+                        }
+                        finally {
+                            if (dest != null) {
+                                dest.close();
+                            }
+                        }
+                    }
+                    finally {
+                        if (fos != null) {
+                            fos.close();
+                        }
+                    }
+                }
+                finally {
+                    if (zis != null) {
+                        zis.close();
+                    }
+                }
+
+                UnzippedFiles.add(ZipFile.getAbsolutePath());
+            }
+        }
+        finally {
+            if (Zip != null) {
+                Zip.close();
+            }
+        }
+
                return UnzippedFiles;
        }
 

Modified: cytoscape/trunk/src/cytoscape/visual/CalculatorIO.java
===================================================================
--- cytoscape/trunk/src/cytoscape/visual/CalculatorIO.java      2009-08-13 
04:16:54 UTC (rev 17804)
+++ cytoscape/trunk/src/cytoscape/visual/CalculatorIO.java      2009-08-13 
10:01:38 UTC (rev 17805)
@@ -122,8 +122,12 @@
         */
        public static void storeCatalog(CalculatorCatalog catalog, File 
outFile) throws IOException {
                final Writer writer = new FileWriter(outFile);
-               storeCatalog(catalog, writer);
-               writer.close();
+        try {
+            storeCatalog(catalog, writer);
+        }
+        finally {
+            writer.close();
+        }
        }
 
        /**
@@ -151,44 +155,51 @@
                // get a Properties description of the catalog
                final Properties props = getProperties(catalog);
 
-               // and dump it to a buffer of bytes
-               buffer = new ByteArrayOutputStream();
-               props.store(buffer, header.toString());
-
-               // convert the bytes to a String we can read from
-               reader = new BufferedReader(new 
StringReader(buffer.toString()));
-
                // read all the lines and store them in a container object
                // store the header lines separately so they don't get sorted
                final List<String> headerLines = new ArrayList<String>();
                final List<String> lines = new ArrayList<String>();
 
-               String oneLine = reader.readLine();
+               // and dump it to a buffer of bytes
+               buffer = new ByteArrayOutputStream();
+        try {
+            props.store(buffer, header.toString());
 
-               while (oneLine != null) {
-                       if (oneLine.startsWith("#"))
-                               headerLines.add(oneLine);
-                       else {
-                               boolean test = true;
-                               for (String key : OLD_CALC_KEYS) {
-                                       if (oneLine.toUpperCase().contains(key) 
== false)
-                                               continue;
-                                       else {
-                                               test = false;
-                                               break;
-                                       }
-                               }
+            // convert the bytes to a String we can read from
+            reader = new BufferedReader(new StringReader(buffer.toString()));
 
-                               if (test)
-                                       lines.add(oneLine);
-                       }
+            try {
+                String oneLine = reader.readLine();
 
-                       oneLine = reader.readLine();
-               }
+                while (oneLine != null) {
+                    if (oneLine.startsWith("#"))
+                        headerLines.add(oneLine);
+                    else {
+                        boolean test = true;
+                        for (String key : OLD_CALC_KEYS) {
+                            if (oneLine.toUpperCase().contains(key) == false)
+                                continue;
+                            else {
+                                test = false;
+                                break;
+                            }
+                        }
 
-               buffer.close();
-               reader.close();
+                        if (test)
+                            lines.add(oneLine);
+                    }
 
+                    oneLine = reader.readLine();
+                }
+            }
+            finally {
+                reader.close();
+            }
+        }
+        finally {
+            buffer.close();
+        }
+
                // now sort all the non-header lines
                Collections.sort(lines);
 

Added: 
cytoscape/trunk/tests/cytoscape/data/readers/CytoscapeSessionReaderTest.java
===================================================================
--- 
cytoscape/trunk/tests/cytoscape/data/readers/CytoscapeSessionReaderTest.java    
                            (rev 0)
+++ 
cytoscape/trunk/tests/cytoscape/data/readers/CytoscapeSessionReaderTest.java    
    2009-08-13 10:01:38 UTC (rev 17805)
@@ -0,0 +1,79 @@
+/*
+ File: XGMMLWriterTest.java
+
+ Copyright (c) 2009, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+package cytoscape.data.readers;
+
+import cytoscape.Cytoscape;
+import junit.framework.TestCase;
+
+/**
+ * Tests the CytoscapeSessionReader class.
+ *
+ * @author noel.ruddock
+ */
+public class CytoscapeSessionReaderTest extends TestCase {
+    public void testDummy() throws Exception {
+    }
+
+    // These test work and pass, but are commented out because they disturb
+    // one of the PluginManager tests when run using "ant test"
+    // All tests function undisturbed when run using "ant test-slow"
+//    public void testBug0001929a() throws Exception {
+//             CytoscapeSessionReader sr;
+//
+//     Cytoscape.buildOntologyServer();
+//     sr = new CytoscapeSessionReader("testData/Bug1929TestA.cys", null);
+//        sr.read();
+//    }
+//
+//    public void testBug0001929b() throws Exception {
+//             CytoscapeSessionReader sr;
+//
+//        //new CytoscapeInit().init(null);
+//     Cytoscape.buildOntologyServer();
+//     sr = new CytoscapeSessionReader("testData/Bug1929TestB.cys", null);
+//        sr.read();
+//    }
+//
+//    public void testBug0001929c() throws Exception {
+//             CytoscapeSessionReader sr;
+//
+//        //new CytoscapeInit().init(null);
+//     Cytoscape.buildOntologyServer();
+//     sr = new CytoscapeSessionReader("testData/Bug1929TestC.cys", null);
+//        sr.read();
+//    }
+}


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