This project has retired. For details please refer to its Attic page.
UserStore 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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.json.simple.JSONArray;
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.UserBean;
33  import org.apache.hadoop.chukwa.util.ExceptionUtil;
34  import org.apache.hadoop.conf.Configuration;
35  import org.apache.hadoop.fs.FSDataInputStream;
36  import org.apache.hadoop.fs.FSDataOutputStream;
37  import org.apache.hadoop.fs.FileStatus;
38  import org.apache.hadoop.fs.FileSystem;
39  import org.apache.hadoop.fs.Path;
40  
41  public class UserStore {
42    private String uid = null;
43    private UserBean profile = null;
44    private static Log log = LogFactory.getLog(UserStore.class);
45    private static Configuration config = new Configuration();
46    private static ChukwaConfiguration chukwaConf = new ChukwaConfiguration();
47    private static String hiccPath = config.get("fs.default.name")+File.separator+chukwaConf.get("chukwa.data.dir")+File.separator+"hicc"+File.separator+"users";
48    
49    public UserStore() throws IllegalAccessException {
50      UserStore.config = HiccWebServer.getConfig();
51    }
52  
53    public UserStore(String uid) throws IllegalAccessException {
54      this.uid = uid;
55      init(uid);
56    }
57  
58    public void init(String uid) throws IllegalAccessException {
59      StringBuilder profilePath = new StringBuilder();
60      profilePath.append(hiccPath);
61      profilePath.append(File.separator);
62      profilePath.append(uid);
63      profilePath.append(".profile");
64      Path profileFile = new Path(profilePath.toString());
65      FileSystem fs;
66      try {
67        fs = FileSystem.get(config);
68        if(fs.exists(profileFile)) {
69          FileStatus[] fstatus = fs.listStatus(profileFile);
70          long size = fstatus[0].getLen();
71          FSDataInputStream viewStream = fs.open(profileFile);
72          byte[] buffer = new byte[(int)size];
73          viewStream.readFully(buffer);
74          viewStream.close();
75          try {
76            JSONObject json = (JSONObject) JSONValue.parse(new String(buffer));
77            profile = new UserBean(json);
78          } catch (Exception e) {
79            log.error(ExceptionUtil.getStackTrace(e));
80            throw new IllegalAccessException("Unable to access user profile database.");
81          }
82        } else {
83          profile = new UserBean();
84          profile.setId(uid);
85          JSONArray ja = new JSONArray();
86          profile.setViews(ja);
87          JSONObject json = new JSONObject();
88          profile.setProperties(json.toString());
89        }
90      } catch (IOException ex) {
91        log.error(ExceptionUtil.getStackTrace(ex));
92      }
93    }
94    
95    public UserBean get() throws IllegalAccessException {
96      if(profile==null) {
97        init(uid);
98      }
99      return profile;
100   }
101   
102   public void set(UserBean profile) throws IllegalAccessException {
103     StringBuilder profilePath = new StringBuilder();
104     profilePath.append(hiccPath);
105     profilePath.append(File.separator);
106     profilePath.append(profile.getId());
107     profilePath.append(".profile");
108     Path profileFile = new Path(profilePath.toString());
109     FileSystem fs;
110     try {
111       fs = FileSystem.get(config);
112       FSDataOutputStream out = fs.create(profileFile,true);
113       out.write(profile.deserialize().toString().getBytes());
114       out.close();
115     } catch (IOException ex) {
116       log.error(ExceptionUtil.getStackTrace(ex));
117       throw new IllegalAccessException("Unable to access user profile database.");
118     }
119     this.profile = profile;
120   }
121   
122   public static JSONArray list() throws IllegalAccessException {
123     StringBuilder profilePath = new StringBuilder();
124     profilePath.append(hiccPath);
125     profilePath.append(File.separator);
126     profilePath.append("*.profile");
127     Path viewFile = new Path(profilePath.toString());
128     FileSystem fs;
129     JSONArray list = new JSONArray();
130     try {
131       fs = FileSystem.get(config);
132       FileStatus[] fstatus = fs.listStatus(viewFile);
133       if(fstatus!=null) {
134         for(int i=0;i<fstatus.length;i++) {
135           long size = fstatus[i].getLen();
136           FSDataInputStream profileStream = fs.open(fstatus[i].getPath());
137           byte[] buffer = new byte[(int)size];
138           profileStream.readFully(buffer);
139           profileStream.close();
140           try {
141             UserBean user = new UserBean((JSONObject) JSONValue.parse(new String(buffer)));
142             list.add(user.getId());
143           } catch (Exception e) {
144             log.error(ExceptionUtil.getStackTrace(e));
145           }
146         }
147       }
148     } catch (IOException ex) {
149       log.error(ExceptionUtil.getStackTrace(ex));
150       throw new IllegalAccessException("Unable to access user profile database."); 
151     }
152     return list;    
153   }
154 }
155