This project has retired. For details please refer to its
Attic page.
WidgetStore 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.HashMap;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.json.simple.JSONObject;
28 import org.json.simple.JSONValue;
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.CatalogBean;
33 import org.apache.hadoop.chukwa.rest.bean.WidgetBean;
34 import org.apache.hadoop.chukwa.util.ExceptionUtil;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.fs.FSDataInputStream;
37 import org.apache.hadoop.fs.FSDataOutputStream;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileSystem;
40 import org.apache.hadoop.fs.Path;
41
42 public class WidgetStore {
43 private static Log log = LogFactory.getLog(WidgetStore.class);
44 private static Configuration config = new Configuration();
45 private static ChukwaConfiguration chukwaConf = new ChukwaConfiguration();
46 private static String hiccPath = config.get("fs.default.name")+File.separator+chukwaConf.get("chukwa.data.dir")+File.separator+"hicc"+File.separator+"widgets";
47 private static CatalogBean catalog = null;
48 private static HashMap<String, WidgetBean> list = new HashMap<String, WidgetBean>();
49
50 public WidgetStore() throws IllegalAccessException {
51 WidgetStore.config = HiccWebServer.getConfig();
52 }
53
54 public void set(WidgetBean widget) throws IllegalAccessException {
55 try {
56 StringBuilder widgetPath = new StringBuilder();
57 widgetPath.append(hiccPath);
58 widgetPath.append(File.separator);
59 widgetPath.append(widget.getId());
60 widgetPath.append(".descriptor");
61 Path widgetFile = new Path(widgetPath.toString());
62 FileSystem fs;
63 try {
64 fs = FileSystem.get(config);
65 FSDataOutputStream out = fs.create(widgetFile,true);
66 out.writeBytes(widget.deserialize().toString());
67 out.close();
68 } catch (IOException ex) {
69 log.error(ExceptionUtil.getStackTrace(ex));
70 }
71 cacheWidgets();
72 } catch (Exception e) {
73 log.error(ExceptionUtil.getStackTrace(e));
74 throw new IllegalAccessException("Unable to access user view database.");
75 }
76 }
77
78 public static void cacheWidgets() throws IllegalAccessException {
79 StringBuilder widgetPath = new StringBuilder();
80 widgetPath.append(hiccPath);
81 Path widgetFiles = new Path(widgetPath.toString());
82 FileSystem fs;
83 catalog = new CatalogBean();
84 catalog.setId("root");
85 catalog.setLabel("root");
86 try {
87 fs = FileSystem.get(config);
88 FileStatus[] fstatus = fs.listStatus(widgetFiles);
89 if(fstatus!=null) {
90 for(int i=0;i<fstatus.length;i++) {
91 long size = fstatus[i].getLen();
92 FSDataInputStream widgetStream = fs.open(fstatus[i].getPath());
93 byte[] buffer = new byte[(int)size];
94 widgetStream.readFully(buffer);
95 widgetStream.close();
96 try {
97 JSONObject widgetBuffer = (JSONObject) JSONValue.parse(new String(buffer));
98 WidgetBean widget = new WidgetBean(widgetBuffer);
99 catalog.addCatalog(widget);
100 list.put(widget.getId(),widget);
101 } catch (Exception e) {
102 log.error(ExceptionUtil.getStackTrace(e));
103 }
104 }
105 }
106 } catch (IOException ex) {
107 log.error(ExceptionUtil.getStackTrace(ex));
108 throw new IllegalAccessException("Unable to access user view database.");
109 }
110 }
111
112 public static CatalogBean getCatalog() throws IllegalAccessException {
113 if(catalog==null) {
114 cacheWidgets();
115 }
116 return catalog;
117 }
118
119 public static HashMap<String, WidgetBean> list() throws IllegalAccessException {
120 if(list.size()==0) {
121 cacheWidgets();
122 }
123 return list;
124 }
125 }
126