This project has retired. For details please refer to its
Attic page.
ChukwaMetricsList xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.inputtools.jplugin;
20
21 import java.io.StringWriter;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.Map.Entry;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 public class ChukwaMetricsList<T extends ChukwaMetrics> {
37 private ArrayList<T> metricsList = null;
38 private long timestamp = new Date().getTime();
39 private String recordType = "JPlugin";
40
41 public ChukwaMetricsList() {
42 }
43
44 public ChukwaMetricsList(String recordType) {
45 setRecordType(recordType);
46 }
47
48 public void setMetricsList(ArrayList<T> metricsList) {
49 this.metricsList = metricsList;
50 }
51
52 public ArrayList<T> getMetricsList() {
53 if(metricsList == null){
54 metricsList = new ArrayList<T>();
55 }
56 return metricsList;
57 }
58
59 public void addMetrics(T metrics) {
60 getMetricsList().add(metrics);
61 }
62
63 public long getTimestamp() {
64 return timestamp;
65 }
66
67 public void setTimestamp(long timestamp) {
68 this.timestamp = timestamp;
69 }
70
71 public String toXml() throws Exception {
72 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73 DocumentBuilder docBuilder = null;
74 docBuilder = factory.newDocumentBuilder();
75 Document doc = docBuilder.newDocument();
76 Element root = doc.createElement(getRecordType());
77 doc.appendChild(root);
78 root.setAttribute("ts", getTimestamp()+"");
79 for(ChukwaMetrics metrics : getMetricsList()) {
80 Element elem = doc.createElement("Metrics");
81 elem.setAttribute("key", metrics.getKey());
82 for(Entry<String, String> attr : metrics.getAttributes().entrySet()) {
83 elem.setAttribute(attr.getKey(), attr.getValue());
84 }
85 root.appendChild(elem);
86 }
87
88 Transformer transformer = TransformerFactory.newInstance().newTransformer();
89 transformer.setOutputProperty("indent", "yes");
90 StringWriter sw = new StringWriter();
91 transformer.transform(new DOMSource(doc), new StreamResult(sw));
92
93 return sw.toString();
94 }
95
96 public void setRecordType(String recordType) {
97 this.recordType = recordType;
98 }
99
100 public String getRecordType() {
101 return recordType;
102 }
103
104 }