KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

44 public class XPathWrapper
45 {
46
47     transient private static Logger log = LoggingManager.getLoggerForClass();
48     
49     private XPathFileContainer container;
50     private int currentRow;
51     private static final int NO_LINE = -1;
52     
53     private static String JavaDoc defaultFile = ""; // for omitted file names
54
private String JavaDoc xpathString;
55     
56     private static Map JavaDoc fileContainers = new HashMap JavaDoc(); // Map file names to containers
57

58     /*
59      * Only needed locally
60      */

61     private XPathWrapper(XPathFileContainer fdc)
62     {
63         super();
64         container = fdc;
65         currentRow = -1;
66     }
67
68     /* The cache of file packs */
69     private static ThreadLocal JavaDoc filePacks = new ThreadLocal JavaDoc(){
70         protected Object JavaDoc initialValue(){
71             return new HashMap JavaDoc();
72         }
73     };
74
75     private static String JavaDoc checkDefault(String JavaDoc file)
76     {
77         if (file.length() == 0)
78         {
79             if (fileContainers.size() == 1 && defaultFile.length() > 0)
80             {
81                 log.warn("Using default: "+defaultFile);
82                 file = defaultFile;
83             }
84             else
85             {
86                 log.error("Cannot determine default file name");
87             }
88         }
89         return file;
90     }
91     /*
92      * called by getXPathString(file,alias)
93      */

94     public static synchronized void open(String JavaDoc file,String JavaDoc xpathString, String JavaDoc alias)
95     {
96         log.info("Opening "+file+ " as " + alias);
97         file = checkDefault(file);
98         if (alias.length() == 0)
99         {
100             log.error("Alias cannot be empty");
101             return;
102         }
103         Map JavaDoc m = (Map JavaDoc) filePacks.get();
104         if (m.get(alias) == null)
105         {
106             XPathFileContainer frcc;
107             try
108             {
109                 frcc = getFile(file, xpathString, alias);
110                 log.info("Stored "+file+" as "+alias);
111                 m.put(alias,new XPathWrapper(frcc));
112             }
113             catch (FileNotFoundException JavaDoc e)
114             {
115                 log.warn(e.getLocalizedMessage());
116             }
117             catch (IOException JavaDoc e)
118             {
119                 log.warn(e.getLocalizedMessage());
120             } catch (ParserConfigurationException JavaDoc e) {
121                 log.warn(e.getLocalizedMessage());
122             } catch (SAXException JavaDoc e) {
123                 log.warn(e.getLocalizedMessage());
124             } catch (TransformerException JavaDoc e) {
125                 log.warn(e.getLocalizedMessage());
126             }
127         }
128     }
129     
130     private static XPathFileContainer getFile(String JavaDoc file, String JavaDoc xpathString, String JavaDoc alias)
131     throws FileNotFoundException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc, TransformerException JavaDoc
132     {
133         XPathFileContainer frcc;
134         if ((frcc = (XPathFileContainer) fileContainers.get(alias)) == null)
135         {
136             frcc = new XPathFileContainer(file, xpathString);
137             fileContainers.put(alias,frcc);
138             log.info("Saved "+file+" as "+alias);
139             if (defaultFile.length() == 0){
140                 defaultFile = file;// Save in case needed later
141
}
142         }
143         return frcc;
144     }
145     
146     /*
147      * Called by CSVRead(x,next) - sets the row to nil so the next
148      * row will be picked up the next time round
149      *
150      */

151     public static void endRow(String JavaDoc file)
152     {
153         file=checkDefault(file);
154         Map JavaDoc my = (Map JavaDoc) filePacks.get();
155         XPathWrapper fw = (XPathWrapper) (my).get(file);
156         if (fw == null)
157         {
158             log.warn("endRow(): no entry for "+file);
159         }
160         else
161         {
162             fw.endRow();
163         }
164     }
165     
166     private void endRow()
167     {
168         if (currentRow == NO_LINE)
169         {
170             log.warn("endRow() called twice in succession");
171         }
172         currentRow = NO_LINE;
173     }
174
175     public static String JavaDoc getXPathString(String JavaDoc file,String JavaDoc xpathString)
176     {
177         Map JavaDoc my = (Map JavaDoc) filePacks.get();
178         XPathWrapper fw = (XPathWrapper) (my).get(file);
179         if (fw == null) // First call
180
{
181             if (file.startsWith("*")) {
182                 log.warn("Cannot perform initial open using alias "+file);
183             }
184             else
185             {
186                 file=checkDefault(file);
187                 log.info("Attaching "+file);
188                 open(file,xpathString,file);
189                 fw = (XPathWrapper) my.get(file);
190             }
191             //TODO improve the error handling
192
if (fw == null) {
193                 log.error("XPathWrapper is null!");
194                 return "";
195             }
196         }
197         return fw.getXPathString();
198     }
199     
200     private String JavaDoc getXPathString()
201     {
202         if (currentRow == NO_LINE) {
203             currentRow = container.nextRow();
204         }
205         log.debug("getting match number "+currentRow);
206         
207         return container.getXPathString(currentRow);
208     }
209
210     /**
211      * Gets the current row number (mainly for error reporting)
212      *
213      * @param file
214      * @return the current row number for this thread
215      */

216     public static int getCurrentRow(String JavaDoc file)
217     {
218         
219         Map JavaDoc my = (Map JavaDoc) filePacks.get();
220         XPathWrapper fw = (XPathWrapper) (my).get(file);
221         if (fw == null) // Not yet open
222
{
223             return -1;
224         }
225         else
226         {
227             return fw.currentRow;
228         }
229     }
230     
231     /**
232      *
233      */

234     public static void clearAll()
235     {
236         log.debug("clearAll()");
237         Map JavaDoc my = (Map JavaDoc) filePacks.get();
238         for (Iterator JavaDoc i=my.entrySet().iterator();i.hasNext();)
239         {
240             Object JavaDoc fw = i.next();
241             log.info("Removing "+fw.toString());
242             i.remove();
243         }
244         fileContainers.clear();
245         defaultFile = "";
246     }
247 }
248
Popular Tags