1 17 package org.apache.jmeter.monitor.parser; 18 19 import java.util.Stack ; 21 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.helpers.DefaultHandler ; 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 39 { 40 private Stack stacktree = new Stack (); 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 55 58 public MonitorHandler() 59 { 60 super(); 61 } 62 63 67 public void setObjectFactory(ObjectFactory factory){ 68 this.factory = factory; 69 } 70 71 public void startDocument () 72 throws SAXException 73 { 74 } 76 77 public void endDocument () 78 throws SAXException 79 { 80 } 83 84 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(); 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 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 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 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 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 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 268 public void endElement (String uri, String localName, String qName) 269 throws SAXException 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 324 public void characters (char ch[], int start, int length) 325 throws SAXException 326 { 327 } 328 329 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 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 368 public Status getContents(){ 369 return this.status; 370 } 371 } 372 | Popular Tags |