1 24 25 package org.objectweb.clif.storage.lib.filestorage; 26 27 import java.io.*; 28 import java.util.Hashtable ; 29 import java.util.Map ; 30 import java.util.Iterator ; 31 import java.net.Socket ; 32 import org.objectweb.clif.storage.api.StorageAdmin; 33 import org.objectweb.clif.storage.api.StorageProxyAdmin; 34 import org.objectweb.clif.storage.api.CollectKey; 35 import org.objectweb.clif.console.lib.TestPlanWriter; 36 import org.objectweb.fractal.api.control.BindingController; 37 38 46 public class ConsoleFileStorageImpl 47 implements StorageAdmin, BindingController 48 { 49 static public final String testDirbase = "report"; 50 static public final String testplanExt = ".testplan"; 51 52 53 protected Map distributedStorage = new Hashtable (); 54 protected BufferedWriter test = null; 55 protected String testDirname = null; 56 protected Serializable testId = null; 57 58 59 63 64 public Object lookupFc(String clientItfName) 65 { 66 if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 67 { 68 return distributedStorage.get(clientItfName); 69 } 70 return null; 71 } 72 73 74 public void bindFc(String clientItfName, Object serverItf) 75 { 76 if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 77 { 78 distributedStorage.put(clientItfName, serverItf); 79 } 80 } 81 82 83 public void unbindFc(String clientItfName) 84 { 85 if (clientItfName.startsWith(StorageProxyAdmin.STORAGEPROXY_ADMIN)) 86 { 87 distributedStorage.remove(clientItfName); 88 } 89 } 90 91 92 public String [] listFc() 93 { 94 return (String [])distributedStorage.keySet().toArray(new String [distributedStorage.size()]); 95 } 96 97 98 102 103 108 public void newTest( 109 Serializable testId, 110 Map testPlan) 111 { 112 this.testId = testId; 113 testDirname = testDirbase + File.separator + testId; 114 try 115 { 116 File baseDir = new File(testDirname); 117 int i = 0; 118 while (! baseDir.mkdirs()) 119 { 120 if (baseDir.exists()) 121 { 122 testDirname = testDirbase + File.separator + testId + "-" + ++i; 123 baseDir = new File(testDirname); 124 } 125 else 126 { 127 throw new IOException("Could not create " + testDirname + " directory."); 128 } 129 } 130 TestPlanWriter.write2prop( 131 new FileOutputStream(testDirname + ".testplan"), 132 testPlan); 133 } 134 catch (Exception e) 135 { 136 e.printStackTrace(System.err); 137 } 138 } 139 140 141 public void collect() 142 { 143 Iterator list = distributedStorage.values().iterator(); 144 StorageProxyAdmin store; 145 byte[] buffer = new byte[FileStorageCollect.BLOCK_SIZE]; 146 try 147 { 148 while (list.hasNext()) 149 { 150 store = (StorageProxyAdmin)list.next(); 151 File directory = new File(testDirname + File.separator + store.getBladeId()); 152 if (directory.mkdirs()) 153 { 154 CollectKey key = store.initCollect(testId); 155 if (key != null) 156 { 157 FileStorageCollectStep step; 158 Socket sock; 159 while ((step = (FileStorageCollectStep) store.collect(key)) != null) 160 { 161 File outputFile = new File(directory, step.getFilename()); 162 if (!outputFile.exists()) 163 { 164 sock = new Socket (); 165 sock.connect(step.getSocketAddress(), 0); 166 InputStream in = sock.getInputStream(); 167 OutputStream out = new FileOutputStream(outputFile); 168 int n = in.read(buffer); 169 while (n != -1) 170 { 171 out.write(buffer, 0, n); 172 n = in.read(buffer); 173 } 174 in.close(); 175 out.close(); 176 sock.close(); 177 } 178 } 179 } 180 } 181 } 182 } 183 catch (Exception ex) 184 { 185 throw new RuntimeException ("Could not collect data", ex); 186 } 187 } 188 189 190 193 public void terminate() 194 { 195 } 196 } 197 | Popular Tags |