This project has retired. For details please refer to its Attic page.
WidgetBean 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  import java.util.Collection;
23  
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.json.simple.JSONArray;
31  import org.json.simple.JSONObject;
32  
33  import org.apache.hadoop.chukwa.datastore.WidgetStore;
34  import org.apache.hadoop.chukwa.util.ExceptionUtil;
35  
36  @XmlRootElement
37  @XmlType(propOrder={"id", "title", "version", "categories", "url", "description","refresh","parameters"})
38  public class WidgetBean {
39    private String id;
40    private String title;
41    private String version;
42    private String categories;
43    private String url;
44    private String description;
45    private int refresh;
46    private ParametersBean[] parameters;
47    private static Log log = LogFactory.getLog(WidgetBean.class);
48  
49    public WidgetBean() {
50      
51    }
52    
53    public WidgetBean(JSONObject json) throws ParseException {
54      try {
55        this.id=(String) json.get("id");
56        this.title=(String) json.get("title");
57        this.version=(String) json.get("version");
58        this.categories=(String) json.get("categories");
59        this.url=(String) json.get("url");
60        this.description=(String) json.get("description");
61        if(json.get("refresh").getClass().equals("String")) {
62          int refresh = Integer.parseInt((String) json.get("refresh"));
63          this.refresh = refresh;
64        } else if(json.get("refresh").getClass().equals("Long")) {
65          this.refresh = ((Long) json.get("refresh")).intValue();
66        }
67        try {
68          int size = ((JSONArray) json.get("parameters")).size();
69          ParametersBean[] list = new ParametersBean[size];
70          for(int i=0;i<size;i++) {
71            JSONArray jsonArray = (JSONArray) json.get("parameters");
72            list[i] = new ParametersBean((JSONObject) jsonArray.get(i));
73          }
74          this.parameters=list;
75        } catch (Exception e) {
76          this.parameters=null;
77        }
78      } catch (Exception e) {
79        log.error(ExceptionUtil.getStackTrace(e));
80        throw new ParseException(ExceptionUtil.getStackTrace(e), 0);
81      }
82    }
83    
84    @XmlElement
85    public String getId() {
86      return id;
87    }
88  
89    @XmlElement
90    public String getTitle() {
91      return title;
92    }
93    
94    @XmlElement
95    public String getVersion() {
96      return version;
97    }
98    
99    @XmlElement
100   public String getCategories() {
101     return categories;
102   }
103   
104   @XmlElement
105   public String getUrl() {
106     return url;
107   }
108   
109   @XmlElement
110   public String getDescription() {
111     return description;
112   }
113   
114   @XmlElement
115   public int getRefresh() {
116     return refresh;
117   }
118   
119   @XmlElement
120   public ParametersBean[] getParameters() {
121     return parameters.clone();
122   }
123   
124   public void setId(String id) {
125     this.id=id;
126   }
127   
128   public void setUrl(String url) {
129     this.url=url;
130   }
131   
132   public void setTitle(String title) {
133     this.title=title;
134   }
135   
136   public void setDescription(String description) {
137     this.description=description;
138   }
139   
140   public void setVersion(String version) {
141     this.version=version;
142   }
143   
144   public void setCategories(String categories) {
145     this.categories=categories;
146   }
147   
148   public void setRefresh(int refresh) {
149     this.refresh=refresh;
150   }
151   
152   public void setParameters(ParametersBean[] p) {
153     this.parameters=p.clone();
154   }
155   
156   public void update() {
157     try {
158       WidgetBean widget = WidgetStore.list().get(this.id);
159       if(widget!=null) {
160         if(widget.getVersion().intern()!=this.version.intern()) {
161           this.categories=widget.getCategories();
162           this.title=widget.getTitle();
163           this.version=widget.getVersion();
164           this.url=widget.getUrl();
165           this.description=widget.getDescription();
166           ParametersBean[] plist = widget.getParameters();
167           for(int i=0;i<this.parameters.length;i++) {
168             Collection<String> value = this.parameters[i].getValue();
169             for(int j=0;j<plist.length;j++) {
170               if(plist[i].getName().intern()==this.parameters[j].getName().intern()) {
171                 plist[j].setValue(value);
172               }
173             }
174           }
175           this.parameters=plist;
176         }
177       } else {
178         log.info("Widget "+this.id+" is deprecated.");
179       }
180     } catch (IllegalAccessException e) {
181       log.error("Unable to update widget: "+this.id+" "+ExceptionUtil.getStackTrace(e));
182     }
183     
184   }
185   
186   public JSONObject deserialize() {
187     JSONObject json = new JSONObject();
188     try {
189       json.put("id", this.id);
190       json.put("title", this.title);
191       json.put("description", this.description);
192       json.put("version", this.version);
193       json.put("categories", this.categories);
194       json.put("refresh", this.refresh);
195       json.put("url", this.url);
196       JSONArray ja = new JSONArray();
197       if(this.parameters!=null) {
198         for(int i=0;i<this.parameters.length;i++) {
199           ja.add(this.parameters[i].deserialize());
200         }
201       }
202       json.put("parameters", (JSONArray) ja);
203     } catch (Exception e) {
204       log.error(ExceptionUtil.getStackTrace(e));
205     }
206     return json;    
207   }
208 }