This project has retired. For details please refer to its Attic page.
GenericChukwaMetricsList 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.StringReader;
22  import java.util.HashMap;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.parsers.ParserConfigurationException;
27  
28  import org.w3c.dom.Attr;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.NamedNodeMap;
32  import org.w3c.dom.NodeList;
33  import org.xml.sax.InputSource;
34  
35  public class GenericChukwaMetricsList extends ChukwaMetricsList<ChukwaMetrics> {
36    private static DocumentBuilderFactory factory;
37    private static DocumentBuilder docBuilder;
38    static {
39      factory = DocumentBuilderFactory.newInstance();
40      try {
41        docBuilder = factory.newDocumentBuilder();
42      } catch (ParserConfigurationException e) {
43        e.printStackTrace();
44      }
45    }
46    
47    public GenericChukwaMetricsList() {
48    }
49    
50    public GenericChukwaMetricsList(String recType) {
51      super(recType);
52    }
53    
54    public void fromXml(String xml) throws Exception {
55      InputSource is = new InputSource(new StringReader(xml));
56      Document doc = docBuilder.parse(is);
57      Element root = doc.getDocumentElement();
58      setRecordType(root.getTagName());
59      long timestamp = Long.parseLong(root.getAttribute("ts"));
60      setTimestamp(timestamp);
61      
62      NodeList children = root.getChildNodes();
63      for(int i=0; i<children.getLength(); i++) {
64        if(!children.item(i).getNodeName().equals("Metrics")) {
65          continue;
66        }
67        NamedNodeMap attrs = children.item(i).getAttributes();
68        if(attrs == null) {
69          continue;
70        }
71        
72        GenericChukwaMetrics metrics = new GenericChukwaMetrics();
73        for(int a=0; a<attrs.getLength(); a++) {
74          Attr attr = (Attr) attrs.item(a);
75          String name = attr.getName();
76          String value = attr.getValue();
77          if(name.equals("key")) {
78            metrics.setKey(value);
79          } else {
80            metrics.put(name, value);
81          }
82        }
83        addMetrics(metrics);
84      }
85    }
86  
87    @SuppressWarnings("serial")
88    public static class GenericChukwaMetrics extends HashMap<String, String> implements ChukwaMetrics {
89      private String key;
90      
91      @Override
92      public HashMap<String, String> getAttributes() {
93        return this;
94      }
95  
96      @Override
97      public String getKey() {
98        return key;
99      }
100 
101     public void setKey(String key) {
102       this.key = key;
103     }
104   }
105 }