001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ComponentStatisticsDataReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.esb.management.common.data.helper;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.Reader;
036: import java.io.Serializable;
037: import java.io.StringReader;
038: import java.net.MalformedURLException;
039: import java.net.URI;
040: import java.net.URISyntaxException;
041: import java.util.HashMap;
042: import java.util.Map;
043: import java.util.Stack;
044:
045: import javax.xml.parsers.ParserConfigurationException;
046: import javax.xml.parsers.SAXParser;
047: import javax.xml.parsers.SAXParserFactory;
048:
049: import org.xml.sax.Attributes;
050: import org.xml.sax.InputSource;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.helpers.DefaultHandler;
053:
054: import com.sun.esb.management.common.data.ComponentStatisticsData;
055:
056: /**
057: * @author graj
058: *
059: */
060: public class ComponentStatisticsDataReader extends DefaultHandler
061: implements ComponentStatisticsDataXMLConstants, Serializable {
062:
063: static final long serialVersionUID = -1L;
064:
065: // Private members needed to parse the XML document
066:
067: // keep track of parsing
068: private boolean parsingInProgress;
069:
070: // keep track of QName
071: private Stack<String> qNameStack = new Stack<String>();
072:
073: private String componentStatisticsDataListVersion;
074:
075: private ComponentStatisticsData data;
076:
077: private Map<String /* instanceName */, ComponentStatisticsData> dataMap;
078:
079: /** Constructor - creates a new instance of ComponentStatisticsDataReader */
080: public ComponentStatisticsDataReader() {
081: }
082:
083: /**
084: * @return the dataMap
085: */
086: public Map<String /* instanceName */, ComponentStatisticsData> getComponentStatisticsDataMap() {
087: return this .dataMap;
088: }
089:
090: /**
091: * Start of document processing.
092: *
093: * @throws org.xml.sax.SAXException
094: * is any SAX exception, possibly wrapping another exception.
095: */
096: public void startDocument() throws SAXException {
097: parsingInProgress = true;
098: qNameStack.removeAllElements();
099: }
100:
101: /**
102: * End of document processing.
103: *
104: * @throws org.xml.sax.SAXException
105: * is any SAX exception, possibly wrapping another exception.
106: */
107: public void endDocument() throws SAXException {
108: parsingInProgress = false;
109: // We have encountered the end of the document. Do any processing that
110: // is desired, for example dump all collected element2 values.
111:
112: }
113:
114: /**
115: * Process the new element.
116: *
117: * @param uri
118: * is the Namespace URI, or the empty string if the element has
119: * no Namespace URI or if Namespace processing is not being
120: * performed.
121: * @param localName
122: * is the The local name (without prefix), or the empty string if
123: * Namespace processing is not being performed.
124: * @param qName
125: * is the qualified name (with prefix), or the empty string if
126: * qualified names are not available.
127: * @param attributes
128: * is the attributes attached to the element. If there are no
129: * attributes, it shall be an empty Attributes object.
130: * @throws org.xml.sax.SAXException
131: * is any SAX exception, possibly wrapping another exception.
132: */
133: public void startElement(String uri, String localName,
134: String qName, Attributes attributes) throws SAXException {
135: if (qName != null) {
136: if (qName.endsWith(COMPONENT_STATISTICS_DATA_LIST_KEY)) {
137: // ELEMENT1 has an attribute, get it by name
138: // Do something with the attribute
139: if ((attributes != null)
140: && (attributes.getLength() > 0)) {
141: String namespace = attributes
142: .getValue(NAMESPACE_KEY);
143: // //////////////////////////////////////////////////////
144: // Read performanceDataListVersion attribute and ensure you
145: // store the right
146: // performanceDataListVersion of the report map list
147: // //////////////////////////////////////////////////////
148: this .componentStatisticsDataListVersion = attributes
149: .getValue(VERSION_KEY);
150: if ((componentStatisticsDataListVersion != null)
151: && (VERSION_VALUE
152: .equals(componentStatisticsDataListVersion))) {
153: this .dataMap = new HashMap<String /* instanceName */, ComponentStatisticsData>();
154: } else {
155: // Invalid componentStatisticsDataListVersion.
156: // Not storing it
157: }
158: }
159: } else if (qName.endsWith(COMPONENT_STATISTICS_DATA_KEY)) {
160: // ELEMENT1 has an attribute, get it by name
161: // Do something with the attribute
162: if (this .dataMap != null) {
163: this .data = new ComponentStatisticsData();
164: }
165: }
166: // Keep track of QNames
167: qNameStack.push(qName);
168: }
169: }
170:
171: /**
172: * Process the character report for current tag.
173: *
174: * @param ch
175: * are the element's characters.
176: * @param start
177: * is the start position in the character array.
178: * @param length
179: * is the number of characters to use from the character array.
180: * @throws org.xml.sax.SAXException
181: * is any SAX exception, possibly wrapping another exception.
182: */
183: public void characters(char[] ch, int start, int length)
184: throws SAXException {
185: String qName;
186: String chars = new String(ch, start, length);
187: // Get current QName
188: qName = (String) qNameStack.peek();
189: if (qName.endsWith(INSTANCE_NAME_KEY)) {
190: if (this .data != null) {
191: this .data.setInstanceName(chars);
192: }
193: } else if (qName.endsWith(COMPONENT_UPTIME_KEY)) {
194: if (this .data != null) {
195: long value = Long.valueOf(chars).longValue();
196: this .data.setComponentUpTime(value);
197: }
198: } else if (qName.endsWith(NUMBER_OF_ACTIVATED_ENDPOINTS_KEY)) {
199: if (this .data != null) {
200: long value = Long.valueOf(chars).longValue();
201: this .data.setNumberOfActivatedEndpoints(value);
202: }
203: } else if (qName.endsWith(NUMBER_OF_RECEIVED_REQUESTS_KEY)) {
204: if (this .data != null) {
205: long value = Long.valueOf(chars).longValue();
206: this .data.setNumberOfReceivedRequests(value);
207: }
208: } else if (qName.endsWith(NUMBER_OF_SENT_REQUESTS_KEY)) {
209: if (this .data != null) {
210: long value = Long.valueOf(chars).longValue();
211: this .data.setNumberOfSentRequests(value);
212: }
213: } else if (qName.endsWith(NUMBER_OF_RECEIVED_REPLIES_KEY)) {
214: if (this .data != null) {
215: long value = Long.valueOf(chars).longValue();
216: this .data.setNumberOfReceivedReplies(value);
217: }
218: } else if (qName.endsWith(NUMBER_OF_SENT_REPLIES_KEY)) {
219: if (this .data != null) {
220: long value = Long.valueOf(chars).longValue();
221: this .data.setNumberOfSentReplies(value);
222: }
223: } else if (qName.endsWith(NUMBER_OF_RECEIVED_DONES_KEY)) {
224: if (this .data != null) {
225: long value = Long.valueOf(chars).longValue();
226: this .data.setNumberOfReceivedDones(value);
227: }
228: } else if (qName.endsWith(NUMBER_OF_SENT_DONES_KEY)) {
229: if (this .data != null) {
230: long value = Long.valueOf(chars).longValue();
231: this .data.setNumberOfSentDones(value);
232: }
233: } else if (qName.endsWith(NUMBER_OF_RECEIVED_FAULTS_KEY)) {
234: if (this .data != null) {
235: long value = Long.valueOf(chars).longValue();
236: this .data.setNumberOfReceivedFaults(value);
237: }
238: } else if (qName.endsWith(NUMBER_OF_SENT_FAULTS_KEY)) {
239: if (this .data != null) {
240: long value = Long.valueOf(chars).longValue();
241: this .data.setNumberOfSentFaults(value);
242: }
243: } else if (qName.endsWith(NUMBER_OF_RECEIVED_ERRORS_KEY)) {
244: if (this .data != null) {
245: long value = Long.valueOf(chars).longValue();
246: this .data.setNumberOfReceivedErrors(value);
247: }
248: } else if (qName.endsWith(NUMBER_OF_SENT_ERRORS_KEY)) {
249: if (this .data != null) {
250: long value = Long.valueOf(chars).longValue();
251: this .data.setNumberOfSentErrors(value);
252: }
253: } else if (qName.endsWith(NUMBER_OF_COMPLETED_EXCHANGES_KEY)) {
254: if (this .data != null) {
255: long value = Long.valueOf(chars).longValue();
256: this .data.setNumberOfCompletedExchanges(value);
257: }
258: } else if (qName.endsWith(NUMBER_OF_ACTIVE_EXCHANGES_KEY)) {
259: if (this .data != null) {
260: long value = Long.valueOf(chars).longValue();
261: this .data.setNumberOfActiveExchanges(value);
262: }
263: } else if (qName.endsWith(NUMBER_OF_ERROR_EXCHANGES_KEY)) {
264: if (this .data != null) {
265: long value = Long.valueOf(chars).longValue();
266: this .data.setNumberOfErrorExchanges(value);
267: }
268: } else if (qName
269: .endsWith(MESSAGE_EXCHANGE_RESPONSE_TIME_AVERAGE_KEY)) {
270: if (this .data != null) {
271: long value = Long.valueOf(chars).longValue();
272: this .data.setMessageExchangeResponseTimeAverage(value);
273: }
274: } else if (qName
275: .endsWith(MESSAGE_EXCHANGE_COMPONENT_TIME_AVERAGE_KEY)) {
276: if (this .data != null) {
277: long value = Long.valueOf(chars).longValue();
278: this .data.setMessageExchangeComponentTimeAverage(value);
279: }
280: } else if (qName
281: .endsWith(MESSAGE_EXCHANGE_DELIVERY_CHANNEL_TIME_AVERAGE_KEY)) {
282: if (this .data != null) {
283: long value = Long.valueOf(chars).longValue();
284: this .data
285: .setMessageExchangeDeliveryChannelTimeAverage(value);
286: }
287: } else if (qName
288: .endsWith(MESSAGE_EXCHANGE_MESSAGE_SERVICE_TIME_AVERAGE_KEY)) {
289: if (this .data != null) {
290: long value = Long.valueOf(chars).longValue();
291: this .data
292: .setMessageExchangeMessageServiceTimeAverage(value);
293: }
294: } else if (qName.endsWith(COMPONENT_EXTENSION_STATUS_KEY)) {
295: if (this .data != null) {
296: this .data.setComponentExtensionStatusAsString(chars);
297: }
298: }
299: }
300:
301: /**
302: * Process the end element tag.
303: *
304: * @param uri
305: * is the Namespace URI, or the empty string if the element has
306: * no Namespace URI or if Namespace processing is not being
307: * performed.
308: * @param localName
309: * is the The local name (without prefix), or the empty string if
310: * Namespace processing is not being performed.
311: * @param qName
312: * is the qualified name (with prefix), or the empty string if
313: * qualified names are not available.
314: * @throws org.xml.sax.SAXException
315: * is any SAX exception, possibly wrapping another exception.
316: */
317: public void endElement(String uri, String localName, String qName)
318: throws SAXException {
319: // Pop QName, since we are done with it
320: qNameStack.pop();
321: if (qName != null) {
322: if (qName.endsWith(COMPONENT_STATISTICS_DATA_KEY)) {
323: // We have encountered the end of ELEMENT1
324: // ...
325: if ((this .dataMap != null) && (this .data != null)) {
326: this .dataMap.put(this .data.getInstanceName(),
327: this .data);
328: this .data = null;
329: }
330: }
331: }
332: }
333:
334: /**
335: *
336: * @param rawXMLData
337: * @return
338: * @throws MalformedURLException
339: * @throws ParserConfigurationException
340: * @throws SAXException
341: * @throws URISyntaxException
342: * @throws IOException
343: */
344: public static Map<String /* instanceName */, ComponentStatisticsData> parseFromXMLData(
345: String rawXMLData) throws MalformedURLException,
346: ParserConfigurationException, SAXException,
347: URISyntaxException, IOException {
348: // System.out.println("Parsing file: "+uriString);
349: // Get an instance of the SAX parser factory
350: SAXParserFactory factory = SAXParserFactory.newInstance();
351:
352: // Get an instance of the SAX parser
353: SAXParser saxParser = factory.newSAXParser();
354:
355: // Initialize the XML Document InputStream
356: Reader reader = new StringReader(rawXMLData);
357:
358: // Create an InputSource from the InputStream
359: InputSource inputSource = new InputSource(reader);
360:
361: // Parse the aspectInput XML document stream, using my event handler
362: ComponentStatisticsDataReader parser = new ComponentStatisticsDataReader();
363: saxParser.parse(inputSource, parser);
364:
365: return parser.getComponentStatisticsDataMap();
366:
367: }
368:
369: /**
370: *
371: * @param fileName
372: * @return
373: * @throws MalformedURLException
374: * @throws ParserConfigurationException
375: * @throws SAXException
376: * @throws URISyntaxException
377: * @throws IOException
378: */
379: public static Map<String /* instanceName */, ComponentStatisticsData> parseFromFile(
380: String fileName) throws MalformedURLException,
381: ParserConfigurationException, SAXException,
382: URISyntaxException, IOException {
383: File file = new File(fileName);
384: return parseFromFile(file);
385: }
386:
387: /**
388: *
389: * @param fileName
390: * @return
391: * @throws MalformedURLException
392: * @throws ParserConfigurationException
393: * @throws SAXException
394: * @throws URISyntaxException
395: * @throws IOException
396: */
397: public static Map<String /* instanceName */, ComponentStatisticsData> parseFromFile(
398: File file) throws MalformedURLException,
399: ParserConfigurationException, SAXException,
400: URISyntaxException, IOException {
401:
402: // Get an instance of the SAX parser factory
403: SAXParserFactory factory = SAXParserFactory.newInstance();
404:
405: // Get an instance of the SAX parser
406: SAXParser saxParser = factory.newSAXParser();
407:
408: // Initialize the URI and XML Document InputStream
409: InputStream inputStream = new FileInputStream(file);
410:
411: // Create an InputSource from the InputStream
412: InputSource inputSource = new InputSource(inputStream);
413:
414: // Parse the aspectInput XML document stream, using my event handler
415: ComponentStatisticsDataReader parser = new ComponentStatisticsDataReader();
416: saxParser.parse(inputSource, parser);
417:
418: return parser.getComponentStatisticsDataMap();
419: }
420:
421: /**
422: *
423: * @param uriString
424: * @return
425: * @throws MalformedURLException
426: * @throws ParserConfigurationException
427: * @throws SAXException
428: * @throws URISyntaxException
429: * @throws IOException
430: */
431: public static Map<String /* instanceName */, ComponentStatisticsData> parseFromURI(
432: String uriString) throws MalformedURLException,
433: ParserConfigurationException, SAXException,
434: URISyntaxException, IOException {
435: URI uri = new URI(uriString);
436: return parseFromURI(uri);
437: }
438:
439: /**
440: *
441: * @param uri
442: * @return
443: * @throws MalformedURLException
444: * @throws ParserConfigurationException
445: * @throws SAXException
446: * @throws URISyntaxException
447: * @throws IOException
448: */
449: public static Map<String /* instanceName */, ComponentStatisticsData> parseFromURI(
450: URI uri) throws MalformedURLException,
451: ParserConfigurationException, SAXException,
452: URISyntaxException, IOException {
453:
454: // Get an instance of the SAX parser factory
455: SAXParserFactory factory = SAXParserFactory.newInstance();
456:
457: // Get an instance of the SAX parser
458: SAXParser saxParser = factory.newSAXParser();
459:
460: // Initialize the URI and XML Document InputStream
461: InputStream inputStream = uri.toURL().openStream();
462:
463: // Create an InputSource from the InputStream
464: InputSource inputSource = new InputSource(inputStream);
465:
466: // Parse the aspectInput XML document stream, using my event handler
467: ComponentStatisticsDataReader parser = new ComponentStatisticsDataReader();
468: saxParser.parse(inputSource, parser);
469:
470: return parser.getComponentStatisticsDataMap();
471: }
472:
473: /**
474: * @param args
475: */
476: public static void main(String[] args) {
477: String uri = "C:/test/schema/componentstatistics/ComponentStatisticsData.xml";
478: try {
479: Map<String /* instanceName */, ComponentStatisticsData> map = null;
480: map = ComponentStatisticsDataReader.parseFromFile(uri);
481: for (String instanceName : map.keySet()) {
482: System.out.println(map.get(instanceName)
483: .getDisplayString());
484: }
485: } catch (MalformedURLException e) {
486: e.printStackTrace();
487: } catch (ParserConfigurationException e) {
488: e.printStackTrace();
489: } catch (SAXException e) {
490: e.printStackTrace();
491: } catch (URISyntaxException e) {
492: e.printStackTrace();
493: } catch (IOException e) {
494: e.printStackTrace();
495: }
496: }
497:
498: }
|