KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > monitor > parser > MonitorHandler


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/MonitorHandler.java,v 1.5 2004/03/20 20:36:38 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 package org.apache.jmeter.monitor.parser;
18
19 //import java.util.List;
20
import java.util.Stack JavaDoc;
21
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.DefaultHandler JavaDoc;
25
26 import org.apache.jmeter.monitor.model.ObjectFactory;
27 import org.apache.jmeter.monitor.model.Connector;
28 import org.apache.jmeter.monitor.model.Jvm;
29 import org.apache.jmeter.monitor.model.Memory;
30 import org.apache.jmeter.monitor.model.RequestInfo;
31 import org.apache.jmeter.monitor.model.Status;
32 import org.apache.jmeter.monitor.model.StatusImpl;
33 import org.apache.jmeter.monitor.model.ThreadInfo;
34 import org.apache.jmeter.monitor.model.Worker;
35 import org.apache.jmeter.monitor.model.Workers;
36 import org.apache.jmeter.monitor.model.WorkersImpl;
37
38 public class MonitorHandler extends DefaultHandler JavaDoc
39 {
40     //private boolean startDoc = false;
41
//private boolean endDoc = false;
42
private Stack JavaDoc stacktree = new Stack JavaDoc();
43
44     private ObjectFactory factory = null;
45     private Status status = null;
46     private Jvm jvm = null;
47     private Memory memory = null;
48     private Connector connector = null;
49     private ThreadInfo threadinfo = null;
50     private RequestInfo requestinfo = null;
51     private Worker worker = null;
52     private Workers workers = null;
53     //private List workerslist = null;
54

55     /**
56      *
57      */

58     public MonitorHandler()
59     {
60         super();
61     }
62
63     /**
64      * Set the ObjectFactory used to create new
65      * @param factory
66      */

67     public void setObjectFactory(ObjectFactory factory){
68         this.factory = factory;
69     }
70     
71     public void startDocument ()
72     throws SAXException JavaDoc
73     {
74         //this.startDoc = true;
75
}
76     
77     public void endDocument ()
78     throws SAXException JavaDoc
79     {
80         //this.startDoc = false;
81
//this.endDoc = true;
82
}
83
84     /**
85      * Receive notification of the start of an element.
86      *
87      * <p>By default, do nothing. Application writers may override this
88      * method in a subclass to take specific actions at the start of
89      * each element (such as allocating a new tree node or writing
90      * output to a file).</p>
91      *
92      * @param uri
93      * @param localName The element type name.
94      * @param qName
95      * @param attributes The specified or defaulted attributes.
96      * @exception org.xml.sax.SAXException Any SAX exception, possibly
97      * wrapping another exception.
98      * @see org.xml.sax.ContentHandler#startElement
99      */

100     public void startElement (String JavaDoc uri, String JavaDoc localName,
101                   String JavaDoc qName, Attributes JavaDoc attributes)
102     throws SAXException JavaDoc
103     {
104         if (qName.equals(Constants.STATUS)){
105             status = factory.createStatus();
106             stacktree.push(status);
107         } else if (qName.equals(Constants.JVM)){
108             jvm = factory.createJvm();
109             if (stacktree.peek() instanceof Status){
110                 status.setJvm(jvm);
111                 stacktree.push(jvm);
112             }
113         } else if (qName.equals(Constants.MEMORY)){
114             memory = factory.createMemory();
115             if (stacktree.peek() instanceof Jvm){
116                 stacktree.push(memory);
117                 if (attributes != null){
118                     for (int idx=0; idx < attributes.getLength(); idx++){
119                         String JavaDoc attr = attributes.getQName(idx);
120                         if (attr.equals(Constants.MEMORY_FREE)){
121                             memory.
122                                 setFree(parseLong(attributes.getValue(idx)));
123                         } else if (attr.equals(Constants.MEMORY_TOTAL)){
124                             memory.
125                                 setTotal(parseLong(attributes.getValue(idx)));
126                         } else if (attr.equals(Constants.MEMORY_MAX)){
127                             memory.
128                                 setMax(parseLong(attributes.getValue(idx)));
129                         }
130                     }
131                 }
132                 jvm.setMemory(memory);
133             }
134         } else if (qName.equals(Constants.CONNECTOR)){
135             connector = factory.createConnector();
136             if (stacktree.peek() instanceof Status ||
137             stacktree.peek() instanceof Connector){
138                 ((StatusImpl)status).addConnector(connector);
139                 stacktree.push(connector);
140                 if (attributes != null){
141                     for (int idx=0; idx < attributes.getLength(); idx++){
142                         String JavaDoc attr = attributes.getQName(idx);
143                         if (attr.equals(Constants.ATTRIBUTE_NAME)){
144                             connector.setName(attributes.getValue(idx));
145                         }
146                     }
147                 }
148             }
149         } else if (qName.equals(Constants.THREADINFO)){
150             threadinfo = factory.createThreadInfo();
151             if (stacktree.peek() instanceof Connector){
152                 stacktree.push(threadinfo);
153                 connector.setThreadInfo(threadinfo);
154                 if (attributes != null){
155                     for (int idx=0; idx < attributes.getLength(); idx++){
156                         String JavaDoc attr = attributes.getQName(idx);
157                         if (attr.equals(Constants.MAXTHREADS)){
158                             threadinfo.setMaxThreads(
159                                 parseInt(attributes.getValue(idx)));
160                         } else if (attr.equals(Constants.MINSPARETHREADS)){
161                             threadinfo.setMinSpareThreads(
162                                 parseInt(attributes.getValue(idx)));
163                         } else if (attr.equals(Constants.MAXSPARETHREADS)){
164                             threadinfo.setMaxSpareThreads(
165                                 parseInt(attributes.getValue(idx)));
166                         } else if (attr.equals(Constants.CURRENTTHREADCOUNT)){
167                             threadinfo.setCurrentThreadCount(
168                                 parseInt(attributes.getValue(idx)));
169                         } else if (attr.equals(Constants.CURRENTBUSYTHREADS)){
170                             threadinfo.setCurrentThreadsBusy(
171                                 parseInt(attributes.getValue(idx)));
172                         }
173                     }
174                 }
175             }
176         } else if (qName.equals(Constants.REQUESTINFO)){
177             requestinfo = factory.createRequestInfo();
178             if (stacktree.peek() instanceof Connector){
179                 stacktree.push(requestinfo);
180                 connector.setRequestInfo(requestinfo);
181                 if (attributes != null){
182                     for (int idx=0; idx < attributes.getLength(); idx++){
183                         String JavaDoc attr = attributes.getQName(idx);
184                         if (attr.equals(Constants.MAXTIME)){
185                             requestinfo.setMaxTime(
186                                 parseInt(attributes.getValue(idx)));
187                         } else if (attr.equals(Constants.PROCESSINGTIME)){
188                             requestinfo.setProcessingTime(
189                                 parseInt(attributes.getValue(idx)));
190                         } else if (attr.equals(Constants.REQUESTCOUNT)){
191                             requestinfo.setRequestCount(
192                                 parseInt(attributes.getValue(idx)));
193                         } else if (attr.equals(Constants.ERRORCOUNT)){
194                             requestinfo.setErrorCount(
195                                 parseInt(attributes.getValue(idx)));
196                         } else if (attr.equals(Constants.BYTESRECEIVED)){
197                             requestinfo.setBytesReceived(
198                                 parseLong(attributes.getValue(idx)));
199                         } else if (attr.equals(Constants.BYTESSENT)){
200                             requestinfo.setBytesSent(
201                                 parseLong(attributes.getValue(idx)));
202                         }
203                     }
204                 }
205             }
206         } else if (qName.equals(Constants.WORKERS)){
207             workers = factory.createWorkers();
208             if (stacktree.peek() instanceof Connector){
209                 connector.setWorkers(workers);
210                 stacktree.push(workers);
211             }
212         } else if (qName.equals(Constants.WORKER)){
213             worker = factory.createWorker();
214             if (stacktree.peek() instanceof Workers ||
215             stacktree.peek() instanceof Worker){
216                 stacktree.push(worker);
217                 ((WorkersImpl)workers).addWorker(worker);
218                 if (attributes != null){
219                     for (int idx=0; idx < attributes.getLength(); idx++){
220                         String JavaDoc attr = attributes.getQName(idx);
221                         if (attr.equals(Constants.STAGE)){
222                             worker.setStage(attributes.getValue(idx));
223                         } else if (attr.equals(Constants.REQUESTPROCESSINGTIME)){
224                             worker.setRequestProcessingTime(
225                                 parseInt(attributes.getValue(idx)));
226                         } else if (attr.equals(Constants.REQUESTBYTESSENT)){
227                             worker.setRequestBytesSent(
228                                 parseLong(attributes.getValue(idx)));
229                         } else if (attr.equals(Constants.REQUESTBYTESRECEIVED)){
230                             worker.setRequestBytesReceived(
231                                 parseLong(attributes.getValue(idx)));
232                         } else if (attr.equals(Constants.REMOTEADDR)){
233                             worker.setRemoteAddr(attributes.getValue(idx));
234                         } else if (attr.equals(Constants.VIRTUALHOST)){
235                             worker.setVirtualHost(attributes.getValue(idx));
236                         } else if (attr.equals(Constants.METHOD)){
237                             worker.setMethod(attributes.getValue(idx));
238                         } else if (attr.equals(Constants.CURRENTURI)){
239                             worker.setCurrentUri(attributes.getValue(idx));
240                         } else if (attr.equals(Constants.CURRENTQUERYSTRING)){
241                             worker.setCurrentQueryString(
242                                 attributes.getValue(idx));
243                         } else if (attr.equals(Constants.PROTOCOL)){
244                             worker.setProtocol(attributes.getValue(idx));
245                         }
246                     }
247                 }
248             }
249         }
250     }
251     
252     
253     /**
254      * Receive notification of the end of an element.
255      *
256      * <p>By default, do nothing. Application writers may override this
257      * method in a subclass to take specific actions at the end of
258      * each element (such as finalising a tree node or writing
259      * output to a file).</p>
260      *
261      * @param uri
262      * @param localName The element type name.
263      * @param qName The specified or defaulted attributes.
264      * @exception org.xml.sax.SAXException Any SAX exception, possibly
265      * wrapping another exception.
266      * @see org.xml.sax.ContentHandler#endElement
267      */

268     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
269     throws SAXException JavaDoc
270     {
271         if (qName.equals(Constants.STATUS)){
272             if (stacktree.peek() instanceof Status){
273                 stacktree.pop();
274             }
275         } else if (qName.equals(Constants.JVM)){
276             if (stacktree.peek() instanceof Jvm){
277                 stacktree.pop();
278             }
279         } else if (qName.equals(Constants.MEMORY)){
280             if (stacktree.peek() instanceof Memory){
281                 stacktree.pop();
282             }
283         } else if (qName.equals(Constants.CONNECTOR)){
284             if (stacktree.peek() instanceof Connector ||
285             stacktree.peek() instanceof Connector){
286                 stacktree.pop();
287             }
288         } else if (qName.equals(Constants.THREADINFO)){
289             if (stacktree.peek() instanceof ThreadInfo){
290                 stacktree.pop();
291             }
292         } else if (qName.equals(Constants.REQUESTINFO)){
293             if (stacktree.peek() instanceof RequestInfo){
294                 stacktree.pop();
295             }
296         } else if (qName.equals(Constants.WORKERS)){
297             if (stacktree.peek() instanceof Workers){
298                 stacktree.pop();
299             }
300         } else if (qName.equals(Constants.WORKER)){
301             if (stacktree.peek() instanceof Worker ||
302             stacktree.peek() instanceof Worker){
303                 stacktree.pop();
304             }
305         }
306     }
307
308     /**
309      * Receive notification of character data inside an element.
310      *
311      * <p>By default, do nothing. Application writers may override this
312      * method to take specific actions for each chunk of character data
313      * (such as adding the data to a node or buffer, or printing it to
314      * a file).</p>
315      *
316      * @param ch The characters.
317      * @param start The start position in the character array.
318      * @param length The number of characters to use from the
319      * character array.
320      * @exception org.xml.sax.SAXException Any SAX exception, possibly
321      * wrapping another exception.
322      * @see org.xml.sax.ContentHandler#characters
323      */

324     public void characters (char ch[], int start, int length)
325     throws SAXException JavaDoc
326     {
327     }
328
329     /**
330      * Convienance method for parsing long. If the string
331      * was not a number, the method returns zero.
332      * @param data
333      * @return the value as a long
334      */

335     public long parseLong(String JavaDoc data){
336         long val = 0;
337         if (data.length() > 0){
338             try {
339                 val = Long.parseLong(data);
340             } catch (NumberFormatException JavaDoc e){
341                 val = 0;
342             }
343         }
344         return val;
345     }
346     
347     /**
348      * Convienance method for parsing integers.
349      * @param data
350      * @return the value as an integer
351      */

352     public int parseInt(String JavaDoc data){
353         int val = 0;
354         if (data.length() > 0){
355             try {
356                 val = Integer.parseInt(data);
357             } catch (NumberFormatException JavaDoc e){
358                 val = 0;
359             }
360         }
361         return val;
362     }
363     
364     /**
365      * method returns the status object.
366      * @return the status
367      */

368     public Status getContents(){
369         return this.status;
370     }
371 }
372
Popular Tags