This project has retired. For details please refer to its Attic page.
HiccWebServer xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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        /* A JAR path */
96        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
97        JarFile jar = new JarFile(jarPath);
98        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
99        List<String> result = new ArrayList<String>(); //avoid duplicates in case it is a subdirectory
100       while(entries.hasMoreElements()) {
101         String name = entries.nextElement().getName();
102         if (name.startsWith(path)) { //filter according to the path
103           String entry = name.substring(path.length());
104           int checkSubdir = entry.indexOf("/");
105           if (checkSubdir == 0 && entry.length()>1) {
106             // if it is a subdirectory, we just return the directory name
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         // Create chukwa directory
152         if(!fs.exists(new Path(chukwaHdfs))) {
153           fs.mkdirs(new Path(chukwaHdfs));
154         }
155         
156         // Create hicc directory        
157         fs.mkdirs(hiccPath);
158         
159         // Populate widgets repository
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         // Create views directory
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         // Create users repository
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         // Populate public repository
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 }