This project has retired. For details please refer to its Attic page.
UserBean 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.rest.bean;
20  
21  import java.text.ParseException;
22  
23  import javax.xml.bind.annotation.XmlElement;
24  import javax.xml.bind.annotation.XmlRootElement;
25  import javax.xml.bind.annotation.XmlType;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.json.simple.JSONArray;
30  import org.json.simple.JSONObject;
31  import org.json.simple.JSONValue;
32  
33  import org.apache.hadoop.chukwa.util.ExceptionUtil;
34  
35  @XmlRootElement
36  @XmlType(propOrder={"id", "views", "properties"})
37  public class UserBean {
38    private String id;
39    private JSONArray views;
40    private JSONObject properties;
41    private static Log log = LogFactory.getLog(UserBean.class);
42    
43    public UserBean() {
44      views = new JSONArray();
45      properties = new JSONObject();
46    }
47    
48    public UserBean(JSONObject json) throws ParseException {
49      try {
50        id = (String) json.get("id");
51        views = (JSONArray) json.get("views");
52        if(json.containsKey("properties")) {
53          properties = (JSONObject) json.get("properties");
54        } else {
55          properties = new JSONObject();
56        }
57      } catch (Exception e) {
58        log.error(ExceptionUtil.getStackTrace(e));
59        throw new ParseException("Error parsing user object.",0);
60      }
61    }
62    
63    @XmlElement
64    public String getId() {
65      return id;
66    }
67    
68    @XmlElement
69    public JSONArray getViews() {
70      return views;
71    }
72    
73    @XmlElement
74    public String getProperties() {
75      return properties.toString();
76    }
77  
78    public void setProperties(String buffer) {
79      try {
80        this.properties = (JSONObject) JSONValue.parse(buffer);
81      } catch (Exception e) {
82        log.error(ExceptionUtil.getStackTrace(e));
83      }
84    }
85    
86    public String getPropertyValue(String key) {
87      return (String) this.properties.get(key);
88    }
89    
90    public void setId(String id) {
91      this.id=id;  
92    }
93  
94    public void setViews(JSONArray ja) {
95      this.views=ja;
96    }
97  
98    public void setProperty(String key, String value) throws ParseException {
99      try {
100       this.properties.put(key, value);
101     } catch (Exception e) {
102       log.error(ExceptionUtil.getStackTrace(e));
103       throw new ParseException("Error parsing user object.",0);      
104     }
105   }
106   
107   public JSONObject deserialize() {
108     JSONObject json = new JSONObject();
109     try {
110       json.put("id", this.id);
111       json.put("views", this.views);
112       json.put("properties", this.properties);
113     } catch (Exception e) {
114       log.error(ExceptionUtil.getStackTrace(e));
115     }
116     return json;
117   }
118 }