JmxMonRESTgetData.java
3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.io.IOException;
import javax.management.remote.JMXServiceURL;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import se.lil.jm.JmxMon;
import se.lil.jm.Registry;
/**
* Servlet implementation class JmxMonRESTgetData
*/
@WebServlet(description = "Calls getData on an existing JmxMon object or creates and calls it", urlPatterns = { "/JmxMonREST/getData" })
public class JmxMonRESTgetData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JmxMonRESTgetData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try {
StringBuffer sb = new StringBuffer();
int age = 0;
if(request.getParameterMap().containsKey("age")) {
try {
age = Integer.parseInt(request.getParameter("age"));
} catch (Exception e) {
response.setStatus(400);
response.getWriter().println("{\"error\":true,\"msg\":\"The specified age parameter is not a valid integer number\"}");
return;
}
}
if(request.getParameterMap().containsKey("connectionString")) {
// We have a connection string, find the monitor or create it and call getData() on the monitor
// Check that we have a valid connection string
try {
new JMXServiceURL(request.getParameter("connectionString"));
} catch (Exception e) {
// Error, we need a correct connection string
response.setStatus(400);
response.getWriter().println("{\"error\":true,\"msg\":\"connectionString is not a correct JMX Service URL\"}");
return;
}
// Try to find the monitor in our list
JmxMon monitor = Registry.findOrCreate(request.getParameter("connectionString"));
// Call getData()
boolean didUpdate = monitor.getData(age);
String name = monitor.getServerName();
if(didUpdate) {
sb.append("{\"error\":false,\"msg\":\"Sucessfully collected data on instance '" + name + "'\"");
sb.append(",\"ms\":"+monitor.getLastRTms()+"}");
} else {
response.setStatus(HttpServletResponse.SC_ACCEPTED);
sb.append("{\"error\":false");
sb.append(",\"msg\":\"Data does not need to be updated on instance '" + name + "' either because another thread did concurrently update the monitor and we just waited for it to complete, or because the age (if) specified was higher than the monitors data age.\"");
sb.append(",\"age\":"+monitor.getAgeTs()+"}");
}
} else {
// No input, just return the list of monitors
sb.append("{\"count\":" + Registry.getList().size() + "}");
}
response.getWriter().println(sb.toString());
} catch (Throwable e) {
response.setStatus(500);
Throwable lastCause = e.getCause();
Throwable thisCause = e;
while(lastCause != null) { thisCause = lastCause; lastCause = thisCause.getCause(); }
//response.getWriter().println("{\"error\":true,\"msg\":\""+e.toString().replace("\n"," ").replace("\"","\\\"").trim()+"\"}");
String rst = "{\"error\":true,\"msg\":\"" + e.toString() + ". Caused by: " + thisCause.toString() + "\"}";
rst.replace("\n"," ").replace("\"","\\\"").trim();
response.getWriter().println(rst);
e.printStackTrace();
}
}
}