This project has retired. For details please refer to its
Attic page.
ViewStore xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.datastore;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
31 import org.apache.hadoop.chukwa.hicc.HiccWebServer;
32 import org.apache.hadoop.chukwa.rest.bean.ViewBean;
33 import org.apache.hadoop.chukwa.util.ExceptionUtil;
34 import org.apache.hadoop.conf.Configuration;
35 import org.apache.hadoop.fs.FSDataInputStream;
36 import org.apache.hadoop.fs.FSDataOutputStream;
37 import org.apache.hadoop.fs.FileStatus;
38 import org.apache.hadoop.fs.FileSystem;
39 import org.apache.hadoop.fs.Path;
40 import org.json.simple.JSONArray;
41
42 public class ViewStore {
43 private String vid = null;
44 private String uid = null;
45 private ViewBean view = null;
46 private static Log log = LogFactory.getLog(ViewStore.class);
47 private static Configuration config = new Configuration();
48 private static ChukwaConfiguration chukwaConf = new ChukwaConfiguration();
49 private static String viewPath = config.get("fs.default.name")+File.separator+chukwaConf.get("chukwa.data.dir")+File.separator+"hicc"+File.separator+"views";
50 private static String publicViewPath = viewPath+File.separator+"public";
51 private static String usersViewPath = viewPath+File.separator+"users";
52 private static String PUBLIC = "public".intern();
53
54 public ViewStore() throws IllegalAccessException {
55 ViewStore.config = HiccWebServer.getConfig();
56 }
57
58 public ViewStore(String uid, String vid) throws IllegalAccessException {
59 this.uid=uid;
60 this.vid=vid;
61 load(uid, vid);
62 }
63
64 public void load(String uid, String vid) throws IllegalAccessException {
65 StringBuilder vp = new StringBuilder();
66 vp.append(usersViewPath);
67 vp.append(File.separator);
68 vp.append(uid);
69 vp.append(File.separator);
70 vp.append(vid);
71 vp.append(".view");
72 Path viewFile = new Path(vp.toString());
73 try {
74 FileSystem fs = FileSystem.get(config);
75 if(!fs.exists(viewFile)) {
76 StringBuilder pubPath = new StringBuilder();
77 pubPath.append(publicViewPath);
78 pubPath.append(File.separator);
79 pubPath.append(vid);
80 pubPath.append(".view");
81 viewFile = new Path(pubPath.toString());
82 }
83 if(fs.exists(viewFile)) {
84 FileStatus[] fstatus = fs.listStatus(viewFile);
85 long size = fstatus[0].getLen();
86 FSDataInputStream viewStream = fs.open(viewFile);
87 byte[] buffer = new byte[(int)size];
88 viewStream.readFully(buffer);
89 viewStream.close();
90 try {
91 view = new ViewBean(buffer);
92 view.update();
93 } catch (Exception e) {
94 log.error(ExceptionUtil.getStackTrace(e));
95 throw new IllegalAccessException("Unable to access view: "+vid);
96 }
97 }
98 } catch (IOException ex) {
99 log.error(ExceptionUtil.getStackTrace(ex));
100 }
101 }
102
103 public String getMessage() {
104 return view.toString();
105 }
106
107 public ViewBean get() throws IllegalAccessException {
108 if(view==null) {
109 load(uid, vid);
110 }
111 if(view==null) {
112
113 try {
114 load(null, vid);
115 } catch(Exception e) {
116 log.error(ExceptionUtil.getStackTrace(e));
117 view = null;
118 }
119 }
120 return view;
121 }
122
123 public void set(ViewBean view) throws IllegalAccessException {
124 try {
125 if (this.view == null || (this.view.getOwner().intern() == view.getOwner().intern())) {
126 if(this.view!=null) {
127 delete();
128 }
129 StringBuilder viewPath = new StringBuilder();
130 if(view.getPermissionType().intern()==PUBLIC) {
131 viewPath.append(publicViewPath);
132 } else {
133 viewPath.append(usersViewPath);
134 viewPath.append(File.separator);
135 viewPath.append(uid);
136 }
137 viewPath.append(File.separator);
138 viewPath.append(view.getName());
139 viewPath.append(".view");
140 Path viewFile = new Path(viewPath.toString());
141 try {
142 FileSystem fs = FileSystem.get(config);
143 FSDataOutputStream out = fs.create(viewFile,true);
144 out.write(view.deserialize().toString().getBytes());
145 out.close();
146 } catch (IOException ex) {
147 log.error(ExceptionUtil.getStackTrace(ex));
148 }
149 this.view = view;
150 } else {
151 if(view.getPermissionType().intern()==PUBLIC) {
152 throw new IllegalAccessException("Unable to save public view, duplicated view exists.");
153 } else {
154 throw new IllegalAccessException("Unable to save user view.");
155 }
156 }
157 } catch (Exception e) {
158 log.error(ExceptionUtil.getStackTrace(e));
159 throw new IllegalAccessException("Unable to access user view.");
160 }
161 }
162
163 public void delete() throws IllegalAccessException {
164 try {
165 if(this.view==null) {
166 get();
167 }
168 if (this.view!=null) {
169 StringBuilder viewPath = new StringBuilder();
170 if(view.getPermissionType().intern()==PUBLIC) {
171 viewPath.append(publicViewPath);
172 } else {
173 viewPath.append(usersViewPath);
174 viewPath.append(File.separator);
175 viewPath.append(uid);
176 }
177 viewPath.append(File.separator);
178 viewPath.append(view.getName());
179 viewPath.append(".view");
180 Path viewFile = new Path(viewPath.toString());
181 try {
182 FileSystem fs = FileSystem.get(config);
183 fs.delete(viewFile, true);
184 } catch (IOException ex) {
185 log.error(ExceptionUtil.getStackTrace(ex));
186 }
187 } else {
188 throw new IllegalAccessException("Unable to delete user view, view does not exist.");
189 }
190 } catch (Exception e) {
191 log.error(ExceptionUtil.getStackTrace(e));
192 throw new IllegalAccessException("Unable to access user view.");
193 }
194 }
195
196 public static JSONArray list(String uid) throws IllegalAccessException {
197 StringBuilder viewPath = new StringBuilder();
198 viewPath.append(usersViewPath);
199 viewPath.append(File.separator);
200 viewPath.append(uid);
201 String[] pathList = new String[2];
202 pathList[0]=viewPath.toString();
203 pathList[1]=publicViewPath;
204 JSONArray list = new JSONArray();
205 for(String path : pathList) {
206 Path viewFile = new Path(path);
207 try {
208 FileSystem fs = FileSystem.get(config);
209 FileStatus[] fstatus = fs.listStatus(viewFile);
210 if(fstatus!=null) {
211 for(int i=0;i<fstatus.length;i++) {
212 if(!fstatus[i].getPath().getName().endsWith(".view")) {
213 continue;
214 }
215 long size = fstatus[i].getLen();
216 FSDataInputStream viewStream = fs.open(fstatus[i].getPath());
217 byte[] buffer = new byte[(int)size];
218 viewStream.readFully(buffer);
219 viewStream.close();
220 try {
221 ViewBean view = new ViewBean(buffer);
222 Map<String, String> json=new LinkedHashMap<String, String>();
223 json.put("name", view.getName());
224 json.put("type", view.getPermissionType());
225 json.put("owner", view.getOwner());
226 if(uid.intern()==view.getOwner().intern()) {
227 json.put("editable","true");
228 } else {
229 json.put("editable","false");
230 }
231 list.add(json);
232 } catch (Exception e) {
233 log.error(ExceptionUtil.getStackTrace(e));
234 }
235 }
236 }
237 } catch (IOException ex) {
238 log.error(ExceptionUtil.getStackTrace(ex));
239 throw new IllegalAccessException("Unable to access user view.");
240 }
241 }
242 return list;
243 }
244 }
245