1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
41 StringBuffer contents = new StringBuffer();
42
43 try {
44
45
46 BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(aFile.getAbsolutePath()), Charset.forName("UTF-8")));
47 try {
48 String line = null;
49
50
51
52
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 }