This project has retired. For details please refer to its Attic page.
ViewStore 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  
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.LinkedHashMap;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
30  import org.apache.hadoop.chukwa.hicc.HiccWebServer;
31  import org.apache.hadoop.chukwa.rest.bean.ViewBean;
32  import org.apache.hadoop.chukwa.util.ExceptionUtil;
33  import org.apache.hadoop.conf.Configuration;
34  import org.apache.hadoop.fs.FSDataInputStream;
35  import org.apache.hadoop.fs.FSDataOutputStream;
36  import org.apache.hadoop.fs.FileStatus;
37  import org.apache.hadoop.fs.FileSystem;
38  import org.apache.hadoop.fs.Path;
39  import org.json.simple.JSONArray;
40  
41  public class ViewStore {
42    private String vid = null;
43    private String uid = null;
44    private ViewBean view = null;
45    private static Log log = LogFactory.getLog(ViewStore.class);
46    private static Configuration config = null;
47    private static ChukwaConfiguration chukwaConf = new ChukwaConfiguration();
48    private static String viewPath = null;
49    private static String publicViewPath = viewPath+File.separator+"public";
50    private static String usersViewPath = viewPath+File.separator+"users";
51    private static String PUBLIC = "public".intern();
52  
53    static {
54      config = HiccWebServer.getConfig();
55      viewPath = config.get("fs.defaultFS")+File.separator+chukwaConf.get("chukwa.data.dir")+File.separator+"hicc"+File.separator+"views";
56    }
57    public ViewStore() throws IllegalAccessException {
58    }
59  
60    public ViewStore(String uid, String vid) throws IllegalAccessException {
61      this.uid=uid;
62      this.vid=vid;
63      load(uid, vid);
64    }
65  
66    public void load(String uid, String vid) throws IllegalAccessException {
67      StringBuilder vp = new StringBuilder();
68      vp.append(usersViewPath);
69      vp.append(File.separator);
70      vp.append(uid);
71      vp.append(File.separator);
72      vp.append(vid);
73      vp.append(".view");
74      Path viewFile = new Path(vp.toString());
75      try {
76        FileSystem fs = FileSystem.get(config);
77        if(!fs.exists(viewFile)) {
78          StringBuilder pubPath = new StringBuilder();
79          pubPath.append(publicViewPath);
80          pubPath.append(File.separator);
81          pubPath.append(vid);
82          pubPath.append(".view");
83          viewFile = new Path(pubPath.toString());        
84        }
85        if(fs.exists(viewFile)) {
86          FileStatus[] fstatus = fs.listStatus(viewFile);
87          long size = fstatus[0].getLen();
88          FSDataInputStream viewStream = fs.open(viewFile);
89          byte[] buffer = new byte[(int)size];
90          viewStream.readFully(buffer);
91          viewStream.close();
92          try {
93            view = new ViewBean(buffer);
94            view.update();
95          } catch (Exception e) {
96            log.error(ExceptionUtil.getStackTrace(e));
97            throw new IllegalAccessException("Unable to access view: "+vid);
98          }
99        }
100     } catch (IOException ex) {
101       log.error(ExceptionUtil.getStackTrace(ex));
102     }
103   }
104   
105   public String getMessage() {
106     return view.toString();  
107   }
108   
109   public ViewBean get() throws IllegalAccessException {
110     if(view==null) {
111       load(uid, vid);
112     }
113     if(view==null) {
114       // Display global default view if user default view does not exist.
115       try {
116         load(null, vid);
117       } catch(Exception e) {
118         log.error(ExceptionUtil.getStackTrace(e));
119         view = null;
120       }
121     }
122     return view;
123   }
124   
125   public void set(ViewBean view) throws IllegalAccessException {
126     try {
127       if (this.view == null || (this.view.getOwner().intern() == view.getOwner().intern())) {
128         if(this.view!=null) {
129           delete();
130         }
131         StringBuilder viewPath = new StringBuilder();
132         if(view.getPermissionType().intern()==PUBLIC) {
133           viewPath.append(publicViewPath);
134         } else {
135           viewPath.append(usersViewPath);
136           viewPath.append(File.separator);
137           viewPath.append(uid);
138         }
139         viewPath.append(File.separator);
140         viewPath.append(view.getName());
141         viewPath.append(".view");
142         Path viewFile = new Path(viewPath.toString());
143         try {
144           FileSystem fs = FileSystem.get(config);
145           FSDataOutputStream out = fs.create(viewFile,true);
146           out.write(view.deserialize().toString().getBytes(Charset.forName("UTF-8")));
147           out.close();
148         } catch (IOException ex) {
149           log.error(ExceptionUtil.getStackTrace(ex));
150         }
151         this.view = view;
152       } else {
153         if(view.getPermissionType().intern()==PUBLIC) {
154           throw new IllegalAccessException("Unable to save public view, duplicated view exists.");
155         } else {
156           throw new IllegalAccessException("Unable to save user view.");          
157         }
158       }
159     } catch (Exception e) {
160       log.error(ExceptionUtil.getStackTrace(e));
161       throw new IllegalAccessException("Unable to access user view.");
162     }
163   }
164 
165   public void delete() throws IllegalAccessException {
166     try {
167       if(this.view==null) {
168         get();
169       }
170       if (this.view!=null) {
171         StringBuilder viewPath = new StringBuilder();
172         if(view.getPermissionType().intern()==PUBLIC) {
173           viewPath.append(publicViewPath);
174         } else {
175           viewPath.append(usersViewPath);
176           viewPath.append(File.separator);
177           viewPath.append(uid);
178         }
179         viewPath.append(File.separator);
180         viewPath.append(view.getName());
181         viewPath.append(".view");
182         Path viewFile = new Path(viewPath.toString());
183         try {
184           FileSystem fs = FileSystem.get(config);
185           fs.delete(viewFile, true);
186         } catch (IOException ex) {
187           log.error(ExceptionUtil.getStackTrace(ex));
188         }
189       } else {
190         throw new IllegalAccessException("Unable to delete user view, view does not exist.");          
191       }
192     } catch (Exception e) {
193       log.error(ExceptionUtil.getStackTrace(e));
194       throw new IllegalAccessException("Unable to access user view.");
195     }
196   }
197   
198   public static JSONArray list(String uid) throws IllegalAccessException {
199     StringBuilder viewPath = new StringBuilder();
200     viewPath.append(usersViewPath);
201     viewPath.append(File.separator);
202     viewPath.append(uid);
203     String[] pathList = new String[2];
204     pathList[0]=viewPath.toString();
205     pathList[1]=publicViewPath;
206     JSONArray list = new JSONArray();
207     for(String path : pathList) {
208       Path viewFile = new Path(path);
209       try {
210         FileSystem fs = FileSystem.get(config);
211         FileStatus[] fstatus = fs.listStatus(viewFile);
212         if(fstatus!=null) {
213           for(int i=0;i<fstatus.length;i++) {
214             if(!fstatus[i].getPath().getName().endsWith(".view")) {
215               continue;
216             }
217             long size = fstatus[i].getLen();
218             FSDataInputStream viewStream = fs.open(fstatus[i].getPath());
219             byte[] buffer = new byte[(int)size];
220             viewStream.readFully(buffer);
221             viewStream.close();
222             try {
223               ViewBean view = new ViewBean(buffer);
224               Map<String, String> json=new LinkedHashMap<String, String>();
225               json.put("name", view.getName());
226               json.put("type", view.getPermissionType());
227               json.put("owner", view.getOwner());
228               if(uid.intern()==view.getOwner().intern()) {
229                 json.put("editable","true");
230               } else {
231                 json.put("editable","false");
232               }
233               list.add(json);
234             } catch (Exception e) {
235               log.error(ExceptionUtil.getStackTrace(e));
236             }
237           }
238         }
239       } catch (IOException ex) {
240         log.error(ExceptionUtil.getStackTrace(ex));
241         throw new IllegalAccessException("Unable to access user view."); 
242       }
243     }
244     return list;
245   }
246 }
247