KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > FileWrapper


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/FileWrapper.java,v 1.6.2.1 2005/02/18 01:42:49 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.functions;
20
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.jorphan.logging.LoggingManager;
28 import org.apache.log.Logger;
29
30 /**
31  * This class wraps the FileRowColContainer for use across multiple
32  * threads.
33  *
34  * It does this by maintaining a list of open files, keyed by file name
35  * (or alias, if used).
36  * A list of open files is also maintained for each thread, together with
37  * the current line number.
38  *
39  * @version $Revision: 1.6.2.1 $ $Date: 2005/02/18 01:42:49 $
40  */

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 JavaDoc defaultFile = ""; // for omitted file names
51

52     private static Map JavaDoc fileContainers = new HashMap JavaDoc(); // Map file names to containers
53

54     /*
55      * Only needed locally
56      */

57     private FileWrapper(FileRowColContainer fdc)
58     {
59         super();
60         container = fdc;
61         currentRow = -1;
62     }
63
64     /* The cache of file packs */
65     private static ThreadLocal JavaDoc filePacks = new ThreadLocal JavaDoc(){
66         protected Object JavaDoc initialValue(){
67             return new HashMap JavaDoc();
68         }
69     };
70
71     private static String JavaDoc checkDefault(String JavaDoc 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     /*
88      * called by CSVRead(file,alias)
89      */

90     public static synchronized void open(String JavaDoc file, String JavaDoc 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 JavaDoc m = (Map JavaDoc) 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 JavaDoc e)
110             {
111                 //Already logged
112
}
113             catch (IOException JavaDoc e)
114             {
115                 //Already logged
116
}
117         }
118     }
119     
120     private static FileRowColContainer getFile(String JavaDoc file, String JavaDoc alias)
121     throws FileNotFoundException JavaDoc, IOException JavaDoc
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;// Save in case needed later
131
}
132         }
133         return frcc;
134     }
135     
136     /*
137      * Called by CSVRead(x,next) - sets the row to nil so the next
138      * row will be picked up the next time round
139      *
140      */

141     public static void endRow(String JavaDoc file)
142     {
143         file=checkDefault(file);
144         Map JavaDoc my = (Map JavaDoc) 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 JavaDoc getColumn(String JavaDoc file,int col)
166     {
167         Map JavaDoc my = (Map JavaDoc) filePacks.get();
168         FileWrapper fw = (FileWrapper) (my).get(file);
169         if (fw == null) // First call
170
{
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             //TODO improve the error handling
182
if (fw == null) return "";
183         }
184         return fw.getColumn(col);
185     }
186     
187     private String JavaDoc 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     /**
198      * Gets the current row number (mainly for error reporting)
199      *
200      * @param file
201      * @return the current row number for this thread
202      */

203     public static int getCurrentRow(String JavaDoc file)
204     {
205         
206         Map JavaDoc my = (Map JavaDoc) filePacks.get();
207         FileWrapper fw = (FileWrapper) (my).get(file);
208         if (fw == null) // Not yet open
209
{
210             return -1;
211         }
212         else
213         {
214             return fw.currentRow;
215         }
216     }
217     
218     /**
219      *
220      */

221     public static void clearAll()
222     {
223         log.debug("clearAll()");
224         Map JavaDoc my = (Map JavaDoc) filePacks.get();
225         for (Iterator JavaDoc i=my.entrySet().iterator();i.hasNext();)
226         {
227             Object JavaDoc fw = i.next();
228             log.info("Removing "+fw.toString());
229             i.remove();
230         }
231         fileContainers.clear();
232         defaultFile = "";
233     }
234 }
235
Popular Tags