This project has retired. For details please refer to its Attic page.
ChukwaMetricsList xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 }