This project has retired. For details please refer to its
Attic page.
HiccWebServer xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.hicc;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32
33 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
34 import org.apache.hadoop.chukwa.util.DaemonWatcher;
35 import org.apache.hadoop.chukwa.util.ExceptionUtil;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.FSDataOutputStream;
38 import org.apache.hadoop.fs.FileSystem;
39 import org.apache.hadoop.fs.Path;
40 import org.mortbay.jetty.Server;
41 import org.mortbay.xml.XmlConfiguration;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 public class HiccWebServer {
46 private static Log log = LogFactory.getLog(HiccWebServer.class);
47 private static URL serverConf = HiccWebServer.class.getResource("/WEB-INF/jetty.xml");
48 private Server server = null;
49 private String chukwaHdfs;
50 private String hiccData;
51 private static HiccWebServer instance = null;
52 private static final Configuration config = new Configuration();
53 protected static final ChukwaConfiguration chukwaConf = new ChukwaConfiguration();
54
55 protected HiccWebServer() {
56 }
57
58 public static HiccWebServer getInstance() {
59 if(instance==null) {
60 instance = new HiccWebServer();
61 }
62 if(serverConf==null) {
63 log.error("Unable to locate jetty-web.xml.");
64 DaemonWatcher.bailout(-1);
65 }
66 return instance;
67 }
68
69 public void start() {
70 try {
71 chukwaHdfs = config.get("fs.default.name")+File.separator+chukwaConf.get("chukwa.data.dir");
72 hiccData = chukwaHdfs+File.separator+"hicc";
73 DaemonWatcher.createInstance("hicc");
74 setupDefaultData();
75 run();
76 } catch(Exception e) {
77 log.error("HDFS unavailable, check configuration in chukwa-env.sh.");
78 DaemonWatcher.bailout(-1);
79 }
80 }
81
82 public static Configuration getConfig() {
83 return config;
84 }
85
86 public List<String> getResourceListing(String path) throws URISyntaxException, IOException {
87 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
88 URL dirURL = contextClassLoader.getResource(path);
89
90 if (dirURL == null) {
91 dirURL = contextClassLoader.getResource(path);
92 }
93
94 if (dirURL.getProtocol().equals("jar")) {
95
96 String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
97 JarFile jar = new JarFile(jarPath);
98 Enumeration<JarEntry> entries = jar.entries();
99 List<String> result = new ArrayList<String>();
100 while(entries.hasMoreElements()) {
101 String name = entries.nextElement().getName();
102 if (name.startsWith(path)) {
103 String entry = name.substring(path.length());
104 int checkSubdir = entry.indexOf("/");
105 if (checkSubdir == 0 && entry.length()>1) {
106
107 result.add(name);
108 }
109 }
110 }
111 return result;
112 }
113
114 throw new UnsupportedOperationException("Cannot list files for URL "+dirURL);
115 }
116
117 public void populateDir(List<String> files, Path path) {
118 try {
119 FileSystem fs = FileSystem.get(config);
120 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
121 for(String source : files) {
122 String name = source.substring(source.indexOf(File.separator));
123 Path dest = new Path(path.toString()+File.separator+name);
124 InputStream is = contextClassLoader.getResourceAsStream(source);
125 StringBuilder sb = new StringBuilder();
126 String line = null;
127 try {
128 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
129 while ((line = reader.readLine()) != null) {
130 sb.append(line + "\n");
131 }
132 FSDataOutputStream out = fs.create(dest);
133 out.write(sb.toString().getBytes());
134 out.close();
135 reader.close();
136 } catch(IOException e) {
137 log.error("Error writing file: "+dest.toString());
138 }
139 }
140 } catch(IOException e) {
141 log.error("HDFS unavailable, check configuration in chukwa-env.sh.");
142 }
143 }
144
145 public void setupDefaultData() {
146 Path hiccPath = new Path(hiccData);
147 try {
148 FileSystem fs = FileSystem.get(config);
149 if(!fs.exists(hiccPath)) {
150 log.info("Initializing HICC Datastore.");
151
152 if(!fs.exists(new Path(chukwaHdfs))) {
153 fs.mkdirs(new Path(chukwaHdfs));
154 }
155
156
157 fs.mkdirs(hiccPath);
158
159
160 StringBuffer hiccWidgets = new StringBuffer();
161 hiccWidgets.append(hiccData);
162 hiccWidgets.append(File.separator);
163 hiccWidgets.append("widgets");
164 Path widgetsPath = new Path(hiccWidgets.toString());
165 fs.mkdirs(widgetsPath);
166 List<String> widgets = getResourceListing("descriptors");
167 populateDir(widgets, widgetsPath);
168
169
170 StringBuffer hiccViews = new StringBuffer();
171 hiccViews.append(hiccData);
172 hiccViews.append(File.separator);
173 hiccViews.append("views");
174 fs.mkdirs(new Path(hiccViews.toString()));
175
176
177 StringBuffer hiccUsers = new StringBuffer();
178 hiccUsers.append(hiccViews);
179 hiccUsers.append(File.separator);
180 hiccUsers.append("users");
181 fs.mkdirs(new Path(hiccUsers.toString()));
182
183
184 StringBuffer hiccPublic = new StringBuffer();
185 hiccPublic.append(hiccViews);
186 hiccPublic.append(File.separator);
187 hiccPublic.append("public");
188 Path viewsPath = new Path(hiccPublic.toString());
189 fs.mkdirs(viewsPath);
190 List<String> views = getResourceListing("views");
191 populateDir(views, viewsPath);
192 log.info("HICC Datastore initialization completed.");
193 }
194 } catch (IOException ex) {
195 log.error(ExceptionUtil.getStackTrace(ex));
196 } catch (URISyntaxException ex) {
197 log.error(ExceptionUtil.getStackTrace(ex));
198 }
199 }
200
201 public void run() {
202 server = new Server();
203 XmlConfiguration configuration;
204 try {
205 configuration = new XmlConfiguration(serverConf);
206 configuration.configure(server);
207 server.start();
208 } catch (Exception e) {
209 log.error(ExceptionUtil.getStackTrace(e));
210 }
211 }
212
213 public void shutdown() {
214 try {
215 server.stop();
216 DaemonWatcher.bailout(0);
217 } catch (Exception e) {
218 log.error(ExceptionUtil.getStackTrace(e));
219 }
220 }
221
222 public static void main(String[] args) {
223 HiccWebServer hicc = HiccWebServer.getInstance();
224 hicc.start();
225 System.out.close();
226 System.err.close();
227 }
228
229 }