Author: jflesch
Date: 2006-11-04 18:00:36 +0000 (Sat, 04 Nov 2006)
New Revision: 10816

Modified:
   trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java
   trunk/apps/Thaw/src/thaw/plugins/index/Index.java
Log:
Use a temporary file when fetching an Index, to avoid issues with PipedStream + 
XML parser

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java      2006-11-04 16:10:00 UTC 
(rev 10815)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPClientGet.java      2006-11-04 18:00:36 UTC 
(rev 10816)
@@ -2,8 +2,6 @@

 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Observer;
@@ -31,6 +29,7 @@
        private int persistence = 0;
        private boolean globalQueue = false;
        private String destinationDir = null;
+       private String finalPath = null;

        private int attempt = -1;
        private String status;
@@ -49,9 +48,7 @@

        private boolean alreadySaved = false;

-       private PipedOutputStream pipedOutputStream = new PipedOutputStream();

-
        /**
         * See setParameters().
         */
@@ -75,7 +72,7 @@
        /**
         * Used to resume query from persistent queue of the node.
         * Think of adding this FCPClientGet as a queryManager observer.
-        * @param destinationDir if null, then you are expected to use the 
streams (see getInputStream())
+        * @param destinationDir if null, then a temporary file will be create 
(path determined only when the file is availabed ; this file will be deleted on 
jvm exit)
         */
        public FCPClientGet(String id, String key, int priority,
                            int persistence, boolean globalQueue,
@@ -114,7 +111,7 @@

        /**
         * Only for initial queries : To resume queries, use 
FCPClientGet(FCPQueueManager, Hashmap).
-        * @param destinationDir if null, you're expected to use the streams
+        * @param destinationDir if null => temporary file
         * @param persistence 0 = Forever ; 1 = Until node reboot ; 2 = Until 
the app disconnect
         */
        public FCPClientGet(String key, int priority,
@@ -597,23 +594,28 @@

                if (file != null) {
                        newFile = new File(file);
+               } else {
+                       try {
+                               Logger.info(this, "Using temporary file");
+                               newFile = File.createTempFile("thaw_", ".tmp");
+                               finalPath = newFile.getPath();
+                               newFile.deleteOnExit();
+                       } catch(java.io.IOException e) {
+                               Logger.error(this, "Error while creating 
temporary file: "+e.toString());
+                       }
                }

                if(reallyWrite) {
                        Logger.info(this, "Getting file from node ... ");

-                       if (newFile != null) {
-                               try {
-                                       outputStream = new 
FileOutputStream(newFile);
-                               } catch(java.io.IOException e) {
-                                       Logger.error(this, "Unable to write 
file on disk ... disk space / perms ? : "+e.toString());
-                                       this.status = "Write error";
-                                       return false;
-                               }
-                       } else {
-                               Logger.info(this, "Use PipedOutputStream");
-                               outputStream = pipedOutputStream;
+                       try {
+                               outputStream = new FileOutputStream(newFile);
+                       } catch(java.io.IOException e) {
+                               Logger.error(this, "Unable to write file on 
disk ... disk space / perms ? : "+e.toString());
+                               this.status = "Write error";
+                               return false;
                        }
+
                } else {
                        Logger.info(this, "File is supposed already written. 
Not rewriting.");
                }
@@ -690,17 +692,7 @@
        }


-       public InputStream getInputStream() {
-               try {
-                       return new PipedInputStream(pipedOutputStream);
-               } catch(java.io.IOException e) {
-                       Logger.error(this, "Error while instanciating 
PipedInputStream: "+e.toString());
-                       return null;
-               }
-       }

-
-
        public boolean removeRequest() {
                FCPMessage stopMessage = new FCPMessage();

@@ -840,6 +832,9 @@
        }

        public String getPath() {
+               if (finalPath != null)
+                       return finalPath;
+
                if(this.destinationDir != null)
                        return this.destinationDir + File.separator + 
this.filename;


Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-11-04 16:10:00 UTC 
(rev 10815)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-11-04 18:00:36 UTC 
(rev 10816)
@@ -22,12 +22,10 @@
 import org.w3c.dom.NodeList;

 import java.io.OutputStream;
-import java.io.PipedOutputStream;
 import java.io.InputStream;
-import java.io.PipedInputStream;
 import java.io.FileOutputStream;
+import java.io.FileInputStream;

-
 import thaw.fcp.*;
 import thaw.plugins.Hsqldb;
 import thaw.core.*;
@@ -64,6 +62,7 @@

        private boolean rewriteKey = true;

+       private boolean xmlParserReady = false;

        private FCPClientPut publicKeyRecalculation = null;

@@ -281,7 +280,7 @@

                                this.revision++;

-                               clientPut = new FCPClientPut(this.targetFile, 
2, this.revision, this.toString(), this.privateKey, 2, false, 0);
+                               clientPut = new FCPClientPut(this.targetFile, 
2, this.revision, this.toString(), this.privateKey, 2, true, 0);
                                this.transfer = clientPut;
                                clientPut.addObserver(this);

@@ -334,8 +333,13 @@

                clientGet.addObserver(this);

-               Thread downloadAndParse = new Thread(new 
DownloadAndParse(clientGet, rewriteKey));
-               downloadAndParse.start();
+               /*
+                * These requests are usually quite fast, and don't consume a 
lot
+                * of bandwith / CPU. So we can skip the queue and start 
immediatly
+                * (and like this, they won't appear in the queue)
+                */
+               //this.queueManager.addQueryToThePendingQueue(clientGet);
+               clientGet.start(queueManager);

                this.setChanged();
                this.notifyObservers();
@@ -346,31 +350,6 @@
        }


-       private class DownloadAndParse implements Runnable {
-               FCPClientGet clientGet;
-               boolean rewriteKey;
-
-               public DownloadAndParse(FCPClientGet clientGet, boolean 
rewriteKey) {
-                       this.clientGet = clientGet;
-               }
-
-               public void run() {
-                       /*
-                        * These requests are usually quite fast, and don't 
consume a lot
-                        * of bandwith / CPU. So we can skip the queue and 
start immediatly
-                        * (and like this, they won't appear in the queue)
-                        */
-                       
//this.queueManager.addQueryToThePendingQueue(clientGet);
-                       clientGet.start(queueManager);
-                       loadXML(clientGet.getInputStream());
-                       save();
-
-                       setChanged();
-                       notifyObservers();
-               }
-       }
-
-
        protected void setTransfer(FCPTransferQuery query) {
                this.transfer = query;

@@ -574,6 +553,14 @@
                                        //      
this.queueManager.remove(this.transfer);
                                        this.queueManager.remove(this.transfer);

+                                       if (transfer.getPath() == null) {
+                                               Logger.error(this, "No path 
?!");
+                                               return;
+                                       }
+
+                                       loadXML(transfer.getPath());
+                                       (new 
java.io.File(transfer.getPath())).delete();
+
                                        this.transfer = null;

                                        this.setChanged();
@@ -991,7 +978,15 @@
                return files;
        }

-       public void loadXML(java.io.InputStream input) {
+       public void loadXML(String filePath) {
+               try {
+                       loadXML(new FileInputStream(filePath));
+               } catch(java.io.FileNotFoundException e) {
+                       Logger.error(this, "Unable to load XML: 
FileNotFoundException ('"+filePath+"') ! : "+e.toString());
+               }
+       }
+
+       public synchronized void loadXML(java.io.InputStream input) {
                DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
                DocumentBuilder xmlBuilder;

@@ -1005,7 +1000,10 @@
                Document xmlDoc;

                try {
+                       Logger.info(this, "XML parser ready");
+                       xmlParserReady = true;
                        xmlDoc = xmlBuilder.parse(input);
+                       Logger.info(this, "Index parsed");
                } catch(org.xml.sax.SAXException e) {
                        Logger.error(this, "Unable to load index because: 
"+e.toString());
                        return;


Reply via email to