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