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: * @(#)ComponentStatisticsDataWriter.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.ComponentStatisticsData;
059:
060: /**
061: * @author graj
062: *
063: */
064: public class ComponentStatisticsDataWriter implements
065: ComponentStatisticsDataXMLConstants, Serializable {
066:
067: static final long serialVersionUID = -1L;
068:
069: static final String FILE_NAME_KEY = "ComponentStatisticsData.xml";
070:
071: /** Constructor - Creates an ComponentStatisticsDataWriter */
072: public ComponentStatisticsDataWriter() {
073: }
074:
075: /**
076: *
077: * @param document
078: * @param directoryPath
079: * @throws TransformerConfigurationException
080: * @throws TransformerException
081: * @throws Exception
082: */
083: public static void writeToFile(Document document,
084: String directoryPath)
085: throws TransformerConfigurationException,
086: TransformerException, Exception {
087: File file = new File(directoryPath);
088: if ((file.isDirectory() == false) || (file.exists() == false)) {
089: throw new Exception("Directory Path: " + directoryPath
090: + " is invalid.");
091: }
092: String fileLocation = file.getAbsolutePath() + File.separator
093: + FILE_NAME_KEY;
094: System.out.println("Writing out to file: " + fileLocation);
095: File outputFile = new File(fileLocation);
096: // Use a Transformer for aspectOutput
097: TransformerFactory tFactory = TransformerFactory.newInstance();
098: Transformer transformer = tFactory.newTransformer();
099: DOMSource source = new DOMSource(document);
100: StreamResult result = new StreamResult(outputFile);
101:
102: // indent the Output to make it more legible...
103: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
104: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
105: transformer
106: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
107: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
108: transformer.setOutputProperty(
109: "{http://xml.apache.org/xslt}indent-amount", "4");
110: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
111:
112: transformer.transform(source, result);
113: }
114:
115: /**
116: * Change the contents of text file in its entirety, overwriting any
117: * existing text. This style of implementation throws all exceptions to the
118: * caller.
119: *
120: * @param aFile
121: * is an existing file which can be written to.
122: * @throws IllegalArgumentException
123: * if param does not comply.
124: * @throws FileNotFoundException
125: * if the file does not exist.
126: * @throws IOException
127: * if problem encountered during write.
128: */
129: public static void setContents(File aFile, String aContents)
130: throws FileNotFoundException, IOException {
131: if (aFile == null) {
132: throw new IllegalArgumentException(
133: "File should not be null.");
134: }
135: if (!aFile.exists()) {
136: aFile.createNewFile();
137: }
138: if (!aFile.isFile()) {
139: throw new IllegalArgumentException(
140: "Should not be a directory: " + aFile);
141: }
142: if (!aFile.canWrite()) {
143: throw new IllegalArgumentException(
144: "File cannot be written: " + aFile);
145: }
146:
147: // declared here only to make visible to finally clause; generic
148: // reference
149: Writer output = null;
150: try {
151: // use buffering
152: // FileWriter always assumes default encoding is OK!
153: output = new BufferedWriter(new FileWriter(aFile));
154: output.write(aContents);
155: } finally {
156: // flush and close both "aspectOutput" and its underlying FileWriter
157: if (output != null) {
158: output.close();
159: }
160: }
161: }
162:
163: /**
164: *
165: * @param NMRStatisticsData
166: * data
167: * @return XML string
168: * @throws ParserConfigurationException
169: * @throws TransformerException
170: */
171: public static String serialize(
172: Map<String /* instanceName */, ComponentStatisticsData> dataMap)
173: throws ParserConfigurationException, TransformerException {
174: Document document = null;
175: ComponentStatisticsDataWriter writer = new ComponentStatisticsDataWriter();
176: if (dataMap != null) {
177: DocumentBuilderFactory factory = DocumentBuilderFactory
178: .newInstance();
179: DocumentBuilder builder = factory.newDocumentBuilder();
180: document = builder.newDocument(); // Create from whole cloth
181:
182: // ////////////////////////////////
183: // <ComponentStatisticsDataList>
184: Element root = (Element) document
185: .createElement(COMPONENT_STATISTICS_DATA_LIST_KEY);
186: // xmlns="http://java.sun.com/xml/ns/esb/management/ComponentStatisticsData"
187: root.setAttribute(NAMESPACE_KEY, NAMESPACE_VALUE);
188: // version = "1.0"
189: root.setAttribute(VERSION_KEY, VERSION_VALUE);
190:
191: for (String instanceName : dataMap.keySet()) {
192: ComponentStatisticsData data = dataMap
193: .get(instanceName);
194: // ////////////////////////////////
195: // <ComponentStatisticsData>
196: Element componentStatisticsDataElementChild = writer
197: .createComponentStatisticsDataElement(document,
198: data);
199: // </ComponentStatisticsData>
200: root.appendChild(componentStatisticsDataElementChild);
201: // ////////////////////////////////
202: }
203:
204: // ////////////////////////////////
205: // </ComponentStatisticsDataList>
206: document.appendChild(root);
207: // ////////////////////////////////
208:
209: }
210: return writer.writeToString(document);
211: }
212:
213: /**
214: *
215: * @param document
216: * @param data
217: * @return
218: */
219: protected Element createComponentStatisticsDataElement(
220: Document document, ComponentStatisticsData data) {
221: Element componentStatisticsDataElement = null;
222: if ((document != null) && (data != null)) {
223:
224: // <ComponentStatisticsData>
225: componentStatisticsDataElement = document
226: .createElement(COMPONENT_STATISTICS_DATA_KEY);
227:
228: // <InstanceName>
229: Element instanceNameElementChild = document
230: .createElement(INSTANCE_NAME_KEY);
231: if (instanceNameElementChild != null) {
232: instanceNameElementChild.setTextContent(data
233: .getInstanceName());
234: }
235: // </InstanceName>
236: componentStatisticsDataElement
237: .appendChild(instanceNameElementChild);
238:
239: // <ComponentUpTime>
240: Element upTimeElementChild = document
241: .createElement(COMPONENT_UPTIME_KEY);
242: if (upTimeElementChild != null) {
243: upTimeElementChild.setTextContent(data
244: .getComponentUpTime()
245: + "");
246: }
247: // </ComponentUpTime>
248: componentStatisticsDataElement
249: .appendChild(upTimeElementChild);
250:
251: // <NumberOfActivatedEndpoints>
252: Element activatedEndpointsElementChild = document
253: .createElement(NUMBER_OF_ACTIVATED_ENDPOINTS_KEY);
254: if (activatedEndpointsElementChild != null) {
255: activatedEndpointsElementChild.setTextContent(data
256: .getNumberOfActivatedEndpoints()
257: + "");
258: }
259: // </NumberOfActivatedEndpoints>
260: componentStatisticsDataElement
261: .appendChild(activatedEndpointsElementChild);
262:
263: // <NumberOfReceivedRequests>
264: Element receivedRequestsElementChild = document
265: .createElement(NUMBER_OF_RECEIVED_REQUESTS_KEY);
266: if (receivedRequestsElementChild != null) {
267: receivedRequestsElementChild.setTextContent(data
268: .getNumberOfReceivedRequests()
269: + "");
270: }
271: // </NumberOfReceivedRequests>
272: componentStatisticsDataElement
273: .appendChild(receivedRequestsElementChild);
274:
275: // <NumberOfSentRequests>
276: Element sentRequestsElementChild = document
277: .createElement(NUMBER_OF_SENT_REQUESTS_KEY);
278: if (sentRequestsElementChild != null) {
279: sentRequestsElementChild.setTextContent(data
280: .getNumberOfSentRequests()
281: + "");
282: }
283: // </NumberOfSentRequests>
284: componentStatisticsDataElement
285: .appendChild(sentRequestsElementChild);
286:
287: // <NumberOfReceivedReplies>
288: Element receivedRepliesElementChild = document
289: .createElement(NUMBER_OF_RECEIVED_REPLIES_KEY);
290: if (receivedRepliesElementChild != null) {
291: receivedRepliesElementChild.setTextContent(data
292: .getNumberOfReceivedReplies()
293: + "");
294: }
295: // </NumberOfReceivedReplies>
296: componentStatisticsDataElement
297: .appendChild(receivedRepliesElementChild);
298:
299: // <NumberOfSentReplies>
300: Element sentRepliesElementChild = document
301: .createElement(NUMBER_OF_SENT_REPLIES_KEY);
302: if (sentRepliesElementChild != null) {
303: sentRepliesElementChild.setTextContent(data
304: .getNumberOfSentReplies()
305: + "");
306: }
307: // </NumberOfSentReplies>
308: componentStatisticsDataElement
309: .appendChild(sentRepliesElementChild);
310:
311: // <NumberOfReceivedDones>
312: Element receivedDonesElementChild = document
313: .createElement(NUMBER_OF_RECEIVED_DONES_KEY);
314: if (receivedDonesElementChild != null) {
315: receivedDonesElementChild.setTextContent(data
316: .getNumberOfReceivedDones()
317: + "");
318: }
319: // </NumberOfReceivedDones>
320: componentStatisticsDataElement
321: .appendChild(receivedDonesElementChild);
322:
323: // <NumberOfSentDones>
324: Element sentDonesElementChild = document
325: .createElement(NUMBER_OF_SENT_DONES_KEY);
326: if (sentDonesElementChild != null) {
327: sentDonesElementChild.setTextContent(data
328: .getNumberOfSentDones()
329: + "");
330: }
331: // </NumberOfSentDones>
332: componentStatisticsDataElement
333: .appendChild(sentDonesElementChild);
334:
335: // <NumberOfReceivedFaults>
336: Element receivedFaultsElementChild = document
337: .createElement(NUMBER_OF_RECEIVED_FAULTS_KEY);
338: if (receivedFaultsElementChild != null) {
339: receivedFaultsElementChild.setTextContent(data
340: .getNumberOfReceivedFaults()
341: + "");
342: }
343: // </NumberOfReceivedFaults>
344: componentStatisticsDataElement
345: .appendChild(receivedFaultsElementChild);
346:
347: // <NumberOfSentFaults>
348: Element sentFaultsElementChild = document
349: .createElement(NUMBER_OF_SENT_FAULTS_KEY);
350: if (sentFaultsElementChild != null) {
351: sentFaultsElementChild.setTextContent(data
352: .getNumberOfSentFaults()
353: + "");
354: }
355: // </NumberOfSentFaults>
356: componentStatisticsDataElement
357: .appendChild(sentFaultsElementChild);
358:
359: // <NumberOfReceivedErrors>
360: Element receivedErrorsElementChild = document
361: .createElement(NUMBER_OF_RECEIVED_ERRORS_KEY);
362: if (receivedErrorsElementChild != null) {
363: receivedErrorsElementChild.setTextContent(data
364: .getNumberOfReceivedErrors()
365: + "");
366: }
367: // </NumberOfReceivedErrors>
368: componentStatisticsDataElement
369: .appendChild(receivedErrorsElementChild);
370:
371: // <NumberOfSentErrors>
372: Element sentErrorsElementChild = document
373: .createElement(NUMBER_OF_SENT_ERRORS_KEY);
374: if (sentErrorsElementChild != null) {
375: sentErrorsElementChild.setTextContent(data
376: .getNumberOfSentErrors()
377: + "");
378: }
379: // </NumberOfSentErrors>
380: componentStatisticsDataElement
381: .appendChild(sentErrorsElementChild);
382:
383: // <NumberOfCompletedExchanges>
384: Element completedExchangesElementChild = document
385: .createElement(NUMBER_OF_COMPLETED_EXCHANGES_KEY);
386: if (completedExchangesElementChild != null) {
387: completedExchangesElementChild.setTextContent(data
388: .getNumberOfCompletedExchanges()
389: + "");
390: }
391: // </NumberOfCompletedExchanges>
392: componentStatisticsDataElement
393: .appendChild(completedExchangesElementChild);
394:
395: // <NumberOfActiveExchanges>
396: Element completedActiveElementChild = document
397: .createElement(NUMBER_OF_ACTIVE_EXCHANGES_KEY);
398: if (completedActiveElementChild != null) {
399: completedActiveElementChild.setTextContent(data
400: .getNumberOfActiveExchanges()
401: + "");
402: }
403: // </NumberOfActiveExchanges>
404: componentStatisticsDataElement
405: .appendChild(completedActiveElementChild);
406:
407: // <NumberOfErrorExchanges>
408: Element completedErrorElementChild = document
409: .createElement(NUMBER_OF_ERROR_EXCHANGES_KEY);
410: if (completedErrorElementChild != null) {
411: completedErrorElementChild.setTextContent(data
412: .getNumberOfErrorExchanges()
413: + "");
414: }
415: // </NumberOfErrorExchanges>
416: componentStatisticsDataElement
417: .appendChild(completedErrorElementChild);
418:
419: // <MessageExchangeResponseTimeAverage>
420: Element responseTimeAverageElementChild = document
421: .createElement(MESSAGE_EXCHANGE_RESPONSE_TIME_AVERAGE_KEY);
422: if (responseTimeAverageElementChild != null) {
423: responseTimeAverageElementChild.setTextContent(data
424: .getMessageExchangeResponseTimeAverage()
425: + "");
426: }
427: // </MessageExchangeResponseTimeAverage>
428: componentStatisticsDataElement
429: .appendChild(responseTimeAverageElementChild);
430:
431: // <MessageExchangeComponentTimeAverage>
432: Element componentTimeAverageElementChild = document
433: .createElement(MESSAGE_EXCHANGE_COMPONENT_TIME_AVERAGE_KEY);
434: if (componentTimeAverageElementChild != null) {
435: componentTimeAverageElementChild.setTextContent(data
436: .getMessageExchangeComponentTimeAverage()
437: + "");
438: }
439: // </MessageExchangeComponentTimeAverage>
440: componentStatisticsDataElement
441: .appendChild(componentTimeAverageElementChild);
442:
443: // <MessageExchangeDeliveryChannelTimeAverage>
444: Element deliveryChannelTimeAverageElementChild = document
445: .createElement(MESSAGE_EXCHANGE_DELIVERY_CHANNEL_TIME_AVERAGE_KEY);
446: if (deliveryChannelTimeAverageElementChild != null) {
447: deliveryChannelTimeAverageElementChild
448: .setTextContent(data
449: .getMessageExchangeDeliveryChannelTimeAverage()
450: + "");
451: }
452: // </MessageExchangeDeliveryChannelTimeAverage>
453: componentStatisticsDataElement
454: .appendChild(deliveryChannelTimeAverageElementChild);
455:
456: // <MessageExchangeMessageServiceTimeAverage>
457: Element messageServiceTimeAverageElementChild = document
458: .createElement(MESSAGE_EXCHANGE_MESSAGE_SERVICE_TIME_AVERAGE_KEY);
459: if (messageServiceTimeAverageElementChild != null) {
460: messageServiceTimeAverageElementChild
461: .setTextContent(data
462: .getMessageExchangeMessageServiceTimeAverage()
463: + "");
464: }
465: // </MessageExchangeMessageServiceTimeAverage>
466: componentStatisticsDataElement
467: .appendChild(messageServiceTimeAverageElementChild);
468:
469: // <ComponentExtensionStatus>
470: Element componentExtensionStatusElementChild = document
471: .createElement(COMPONENT_EXTENSION_STATUS_KEY);
472: if (componentExtensionStatusElementChild != null) {
473: componentExtensionStatusElementChild
474: .setTextContent(data
475: .getComponentExtensionStatusAsString());
476: }
477: // </ComponentExtensionStatus>
478: componentStatisticsDataElement
479: .appendChild(componentExtensionStatusElementChild);
480:
481: }
482:
483: return componentStatisticsDataElement;
484: }
485:
486: /**
487: * @param document
488: * @return
489: * @throws TransformerException
490: */
491: protected String writeToString(Document document)
492: throws TransformerException {
493: // Use a Transformer for aspectOutput
494: TransformerFactory tFactory = TransformerFactory.newInstance();
495: Transformer transformer = tFactory.newTransformer();
496: DOMSource source = new DOMSource(document);
497: StringWriter writer = new StringWriter();
498: StreamResult result = new StreamResult(writer);
499:
500: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
501: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
502: transformer
503: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
504: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
505:
506: // indent the aspectOutput to make it more legible...
507: transformer.setOutputProperty(
508: "{http://xml.apache.org/xslt}indent-amount", "4");
509: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
510: transformer.transform(source, result);
511:
512: return result.getWriter().toString();
513: }
514:
515: /**
516: * @param args
517: */
518: public static void main(String[] args) {
519: String uri = "C:/test/schema/componentstatistics/ComponentStatisticsData.xml";
520: try {
521: Map<String /* instanceName */, ComponentStatisticsData> map = null;
522: map = ComponentStatisticsDataReader.parseFromFile(uri);
523: for (String instanceName : map.keySet()) {
524: System.out.println(map.get(instanceName)
525: .getDisplayString());
526: }
527:
528: String content = ComponentStatisticsDataWriter
529: .serialize(map);
530: System.out.println(content);
531: ComponentStatisticsDataWriter.setContents(new File(uri),
532: content);
533:
534: } catch (MalformedURLException e) {
535: e.printStackTrace();
536: } catch (FileNotFoundException e) {
537: e.printStackTrace();
538: } catch (ParserConfigurationException e) {
539: e.printStackTrace();
540: } catch (SAXException e) {
541: e.printStackTrace();
542: } catch (URISyntaxException e) {
543: e.printStackTrace();
544: } catch (IOException e) {
545: e.printStackTrace();
546: } catch (TransformerException e) {
547: e.printStackTrace();
548: }
549: }
550:
551: }
|