Source Code Cross Referenced for PerformanceDataMapWriter.java in  » ESB » open-esb » com » sun » esb » management » common » data » helper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » open esb » com.sun.esb.management.common.data.helper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * @(#)PerformanceDataMapWriter.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.PerformanceData;
059:
060:        /**
061:         * Writes Performance Measurement data to a String or file as XML
062:         *
063:         * @author graj
064:         */
065:        public class PerformanceDataMapWriter implements 
066:                PerformanceDataXMLConstants, Serializable {
067:            private static final long serialVersionUID = 1L;
068:
069:            static final String FILE_NAME_KEY = "performancedatamap.xml";
070:
071:            /** Constructor - Creates a new instance of PerformanceDataMapWriter */
072:            public PerformanceDataMapWriter() {
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 aspectOutput 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 performance
166:             *            data Map<String, PerforamanceData>
167:             * @return
168:             * @throws ParserConfigurationException
169:             * @throws TransformerException
170:             */
171:            public static String serialize(Map<String, PerformanceData> dataMap)
172:                    throws ParserConfigurationException, TransformerException {
173:                Document document = null;
174:                PerformanceDataMapWriter writer = new PerformanceDataMapWriter();
175:                if (dataMap != null) {
176:                    DocumentBuilderFactory factory = DocumentBuilderFactory
177:                            .newInstance();
178:                    DocumentBuilder builder = factory.newDocumentBuilder();
179:                    document = builder.newDocument(); // Create from whole cloth
180:
181:                    // ////////////////////////////////
182:                    // <PerformanceMeasurementDataList>
183:                    Element root = (Element) document
184:                            .createElement(PERFORMANCE_MEASUREMENT_DATA_LIST_KEY);
185:                    // xmlns="http://java.sun.com/xml/ns/esb/management/PerformanceMeasurementDataList"
186:                    root.setAttribute(NAMESPACE_KEY, NAMESPACE_VALUE);
187:                    // version = "1.0"
188:                    root.setAttribute(VERSION_KEY, VERSION_VALUE);
189:
190:                    for (String category : dataMap.keySet()) {
191:                        PerformanceData data = dataMap.get(category);
192:                        // ////////////////////////////////
193:                        // <PerformanceMeasurementData>
194:                        Element performanceMeasurementDataElementChild = writer
195:                                .createPerformanceMeasurementDataElement(
196:                                        document, data);
197:                        // </PerformanceMeasurementData>
198:                        root
199:                                .appendChild(performanceMeasurementDataElementChild);
200:                        // ////////////////////////////////
201:                    }
202:                    // </PerformanceMeasurementDataList>
203:                    document.appendChild(root);
204:                    // ////////////////////////////////
205:
206:                }
207:                return writer.writeToString(document);
208:            }
209:
210:            /**
211:             *
212:             * @param document
213:             * @param data
214:             * @return
215:             */
216:            protected Element createPerformanceMeasurementDataElement(
217:                    Document document, PerformanceData data) {
218:                Element performanceDataElement = null;
219:                if ((document != null) && (data != null)) {
220:                    // <PerformanceMeasurementData>
221:                    performanceDataElement = document
222:                            .createElement(PERFORMANCE_MEASUREMENT_DATA_KEY);
223:
224:                    // <category>
225:                    Element categoryElementChild = document
226:                            .createElement(CATEGORY_KEY);
227:                    if (categoryElementChild != null) {
228:                        categoryElementChild.setTextContent(data.getCategory());
229:                    }
230:                    // </category>
231:                    performanceDataElement.appendChild(categoryElementChild);
232:
233:                    // <endpoint>
234:                    Element endpointElementChild = document
235:                            .createElement(ENDPOINT_KEY);
236:                    if (endpointElementChild != null) {
237:                        endpointElementChild.setTextContent(data.getEndpoint());
238:                    }
239:                    // </endpoint>
240:                    performanceDataElement.appendChild(endpointElementChild);
241:
242:                    // <sourceClassName>
243:                    Element sourceClassNameElementChild = document
244:                            .createElement(SOURCE_CLASS_NAME_KEY);
245:                    if (sourceClassNameElementChild != null) {
246:                        sourceClassNameElementChild.setTextContent(data
247:                                .getSourceClassName());
248:                    }
249:                    // </sourceClassName>
250:                    performanceDataElement
251:                            .appendChild(sourceClassNameElementChild);
252:
253:                    // <numberOfMeasurementObjects>
254:                    Element numberOfMeasurementObjectsElementChild = document
255:                            .createElement(NUMBER_OF_MEASUREMENT_OBJECTS_KEY);
256:                    if (numberOfMeasurementObjectsElementChild != null) {
257:                        numberOfMeasurementObjectsElementChild
258:                                .setTextContent(data
259:                                        .getNumberOfMeasurementObjects()
260:                                        + "");
261:                    }
262:                    // </numberOfMeasurementObjects>
263:                    performanceDataElement
264:                            .appendChild(numberOfMeasurementObjectsElementChild);
265:
266:                    // <numberOfMeasurements>
267:                    Element numberOfMeasurementsElementChild = document
268:                            .createElement(NUMBER_OF_MEASUREMENTS_KEY);
269:                    if (categoryElementChild != null) {
270:                        numberOfMeasurementsElementChild.setTextContent(data
271:                                .getNumberOfMeasurements()
272:                                + "");
273:                    }
274:                    // </numberOfMeasurements>
275:                    performanceDataElement
276:                            .appendChild(numberOfMeasurementsElementChild);
277:
278:                    // <average>
279:                    Element averageElementChild = document
280:                            .createElement(AVERAGE_KEY);
281:                    if (averageElementChild != null) {
282:                        averageElementChild.setTextContent(data.getAverage()
283:                                + "");
284:                    }
285:                    // </average>
286:                    performanceDataElement.appendChild(averageElementChild);
287:
288:                    // <averageWithoutFirstMeasurement>
289:                    Element averageWithoutFirstMeasurementElementChild = document
290:                            .createElement(AVERAGE_WITHOUT_FIRST_MEASUREMENT_KEY);
291:                    if (averageWithoutFirstMeasurementElementChild != null) {
292:                        averageWithoutFirstMeasurementElementChild
293:                                .setTextContent(data
294:                                        .getAverageWithoutFirstMeasurement()
295:                                        + "");
296:                    }
297:                    // </averageWithoutFirstMeasurement>
298:                    performanceDataElement
299:                            .appendChild(averageWithoutFirstMeasurementElementChild);
300:
301:                    // <firstMeasurementTime>
302:                    Element firstMeasurementTimeElementChild = document
303:                            .createElement(FIRST_MEASUREMENT_TIME_KEY);
304:                    if (firstMeasurementTimeElementChild != null) {
305:                        firstMeasurementTimeElementChild.setTextContent(data
306:                                .getFirstMeasurementTime()
307:                                + "");
308:                    }
309:                    // </firstMeasurementTime>
310:                    performanceDataElement
311:                            .appendChild(firstMeasurementTimeElementChild);
312:
313:                    // <load>
314:                    Element loadElementChild = document.createElement(LOAD_KEY);
315:                    if (loadElementChild != null) {
316:                        loadElementChild.setTextContent(data.getLoad() + "");
317:                    }
318:                    // </load>
319:                    performanceDataElement.appendChild(loadElementChild);
320:
321:                    // <median>
322:                    Element medianElementChild = document
323:                            .createElement(MEDIAN_KEY);
324:                    if (medianElementChild != null) {
325:                        medianElementChild
326:                                .setTextContent(data.getMedian() + "");
327:                    }
328:                    // </median>
329:                    performanceDataElement.appendChild(medianElementChild);
330:
331:                    // <throughput>
332:                    Element throughputElementChild = document
333:                            .createElement(THROUGHPUT_KEY);
334:                    if (throughputElementChild != null) {
335:                        throughputElementChild.setTextContent(data
336:                                .getThroughput()
337:                                + "");
338:                    }
339:                    // </throughput>
340:                    performanceDataElement.appendChild(throughputElementChild);
341:
342:                    // <timeTaken>
343:                    Element timeTakenElementChild = document
344:                            .createElement(TIME_TAKEN_KEY);
345:                    if (timeTakenElementChild != null) {
346:                        timeTakenElementChild.setTextContent(data
347:                                .getTimeTaken()
348:                                + "");
349:                    }
350:                    // </timeTaken>
351:                    performanceDataElement.appendChild(timeTakenElementChild);
352:
353:                    // <totalTime>
354:                    Element totalTimeElementChild = document
355:                            .createElement(TOTAL_TIME_KEY);
356:                    if (totalTimeElementChild != null) {
357:                        totalTimeElementChild.setTextContent(data
358:                                .getTotalTime()
359:                                + "");
360:                    }
361:                    // </totalTime>
362:                    performanceDataElement.appendChild(totalTimeElementChild);
363:                }
364:                return performanceDataElement;
365:            }
366:
367:            /**
368:             * @param document
369:             * @return
370:             * @throws TransformerException
371:             */
372:            protected String writeToString(Document document)
373:                    throws TransformerException {
374:                // Use a Transformer for aspectOutput
375:                TransformerFactory tFactory = TransformerFactory.newInstance();
376:                Transformer transformer = tFactory.newTransformer();
377:                DOMSource source = new DOMSource(document);
378:                StringWriter writer = new StringWriter();
379:                StreamResult result = new StreamResult(writer);
380:
381:                transformer.setOutputProperty(OutputKeys.METHOD, "xml");
382:                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
383:                transformer
384:                        .setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
385:                transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
386:
387:                // indent the aspectOutput to make it more legible...
388:                transformer.setOutputProperty(
389:                        "{http://xml.apache.org/xslt}indent-amount", "4");
390:                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
391:                transformer.transform(source, result);
392:
393:                return result.getWriter().toString();
394:            }
395:
396:            /**
397:             * @param args
398:             */
399:            public static void main(String[] args) {
400:                // String uri = "C:/test/performance/instance.xml";
401:                String uri = "C:/test/schema/performance/PerformanceMeasurementDataList.xml";
402:                try {
403:                    Map<String, PerformanceData> map = PerformanceDataMapReader
404:                            .parseFromFile(uri);
405:                    for (String category : map.keySet()) {
406:                        System.out
407:                                .println(map.get(category).getDisplayString());
408:                    }
409:                    String content = PerformanceDataMapWriter.serialize(map);
410:                    System.out.println(content);
411:                    PerformanceDataMapWriter
412:                            .setContents(new File(uri), content);
413:                } catch (MalformedURLException e) {
414:                    // TODO Auto-generated catch block
415:                    e.printStackTrace();
416:                } catch (FileNotFoundException e) {
417:                    // TODO Auto-generated catch block
418:                    e.printStackTrace();
419:                } catch (ParserConfigurationException e) {
420:                    // TODO Auto-generated catch block
421:                    e.printStackTrace();
422:                } catch (SAXException e) {
423:                    // TODO Auto-generated catch block
424:                    e.printStackTrace();
425:                } catch (URISyntaxException e) {
426:                    // TODO Auto-generated catch block
427:                    e.printStackTrace();
428:                } catch (IOException e) {
429:                    // TODO Auto-generated catch block
430:                    e.printStackTrace();
431:                } catch (TransformerException e) {
432:                    // TODO Auto-generated catch block
433:                    e.printStackTrace();
434:                }
435:            }
436:
437:        }
w___w___w_.jav__a___2__s_.___c_o_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.