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.io.*;
23  import java.nio.charset.Charset;
24  import java.util.*;
25  
26  import org.json.simple.JSONArray;
27  import org.json.simple.JSONObject;
28  import org.json.simple.JSONValue;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.chukwa.util.ExceptionUtil;
32  
33  public class Views {
34    public JSONArray viewsData;
35    private String path = System.getenv("CHUKWA_DATA_DIR")
36        + "/views/workspace_view_list.cache";
37    private static final Log log = LogFactory.getLog(Views.class);
38  
39    static public String getContents(File aFile) {
40      // ...checks on aFile are elided
41      StringBuffer contents = new StringBuffer();
42  
43      try {
44        // use buffering, reading one line at a time
45        // FileReader always assumes default encoding is OK!
46        BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(aFile.getAbsolutePath()), Charset.forName("UTF-8")));
47        try {
48          String line = null; // not declared within while loop
49          /*
50           * readLine is a bit quirky : it returns the content of a line MINUS the
51           * newline. it returns null only for the END of the stream. it returns
52           * an empty String if two newlines appear in a row.
53           */
54          while ((line = input.readLine()) != null) {
55            contents.append(line);
56            contents.append(System.getProperty("line.separator"));
57          }
58        } finally {
59          input.close();
60        }
61      } catch (IOException ex) {
62        ex.printStackTrace();
63      }
64  
65      return contents.toString();
66    }
67  
68    public Views() {
69      File aFile = new File(path);
70      String buffer = getContents(aFile);
71      try {
72        viewsData = (JSONArray) JSONValue.parse(buffer);
73      } catch (Exception e) {
74        log.debug(ExceptionUtil.getStackTrace(e));
75      }
76    }
77  
78    public String getOwner(int i) {
79      String owner = null;
80      try {
81        owner = ((JSONObject) viewsData.get(i)).get("owner")
82            .toString();
83      } catch (Exception e) {
84        log.debug(ExceptionUtil.getStackTrace(e));
85      }
86      return owner;
87    }
88  
89    public Iterator getPermission(int i) {
90      Iterator permission = null;
91      try {
92        permission = ((JSONObject) ((JSONObject) viewsData.get(i))
93            .get("permission")).keySet().iterator();
94      } catch (Exception e) {
95        log.debug(ExceptionUtil.getStackTrace(e));
96      }
97      return permission;
98    }
99  
100   public String getReadPermission(int i, String who) {
101     String read = null;
102     try {
103       JSONObject view = (JSONObject) viewsData.get(i);
104       JSONObject permission = (JSONObject) view.get("permission");
105       JSONObject user = (JSONObject) permission.get(who);
106       read = user.get("read").toString();
107     } catch (Exception e) {
108       log.debug(ExceptionUtil.getStackTrace(e));
109     }
110     return read;
111   }
112 
113   public String getWritePermission(int i, String who) {
114     String write = null;
115     try {
116       write = ((JSONObject) ((JSONObject) ((JSONObject) viewsData.get(i)).get("permission")).get(who)).get("write").toString();
117     } catch (Exception e) {
118       log.debug(ExceptionUtil.getStackTrace(e));
119     }
120     return write;
121   }
122 
123   public String getDescription(int i) {
124     String description = null;
125     try {
126       description = ((JSONObject) viewsData.get(i)).get(
127           "description").toString();
128     } catch (Exception e) {
129       log.debug(ExceptionUtil.getStackTrace(e));
130     }
131     return description;
132   }
133 
134   public String getKey(int i) {
135     String key = null;
136     try {
137       key = ((JSONObject) viewsData.get(i)).get("key").toString();
138     } catch (Exception e) {
139       log.debug(ExceptionUtil.getStackTrace(e));
140     }
141     return key;
142   }
143 
144   public int length() {
145     return viewsData.size();
146   }
147 }