-
Notifications
You must be signed in to change notification settings - Fork 161
/
DashboardRendererServiceImpl.java
71 lines (60 loc) · 2.29 KB
/
DashboardRendererServiceImpl.java
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
package org.jbpm.dashboard.renderer.service.impl;
import org.apache.commons.lang.StringUtils;
import org.jboss.errai.bus.server.annotations.Service;
import org.jbpm.dashboard.renderer.service.DashboardRendererService;
import org.jbpm.dashboard.renderer.service.ConnectionStatus;
import javax.enterprise.context.ApplicationScoped;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
@Service
@ApplicationScoped
public class DashboardRendererServiceImpl implements DashboardRendererService {
@Override
public ConnectionStatus getAppStatus(String theUrl) {
ConnectionStatus connectionStatus = new ConnectionStatus();
// Get a list of the urls to check for the given url
List<String> urls = explodeUrl(theUrl);
Exception exc = null;
for (String anUrl : urls) {
try {
// Check whether the service is available
int status = pingUrl(anUrl);
connectionStatus.setStatus(status);
return connectionStatus;
} catch (Exception e){
exc = e;
}
}
if (exc != null) exc.printStackTrace();
connectionStatus.setStatus(-1);
return connectionStatus;
}
protected int pingUrl(String anUrl) throws Exception {
URL url = new URL(anUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
return urlConnection.getResponseCode();
}
protected List<String> explodeUrl(String anUrl) {
List<String> results = new ArrayList<String>();
// Add the target URL
results.add(anUrl);
try {
// Get the dashbuilder address (defaults to localhost).
String bindAddress = System.getProperty("dashbuilder.bind.address");
if (StringUtils.isBlank(bindAddress)) {
bindAddress = InetAddress.getLocalHost().getHostAddress();
}
// Add the bind address
String host = new URL(anUrl).getHost();
if (!host.equals(bindAddress)) {
results.add(anUrl.replace(host, bindAddress));
}
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
}