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: * @(#)ServiceAssemblyStatisticsDataWriter.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.List;
042: import java.util.Map;
043:
044: import javax.xml.parsers.DocumentBuilder;
045: import javax.xml.parsers.DocumentBuilderFactory;
046: import javax.xml.parsers.ParserConfigurationException;
047: import javax.xml.transform.OutputKeys;
048: import javax.xml.transform.Transformer;
049: import javax.xml.transform.TransformerConfigurationException;
050: import javax.xml.transform.TransformerException;
051: import javax.xml.transform.TransformerFactory;
052: import javax.xml.transform.dom.DOMSource;
053: import javax.xml.transform.stream.StreamResult;
054:
055: import org.w3c.dom.Document;
056: import org.w3c.dom.Element;
057: import org.xml.sax.SAXException;
058:
059: import com.sun.esb.management.common.data.ServiceAssemblyStatisticsData;
060: import com.sun.esb.management.common.data.ServiceUnitStatisticsData;
061:
062: /**
063: * @author graj
064: *
065: */
066: public class ServiceAssemblyStatisticsDataWriter implements
067: ServiceAssemblyStatisticsDataXMLConstants, Serializable {
068: static final long serialVersionUID = -1L;
069:
070: static final String FILE_NAME_KEY = "ServiceAssemblyStatisticsData.xml";
071:
072: /** Constructor - Creates an ServiceAssemblyStatisticsDataWriter */
073: public ServiceAssemblyStatisticsDataWriter() {
074: }
075:
076: /**
077: *
078: * @param document
079: * @param directoryPath
080: * @throws TransformerConfigurationException
081: * @throws TransformerException
082: * @throws Exception
083: */
084: public static void writeToFile(Document document,
085: String directoryPath)
086: throws TransformerConfigurationException,
087: TransformerException, Exception {
088: File file = new File(directoryPath);
089: if ((file.isDirectory() == false) || (file.exists() == false)) {
090: throw new Exception("Directory Path: " + directoryPath
091: + " is invalid.");
092: }
093: String fileLocation = file.getAbsolutePath() + File.separator
094: + FILE_NAME_KEY;
095: System.out.println("Writing out to file: " + fileLocation);
096: File outputFile = new File(fileLocation);
097: // Use a Transformer for aspectOutput
098: TransformerFactory tFactory = TransformerFactory.newInstance();
099: Transformer transformer = tFactory.newTransformer();
100: DOMSource source = new DOMSource(document);
101: StreamResult result = new StreamResult(outputFile);
102:
103: // indent the Output to make it more legible...
104: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
105: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
106: transformer
107: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
108: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
109: transformer.setOutputProperty(
110: "{http://xml.apache.org/xslt}indent-amount", "4");
111: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
112:
113: transformer.transform(source, result);
114: }
115:
116: /**
117: * Change the contents of text file in its entirety, overwriting any
118: * existing text. This style of implementation throws all exceptions to the
119: * caller.
120: *
121: * @param aFile
122: * is an existing file which can be written to.
123: * @throws IllegalArgumentException
124: * if param does not comply.
125: * @throws FileNotFoundException
126: * if the file does not exist.
127: * @throws IOException
128: * if problem encountered during write.
129: */
130: public static void setContents(File aFile, String aContents)
131: throws FileNotFoundException, IOException {
132: if (aFile == null) {
133: throw new IllegalArgumentException(
134: "File should not be null.");
135: }
136: if (!aFile.exists()) {
137: aFile.createNewFile();
138: }
139: if (!aFile.isFile()) {
140: throw new IllegalArgumentException(
141: "Should not be a directory: " + aFile);
142: }
143: if (!aFile.canWrite()) {
144: throw new IllegalArgumentException(
145: "File cannot be written: " + aFile);
146: }
147:
148: // declared here only to make visible to finally clause; generic
149: // reference
150: Writer output = null;
151: try {
152: // use buffering
153: // FileWriter always assumes default encoding is OK!
154: output = new BufferedWriter(new FileWriter(aFile));
155: output.write(aContents);
156: } finally {
157: // flush and close both "aspectOutput" and its underlying FileWriter
158: if (output != null) {
159: output.close();
160: }
161: }
162: }
163:
164: /**
165: *
166: * @param ServiceAssemblyStatisticsData
167: * data
168: * @return XML string
169: * @throws ParserConfigurationException
170: * @throws TransformerException
171: */
172: public static String serialize(
173: Map<String /* instanceName */, ServiceAssemblyStatisticsData> dataMap)
174: throws ParserConfigurationException, TransformerException {
175: Document document = null;
176: ServiceAssemblyStatisticsDataWriter writer = new ServiceAssemblyStatisticsDataWriter();
177: if (dataMap != null) {
178: DocumentBuilderFactory factory = DocumentBuilderFactory
179: .newInstance();
180: DocumentBuilder builder = factory.newDocumentBuilder();
181: document = builder.newDocument(); // Create from whole cloth
182:
183: // ////////////////////////////////
184: // <ServiceAssemblyStatisticsDataList>
185: Element root = (Element) document
186: .createElement(ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_STATISTICS_DATA_LIST_KEY);
187: // xmlns="http://java.sun.com/xml/ns/esb/management/ServiceAssemblyStatisticsDataList"
188: root
189: .setAttribute(
190: NAMESPACE_KEY,
191: ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_NAMESPACE_VALUE);
192: // version = "1.0"
193: root
194: .setAttribute(
195: VERSION_KEY,
196: ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_VERSION_VALUE);
197:
198: for (String instanceName : dataMap.keySet()) {
199: ServiceAssemblyStatisticsData data = dataMap
200: .get(instanceName);
201: // ////////////////////////////////
202: // <ServiceAssemblyStatisticsData>
203: Element serviceAssemblyStatisticsDataElementChild = writer
204: .createServiceAssemblyStatisticsDataElement(
205: document, data);
206: // </ServiceAssemblyStatisticsData>
207: root
208: .appendChild(serviceAssemblyStatisticsDataElementChild);
209: // ////////////////////////////////
210: }
211: // ////////////////////////////////
212: // </ServiceAssemblyStatisticsDataList>
213: document.appendChild(root);
214: // ////////////////////////////////
215:
216: }
217: return writer.writeToString(document);
218: }
219:
220: /**
221: *
222: * @param document
223: * @param data
224: * @return
225: */
226: protected Element createServiceAssemblyStatisticsDataElement(
227: Document document, ServiceAssemblyStatisticsData data) {
228: Element serviceAssemblyStatisticsDataElement = null;
229: if ((document != null) && (data != null)) {
230:
231: // <ServiceAssemblyStatisticsData>
232: serviceAssemblyStatisticsDataElement = document
233: .createElement(ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_STATISTICS_DATA_KEY);
234:
235: // <InstanceName>
236: Element instanceNameElementChild = document
237: .createElement(ServiceAssemblyStatisticsDataXMLConstants.INSTANCE_NAME_KEY);
238: if (instanceNameElementChild != null) {
239: instanceNameElementChild.setTextContent(data
240: .getInstanceName());
241: }
242: // </InstanceName>
243: serviceAssemblyStatisticsDataElement
244: .appendChild(instanceNameElementChild);
245:
246: // <ServiceAssemblyName>
247: Element serviceAssemblyElementChild = document
248: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_NAME_KEY);
249: if (serviceAssemblyElementChild != null) {
250: serviceAssemblyElementChild.setTextContent(data
251: .getName());
252: }
253: // </ServiceAssemblyName>
254: serviceAssemblyStatisticsDataElement
255: .appendChild(serviceAssemblyElementChild);
256:
257: // <ServiceAssemblyLastStartupTime>
258: Element serviceAssemblyLastStartupElementChild = document
259: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_LAST_STARTUP_TIME_KEY);
260: if (serviceAssemblyLastStartupElementChild != null) {
261: serviceAssemblyLastStartupElementChild
262: .setTextContent(data.getLastStartupTime() + "");
263: }
264: // </ServiceAssemblyLastStartupTime>
265: serviceAssemblyStatisticsDataElement
266: .appendChild(serviceAssemblyLastStartupElementChild);
267:
268: // <ServiceAssemblyStartupTimeAverage>
269: Element serviceAssemblyStartupTimeAverageElementChild = document
270: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_STARTUP_TIME_AVERAGE_KEY);
271: if (serviceAssemblyStartupTimeAverageElementChild != null) {
272: serviceAssemblyStartupTimeAverageElementChild
273: .setTextContent(data.getStartupTimeAverage()
274: + "");
275: }
276: // </ServiceAssemblyStartupTimeAverage>
277: serviceAssemblyStatisticsDataElement
278: .appendChild(serviceAssemblyStartupTimeAverageElementChild);
279:
280: // <ServiceAssemblyShutdownTimeAverage>
281: Element serviceAssemblyShutdownTimeAverageElementChild = document
282: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_SHUTDOWN_TIME_AVERAGE_KEY);
283: if (serviceAssemblyShutdownTimeAverageElementChild != null) {
284: serviceAssemblyShutdownTimeAverageElementChild
285: .setTextContent(data.getShutdownTimeAverage()
286: + "");
287: }
288: // </ServiceAssemblyShutdownTimeAverage>
289: serviceAssemblyStatisticsDataElement
290: .appendChild(serviceAssemblyShutdownTimeAverageElementChild);
291:
292: // <ServiceAssemblyStopTimeAverage>
293: Element serviceAssemblyStopTimeAverageElementChild = document
294: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_STOP_TIME_AVERAGE_KEY);
295: if (serviceAssemblyStopTimeAverageElementChild != null) {
296: serviceAssemblyStopTimeAverageElementChild
297: .setTextContent(data.getStopTimeAverage() + "");
298: }
299: // </ServiceAssemblyStopTimeAverage>
300: serviceAssemblyStatisticsDataElement
301: .appendChild(serviceAssemblyStopTimeAverageElementChild);
302:
303: // <ServiceAssemblyUpTime>
304: Element serviceAssemblyUpTimeElementChild = document
305: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_UP_TIME_KEY);
306: if (serviceAssemblyUpTimeElementChild != null) {
307: serviceAssemblyUpTimeElementChild.setTextContent(data
308: .getUpTime()
309: + "");
310: }
311: // </ServiceAssemblyUpTime>
312: serviceAssemblyStatisticsDataElement
313: .appendChild(serviceAssemblyUpTimeElementChild);
314:
315: // ////////////////////////////////
316: // <ServiceUnitStatisticsDataList>
317: Element serviceUnitStatisticsDataListElementChild = this
318: .createServiceUnitStatisticsDataListElement(
319: document, data
320: .getServiceUnitStatisticsList());
321: // </ServiceAssemblyStatisticsData>
322: serviceAssemblyStatisticsDataElement
323: .appendChild(serviceUnitStatisticsDataListElementChild);
324: // ////////////////////////////////
325: }
326: return serviceAssemblyStatisticsDataElement;
327: }
328:
329: /**
330: *
331: * @param document
332: * @param list
333: * @return
334: */
335: Element createServiceUnitStatisticsDataListElement(
336: Document document, List<ServiceUnitStatisticsData> list) {
337: Element serviceUnitStatisticsDataListElement = null;
338: if ((document != null) && (list != null)) {
339: // <ServiceUnitStatisticsDataList>
340: serviceUnitStatisticsDataListElement = document
341: .createElement(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_LIST_KEY);
342: // xmlns="http://java.sun.com/xml/ns/esb/management/ServiceUnitStatisticsDataList"
343: serviceUnitStatisticsDataListElement
344: .setAttribute(
345: NAMESPACE_KEY,
346: ServiceAssemblyStatisticsDataXMLConstants.UNIT_NAMESPACE_VALUE);
347: // version = "1.0"
348: serviceUnitStatisticsDataListElement
349: .setAttribute(
350: VERSION_KEY,
351: ServiceAssemblyStatisticsDataXMLConstants.UNIT_VERSION_VALUE);
352:
353: for (ServiceUnitStatisticsData data : list) {
354: // <ServiceUnitStatisticsData>
355: Element serviceUnitDataElementChild = createServiceUnitStatisticsDataListElement(
356: document, data);
357: // </ServiceUnitStatisticsData>
358: serviceUnitStatisticsDataListElement
359: .appendChild(serviceUnitDataElementChild);
360: }
361: }
362: return serviceUnitStatisticsDataListElement;
363: }
364:
365: /**
366: *
367: * @param document
368: * @param data
369: * @return
370: */
371: Element createServiceUnitStatisticsDataListElement(
372: Document document, ServiceUnitStatisticsData data) {
373: Element serviceUnitStatisticsDataElement = null;
374: if ((document != null) && (data != null)) {
375: // <ServiceUnitStatisticsData>
376: serviceUnitStatisticsDataElement = document
377: .createElement(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_KEY);
378:
379: // <ServiceUnitName>
380: Element serviceUnitElementChild = document
381: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_NAME_KEY);
382: if (serviceUnitElementChild != null) {
383: serviceUnitElementChild.setTextContent(data.getName());
384: }
385: // </ServiceAssemblyName>
386: serviceUnitStatisticsDataElement
387: .appendChild(serviceUnitElementChild);
388:
389: // <ServiceAssemblyStartupTimeAverage>
390: Element serviceUnitStartupTimeAverageElementChild = document
391: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_STARTUP_TIME_AVERAGE_KEY);
392: if (serviceUnitStartupTimeAverageElementChild != null) {
393: serviceUnitStartupTimeAverageElementChild
394: .setTextContent(data.getStartupTimeAverage()
395: + "");
396: }
397: // </ServiceAssemblyStartupTimeAverage>
398: serviceUnitStatisticsDataElement
399: .appendChild(serviceUnitStartupTimeAverageElementChild);
400:
401: // <ServiceAssemblyShutdownTimeAverage>
402: Element serviceUnitShutdownTimeAverageElementChild = document
403: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_SHUTDOWN_TIME_AVERAGE_KEY);
404: if (serviceUnitShutdownTimeAverageElementChild != null) {
405: serviceUnitShutdownTimeAverageElementChild
406: .setTextContent(data.getShutdownTimeAverage()
407: + "");
408: }
409: // </ServiceAssemblyShutdownTimeAverage>
410: serviceUnitStatisticsDataElement
411: .appendChild(serviceUnitShutdownTimeAverageElementChild);
412:
413: // <ServiceAssemblyStopTimeAverage>
414: Element serviceUnitStopTimeAverageElementChild = document
415: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_STOP_TIME_AVERAGE_KEY);
416: if (serviceUnitStopTimeAverageElementChild != null) {
417: serviceUnitStopTimeAverageElementChild
418: .setTextContent(data.getStopTimeAverage() + "");
419: }
420: // </ServiceAssemblyStopTimeAverage>
421: serviceUnitStatisticsDataElement
422: .appendChild(serviceUnitStopTimeAverageElementChild);
423:
424: // <EndpointNameList>
425: Element serviceUnitEndpointListElement = document
426: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_ENDPOINT_NAME_LIST_KEY);
427:
428: for (String endpointName : data.getEndpointNameList()) {
429: // <EndpointName>
430: Element serviceUnitEndpointNameElement = document
431: .createElement(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_ENDPOINT_NAME_KEY);
432: if (serviceUnitEndpointNameElement != null) {
433: serviceUnitEndpointNameElement
434: .setTextContent(endpointName);
435: }
436: // </EndpointName>
437: serviceUnitEndpointListElement
438: .appendChild(serviceUnitEndpointNameElement);
439: }
440: // </EndpointNameList>
441: serviceUnitStatisticsDataElement
442: .appendChild(serviceUnitEndpointListElement);
443:
444: }
445: return serviceUnitStatisticsDataElement;
446:
447: }
448:
449: /**
450: * @param document
451: * @return
452: * @throws TransformerException
453: */
454: protected String writeToString(Document document)
455: throws TransformerException {
456: // Use a Transformer for aspectOutput
457: TransformerFactory tFactory = TransformerFactory.newInstance();
458: Transformer transformer = tFactory.newTransformer();
459: DOMSource source = new DOMSource(document);
460: StringWriter writer = new StringWriter();
461: StreamResult result = new StreamResult(writer);
462:
463: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
464: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
465: transformer
466: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
467: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
468:
469: // indent the aspectOutput to make it more legible...
470: transformer.setOutputProperty(
471: "{http://xml.apache.org/xslt}indent-amount", "4");
472: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
473: transformer.transform(source, result);
474:
475: return result.getWriter().toString();
476: }
477:
478: /**
479: * @param args
480: */
481: public static void main(String[] args) {
482: String uri = "C:/test/schema/assemblystatistics/ServiceAssemblyStatisticsData.xml";
483: try {
484: Map<String /* instanceName */, ServiceAssemblyStatisticsData> map = null;
485: map = ServiceAssemblyStatisticsDataReader
486: .parseFromFile(uri);
487: for (String instanceName : map.keySet()) {
488: System.out.println(map.get(instanceName)
489: .getDisplayString());
490: }
491:
492: String content = ServiceAssemblyStatisticsDataWriter
493: .serialize(map);
494: System.out.println(content);
495: ServiceAssemblyStatisticsDataWriter.setContents(new File(
496: uri), content);
497:
498: } catch (MalformedURLException e) {
499: e.printStackTrace();
500: } catch (FileNotFoundException e) {
501: e.printStackTrace();
502: } catch (ParserConfigurationException e) {
503: e.printStackTrace();
504: } catch (SAXException e) {
505: e.printStackTrace();
506: } catch (URISyntaxException e) {
507: e.printStackTrace();
508: } catch (IOException e) {
509: e.printStackTrace();
510: } catch (TransformerException e) {
511: e.printStackTrace();
512: }
513: }
514:
515: }
|