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: * @(#)EndpointStatisticsDataWriter.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.BufferedWriter;
032: import java.io.File;
033: import java.io.FileNotFoundException;
034: import java.io.FileWriter;
035: import java.io.IOException;
036: import java.io.Serializable;
037: import java.io.StringWriter;
038: import java.io.Writer;
039: import java.net.MalformedURLException;
040: import java.net.URISyntaxException;
041: import java.util.Map;
042:
043: import javax.xml.parsers.DocumentBuilder;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046: import javax.xml.transform.OutputKeys;
047: import javax.xml.transform.Transformer;
048: import javax.xml.transform.TransformerConfigurationException;
049: import javax.xml.transform.TransformerException;
050: import javax.xml.transform.TransformerFactory;
051: import javax.xml.transform.dom.DOMSource;
052: import javax.xml.transform.stream.StreamResult;
053:
054: import org.w3c.dom.Document;
055: import org.w3c.dom.Element;
056: import org.xml.sax.SAXException;
057:
058: import com.sun.esb.management.common.data.ConsumingEndpointStatisticsData;
059: import com.sun.esb.management.common.data.IEndpointStatisticsData;
060: import com.sun.esb.management.common.data.PerformanceData;
061: import com.sun.esb.management.common.data.ProvisioningEndpointStatisticsData;
062:
063: /**
064: * @author graj
065: *
066: */
067: public class EndpointStatisticsDataWriter implements
068: EndpointStatisticsDataXMLConstants, Serializable {
069:
070: static final long serialVersionUID = -1L;
071:
072: static final String FILE_NAME_KEY = "EndpointStatisticsData.xml";
073:
074: /** Constructor - Creates an EndpointStatisticsDataWriter */
075: public EndpointStatisticsDataWriter() {
076: }
077:
078: /**
079: *
080: * @param document
081: * @param directoryPath
082: * @throws TransformerConfigurationException
083: * @throws TransformerException
084: * @throws Exception
085: */
086: public static void writeToFile(Document document,
087: String directoryPath)
088: throws TransformerConfigurationException,
089: TransformerException, Exception {
090: File file = new File(directoryPath);
091: if ((file.isDirectory() == false) || (file.exists() == false)) {
092: throw new Exception("Directory Path: " + directoryPath
093: + " is invalid.");
094: }
095: String fileLocation = file.getAbsolutePath() + File.separator
096: + FILE_NAME_KEY;
097: System.out.println("Writing out to file: " + fileLocation);
098: File outputFile = new File(fileLocation);
099: // Use a Transformer for aspectOutput
100: TransformerFactory tFactory = TransformerFactory.newInstance();
101: Transformer transformer = tFactory.newTransformer();
102: DOMSource source = new DOMSource(document);
103: StreamResult result = new StreamResult(outputFile);
104:
105: // indent the Output to make it more legible...
106: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
107: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
108: transformer
109: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
110: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
111: transformer.setOutputProperty(
112: "{http://xml.apache.org/xslt}indent-amount", "4");
113: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
114:
115: transformer.transform(source, result);
116: }
117:
118: /**
119: * Change the contents of text file in its entirety, overwriting any
120: * existing text. This style of implementation throws all exceptions to the
121: * caller.
122: *
123: * @param aFile
124: * is an existing file which can be written to.
125: * @throws IllegalArgumentException
126: * if param does not comply.
127: * @throws FileNotFoundException
128: * if the file does not exist.
129: * @throws IOException
130: * if problem encountered during write.
131: */
132: public static void setContents(File aFile, String aContents)
133: throws FileNotFoundException, IOException {
134: if (aFile == null) {
135: throw new IllegalArgumentException(
136: "File should not be null.");
137: }
138: if (!aFile.exists()) {
139: aFile.createNewFile();
140: }
141: if (!aFile.isFile()) {
142: throw new IllegalArgumentException(
143: "Should not be a directory: " + aFile);
144: }
145: if (!aFile.canWrite()) {
146: throw new IllegalArgumentException(
147: "File cannot be written: " + aFile);
148: }
149:
150: // declared here only to make visible to finally clause; generic
151: // reference
152: Writer output = null;
153: try {
154: // use buffering
155: // FileWriter always assumes default encoding is OK!
156: output = new BufferedWriter(new FileWriter(aFile));
157: output.write(aContents);
158: } finally {
159: // flush and close both "aspectOutput" and its underlying FileWriter
160: if (output != null) {
161: output.close();
162: }
163: }
164: }
165:
166: /**
167: *
168: * @param NMRStatisticsData
169: * data
170: * @return XML string
171: * @throws ParserConfigurationException
172: * @throws TransformerException
173: */
174: public static String serialize(
175: Map<String /* instanceName */, IEndpointStatisticsData> dataMap)
176: throws ParserConfigurationException, TransformerException {
177: Document document = null;
178: EndpointStatisticsDataWriter writer = new EndpointStatisticsDataWriter();
179: if (dataMap != null) {
180: DocumentBuilderFactory factory = DocumentBuilderFactory
181: .newInstance();
182: DocumentBuilder builder = factory.newDocumentBuilder();
183: document = builder.newDocument(); // Create from whole cloth
184:
185: // ////////////////////////////////
186: // <EndpointStatisticsDataList>
187: Element root = (Element) document
188: .createElement(EndpointStatisticsDataXMLConstants.ENDPOINT_STATISTICS_DATA_LIST_KEY);
189: // xmlns="http://java.sun.com/xml/ns/esb/management/EndpointStatisticsData"
190: root
191: .setAttribute(
192: EndpointStatisticsDataXMLConstants.ENDPOINT_NAMESPACE_KEY,
193: EndpointStatisticsDataXMLConstants.ENDPOINT_NAMESPACE_VALUE);
194: // version = "1.0"
195: root
196: .setAttribute(
197: EndpointStatisticsDataXMLConstants.ENDPOINT_VERSION_KEY,
198: EndpointStatisticsDataXMLConstants.ENDPOINT_VERSION_VALUE);
199:
200: for (String instanceName : dataMap.keySet()) {
201: IEndpointStatisticsData data = dataMap
202: .get(instanceName);
203: if (data.isProvisioningEndpoint() == true) {
204: // ////////////////////////////////
205: // <ProvisioningEndpointStatisticsData>
206: Element endpointStatisticsDataElementChild = writer
207: .createProvisioningEndpointStatisticsDataElement(
208: document,
209: (ProvisioningEndpointStatisticsData) data);
210: // </ProvisioningEndpointStatisticsData>
211: root
212: .appendChild(endpointStatisticsDataElementChild);
213: // ////////////////////////////////
214: } else {
215: // ////////////////////////////////
216: // <ConsumingEndpointStatisticsData>
217: Element endpointStatisticsDataElementChild = writer
218: .createConsumingEndpointStatisticsDataElement(
219: document,
220: (ConsumingEndpointStatisticsData) data);
221: // </ConsumingEndpointStatisticsData>
222: root
223: .appendChild(endpointStatisticsDataElementChild);
224: // ////////////////////////////////
225: }
226: }
227:
228: // ////////////////////////////////
229: // </EndpointStatisticsDataList>
230: document.appendChild(root);
231: // ////////////////////////////////
232:
233: }
234: return writer.writeToString(document);
235: }
236:
237: /**
238: *
239: * @param document
240: * @param data
241: * @return
242: */
243: protected Element createProvisioningEndpointStatisticsDataElement(
244: Document document,
245: ProvisioningEndpointStatisticsData provisioningData) {
246: Element endpointStatisticsDataElement = null;
247: if ((document != null) && (provisioningData != null)) {
248:
249: // <ProvisioningEndpointStatisticsData>
250: endpointStatisticsDataElement = document
251: .createElement(EndpointStatisticsDataXMLConstants.PROVISIONING_ENDPOINT_STATISTICS_DATA_KEY);
252:
253: // Write all the Provisioning Endpoints first
254: Element activationTimeElementChild = document
255: .createElement(EndpointStatisticsDataXMLConstants.ACTIVATION_TIME_KEY);
256: if (activationTimeElementChild != null) {
257: activationTimeElementChild
258: .setTextContent(provisioningData
259: .getActivationTime()
260: + "");
261: }
262: endpointStatisticsDataElement
263: .appendChild(activationTimeElementChild);
264:
265: Element numberOfActiveExchangesElementChild = document
266: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_ACTIVE_EXCHANGES_KEY);
267: if (numberOfActiveExchangesElementChild != null) {
268: numberOfActiveExchangesElementChild
269: .setTextContent(provisioningData
270: .getNumberOfActiveExchanges()
271: + "");
272: }
273: endpointStatisticsDataElement
274: .appendChild(numberOfActiveExchangesElementChild);
275:
276: Element numberOfReceivedRequestsElementChild = document
277: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_REQUESTS_KEY);
278: if (numberOfReceivedRequestsElementChild != null) {
279: numberOfReceivedRequestsElementChild
280: .setTextContent(provisioningData
281: .getNumberOfReceivedRequests()
282: + "");
283: }
284: endpointStatisticsDataElement
285: .appendChild(numberOfReceivedRequestsElementChild);
286:
287: Element numberOfSentRepliesElementChild = document
288: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_REPLIES_KEY);
289: if (numberOfSentRepliesElementChild != null) {
290: numberOfSentRepliesElementChild
291: .setTextContent(provisioningData
292: .getNumberOfSentReplies()
293: + "");
294: }
295: endpointStatisticsDataElement
296: .appendChild(numberOfSentRepliesElementChild);
297:
298: Element uptimeElementChild = document
299: .createElement(EndpointStatisticsDataXMLConstants.UPTIME_KEY);
300: if (uptimeElementChild != null) {
301: uptimeElementChild.setTextContent(provisioningData
302: .getUptime()
303: + "");
304: }
305: endpointStatisticsDataElement
306: .appendChild(uptimeElementChild);
307:
308: Element messageExchangeResponseTimeAverageElementChild = document
309: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_RESPONSE_TIME_AVERAGE_KEY);
310: if (messageExchangeResponseTimeAverageElementChild != null) {
311: messageExchangeResponseTimeAverageElementChild
312: .setTextContent(provisioningData
313: .getMessageExchangeResponseTimeAverage()
314: + "");
315: }
316: endpointStatisticsDataElement
317: .appendChild(messageExchangeResponseTimeAverageElementChild);
318:
319: // Write all the base Endpoint Statistics Data.
320: Element instanceNameElementChild = document
321: .createElement(EndpointStatisticsDataXMLConstants.INSTANCE_NAME_KEY);
322: if (instanceNameElementChild != null) {
323: instanceNameElementChild
324: .setTextContent(provisioningData
325: .getInstanceName());
326: }
327: endpointStatisticsDataElement
328: .appendChild(instanceNameElementChild);
329:
330: Element componentNameElementChild = document
331: .createElement(EndpointStatisticsDataXMLConstants.COMPONENT_NAME_KEY);
332: if (componentNameElementChild != null) {
333: componentNameElementChild
334: .setTextContent(provisioningData
335: .getComponentName());
336: }
337: endpointStatisticsDataElement
338: .appendChild(componentNameElementChild);
339:
340: Element messageExchangeComponentTimeAverageElementChild = document
341: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_COMPONENT_TIME_AVERAGE_KEY);
342: if (messageExchangeComponentTimeAverageElementChild != null) {
343: messageExchangeComponentTimeAverageElementChild
344: .setTextContent(provisioningData
345: .getMessageExchangeComponentTimeAverage()
346: + "");
347: }
348: endpointStatisticsDataElement
349: .appendChild(messageExchangeComponentTimeAverageElementChild);
350:
351: Element messageExchangeDeliveryChannelTimeAverageElementChild = document
352: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_DELIVERY_CHANNEL_TIME_AVERAGE_KEY);
353: if (messageExchangeDeliveryChannelTimeAverageElementChild != null) {
354: messageExchangeDeliveryChannelTimeAverageElementChild
355: .setTextContent(provisioningData
356: .getMessageExchangeDeliveryChannelTimeAverage()
357: + "");
358: }
359: endpointStatisticsDataElement
360: .appendChild(messageExchangeDeliveryChannelTimeAverageElementChild);
361:
362: Element messageExchangeServiceTimeAverageElementChild = document
363: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_SERVICE_TIME_AVERAGE_KEY);
364: if (messageExchangeServiceTimeAverageElementChild != null) {
365: messageExchangeServiceTimeAverageElementChild
366: .setTextContent(provisioningData
367: .getMessageExchangeServiceTimeAverage()
368: + "");
369: }
370: endpointStatisticsDataElement
371: .appendChild(messageExchangeServiceTimeAverageElementChild);
372:
373: Element numberOfReceivedDonesElementChild = document
374: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_DONES_KEY);
375: if (numberOfReceivedDonesElementChild != null) {
376: numberOfReceivedDonesElementChild
377: .setTextContent(provisioningData
378: .getNumberOfReceivedDones()
379: + "");
380: }
381: endpointStatisticsDataElement
382: .appendChild(numberOfReceivedDonesElementChild);
383:
384: Element numberOfReceivedErrorsElementChild = document
385: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_ERRORS_KEY);
386: if (numberOfReceivedErrorsElementChild != null) {
387: numberOfReceivedErrorsElementChild
388: .setTextContent(provisioningData
389: .getNumberOfReceivedErrors()
390: + "");
391: }
392: endpointStatisticsDataElement
393: .appendChild(numberOfReceivedErrorsElementChild);
394:
395: Element numberOfReceivedFaultsElementChild = document
396: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_FAULTS_KEY);
397: if (numberOfReceivedFaultsElementChild != null) {
398: numberOfReceivedFaultsElementChild
399: .setTextContent(provisioningData
400: .getNumberOfReceivedFaults()
401: + "");
402: }
403: endpointStatisticsDataElement
404: .appendChild(numberOfReceivedFaultsElementChild);
405:
406: Element numberOfSentDonesElementChild = document
407: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_DONES_KEY);
408: if (numberOfSentDonesElementChild != null) {
409: numberOfSentDonesElementChild
410: .setTextContent(provisioningData
411: .getNumberOfSentDones()
412: + "");
413: }
414: endpointStatisticsDataElement
415: .appendChild(numberOfSentDonesElementChild);
416:
417: Element numberOfSentErrorsElementChild = document
418: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_ERRORS_KEY);
419: if (numberOfSentErrorsElementChild != null) {
420: numberOfSentErrorsElementChild
421: .setTextContent(provisioningData
422: .getNumberOfSentErrors()
423: + "");
424: }
425: endpointStatisticsDataElement
426: .appendChild(numberOfSentErrorsElementChild);
427:
428: Element numberOfSentFaultsElementChild = document
429: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_FAULTS_KEY);
430: if (numberOfSentFaultsElementChild != null) {
431: numberOfSentFaultsElementChild
432: .setTextContent(provisioningData
433: .getNumberOfSentFaults()
434: + "");
435: }
436: endpointStatisticsDataElement
437: .appendChild(numberOfSentFaultsElementChild);
438:
439: // ////////////////////////////////
440: // <PerformanceMeasurementDataList>
441: Element performanceDataListElement = (Element) document
442: .createElement(PerformanceDataXMLConstants.PERFORMANCE_MEASUREMENT_DATA_LIST_KEY);
443: // xmlns="http://java.sun.com/xml/ns/esb/management/PerformanceMeasurementDataList"
444: performanceDataListElement.setAttribute(
445: PerformanceDataXMLConstants.NAMESPACE_KEY,
446: PerformanceDataXMLConstants.NAMESPACE_VALUE);
447: // version = "1.0"
448: performanceDataListElement.setAttribute(
449: PerformanceDataXMLConstants.VERSION_KEY,
450: PerformanceDataXMLConstants.VERSION_VALUE);
451: Map<String, PerformanceData> dataMap = provisioningData
452: .getCategoryToPerformanceDataMap();
453:
454: for (String category : dataMap.keySet()) {
455: PerformanceData data = dataMap.get(category);
456: // ////////////////////////////////
457: // <PerformanceMeasurementData>
458: Element performanceMeasurementDataElementChild = this
459: .createPerformanceMeasurementDataElement(
460: document, data);
461: // </PerformanceMeasurementData>
462: performanceDataListElement
463: .appendChild(performanceMeasurementDataElementChild);
464: // ////////////////////////////////
465: }
466: // </PerformanceMeasurementDataList>
467: endpointStatisticsDataElement
468: .appendChild(performanceDataListElement);
469: // ////////////////////////////////
470: }
471:
472: return endpointStatisticsDataElement;
473: }
474:
475: /**
476: *
477: * @param document
478: * @param data
479: * @return
480: */
481: protected Element createConsumingEndpointStatisticsDataElement(
482: Document document,
483: ConsumingEndpointStatisticsData consumingData) {
484: Element endpointStatisticsDataElement = null;
485: if ((document != null) && (consumingData != null)) {
486:
487: // <ConsumingEndpointStatisticsData>
488: endpointStatisticsDataElement = document
489: .createElement(EndpointStatisticsDataXMLConstants.CONSUMING_ENDPOINT_STATISTICS_DATA_KEY);
490: // Write all the Consuming Endpoints.
491: Element numberOfReceivedRepliesElementChild = document
492: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_REPLIES_KEY);
493: if (numberOfReceivedRepliesElementChild != null) {
494: numberOfReceivedRepliesElementChild
495: .setTextContent(consumingData
496: .getNumberOfReceivedReplies()
497: + "");
498: }
499: endpointStatisticsDataElement
500: .appendChild(numberOfReceivedRepliesElementChild);
501:
502: Element numberOfSentRequestsElementChild = document
503: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_REQUESTS_KEY);
504: if (numberOfSentRequestsElementChild != null) {
505: numberOfSentRequestsElementChild
506: .setTextContent(consumingData
507: .getNumberOfSentRequests()
508: + "");
509: }
510: endpointStatisticsDataElement
511: .appendChild(numberOfSentRequestsElementChild);
512:
513: Element messageExchangeStatusTimeAverageElementChild = document
514: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_STATUS_TIME_AVERAGE_KEY);
515: if (messageExchangeStatusTimeAverageElementChild != null) {
516: messageExchangeStatusTimeAverageElementChild
517: .setTextContent(consumingData
518: .getMessageExchangeStatusTimeAverage()
519: + "");
520: }
521: endpointStatisticsDataElement
522: .appendChild(messageExchangeStatusTimeAverageElementChild);
523:
524: // Write all the base Endpoint Statistics Data.
525: Element instanceNameElementChild = document
526: .createElement(EndpointStatisticsDataXMLConstants.INSTANCE_NAME_KEY);
527: if (instanceNameElementChild != null) {
528: instanceNameElementChild.setTextContent(consumingData
529: .getInstanceName());
530: }
531: endpointStatisticsDataElement
532: .appendChild(instanceNameElementChild);
533:
534: Element componentNameElementChild = document
535: .createElement(EndpointStatisticsDataXMLConstants.COMPONENT_NAME_KEY);
536: if (componentNameElementChild != null) {
537: componentNameElementChild.setTextContent(consumingData
538: .getComponentName());
539: }
540: endpointStatisticsDataElement
541: .appendChild(componentNameElementChild);
542:
543: Element messageExchangeComponentTimeAverageElementChild = document
544: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_COMPONENT_TIME_AVERAGE_KEY);
545: if (messageExchangeComponentTimeAverageElementChild != null) {
546: messageExchangeComponentTimeAverageElementChild
547: .setTextContent(consumingData
548: .getMessageExchangeComponentTimeAverage()
549: + "");
550: }
551: endpointStatisticsDataElement
552: .appendChild(messageExchangeComponentTimeAverageElementChild);
553:
554: Element messageExchangeDeliveryChannelTimeAverageElementChild = document
555: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_DELIVERY_CHANNEL_TIME_AVERAGE_KEY);
556: if (messageExchangeDeliveryChannelTimeAverageElementChild != null) {
557: messageExchangeDeliveryChannelTimeAverageElementChild
558: .setTextContent(consumingData
559: .getMessageExchangeDeliveryChannelTimeAverage()
560: + "");
561: }
562: endpointStatisticsDataElement
563: .appendChild(messageExchangeDeliveryChannelTimeAverageElementChild);
564:
565: Element messageExchangeServiceTimeAverageElementChild = document
566: .createElement(EndpointStatisticsDataXMLConstants.MESSAGE_EXCHANGE_SERVICE_TIME_AVERAGE_KEY);
567: if (messageExchangeServiceTimeAverageElementChild != null) {
568: messageExchangeServiceTimeAverageElementChild
569: .setTextContent(consumingData
570: .getMessageExchangeServiceTimeAverage()
571: + "");
572: }
573: endpointStatisticsDataElement
574: .appendChild(messageExchangeServiceTimeAverageElementChild);
575:
576: Element numberOfReceivedDonesElementChild = document
577: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_DONES_KEY);
578: if (numberOfReceivedDonesElementChild != null) {
579: numberOfReceivedDonesElementChild
580: .setTextContent(consumingData
581: .getNumberOfReceivedDones()
582: + "");
583: }
584: endpointStatisticsDataElement
585: .appendChild(numberOfReceivedDonesElementChild);
586:
587: Element numberOfReceivedErrorsElementChild = document
588: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_ERRORS_KEY);
589: if (numberOfReceivedErrorsElementChild != null) {
590: numberOfReceivedErrorsElementChild
591: .setTextContent(consumingData
592: .getNumberOfReceivedErrors()
593: + "");
594: }
595: endpointStatisticsDataElement
596: .appendChild(numberOfReceivedErrorsElementChild);
597:
598: Element numberOfReceivedFaultsElementChild = document
599: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_RECEIVED_FAULTS_KEY);
600: if (numberOfReceivedFaultsElementChild != null) {
601: numberOfReceivedFaultsElementChild
602: .setTextContent(consumingData
603: .getNumberOfReceivedFaults()
604: + "");
605: }
606: endpointStatisticsDataElement
607: .appendChild(numberOfReceivedFaultsElementChild);
608:
609: Element numberOfSentDonesElementChild = document
610: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_DONES_KEY);
611: if (numberOfSentDonesElementChild != null) {
612: numberOfSentDonesElementChild
613: .setTextContent(consumingData
614: .getNumberOfSentDones()
615: + "");
616: }
617: endpointStatisticsDataElement
618: .appendChild(numberOfSentDonesElementChild);
619:
620: Element numberOfSentErrorsElementChild = document
621: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_ERRORS_KEY);
622: if (numberOfSentErrorsElementChild != null) {
623: numberOfSentErrorsElementChild
624: .setTextContent(consumingData
625: .getNumberOfSentErrors()
626: + "");
627: }
628: endpointStatisticsDataElement
629: .appendChild(numberOfSentErrorsElementChild);
630:
631: Element numberOfSentFaultsElementChild = document
632: .createElement(EndpointStatisticsDataXMLConstants.NUMBER_OF_SENT_FAULTS_KEY);
633: if (numberOfSentFaultsElementChild != null) {
634: numberOfSentFaultsElementChild
635: .setTextContent(consumingData
636: .getNumberOfSentFaults()
637: + "");
638: }
639: endpointStatisticsDataElement
640: .appendChild(numberOfSentFaultsElementChild);
641:
642: // ////////////////////////////////
643: // <PerformanceMeasurementDataList>
644: Element performanceDataListElement = (Element) document
645: .createElement(PerformanceDataXMLConstants.PERFORMANCE_MEASUREMENT_DATA_LIST_KEY);
646: // xmlns="http://java.sun.com/xml/ns/esb/management/PerformanceMeasurementDataList"
647: performanceDataListElement.setAttribute(
648: PerformanceDataXMLConstants.NAMESPACE_KEY,
649: PerformanceDataXMLConstants.NAMESPACE_VALUE);
650: // version = "1.0"
651: performanceDataListElement.setAttribute(
652: PerformanceDataXMLConstants.VERSION_KEY,
653: PerformanceDataXMLConstants.VERSION_VALUE);
654: Map<String, PerformanceData> dataMap = consumingData
655: .getCategoryToPerformanceDataMap();
656:
657: for (String category : dataMap.keySet()) {
658: PerformanceData data = dataMap.get(category);
659: // ////////////////////////////////
660: // <PerformanceMeasurementData>
661: Element performanceMeasurementDataElementChild = this
662: .createPerformanceMeasurementDataElement(
663: document, data);
664: // </PerformanceMeasurementData>
665: performanceDataListElement
666: .appendChild(performanceMeasurementDataElementChild);
667: // ////////////////////////////////
668: }
669: // </PerformanceMeasurementDataList>
670: endpointStatisticsDataElement
671: .appendChild(performanceDataListElement);
672: // ////////////////////////////////
673: }
674: return endpointStatisticsDataElement;
675: }
676:
677: /**
678: *
679: * @param document
680: * @param data
681: * @return
682: */
683: protected Element createPerformanceMeasurementDataElement(
684: Document document, PerformanceData data) {
685: Element performanceDataElement = null;
686: if ((document != null) && (data != null)) {
687: // <PerformanceMeasurementData>
688: performanceDataElement = document
689: .createElement(PerformanceDataXMLConstants.PERFORMANCE_MEASUREMENT_DATA_KEY);
690:
691: // <category>
692: Element categoryElementChild = document
693: .createElement(PerformanceDataXMLConstants.CATEGORY_KEY);
694: if (categoryElementChild != null) {
695: categoryElementChild.setTextContent(data.getCategory());
696: }
697: // </category>
698: performanceDataElement.appendChild(categoryElementChild);
699:
700: // <endpoint>
701: Element endpointElementChild = document
702: .createElement(PerformanceDataXMLConstants.ENDPOINT_KEY);
703: if (endpointElementChild != null) {
704: endpointElementChild.setTextContent(data.getEndpoint());
705: }
706: // </endpoint>
707: performanceDataElement.appendChild(endpointElementChild);
708:
709: // <sourceClassName>
710: Element sourceClassNameElementChild = document
711: .createElement(PerformanceDataXMLConstants.SOURCE_CLASS_NAME_KEY);
712: if (sourceClassNameElementChild != null) {
713: sourceClassNameElementChild.setTextContent(data
714: .getSourceClassName());
715: }
716: // </sourceClassName>
717: performanceDataElement
718: .appendChild(sourceClassNameElementChild);
719:
720: // <numberOfMeasurementObjects>
721: Element numberOfMeasurementObjectsElementChild = document
722: .createElement(PerformanceDataXMLConstants.NUMBER_OF_MEASUREMENT_OBJECTS_KEY);
723: if (numberOfMeasurementObjectsElementChild != null) {
724: numberOfMeasurementObjectsElementChild
725: .setTextContent(data
726: .getNumberOfMeasurementObjects()
727: + "");
728: }
729: // </numberOfMeasurementObjects>
730: performanceDataElement
731: .appendChild(numberOfMeasurementObjectsElementChild);
732:
733: // <numberOfMeasurements>
734: Element numberOfMeasurementsElementChild = document
735: .createElement(PerformanceDataXMLConstants.NUMBER_OF_MEASUREMENTS_KEY);
736: if (categoryElementChild != null) {
737: numberOfMeasurementsElementChild.setTextContent(data
738: .getNumberOfMeasurements()
739: + "");
740: }
741: // </numberOfMeasurements>
742: performanceDataElement
743: .appendChild(numberOfMeasurementsElementChild);
744:
745: // <average>
746: Element averageElementChild = document
747: .createElement(PerformanceDataXMLConstants.AVERAGE_KEY);
748: if (averageElementChild != null) {
749: averageElementChild.setTextContent(data.getAverage()
750: + "");
751: }
752: // </average>
753: performanceDataElement.appendChild(averageElementChild);
754:
755: // <averageWithoutFirstMeasurement>
756: Element averageWithoutFirstMeasurementElementChild = document
757: .createElement(PerformanceDataXMLConstants.AVERAGE_WITHOUT_FIRST_MEASUREMENT_KEY);
758: if (averageWithoutFirstMeasurementElementChild != null) {
759: averageWithoutFirstMeasurementElementChild
760: .setTextContent(data
761: .getAverageWithoutFirstMeasurement()
762: + "");
763: }
764: // </averageWithoutFirstMeasurement>
765: performanceDataElement
766: .appendChild(averageWithoutFirstMeasurementElementChild);
767:
768: // <firstMeasurementTime>
769: Element firstMeasurementTimeElementChild = document
770: .createElement(PerformanceDataXMLConstants.FIRST_MEASUREMENT_TIME_KEY);
771: if (firstMeasurementTimeElementChild != null) {
772: firstMeasurementTimeElementChild.setTextContent(data
773: .getFirstMeasurementTime()
774: + "");
775: }
776: // </firstMeasurementTime>
777: performanceDataElement
778: .appendChild(firstMeasurementTimeElementChild);
779:
780: // <load>
781: Element loadElementChild = document
782: .createElement(PerformanceDataXMLConstants.LOAD_KEY);
783: if (loadElementChild != null) {
784: loadElementChild.setTextContent(data.getLoad() + "");
785: }
786: // </load>
787: performanceDataElement.appendChild(loadElementChild);
788:
789: // <median>
790: Element medianElementChild = document
791: .createElement(PerformanceDataXMLConstants.MEDIAN_KEY);
792: if (medianElementChild != null) {
793: medianElementChild
794: .setTextContent(data.getMedian() + "");
795: }
796: // </median>
797: performanceDataElement.appendChild(medianElementChild);
798:
799: // <throughput>
800: Element throughputElementChild = document
801: .createElement(PerformanceDataXMLConstants.THROUGHPUT_KEY);
802: if (throughputElementChild != null) {
803: throughputElementChild.setTextContent(data
804: .getThroughput()
805: + "");
806: }
807: // </throughput>
808: performanceDataElement.appendChild(throughputElementChild);
809:
810: // <timeTaken>
811: Element timeTakenElementChild = document
812: .createElement(PerformanceDataXMLConstants.TIME_TAKEN_KEY);
813: if (timeTakenElementChild != null) {
814: timeTakenElementChild.setTextContent(data
815: .getTimeTaken()
816: + "");
817: }
818: // </timeTaken>
819: performanceDataElement.appendChild(timeTakenElementChild);
820:
821: // <totalTime>
822: Element totalTimeElementChild = document
823: .createElement(PerformanceDataXMLConstants.TOTAL_TIME_KEY);
824: if (totalTimeElementChild != null) {
825: totalTimeElementChild.setTextContent(data
826: .getTotalTime()
827: + "");
828: }
829: // </totalTime>
830: performanceDataElement.appendChild(totalTimeElementChild);
831: }
832: return performanceDataElement;
833: }
834:
835: /**
836: * @param document
837: * @return
838: * @throws TransformerException
839: */
840: protected String writeToString(Document document)
841: throws TransformerException {
842: // Use a Transformer for aspectOutput
843: TransformerFactory tFactory = TransformerFactory.newInstance();
844: Transformer transformer = tFactory.newTransformer();
845: DOMSource source = new DOMSource(document);
846: StringWriter writer = new StringWriter();
847: StreamResult result = new StreamResult(writer);
848:
849: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
850: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
851: transformer
852: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
853: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
854:
855: // indent the aspectOutput to make it more legible...
856: transformer.setOutputProperty(
857: "{http://xml.apache.org/xslt}indent-amount", "4");
858: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
859: transformer.transform(source, result);
860:
861: return result.getWriter().toString();
862: }
863:
864: /**
865: * @param args
866: */
867: public static void main(String[] args) {
868: String uri = "C:/test/schema/endpointstatistics/EndpointStatisticsData.xml";
869: try {
870: Map<String /* instanceName */, IEndpointStatisticsData> map = null;
871: map = EndpointStatisticsDataReader.parseFromFile(uri);
872: for (String instanceName : map.keySet()) {
873: System.out.println(map.get(instanceName)
874: .getDisplayString());
875: }
876:
877: String content = EndpointStatisticsDataWriter
878: .serialize(map);
879: System.out.println(content);
880: EndpointStatisticsDataWriter.setContents(new File(uri),
881: content);
882:
883: } catch (MalformedURLException e) {
884: e.printStackTrace();
885: } catch (FileNotFoundException e) {
886: e.printStackTrace();
887: } catch (ParserConfigurationException e) {
888: e.printStackTrace();
889: } catch (SAXException e) {
890: e.printStackTrace();
891: } catch (URISyntaxException e) {
892: e.printStackTrace();
893: } catch (IOException e) {
894: e.printStackTrace();
895: } catch (TransformerException e) {
896: e.printStackTrace();
897: }
898: }
899:
900: }
|