1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 }