1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 30 41 public class FileWrapper 42 { 43 44 transient private static Logger log = LoggingManager.getLoggerForClass(); 45 46 private FileRowColContainer container; 47 private int currentRow; 48 private static final int NO_LINE = -1; 49 50 private static String defaultFile = ""; 52 private static Map fileContainers = new HashMap (); 54 57 private FileWrapper(FileRowColContainer fdc) 58 { 59 super(); 60 container = fdc; 61 currentRow = -1; 62 } 63 64 65 private static ThreadLocal filePacks = new ThreadLocal (){ 66 protected Object initialValue(){ 67 return new HashMap (); 68 } 69 }; 70 71 private static String checkDefault(String file) 72 { 73 if (file.length() == 0) 74 { 75 if (fileContainers.size() == 1 && defaultFile.length() > 0) 76 { 77 log.warn("Using default: "+defaultFile); 78 file = defaultFile; 79 } 80 else 81 { 82 log.error("Cannot determine default file name"); 83 } 84 } 85 return file; 86 } 87 90 public static synchronized void open(String file, String alias) 91 { 92 log.info("Opening "+file+ " as " + alias); 93 file = checkDefault(file); 94 if (alias.length() == 0) 95 { 96 log.error("Alias cannot be empty"); 97 return; 98 } 99 Map m = (Map ) filePacks.get(); 100 if (m.get(alias) == null) 101 { 102 FileRowColContainer frcc; 103 try 104 { 105 frcc = getFile(file, alias); 106 log.info("Stored "+file+" as "+alias); 107 m.put(alias,new FileWrapper(frcc)); 108 } 109 catch (FileNotFoundException e) 110 { 111 } 113 catch (IOException e) 114 { 115 } 117 } 118 } 119 120 private static FileRowColContainer getFile(String file, String alias) 121 throws FileNotFoundException , IOException 122 { 123 FileRowColContainer frcc; 124 if ((frcc = (FileRowColContainer) fileContainers.get(alias)) == null) 125 { 126 frcc = new FileRowColContainer(file); 127 fileContainers.put(alias,frcc); 128 log.info("Saved "+file+" as "+alias+" delimiter=<"+frcc.getDelimiter()+">"); 129 if (defaultFile.length() == 0){ 130 defaultFile = file; } 132 } 133 return frcc; 134 } 135 136 141 public static void endRow(String file) 142 { 143 file=checkDefault(file); 144 Map my = (Map ) filePacks.get(); 145 FileWrapper fw = (FileWrapper) (my).get(file); 146 if (fw == null) 147 { 148 log.warn("endRow(): no entry for "+file); 149 } 150 else 151 { 152 fw.endRow(); 153 } 154 } 155 156 private void endRow() 157 { 158 if (currentRow == NO_LINE) 159 { 160 log.warn("endRow() called twice in succession"); 161 } 162 currentRow = NO_LINE; 163 } 164 165 public static String getColumn(String file,int col) 166 { 167 Map my = (Map ) filePacks.get(); 168 FileWrapper fw = (FileWrapper) (my).get(file); 169 if (fw == null) { 171 if (file.startsWith("*")) { 172 log.warn("Cannot perform initial open using alias "+file); 173 } 174 else 175 { 176 file=checkDefault(file); 177 log.info("Attaching "+file); 178 open(file,file); 179 fw = (FileWrapper) my.get(file); 180 } 181 if (fw == null) return ""; 183 } 184 return fw.getColumn(col); 185 } 186 187 private String getColumn(int col) 188 { 189 if (currentRow == NO_LINE) 190 { 191 currentRow = container.nextRow(); 192 193 } 194 return container.getColumn(currentRow,col); 195 } 196 197 203 public static int getCurrentRow(String file) 204 { 205 206 Map my = (Map ) filePacks.get(); 207 FileWrapper fw = (FileWrapper) (my).get(file); 208 if (fw == null) { 210 return -1; 211 } 212 else 213 { 214 return fw.currentRow; 215 } 216 } 217 218 221 public static void clearAll() 222 { 223 log.debug("clearAll()"); 224 Map my = (Map ) filePacks.get(); 225 for (Iterator i=my.entrySet().iterator();i.hasNext();) 226 { 227 Object fw = i.next(); 228 log.info("Removing "+fw.toString()); 229 i.remove(); 230 } 231 fileContainers.clear(); 232 defaultFile = ""; 233 } 234 } 235 | Popular Tags |