00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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);
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;
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
00110 String inFile = this.baseFilePath+File.separator+inFileName;
00111
00112
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
00125 Object waiter = new Object();
00126 String outFile = this.baseFilePath+File.separator+outFileName;
00127 if (waitfile == null) waitfile = outFileName;
00128
00129
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
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 }