View Javadoc

1   /*
2    * #%L
3    * Bitrepository Monitoring Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.monitoringservice.webservice;
23  
24  import java.util.Map;
25  
26  import javax.ws.rs.GET;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.Produces;
29  import javax.xml.datatype.XMLGregorianCalendar;
30  
31  import org.bitrepository.common.utils.TimeUtils;
32  import org.bitrepository.monitoringservice.MonitoringService;
33  import org.bitrepository.monitoringservice.MonitoringServiceFactory;
34  import org.bitrepository.monitoringservice.status.ComponentStatus;
35  import org.json.JSONArray;
36  import org.json.JSONException;
37  import org.json.JSONObject;
38  
39  @Path("/MonitoringService")
40  public class RestMonitoringService {
41      
42      private MonitoringService service;
43      
44      public RestMonitoringService() {
45          service = MonitoringServiceFactory.getMonitoringService();
46      }
47      
48      @GET
49      @Path("/getMonitoringConfiguration/")
50      @Produces("application/json")
51      public String getMonitoringServiceConfiguration() {
52          JSONArray array = new JSONArray();
53          
54          array.put(makeConfigurationEntry("Check interval", TimeUtils.millisecondsToHuman(service.getCollectionInterval())));
55          array.put(makeConfigurationEntry("Max retries", Long.toString(service.getMaxRetries())));
56          
57          return array.toString();
58      }
59      
60      @GET
61      @Path("/getComponentStatus/")
62      @Produces("application/json")
63      public String getComponentStatus() {
64          Map<String, ComponentStatus> statusMap = service.getStatus();
65          JSONArray array = new JSONArray();
66          
67          for(String component : statusMap.keySet()) {
68              array.put(makeJSONStatusObject(component, statusMap.get(component)));
69          }
70          return array.toString();
71      }
72      
73      private JSONObject makeJSONStatusObject(String componentID, ComponentStatus status) {
74          JSONObject obj = new JSONObject();
75          try {
76              obj.put("componentID", componentID);
77              obj.put("status", status.getStatus());
78              obj.put("info", status.getInfo());
79              XMLGregorianCalendar cal = status.getLastReply();
80              if(cal != null) {
81                  obj.put("timeStamp", TimeUtils.shortDate(cal));
82              } else {
83                  obj.put("timeStamp", "N/A");    
84              }
85              
86              return obj;
87          } catch (JSONException e) {
88              return (JSONObject) JSONObject.NULL;
89          }
90      }
91      
92      private JSONObject makeConfigurationEntry(String option, String value) {
93          JSONObject obj = new JSONObject();
94          try {
95              obj.put("confOption", option);            
96              obj.put("confValue", value);
97              return obj;
98          } catch (JSONException e) {
99              return (JSONObject) JSONObject.NULL;
100         }
101     }
102 }