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.hicc;
20  
21  
22  import java.net.*;
23  import java.io.*;
24  import org.json.*;
25  import org.apache.log4j.Logger;
26  import org.apache.hadoop.chukwa.util.ExceptionUtil;
27  
28  public class JSONLoader {
29    public JSONArray jsonData;
30    private static Logger log = Logger.getLogger(JSONLoader.class);
31  
32    static public String getContents(String source) {
33      // ...checks on aFile are elided
34      StringBuffer contents = new StringBuffer();
35  
36      try {
37        // use buffering, reading one line at a time
38        // FileReader always assumes default encoding is OK!
39        URL yahoo = new URL(source);
40        BufferedReader in = new BufferedReader(new InputStreamReader(yahoo
41            .openStream()));
42  
43        String inputLine;
44  
45        while ((inputLine = in.readLine()) != null) {
46          contents.append(inputLine);
47          contents.append(System.getProperty("line.separator"));
48        }
49        in.close();
50      } catch (IOException ex) {
51        ex.printStackTrace();
52      }
53  
54      return contents.toString();
55    }
56  
57    public JSONLoader(String source) {
58      String buffer = getContents(source);
59      try {
60        JSONObject rows = new JSONObject(buffer);
61        jsonData = new JSONArray(rows.get("rows").toString());
62      } catch (JSONException e) {
63        log.debug(ExceptionUtil.getStackTrace(e)); 
64      }
65    }
66  
67    public String getTS(int i) {
68      String ts = null;
69      try {
70        ts = ((JSONObject) jsonData.get(i)).get("ts").toString();
71      } catch (JSONException e) {
72        log.debug(ExceptionUtil.getStackTrace(e)); 
73      }
74      return ts;
75    }
76  
77    public String getTags(int i) {
78      String tags = null;
79      try {
80        tags = ((JSONObject) jsonData.get(i)).get("tags")
81            .toString();
82      } catch (JSONException e) {
83        log.debug(ExceptionUtil.getStackTrace(e)); 
84      }
85      return tags;
86    }
87  
88    public String getValue(int i) {
89      String value = null;
90      try {
91        value = ((JSONObject) jsonData.get(i)).get("value")
92            .toString();
93      } catch (JSONException e) {
94        log.debug(ExceptionUtil.getStackTrace(e)); 
95      }
96      return value;
97    }
98  
99    public int length() {
100     return jsonData.length();
101   }
102 }