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: * @(#)ApplicationVerificationReportWriter.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.ArrayList;
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.ApplicationVerificationReport;
059: import com.sun.esb.management.common.data.EndpointInformation;
060: import com.sun.esb.management.common.data.JavaEEVerifierReport;
061:
062: /**
063: * Writes Application Verification report data to a String or file as XML
064: *
065: * @author graj
066: *
067: */
068: public class ApplicationVerificationReportWriter implements
069: ApplicationVerificationReportXMLConstants, Serializable {
070: private static final long serialVersionUID = 1L;
071:
072: static final String FILE_NAME_KEY = "ApplicationVerificationReport.xml";
073:
074: /** Constructor - Creates an ApplicationVerificationReportWriter */
075: public ApplicationVerificationReportWriter() {
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 performance
169: * data Map<String, PerforamanceData>
170: * @return
171: * @throws ParserConfigurationException
172: * @throws TransformerException
173: */
174: public static String serialize(ApplicationVerificationReport report)
175: throws ParserConfigurationException, TransformerException {
176: Document document = null;
177: PerformanceDataMapWriter writer = new PerformanceDataMapWriter();
178: if (report != null) {
179: DocumentBuilderFactory factory = DocumentBuilderFactory
180: .newInstance();
181: DocumentBuilder builder = factory.newDocumentBuilder();
182: document = builder.newDocument(); // Create from whole cloth
183:
184: // ////////////////////////////////
185: // <ApplicationVerificationReport>
186: Element root = (Element) document
187: .createElement(APPLICATION_VERIFICATION_REPORT_KEY);
188: // xmlns="http://java.sun.com/xml/ns/esb/management/ApplicationVerificationReport"
189: root.setAttribute(NAMESPACE_KEY, NAMESPACE_VALUE);
190: // version = "1.0"
191: root.setAttribute(VERSION_KEY, VERSION_VALUE);
192:
193: // <ServiceAssemblyName>
194: Element serviceAssemblyNameElementChild = document
195: .createElement(SERVICE_ASSEMBLY_NAME_KEY);
196: if (serviceAssemblyNameElementChild != null) {
197: serviceAssemblyNameElementChild.setTextContent(report
198: .getServiceAssemblyName());
199: }
200: // </ServiceAssemblyName>
201: root.appendChild(serviceAssemblyNameElementChild);
202:
203: // <ServiceAssemblyDescription>
204: Element serviceAssemblyDescriptionElementChild = document
205: .createElement(SERVICE_ASSEMBLY_DESCRIPTION_KEY);
206: if (serviceAssemblyDescriptionElementChild != null) {
207: serviceAssemblyDescriptionElementChild
208: .setTextContent(report
209: .getServiceAssemblyDescription());
210: }
211: // </ServiceAssemblyDescription>
212: root.appendChild(serviceAssemblyDescriptionElementChild);
213:
214: // <NumberOfServiceUnits>
215: Element numberOfServiceUnitsElementChild = document
216: .createElement(NUMBER_OF_SERVICE_UNITS_KEY);
217: if (numberOfServiceUnitsElementChild != null) {
218: numberOfServiceUnitsElementChild.setTextContent(report
219: .getNumberOfServiceUnits()
220: + "");
221: }
222: // </NumberOfServiceUnits>
223: root.appendChild(numberOfServiceUnitsElementChild);
224:
225: // <AllComponentsInstalled>
226: Element allComponentsInstalledElementChild = document
227: .createElement(ALL_COMPONENTS_INSTALLED_KEY);
228: if (allComponentsInstalledElementChild != null) {
229: allComponentsInstalledElementChild
230: .setTextContent(report
231: .areAllComponentsInstalled()
232: + "");
233: }
234: // </AllComponentsInstalled>
235: root.appendChild(allComponentsInstalledElementChild);
236:
237: // <TemplateZIPID>
238: Element templateZipIdElementChild = document
239: .createElement(TEMPLATE_ZIPID_KEY);
240: if (templateZipIdElementChild != null) {
241: templateZipIdElementChild.setTextContent(report
242: .getTemplateDirectory());
243: }
244: // </TemplateZIPID>
245: root.appendChild(templateZipIdElementChild);
246:
247: // <MissingComponentsList>
248: Element missingComponentsElementChild = createMissingComponentsList(
249: document, report);
250: if (missingComponentsElementChild != null) {
251: root.appendChild(missingComponentsElementChild);
252: }
253: // </MissingComponentsList>
254:
255: // <EndpointInformationList>
256: Element endpointInformationListElementChild = createEndpointInformationList(
257: document, report);
258: if (endpointInformationListElementChild != null) {
259: root.appendChild(endpointInformationListElementChild);
260: }
261: // </EndpointInformationList>
262:
263: //if the list of javaee verifier reports is not empty append them
264: if (report.getJavaEEVerifierReports().size() > 0) {
265: // <JavaEEVerifierReports>
266: Element javaEEVerifierReportsElementChild = createJavaEEVerifierReports(
267: document, report);
268: if (javaEEVerifierReportsElementChild != null) {
269: root.appendChild(javaEEVerifierReportsElementChild);
270: }
271: // </JavaEEVerifierReports>
272: }
273:
274: // ////////////////////////////////
275: // </ApplicationVerificationReport>
276: document.appendChild(root);
277: // ////////////////////////////////
278:
279: }
280: return writer.writeToString(document);
281: }
282:
283: /**
284: * Create Missing Components List
285: *
286: * @param document
287: * @param data
288: * @return
289: */
290: protected static Element createMissingComponentsList(
291: Document document, ApplicationVerificationReport data) {
292: Element missingComponentsListElement = null;
293: if ((document != null) && (data != null)) {
294: // <MissingComponentsList>
295: missingComponentsListElement = document
296: .createElement(MISSING_COMPONENTS_LIST_KEY);
297:
298: for (String missingComponentName : data
299: .getMissingComponentsList()) {
300: // <MissingComponentName>
301: Element missingComponentNameElementChild = document
302: .createElement(MISSING_COMPONENT_NAME_KEY);
303: if (missingComponentNameElementChild != null) {
304: missingComponentNameElementChild
305: .setTextContent(missingComponentName);
306: }
307: // </MissingComponentName>
308: missingComponentsListElement
309: .appendChild(missingComponentNameElementChild);
310: }
311: }
312: return missingComponentsListElement;
313: }
314:
315: /**
316: * Create Endpoint Information List
317: *
318: * @param document
319: * @param data
320: * @return
321: */
322: protected static Element createEndpointInformationList(
323: Document document, ApplicationVerificationReport data) {
324: Element endpointInformationListElement = null;
325: if ((document != null) && (data != null)) {
326: // <EndpointInformationList>
327: endpointInformationListElement = document
328: .createElement(ENDPOINT_INFORMATION_LIST_KEY);
329:
330: for (EndpointInformation endpointInformation : data
331: .getEndpointInformationList()) {
332: // <Endpoint>
333: Element endpointElementChild = createEndpointInformation(
334: document, endpointInformation);
335: if (endpointElementChild != null) {
336: endpointInformationListElement
337: .appendChild(endpointElementChild);
338: }
339: // </Endpoint>
340: }
341: }
342: return endpointInformationListElement;
343: }
344:
345: /**
346: * Create Endpoint Information
347: *
348: * @param document
349: * @param data
350: * @return
351: */
352: protected static Element createEndpointInformation(
353: Document document, EndpointInformation data) {
354: Element endpointInformationElement = null;
355: if ((document != null) && (data != null)) {
356: // <Endpoint>
357: endpointInformationElement = document
358: .createElement(ENDPOINT_KEY);
359:
360: // <EndpointName>
361: Element endpointNameElementChild = document
362: .createElement(ENDPOINT_NAME_KEY);
363: if (endpointNameElementChild != null) {
364: endpointNameElementChild.setTextContent(data
365: .getEndpointName());
366: }
367: // </EndpointName>
368: endpointInformationElement
369: .appendChild(endpointNameElementChild);
370:
371: // <ServiceUnitName>
372: Element serviceUnitNameElementChild = document
373: .createElement(SERVICE_UNIT_NAME_KEY);
374: if (serviceUnitNameElementChild != null) {
375: serviceUnitNameElementChild.setTextContent(data
376: .getServiceUnitName());
377: }
378: // </ServiceUnitName>
379: endpointInformationElement
380: .appendChild(serviceUnitNameElementChild);
381:
382: // <ComponentName>
383: Element componentNameElementChild = document
384: .createElement(COMPONENT_NAME_KEY);
385: if (componentNameElementChild != null) {
386: componentNameElementChild.setTextContent(data
387: .getComponentName());
388: }
389: // </ComponentName>
390: endpointInformationElement
391: .appendChild(componentNameElementChild);
392:
393: // <Status>
394: Element statusElementChild = document
395: .createElement(STATUS_KEY);
396: if (statusElementChild != null) {
397: statusElementChild.setTextContent(data.getStatus());
398: }
399: // </Status>
400: endpointInformationElement.appendChild(statusElementChild);
401:
402: // <MissingApplicationVariables>
403: Element missingAppVarsElementChild = document
404: .createElement(MISSING_APPVARS_KEY);
405:
406: String[] missingAppVars = data
407: .getMissingApplicationVariables();
408: if (missingAppVars != null && missingAppVars.length > 0) {
409: for (int i = 0; i < missingAppVars.length; i++) {
410: // <ApplicationVariable>
411: Element appVariableElementChild = document
412: .createElement(MISSING_APPVAR_NAME_KEY);
413: if (appVariableElementChild != null) {
414: appVariableElementChild
415: .setTextContent(missingAppVars[i]);
416: }
417: //</ApplicationVariable>
418: missingAppVarsElementChild
419: .appendChild(appVariableElementChild);
420: }
421: }
422:
423: //</MissingApplicationVariables>
424: endpointInformationElement
425: .appendChild(missingAppVarsElementChild);
426:
427: // <MissingApplicationConfigurations>
428: Element missingAppConfigsElementChild = document
429: .createElement(MISSING_APPCONFIGS_KEY);
430:
431: String[] missingAppConfigs = data
432: .getMissingApplicationConfigurations();
433: if (missingAppConfigs != null
434: && missingAppConfigs.length > 0) {
435: for (int i = 0; i < missingAppConfigs.length; i++) {
436: // <ApplicationConfiguration>
437: Element appConfigurationElementChild = document
438: .createElement(MISSING_APPCONFIG_NAME_KEY);
439: if (appConfigurationElementChild != null) {
440: appConfigurationElementChild
441: .setTextContent(missingAppConfigs[i]);
442: }
443: //</ApplicationConfiguration>
444: missingAppConfigsElementChild
445: .appendChild(appConfigurationElementChild);
446: }
447: }
448:
449: //</MissingApplicationConfigurations>
450: endpointInformationElement
451: .appendChild(missingAppConfigsElementChild);
452:
453: }
454: return endpointInformationElement;
455: }
456:
457: /**
458: * Creates JavaEEVerifierReports, list of all
459: * JavaEEVerifier Reports
460: *
461: * @param document
462: * @param data
463: * @return
464: */
465: protected static Element createJavaEEVerifierReports(
466: Document document, ApplicationVerificationReport data) {
467: Element javaEEVerifierReportsListElement = null;
468: if ((document != null) && (data != null)) {
469: // <JavaEEVerifierReports>
470: javaEEVerifierReportsListElement = document
471: .createElement(JAVAEE_VERIFIER_REPORTS_LIST_KEY);
472:
473: for (JavaEEVerifierReport javaEEReport : data
474: .getJavaEEVerifierReports()) {
475: // <JavaEEVerifierReport>
476: Element reportElementChild = createJavaEEVerifierReport(
477: document, javaEEReport);
478: if (reportElementChild != null) {
479: javaEEVerifierReportsListElement
480: .appendChild(reportElementChild);
481: }
482: // </JavaEEVerifierReport>
483: }
484: }
485: return javaEEVerifierReportsListElement;
486: }
487:
488: /**
489: * Create JavaEEVerifierReport
490: *
491: * @param document
492: * @param data
493: * @return
494: */
495: protected static Element createJavaEEVerifierReport(
496: Document document, JavaEEVerifierReport data) {
497: Element javaEEReportElement = null;
498: if ((document != null) && (data != null)) {
499: // <JavaEEVerifierReport>
500: javaEEReportElement = document
501: .createElement(JAVAEE_VERIFIER_REPORT_KEY);
502:
503: // <ServiceUnitName>
504: Element serviceUnitNameChild = document
505: .createElement(JAVAEE_VERIFIER_SERVICE_UNIT_NAME);
506: if (serviceUnitNameChild != null) {
507: serviceUnitNameChild.setTextContent(data
508: .getServiceUnitName());
509: }
510: // </ServiceUnitName>
511: javaEEReportElement.appendChild(serviceUnitNameChild);
512:
513: // <ReportTable>
514: Element reportTableChildElement = document
515: .createElement(JAVAEE_VERIFIER_REPORT_TABLE_KEY);
516:
517: ArrayList<JavaEEVerifierReport.JavaEEReportItem> reportTable = data
518: .getJavaEEVerifierReport();
519: for (JavaEEVerifierReport.JavaEEReportItem item : reportTable) {
520: // <ReportItem>
521: Element reportItemChildElement = document
522: .createElement(JAVAEE_VERIFIER_REPORT_ITEM_KEY);
523:
524: // <EarFileName>
525: Element earFileName = document
526: .createElement(JAVAEE_VERIFIER_EAR_FILE_NAME_ITEM_KEY);
527: if (earFileName != null) {
528: earFileName.setTextContent(item.getEarFileName());
529: }
530: reportItemChildElement.appendChild(earFileName);
531: // </EarFileName>
532:
533: // <ReferenceBy>
534: Element referenceBy = document
535: .createElement(JAVAEE_VERIFIER_REFERENCE_BY_ITEM_KEY);
536: if (referenceBy != null) {
537: referenceBy.setTextContent(item.getReferenceBy());
538: }
539: reportItemChildElement.appendChild(referenceBy);
540: // </ReferenceBy>
541:
542: // <ReferenceClass>
543: Element referenceClass = document
544: .createElement(JAVAEE_VERIFIER_REFERENCE_CLASS_ITEM_KEY);
545: if (referenceClass != null) {
546: referenceClass.setTextContent(item
547: .getReferenceClass());
548: }
549: reportItemChildElement.appendChild(referenceClass);
550: // </ReferenceClass>
551:
552: // <JndiName>
553: Element jndiName = document
554: .createElement(JAVAEE_VERIFIER_JNDI_NAME_ITEM_KEY);
555: if (jndiName != null) {
556: jndiName.setTextContent(item.getJndiName());
557: }
558: reportItemChildElement.appendChild(jndiName);
559: // </JndiName>
560:
561: // <JndiClass>
562: Element jndiClass = document
563: .createElement(JAVAEE_VERIFIER_JNDI_CLASS_ITEM_KEY);
564: if (jndiClass != null) {
565: jndiClass.setTextContent(item.getJndiClass());
566: }
567: reportItemChildElement.appendChild(jndiClass);
568: // </JndiClass>
569:
570: // <message>
571: Element message = document
572: .createElement(JAVAEE_VERIFIER_MESSAGE_ITEM_KEY);
573: if (message != null) {
574: message.setTextContent(item.getMessage());
575: }
576: reportItemChildElement.appendChild(message);
577: // </message>
578:
579: // <Status>
580: Element status = document
581: .createElement(JAVAEE_VERIFIER_STATUS_ITEM_KEY);
582: if (status != null) {
583: status.setTextContent(new Integer(item.getStatus())
584: .toString());
585: }
586: reportItemChildElement.appendChild(status);
587: // </Status>
588:
589: reportTableChildElement
590: .appendChild(reportItemChildElement);
591: // </ReportItem>
592:
593: }
594: javaEEReportElement.appendChild(reportTableChildElement);
595: }
596: return javaEEReportElement;
597: }
598:
599: /**
600: * @param document
601: * @return
602: * @throws TransformerException
603: */
604: protected String writeToString(Document document)
605: throws TransformerException {
606: // Use a Transformer for aspectOutput
607: TransformerFactory tFactory = TransformerFactory.newInstance();
608: Transformer transformer = tFactory.newTransformer();
609: DOMSource source = new DOMSource(document);
610: StringWriter writer = new StringWriter();
611: StreamResult result = new StreamResult(writer);
612:
613: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
614: transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
615: transformer
616: .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
617: transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
618:
619: // indent the aspectOutput to make it more legible...
620: transformer.setOutputProperty(
621: "{http://xml.apache.org/xslt}indent-amount", "4");
622: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
623: transformer.transform(source, result);
624:
625: return result.getWriter().toString();
626: }
627:
628: /**
629: * @param args
630: */
631: public static void main(String[] args) {
632: // String uri = "C:/test/schema/verification/instance.xml";
633: String uri = "C:/test/schema/verification/ApplicationVerificationReport.xml";
634: try {
635: ApplicationVerificationReport report = ApplicationVerificationReportReader
636: .parseFromFile(uri);
637: System.out.println(report.getDisplayString());
638: String content = ApplicationVerificationReportWriter
639: .serialize(report);
640: System.out.println(content);
641: ApplicationVerificationReportWriter.setContents(new File(
642: uri), content);
643: } catch (MalformedURLException e) {
644: // TODO Auto-generated catch block
645: e.printStackTrace();
646: } catch (FileNotFoundException e) {
647: // TODO Auto-generated catch block
648: e.printStackTrace();
649: } catch (ParserConfigurationException e) {
650: // TODO Auto-generated catch block
651: e.printStackTrace();
652: } catch (SAXException e) {
653: // TODO Auto-generated catch block
654: e.printStackTrace();
655: } catch (URISyntaxException e) {
656: // TODO Auto-generated catch block
657: e.printStackTrace();
658: } catch (IOException e) {
659: // TODO Auto-generated catch block
660: e.printStackTrace();
661: } catch (TransformerException e) {
662: // TODO Auto-generated catch block
663: e.printStackTrace();
664: }
665: }
666:
667: }
|