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.hicc;
20  
21  
22  import java.io.*;
23  import java.util.*;
24  import org.json.*;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.chukwa.util.ExceptionUtil;
28  
29  public class Views {
30    public JSONArray viewsData;
31    private String path = System.getenv("CHUKWA_DATA_DIR")
32        + "/views/workspace_view_list.cache";
33    private static final Log log = LogFactory.getLog(Views.class);
34  
35    static public String getContents(File aFile) {
36      // ...checks on aFile are elided
37      StringBuffer contents = new StringBuffer();
38  
39      try {
40        // use buffering, reading one line at a time
41        // FileReader always assumes default encoding is OK!
42        BufferedReader input = new BufferedReader(new FileReader(aFile));
43        try {
44          String line = null; // not declared within while loop
45          /*
46           * readLine is a bit quirky : it returns the content of a line MINUS the
47           * newline. it returns null only for the END of the stream. it returns
48           * an empty String if two newlines appear in a row.
49           */
50          while ((line = input.readLine()) != null) {
51            contents.append(line);
52            contents.append(System.getProperty("line.separator"));
53          }
54        } finally {
55          input.close();
56        }
57      } catch (IOException ex) {
58        ex.printStackTrace();
59      }
60  
61      return contents.toString();
62    }
63  
64    public Views() {
65      File aFile = new File(path);
66      String buffer = getContents(aFile);
67      try {
68        viewsData = new JSONArray(buffer);
69      } catch (JSONException e) {
70        log.debug(ExceptionUtil.getStackTrace(e));
71      }
72    }
73  
74    public String getOwner(int i) {
75      String owner = null;
76      try {
77        owner = ((JSONObject) viewsData.get(i)).get("owner")
78            .toString();
79      } catch (JSONException e) {
80        log.debug(ExceptionUtil.getStackTrace(e));
81      }
82      return owner;
83    }
84  
85    public Iterator getPermission(int i) {
86      Iterator permission = null;
87      try {
88        permission = ((JSONObject) ((JSONObject) viewsData.get(i))
89            .get("permission")).keys();
90      } catch (JSONException e) {
91        log.debug(ExceptionUtil.getStackTrace(e));
92      }
93      return permission;
94    }
95  
96    public String getReadPermission(int i, String who) {
97      String read = null;
98      try {
99        JSONObject view = (JSONObject) viewsData.get(i);
100       JSONObject permission = (JSONObject) view.get("permission");
101       JSONObject user = (JSONObject) permission.get(who);
102       read = user.get("read").toString();
103     } catch (JSONException e) {
104       log.debug(ExceptionUtil.getStackTrace(e));
105     }
106     return read;
107   }
108 
109   public String getWritePermission(int i, String who) {
110     String write = null;
111     try {
112       write = ((JSONObject) ((JSONObject) ((JSONObject) viewsData.get(i)).get("permission")).get(who)).get("write").toString();
113     } catch (JSONException e) {
114       log.debug(ExceptionUtil.getStackTrace(e));
115     }
116     return write;
117   }
118 
119   public String getDescription(int i) {
120     String description = null;
121     try {
122       description = ((JSONObject) viewsData.get(i)).get(
123           "description").toString();
124     } catch (JSONException e) {
125       log.debug(ExceptionUtil.getStackTrace(e));
126     }
127     return description;
128   }
129 
130   public String getKey(int i) {
131     String key = null;
132     try {
133       key = ((JSONObject) viewsData.get(i)).get("key").toString();
134     } catch (JSONException e) {
135       log.debug(ExceptionUtil.getStackTrace(e));
136     }
137     return key;
138   }
139 
140   public int length() {
141     return viewsData.length();
142   }
143 }