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.util.*;
24 import org.json.*;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.chukwa.util.ExceptionUtil;
28
29 public class Views {
30 public JSONArray viewsData;
31 private String path = System.getenv("CHUKWA_DATA_DIR")
32 + "/views/workspace_view_list.cache";
33 private static final Log log = LogFactory.getLog(Views.class);
34
35 static public String getContents(File aFile) {
36
37 StringBuffer contents = new StringBuffer();
38
39 try {
40
41
42 BufferedReader input = new BufferedReader(new FileReader(aFile));
43 try {
44 String line = null;
45
46
47
48
49
50 while ((line = input.readLine()) != null) {
51 contents.append(line);
52 contents.append(System.getProperty("line.separator"));
53 }
54 } finally {
55 input.close();
56 }
57 } catch (IOException ex) {
58 ex.printStackTrace();
59 }
60
61 return contents.toString();
62 }
63
64 public Views() {
65 File aFile = new File(path);
66 String buffer = getContents(aFile);
67 try {
68 viewsData = new JSONArray(buffer);
69 } catch (JSONException e) {
70 log.debug(ExceptionUtil.getStackTrace(e));
71 }
72 }
73
74 public String getOwner(int i) {
75 String owner = null;
76 try {
77 owner = ((JSONObject) viewsData.get(i)).get("owner")
78 .toString();
79 } catch (JSONException e) {
80 log.debug(ExceptionUtil.getStackTrace(e));
81 }
82 return owner;
83 }
84
85 public Iterator getPermission(int i) {
86 Iterator permission = null;
87 try {
88 permission = ((JSONObject) ((JSONObject) viewsData.get(i))
89 .get("permission")).keys();
90 } catch (JSONException e) {
91 log.debug(ExceptionUtil.getStackTrace(e));
92 }
93 return permission;
94 }
95
96 public String getReadPermission(int i, String who) {
97 String read = null;
98 try {
99 JSONObject view = (JSONObject) viewsData.get(i);
100 JSONObject permission = (JSONObject) view.get("permission");
101 JSONObject user = (JSONObject) permission.get(who);
102 read = user.get("read").toString();
103 } catch (JSONException e) {
104 log.debug(ExceptionUtil.getStackTrace(e));
105 }
106 return read;
107 }
108
109 public String getWritePermission(int i, String who) {
110 String write = null;
111 try {
112 write = ((JSONObject) ((JSONObject) ((JSONObject) viewsData.get(i)).get("permission")).get(who)).get("write").toString();
113 } catch (JSONException e) {
114 log.debug(ExceptionUtil.getStackTrace(e));
115 }
116 return write;
117 }
118
119 public String getDescription(int i) {
120 String description = null;
121 try {
122 description = ((JSONObject) viewsData.get(i)).get(
123 "description").toString();
124 } catch (JSONException e) {
125 log.debug(ExceptionUtil.getStackTrace(e));
126 }
127 return description;
128 }
129
130 public String getKey(int i) {
131 String key = null;
132 try {
133 key = ((JSONObject) viewsData.get(i)).get("key").toString();
134 } catch (JSONException e) {
135 log.debug(ExceptionUtil.getStackTrace(e));
136 }
137 return key;
138 }
139
140 public int length() {
141 return viewsData.length();
142 }
143 }