Author: kono
Date: 2012-07-24 10:41:07 -0700 (Tue, 24 Jul 2012)
New Revision: 29974

Modified:
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/layout/ApplyPreferredLayoutTask.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/loadvizmap/LoadVizmapFileTaskFactoryImpl.java
Log:
Add code to close the stream to avoid resource leak.

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/layout/ApplyPreferredLayoutTask.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/layout/ApplyPreferredLayoutTask.java
      2012-07-24 16:32:05 UTC (rev 29973)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/layout/ApplyPreferredLayoutTask.java
      2012-07-24 17:41:07 UTC (rev 29974)
@@ -27,16 +27,15 @@
  */
 package org.cytoscape.task.internal.layout;
 
+import java.util.Collection;
+import java.util.Properties;
+
 import org.cytoscape.task.AbstractNetworkViewCollectionTask;
-import org.cytoscape.task.AbstractNetworkViewTask;
-import org.cytoscape.view.model.CyNetworkView;
-import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
 import org.cytoscape.view.layout.CyLayoutAlgorithm;
+import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
+import org.cytoscape.view.model.CyNetworkView;
 import org.cytoscape.work.TaskMonitor;
 
-import java.util.Collection;
-import java.util.Properties;
-
 public class ApplyPreferredLayoutTask extends 
AbstractNetworkViewCollectionTask {
 
        private static final String DEF_LAYOUT = "force-directed";

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/loadvizmap/LoadVizmapFileTaskFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/loadvizmap/LoadVizmapFileTaskFactoryImpl.java
     2012-07-24 16:32:05 UTC (rev 29973)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/loadvizmap/LoadVizmapFileTaskFactoryImpl.java
     2012-07-24 17:41:07 UTC (rev 29974)
@@ -12,6 +12,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+
 import org.cytoscape.io.read.VizmapReaderManager;
 import org.cytoscape.task.read.LoadVizmapFileTaskFactory;
 import org.cytoscape.view.vizmap.VisualMappingManager;
@@ -20,20 +21,23 @@
 import org.cytoscape.work.SynchronousTaskManager;
 import org.cytoscape.work.TaskIterator;
 import org.cytoscape.work.TunableSetter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-
 public class LoadVizmapFileTaskFactoryImpl extends AbstractTaskFactory 
implements LoadVizmapFileTaskFactory {
 
+       private static final Logger logger = 
LoggerFactory.getLogger(LoadVizmapFileTaskFactoryImpl.class);
+
        private final VizmapReaderManager vizmapReaderMgr;
        private final VisualMappingManager vmMgr;
        private final SynchronousTaskManager<?> syncTaskManager;
 
-       private LoadVizmapFileTask task; 
+       private LoadVizmapFileTask task;
 
-       private final TunableSetter tunableSetter; 
+       private final TunableSetter tunableSetter;
 
-       
-       public LoadVizmapFileTaskFactoryImpl(VizmapReaderManager 
vizmapReaderMgr, VisualMappingManager vmMgr, SynchronousTaskManager<?> 
syncTaskManager, TunableSetter tunableSetter) {
+       public LoadVizmapFileTaskFactoryImpl(VizmapReaderManager 
vizmapReaderMgr, VisualMappingManager vmMgr,
+                       SynchronousTaskManager<?> syncTaskManager, 
TunableSetter tunableSetter) {
                this.vizmapReaderMgr = vizmapReaderMgr;
                this.vmMgr = vmMgr;
                this.syncTaskManager = syncTaskManager;
@@ -43,14 +47,15 @@
        @Override
        public TaskIterator createTaskIterator() {
                task = new LoadVizmapFileTask(vizmapReaderMgr, vmMgr);
-               return new TaskIterator(2,task);
+               return new TaskIterator(2, task);
        }
 
        public Set<VisualStyle> loadStyles(File f) {
                // Set up map containing values to be assigned to tunables.
-               // The name "file" is the name of the tunable field in 
LoadVizmapFileTask.
-               Map<String,Object> m = new HashMap<String,Object>();
-               m.put("file",f);
+               // The name "file" is the name of the tunable field in
+               // LoadVizmapFileTask.
+               Map<String, Object> m = new HashMap<String, Object>();
+               m.put("file", f);
 
                syncTaskManager.setExecutionContext(m);
                syncTaskManager.execute(createTaskIterator());
@@ -59,7 +64,7 @@
        }
 
        public Set<VisualStyle> loadStyles(InputStream is) {
-               //Save the contents of inputStream in a tmp file
+               // Save the contents of inputStream in a tmp file
                File f = this.getFileFromStream(is);
                return this.loadStyles(f);
        }
@@ -70,14 +75,14 @@
                final Map<String, Object> m = new HashMap<String, Object>();
                m.put("file", file);
 
-               return 
tunableSetter.createTaskIterator(this.createTaskIterator(), m); 
+               return 
tunableSetter.createTaskIterator(this.createTaskIterator(), m);
        }
-       
+
        // Read the inputStream and save the content in a tmp file
-       private File getFileFromStream(InputStream is){
+       private File getFileFromStream(InputStream is) {
 
                File returnFile = null;
-               
+
                // Get the contents from inputStream
                ArrayList<String> list = new ArrayList<String>();
 
@@ -85,44 +90,51 @@
                String line;
 
                try {
-                       bf = new BufferedReader(new InputStreamReader(is));     
+                       bf = new BufferedReader(new InputStreamReader(is));
                        while (null != (line = bf.readLine())) {
                                list.add(line);
                        }
-               }
-               catch (IOException e){
-               }
-               finally {
+               } catch (IOException e) {
+                       logger.error("Could not read the VizMap file.", e);
+               } finally {
                        try {
-                               if (bf != null) bf.close();
+                               if (bf != null)
+                                       bf.close();
+                       } catch (IOException e) {
+                               logger.error("Could not Close the stream.", e);
+                               bf = null;
                        }
-                       catch (IOException e) {
-                       }
                }
 
-               if (list.size()==0){
+               if (list.size() == 0)
                        return null;
-               }
 
                // Save the content to a tmp file
-               Writer output;
+               Writer output = null;
                try {
                        returnFile = File.createTempFile("visualStyles", 
".props", new File(System.getProperty("java.io.tmpdir")));
                        returnFile.deleteOnExit();
-                       
-                       //use buffering
+
+                       // use buffering
                        output = new BufferedWriter(new FileWriter(returnFile));
-                       //FileWriter always assumes default encoding is OK!
-                       for (int i=0; i< list.size(); i++){
-                               output.write( list.get(i)+ "\n");               
                
+                       // FileWriter always assumes default encoding is OK!
+                       for (int i = 0; i < list.size(); i++) {
+                               output.write(list.get(i) + "\n");
                        }
-                   output.close();
-               }
-               catch(IOException ioe){
+               } catch (IOException ioe) {
                        ioe.printStackTrace();
+               } finally {
+                       if (output != null) {
+                               try {
+                                       output.close();
+                                       output = null;
+                               } catch (IOException e) {
+                                       logger.error("Could not close stream.", 
e);
+                                       output = null;
+                               }
+
+                       }
                }
-
                return returnFile;
        }
-
 }

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