ReadWriteFileTransfertManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2006-2008 Ubikis
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.common.util.file;
00029 
00030 import java.io.BufferedOutputStream;
00031 import java.io.File;
00032 import java.io.FileOutputStream;
00033 import java.io.IOException;
00034 import java.io.OutputStream;
00035 
00036 import org.openmobileis.common.util.log.LogManager;
00037 
00048 public final class ReadWriteFileTransfertManager {
00049         
00050         class ReadWriteWaiter implements Runnable       {
00051                 private File waitFile;
00052                 private Object notifier;
00053                 private long timeout = 0;
00054                 private boolean hastimeout = false;
00055                 
00056                 ReadWriteWaiter(String waitFileName, Object notifier, long timeout)     {
00057                         this.waitFile = new File(waitFileName);
00058                         this.notifier = notifier;
00059                         this.timeout = timeout;
00060                 }
00061                 public void run()       {
00062                         long time =0;
00063                                 try     {
00064                                         while (true)    {
00065                                         Thread.currentThread().sleep(400);
00066                                         if (this.waitFile.exists())     {
00067                                                 Thread.currentThread().sleep(200); //wait end of the writing;
00068                                                  synchronized (this.notifier) {
00069                                                          this.notifier.notifyAll();
00070                                                  }
00071                                                 return;
00072                                         } else if (this.timeout > 0 && time > this.timeout)     {
00073                                                 this.hastimeout = true;
00074                                                  synchronized (this.notifier) {
00075                                                          this.notifier.notifyAll();
00076                                                  }
00077                                                 return;
00078                                         }
00079                                         time+=400;
00080                                         }
00081                                 } catch (Throwable ex)  {
00082                                         LogManager.traceError(0, ex.toString());
00083                                 }
00084                 }
00085                 public boolean isHastimeout() {
00086                         return hastimeout;
00087                 }
00088         }
00089         private String baseFilePath = null;
00090         private long timeout = 0; // no timeout.
00094         public ReadWriteFileTransfertManager(String directory) {
00095                 this.baseFilePath = directory;
00096         }
00097         
00108         public byte[] sendReadWriteCommand(byte[] command, String inFileName, String outFileName, String waitfile) throws IOException   {
00109                 //write the file.
00110                 String inFile = this.baseFilePath+File.separator+inFileName;
00111                 
00112 //              LogManager.traceDebug(0, "ReadWriteFileTransfertManager write file :"+inFile);
00113                 
00114                  OutputStream out = new BufferedOutputStream(new FileOutputStream(inFile+".tmp"));
00115                  try    {
00116                  out.write(command);
00117                  } finally      {
00118                          out.flush();
00119                          out.close();
00120                  }
00121                  File toren = new File(inFile+".tmp");
00122                  toren.renameTo(new File(inFile));
00123                  
00124                  //wait for file
00125                  Object waiter = new Object();
00126                  String outFile = this.baseFilePath+File.separator+outFileName;
00127                  if (waitfile == null) waitfile = outFileName;
00128                  
00129         //       LogManager.traceDebug(0, "ReadWriteFileTransfertManager wait file :"+waitfile);
00130                  
00131            ReadWriteWaiter rw = new ReadWriteWaiter(this.baseFilePath+File.separator+waitfile, waiter, this.timeout);
00132                  Thread th = new Thread(rw);
00133                  th.start();
00134                  synchronized (waiter) {
00135                          try    {
00136                                  if (this.timeout >0) waiter.wait(timeout*2);
00137                                  else waiter.wait();
00138                          } catch (InterruptedException ex)      {
00139                                  throw new IOException(ex.toString());
00140                          }
00141                          if (rw.isHastimeout()) {
00142                                  throw new IOException("Timeout Exception.");
00143                          }
00144                          File tout = new File(outFile);
00145                          if (!tout.exists())    {
00146                                  throw new IOException("Timeout Exception.");
00147                          }
00148                          byte[] retb = FileUtilities.readFile(tout);
00149                          return retb;
00150                 }
00151         }
00152 
00153         public long getTimeout() {
00154                 return timeout;
00155         }
00156 
00157         public void setTimeout(long timeout) {
00158                 this.timeout = timeout;
00159         }
00160         
00161         public static void main(String args[]){
00162                 LogManager.registerLogManager(null);
00163                 String workingdir = System.getProperty("user.dir");
00164                 ReadWriteFileTransfertManager manager = new ReadWriteFileTransfertManager(workingdir);
00165 //              manager.setTimeout(2000);
00166                 try     {
00167                         byte[] testb = manager.sendReadWriteCommand(new String("test file data").getBytes(), "int.test", "out.test", null);
00168                         System.out.println(new String(testb));
00169                 } catch (Throwable ex)  {
00170                         ex.printStackTrace();
00171                 }
00172         }
00173 
00174 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4