001 // $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 $ 002 /* 003 * Copyright 2004 The Apache Software Foundation. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.jmeter.monitor.parser; 018 019 //import java.util.List; 020 import java.util.Stack; 021 022 import org.xml.sax.Attributes; 023 import org.xml.sax.SAXException; 024 import org.xml.sax.helpers.DefaultHandler; 025 026 import org.apache.jmeter.monitor.model.ObjectFactory; 027 import org.apache.jmeter.monitor.model.Connector; 028 import org.apache.jmeter.monitor.model.Jvm; 029 import org.apache.jmeter.monitor.model.Memory; 030 import org.apache.jmeter.monitor.model.RequestInfo; 031 import org.apache.jmeter.monitor.model.Status; 032 import org.apache.jmeter.monitor.model.StatusImpl; 033 import org.apache.jmeter.monitor.model.ThreadInfo; 034 import org.apache.jmeter.monitor.model.Worker; 035 import org.apache.jmeter.monitor.model.Workers; 036 import org.apache.jmeter.monitor.model.WorkersImpl; 037 038 public class MonitorHandler extends DefaultHandler 039 { 040 //private boolean startDoc = false; 041 //private boolean endDoc = false; 042 private Stack stacktree = new Stack(); 043 044 private ObjectFactory factory = null; 045 private Status status = null; 046 private Jvm jvm = null; 047 private Memory memory = null; 048 private Connector connector = null; 049 private ThreadInfo threadinfo = null; 050 private RequestInfo requestinfo = null; 051 private Worker worker = null; 052 private Workers workers = null; 053 //private List workerslist = null; 054 055 /** 056 * 057 */ 058 public MonitorHandler() 059 { 060 super(); 061 } 062 063 /** 064 * Set the ObjectFactory used to create new 065 * @param factory 066 */ 067 public void setObjectFactory(ObjectFactory factory){ 068 this.factory = factory; 069 } 070 071 public void startDocument () 072 throws SAXException 073 { 074 //this.startDoc = true; 075 } 076 077 public void endDocument () 078 throws SAXException 079 { 080 //this.startDoc = false; 081 //this.endDoc = true; 082 } 083 084 /** 085 * Receive notification of the start of an element. 086 * 087 * <p>By default, do nothing. Application writers may override this 088 * method in a subclass to take specific actions at the start of 089 * each element (such as allocating a new tree node or writing 090 * output to a file).</p> 091 * 092 * @param uri 093 * @param localName The element type name. 094 * @param qName 095 * @param attributes The specified or defaulted attributes. 096 * @exception org.xml.sax.SAXException Any SAX exception, possibly 097 * wrapping another exception. 098 * @see org.xml.sax.ContentHandler#startElement 099 */ 100 public void startElement (String uri, String localName, 101 String qName, Attributes attributes) 102 throws SAXException 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(); Rate109 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(); Rate115 if (stacktree.peek() instanceof Jvm){ 116 stacktree.push(memory); 117 if (attributes != null){ 118 for (int idx=0; idx < attributes.getLength(); idx++){ 119 String 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(); Rate136 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 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(); Rate151 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 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(); Rate178 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 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(); Rate208 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(); Rate214 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 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 uri, String localName, String qName) 269 throws SAXException 270 { 271 if (qName.equals(Constants.STATUS)){ Rate272 if (stacktree.peek() instanceof Status){ 273 stacktree.pop(); 274 } 275 } else if (qName.equals(Constants.JVM)){ Rate276 if (stacktree.peek() instanceof Jvm){ 277 stacktree.pop(); 278 } 279 } else if (qName.equals(Constants.MEMORY)){ Rate280 if (stacktree.peek() instanceof Memory){ 281 stacktree.pop(); 282 } 283 } else if (qName.equals(Constants.CONNECTOR)){ Rate284 if (stacktree.peek() instanceof Connector || 285 stacktree.peek() instanceof Connector){ 286 stacktree.pop(); 287 } 288 } else if (qName.equals(Constants.THREADINFO)){ Rate289 if (stacktree.peek() instanceof ThreadInfo){ 290 stacktree.pop(); 291 } 292 } else if (qName.equals(Constants.REQUESTINFO)){ Rate293 if (stacktree.peek() instanceof RequestInfo){ 294 stacktree.pop(); 295 } 296 } else if (qName.equals(Constants.WORKERS)){ Rate297 if (stacktree.peek() instanceof Workers){ 298 stacktree.pop(); 299 } 300 } else if (qName.equals(Constants.WORKER)){ Rate301 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 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 data){ 336 long val = 0; 337 if (data.length() > 0){ 338 try { 339 val = Long.parseLong(data); 340 } catch (NumberFormatException 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 data){ 353 int val = 0; 354 if (data.length() > 0){ 355 try { 356 val = Integer.parseInt(data); 357 } catch (NumberFormatException 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 }