This project has retired. For details please refer to its
Attic page.
Workspace 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.hicc;
20
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import java.sql.*;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.chukwa.util.XssFilter;
30 import org.json.*;
31 import org.apache.hadoop.chukwa.util.ExceptionUtil;
32
33 public class Workspace extends HttpServlet {
34 public static final long serialVersionUID = 101L;
35 private static final Log log = LogFactory.getLog(Workspace.class);
36 private String path = System.getenv("CHUKWA_DATA_DIR");
37 transient private JSONObject hash = new JSONObject();
38 transient private XssFilter xf;
39
40 @Override
41 protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
42 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
43 }
44
45 public void doGet(HttpServletRequest request, HttpServletResponse response)
46 throws IOException, ServletException {
47 xf = new XssFilter(request);
48 response.setContentType("text/plain");
49 String method = xf.getParameter("method");
50 if (method.equals("get_views_list")) {
51 getViewsList(request, response);
52 }
53 if (method.equals("get_view")) {
54 getView(request, response);
55 }
56 if (method.equals("save_view")) {
57 saveView(request, response);
58 }
59 if (method.equals("change_view_info")) {
60 changeViewInfo(request, response);
61 }
62 if (method.equals("get_widget_list")) {
63 getWidgetList(request, response);
64 }
65 if (method.equals("clone_view")) {
66 cloneView(request, response);
67 }
68 if (method.equals("delete_view")) {
69 deleteView(request, response);
70 }
71 }
72
73 public void doPost(HttpServletRequest request, HttpServletResponse response)
74 throws IOException, ServletException {
75 doGet(request, response);
76 }
77
78 static public String getContents(File aFile) {
79
80 StringBuffer contents = new StringBuffer();
81
82 try {
83
84
85 BufferedReader input = new BufferedReader(new FileReader(aFile));
86 try {
87 String line = null;
88
89
90
91
92
93 while ((line = input.readLine()) != null) {
94 contents.append(line);
95 contents.append(System.getProperty("line.separator"));
96 }
97 } finally {
98 input.close();
99 }
100 } catch (IOException ex) {
101 ex.printStackTrace();
102 }
103
104 return contents.toString();
105 }
106
107 public void setContents(String fName, String buffer) {
108 try {
109 FileWriter fstream = new FileWriter(fName);
110 BufferedWriter out = new BufferedWriter(fstream);
111 out.write(buffer);
112 out.close();
113 } catch (Exception e) {
114 System.err.println("Error: " + e.getMessage());
115 }
116 }
117
118 public void cloneView(HttpServletRequest request, HttpServletResponse response)
119 throws IOException, ServletException {
120 PrintWriter out = response.getWriter();
121 String name = xf.getParameter("name");
122 String template = xf.getParameter("clone_name");
123 File aFile = new File(path + "/views/" + template);
124 String config = getContents(aFile);
125 int i = 0;
126 boolean check = true;
127 while (check) {
128 String tmpName = name;
129 if (i > 0) {
130 tmpName = name + i;
131 }
132 File checkFile = new File(path + "/views/" + tmpName + ".view");
133 check = checkFile.exists();
134 if (!check) {
135 name = tmpName;
136 }
137 i = i + 1;
138 }
139 setContents(path + "/views/" + name + ".view", config);
140 File deleteCache = new File(path + "/views/workspace_view_list.cache");
141 if(!deleteCache.delete()) {
142 log.warn("Can not delete "+path + "/views/workspace_view_list.cache");
143 }
144 genViewCache(path + "/views");
145 aFile = new File(path + "/views/workspace_view_list.cache");
146 String viewsCache = getContents(aFile);
147 out.println(viewsCache);
148 }
149
150 public void deleteView(HttpServletRequest request,
151 HttpServletResponse response) throws IOException, ServletException {
152 String name = xf.getParameter("name");
153 File aFile = new File(path + "/views/" + name + ".view");
154 if(!aFile.delete()) {
155 log.warn("Can not delete " + path + "/views/" + name + ".view");
156 }
157 File deleteCache = new File(path + "/views/workspace_view_list.cache");
158 if(!deleteCache.delete()) {
159 log.warn("Can not delete "+path + "/views/workspace_view_list.cache");
160 }
161 genViewCache(path + "/views");
162 }
163
164 public void getViewsList(HttpServletRequest request,
165 HttpServletResponse response) throws IOException, ServletException {
166 PrintWriter out = response.getWriter();
167 genViewCache(path + "/views");
168 File aFile = new File(path + "/views/workspace_view_list.cache");
169 String viewsCache = getContents(aFile);
170 out.println(viewsCache);
171 }
172
173 public void getView(HttpServletRequest request, HttpServletResponse response)
174 throws IOException, ServletException {
175 PrintWriter out = response.getWriter();
176 String id = xf.getParameter("id");
177 genViewCache(path + "/views");
178 File aFile = new File(path + "/views/" + id + ".view");
179 String view = getContents(aFile);
180 out.println(view);
181 }
182
183 public void changeViewInfo(HttpServletRequest request,
184 HttpServletResponse response) throws IOException, ServletException {
185 PrintWriter out = response.getWriter();
186 String id = xf.getParameter("name");
187 String config = request.getParameter("config");
188 try {
189 JSONObject jt = new JSONObject(config);
190 File aFile = new File(path + "/views/" + id + ".view");
191 String original = getContents(aFile);
192 JSONObject updateObject = new JSONObject(original);
193 updateObject.put("description", jt.get("description"));
194 setContents(path + "/views/" + id + ".view", updateObject.toString());
195 if (!rename(id, jt.get("description").toString())) {
196 throw new Exception("Rename view file failed");
197 }
198 File deleteCache = new File(path + "/views/workspace_view_list.cache");
199 if(!deleteCache.delete()) {
200 log.warn("Can not delete "+path + "/views/workspace_view_list.cache");
201 }
202 genViewCache(path + "/views");
203 out.println("Workspace is stored successfully.");
204 } catch (Exception e) {
205 out.println("Workspace store failed.");
206 }
207 }
208
209 public void saveView(HttpServletRequest request, HttpServletResponse response)
210 throws IOException, ServletException {
211 PrintWriter out = response.getWriter();
212 String id = xf.getParameter("name");
213 String config = request.getParameter("config");
214 setContents(path + "/views/" + id + ".view", config);
215 out.println("Workspace is stored successfully.");
216 }
217
218 public void getWidgetList(HttpServletRequest request,
219 HttpServletResponse response) throws IOException, ServletException {
220 PrintWriter out = response.getWriter();
221 genWidgetCache(path + "/descriptors");
222 File aFile = new File(path + "/descriptors/workspace_plugin.cache");
223 String viewsCache = getContents(aFile);
224 out.println(viewsCache);
225 }
226
227 private void genViewCache(String source) {
228 File cacheFile = new File(source + "/workspace_view_list.cache");
229 if (!cacheFile.exists()) {
230 File dir = new File(source);
231 File[] filesWanted = dir.listFiles(new FilenameFilter() {
232 public boolean accept(File dir, String name) {
233 return name.endsWith(".view");
234 }
235 });
236 JSONObject[] cacheGroup = new JSONObject[filesWanted.length];
237 for (int i = 0; i < filesWanted.length; i++) {
238 String buffer = getContents(filesWanted[i]);
239 try {
240 JSONObject jt = new JSONObject(buffer);
241 String fn = filesWanted[i].getName();
242 jt.put("key", fn.substring(0, (fn.length() - 5)));
243 cacheGroup[i] = jt;
244 } catch (Exception e) {
245 log.debug(ExceptionUtil.getStackTrace(e));
246 }
247 }
248 String viewList = convertObjectsToViewList(cacheGroup);
249 setContents(source + "/workspace_view_list.cache", viewList);
250 }
251 }
252
253 public String convertObjectsToViewList(JSONObject[] objArray) {
254 JSONArray jsonArr = new JSONArray();
255 JSONObject permission = new JSONObject();
256 JSONObject user = new JSONObject();
257 try {
258 permission.put("read", 1);
259 permission.put("modify", 1);
260 user.put("all", permission);
261 } catch (Exception e) {
262 System.err.println("JSON Exception: " + e.getMessage());
263 }
264 for (int i = 0; i < objArray.length; i++) {
265 try {
266 JSONObject jsonObj = new JSONObject();
267 jsonObj.put("key", objArray[i].get("key"));
268 jsonObj.put("description", objArray[i].get("description"));
269 jsonObj.put("owner", "");
270 jsonObj.put("permission", user);
271 jsonArr.put(jsonObj);
272 } catch (Exception e) {
273 System.err.println("JSON Exception: " + e.getMessage());
274 }
275 }
276 return jsonArr.toString();
277 }
278
279 private void genWidgetCache(String source) {
280 File cacheFile = new File(source + "/workspace_plugin.cache");
281 File cacheDir = new File(source);
282 if (!cacheFile.exists()
283 || cacheFile.lastModified() < cacheDir.lastModified()) {
284 File dir = new File(source);
285 File[] filesWanted = dir.listFiles(new FilenameFilter() {
286 public boolean accept(File dir, String name) {
287 return name.endsWith(".descriptor");
288 }
289 });
290 JSONObject[] cacheGroup = new JSONObject[filesWanted.length];
291 for (int i = 0; i < filesWanted.length; i++) {
292 String buffer = getContents(filesWanted[i]);
293 try {
294 JSONObject jt = new JSONObject(buffer);
295 cacheGroup[i] = jt;
296 } catch (Exception e) {
297 log.debug(ExceptionUtil.getStackTrace(e));
298 }
299 }
300 String widgetList = convertObjectsToWidgetList(cacheGroup);
301 setContents(source + "/workspace_plugin.cache", widgetList);
302 }
303 }
304
305 public String convertObjectsToWidgetList(JSONObject[] objArray) {
306 JSONObject jsonObj = new JSONObject();
307 JSONArray jsonArr = new JSONArray();
308 for (int i = 0; i < objArray.length; i++) {
309 jsonArr.put(objArray[i]);
310 }
311 try {
312 jsonObj.put("detail", jsonArr);
313 } catch (Exception e) {
314 System.err.println("JSON Exception: " + e.getMessage());
315 }
316 for (int i = 0; i < objArray.length; i++) {
317 try {
318 String[] categoriesArray = objArray[i].get("categories").toString()
319 .split(",");
320 hash = addToHash(hash, categoriesArray, objArray[i]);
321 } catch (JSONException e) {
322 System.err.println("JSON Exception: " + e.getMessage());
323 }
324 }
325 try {
326 jsonObj.put("children", hash);
327 } catch (Exception e) {
328 System.err.println("JSON Exception: " + e.getMessage());
329 }
330 return jsonObj.toString();
331 }
332
333 public JSONObject addToHash(JSONObject hash, String[] categoriesArray,
334 JSONObject obj) {
335 JSONObject subHash = hash;
336 for (int i = 0; i < categoriesArray.length; i++) {
337 String id = categoriesArray[i];
338 if (i >= categoriesArray.length - 1) {
339 try {
340 subHash.put("leaf:" + obj.get("title"), obj.get("id"));
341 } catch (Exception e) {
342 System.err.println("JSON Exception: " + e.getMessage());
343 }
344 } else {
345 try {
346 subHash = subHash.getJSONObject("node:" + id);
347 } catch (JSONException e) {
348 try {
349 JSONObject tmpHash = new JSONObject();
350 subHash.put("node:" + id, tmpHash);
351 subHash = tmpHash;
352 } catch (JSONException ex) {
353 log.debug(ExceptionUtil.getStackTrace(e));
354 }
355 }
356 }
357 }
358 return hash;
359 }
360
361 private boolean rename(String id, String desc) {
362 try {
363 File view = new File(path + "/views/" + id + ".view");
364 File newFile = new File(path + File.separator + "views" + File.separator
365 + desc + ".view");
366 if(!view.renameTo(newFile)) {
367 log.warn("Can not rename " + path + "/views/" + id + ".view to " +
368 path + File.separator + "views" + File.separator + desc + ".view");
369 }
370 } catch (Exception e) {
371 return false;
372 }
373 return true;
374 }
375
376 }