1 24 25 package org.objectweb.clif.storage.lib.filestorage; 26 27 import java.io.BufferedWriter ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.io.Serializable ; 31 import java.io.File ; 32 import java.io.OutputStream ; 33 import java.io.FileOutputStream ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import org.objectweb.clif.storage.api.*; 38 import org.objectweb.clif.supervisor.api.ClifException; 39 40 41 48 public class FileStorageProxyImpl 49 implements 50 StorageWrite, 51 StorageProxyAdmin 52 { 53 static public final String CSV_SEPARATOR = ";"; 54 static public final String CSV_EXTENSION= ".csv"; 55 static public final String nodePropFilename = "server.prop"; 56 static public final String baseTestDirname = "report"; 57 58 59 protected String bladeId; 60 protected long dateOrigin; 61 private Map writers = new HashMap (); 62 private String currentDir = null; 63 64 65 public FileStorageProxyImpl() 66 { 67 } 68 69 70 74 75 79 public synchronized void write(BladeEvent event) 80 { 81 BufferedWriter wrt = (BufferedWriter )writers.get(event.getTypeLabel()); 82 if (wrt == null) 83 { 84 String filename = currentDir 85 + File.separator 86 + event.getTypeLabel(); 87 try 88 { 89 wrt = new BufferedWriter (new FileWriter (filename)); 90 writers.put(event.getTypeLabel(), wrt); 91 } 92 catch (IOException ex) 93 { 94 throw new RuntimeException ("Storage proxy can't create file " + filename, ex); 95 } 96 } 97 try 98 { 99 wrt.write(event.toString(dateOrigin, CSV_SEPARATOR)); 100 wrt.newLine(); 101 } 102 catch (Exception e) 103 { 104 e.printStackTrace(System.err); 105 } 106 } 107 108 109 113 114 117 public void newTest(Serializable testId) 118 throws ClifException 119 { 120 closeTest(); 121 dateOrigin = System.currentTimeMillis(); 122 currentDir = baseTestDirname + File.separator + testId + File.separator + bladeId; 123 File baseDir = new File (currentDir); 124 int i = 0; 125 while (baseDir.exists()) 126 { 127 currentDir = baseTestDirname + File.separator + testId + "-" + ++i + File.separator + bladeId; 128 baseDir = new File (currentDir); 129 } 130 try 131 { 132 while (!baseDir.exists()) 133 { 134 baseDir.mkdirs(); 135 } 136 } 137 catch(Exception ex) 138 { 139 throw new ClifException("Could not create " + currentDir + " directory.", ex); 140 } 141 try 142 { 143 OutputStream out = new FileOutputStream (new File (currentDir, nodePropFilename)); 144 System.getProperties().store(out, "System properties for blade " + bladeId); 145 out.close(); 146 } 147 catch (IOException ex) 148 { 149 throw new ClifException( 150 "Could not write file " + currentDir + File.separator + nodePropFilename, 151 ex); 152 } 153 } 154 155 156 159 public void closeTest() 160 { 161 try 162 { 163 Iterator iter = writers.values().iterator(); 164 while (iter.hasNext()) 165 { 166 ((BufferedWriter )iter.next()).close(); 167 iter.remove(); 168 } 169 } 170 catch (Exception e) 171 { 172 e.printStackTrace(System.err); 173 } 174 } 175 176 177 180 public String getBladeId() 181 { 182 return bladeId; 183 } 184 185 186 189 public void init(String bladeId) 190 { 191 this.bladeId = bladeId; 192 } 193 194 195 202 public CollectKey initCollect(Serializable testId) 203 { 204 closeTest(); 205 FileStorageCollect collect = FileStorageCollect.newCollect(new File (currentDir)); 206 if (collect != null) 207 { 208 return collect.getKey(); 209 } 210 return null; 211 } 212 213 214 public long getCollectSize(CollectKey key) 215 { 216 return FileStorageCollect.getSize(key); 217 } 218 219 220 public Serializable collect(CollectKey key) 221 { 222 return FileStorageCollect.collect(key); 223 } 224 225 226 public void closeCollect(CollectKey key) 227 { 228 FileStorageCollect.close(key); 229 } 230 } 231 | Popular Tags |