1
|
package eu.dnetlib.services;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.lang.annotation.Annotation;
|
5
|
import java.lang.management.ManagementFactory;
|
6
|
import java.lang.management.OperatingSystemMXBean;
|
7
|
import java.util.HashMap;
|
8
|
import java.util.Map;
|
9
|
|
10
|
import javax.annotation.PostConstruct;
|
11
|
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.context.Lifecycle;
|
16
|
import org.springframework.web.bind.annotation.RequestMapping;
|
17
|
import org.springframework.web.bind.annotation.RestController;
|
18
|
|
19
|
import eu.dnetlib.conf.DnetGenericApplicationProperties;
|
20
|
import eu.dnetlib.enabling.annotations.DnetService;
|
21
|
import eu.dnetlib.enabling.annotations.DnetServiceType;
|
22
|
import eu.dnetlib.exceptions.DnetGenericException;
|
23
|
|
24
|
public abstract class BaseService implements Lifecycle {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(BaseService.class);
|
27
|
|
28
|
private boolean started = false;
|
29
|
|
30
|
private String profileId;
|
31
|
|
32
|
private String baseUrl;
|
33
|
|
34
|
private Map<String, String> serviceProperties = new HashMap<>();
|
35
|
|
36
|
private Map<String, String> extraProtocols = new HashMap<>();
|
37
|
|
38
|
private DnetServiceType serviceType;
|
39
|
|
40
|
@Autowired
|
41
|
private DnetGenericApplicationProperties containerConfiguration;
|
42
|
|
43
|
@PostConstruct
|
44
|
public void init() throws DnetGenericException {
|
45
|
|
46
|
verifyRequiredAnnotations(DnetService.class);
|
47
|
verifyRequiredAnnotations(RestController.class);
|
48
|
verifyRequiredAnnotations(RequestMapping.class);
|
49
|
|
50
|
serviceType = getClass().getAnnotation(DnetService.class).value();
|
51
|
baseUrl = containerConfiguration.getUrl() + getClass().getAnnotation(RequestMapping.class).value()[0];
|
52
|
|
53
|
final File dir = new File(containerConfiguration.getBaseDir());
|
54
|
if (!dir.exists()) {
|
55
|
dir.mkdirs();
|
56
|
}
|
57
|
}
|
58
|
|
59
|
private void verifyRequiredAnnotations(final Class<? extends Annotation> a) throws DnetGenericException {
|
60
|
if (!getClass().isAnnotationPresent(a)) {
|
61
|
final String message = "A required annotation is missing (@" + a.getSimpleName() + ") in class " + getClass();
|
62
|
log.error(message);
|
63
|
throw new DnetGenericException(message);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public void start() {
|
69
|
log.info("Starting service " + serviceType);
|
70
|
if (started) {
|
71
|
log.warn("Service " + this + "already started, check bean initializations!");
|
72
|
}
|
73
|
started = true;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public boolean isRunning() {
|
78
|
log.debug("called isRunning " + this);
|
79
|
return false;
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
public void stop() {
|
84
|
log.info("Stopping service " + this);
|
85
|
}
|
86
|
|
87
|
@RequestMapping("identify")
|
88
|
public ServiceRunningInstance identify() {
|
89
|
final File f = new File(containerConfiguration.getBaseDir());
|
90
|
final Runtime runtime = Runtime.getRuntime();
|
91
|
final OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
|
92
|
|
93
|
final ServiceRunningInstance res = new ServiceRunningInstance();
|
94
|
res.setServiceId(profileId);
|
95
|
res.setBaseUrl(baseUrl);
|
96
|
|
97
|
res.setFreeDiskSpace(bytesToMegaBytes(f.getFreeSpace()));
|
98
|
res.setTotalDiskSpace(bytesToMegaBytes(f.getTotalSpace()));
|
99
|
|
100
|
res.setFreeMemory(bytesToMegaBytes(runtime.freeMemory()));
|
101
|
res.setTotalMemory(bytesToMegaBytes(runtime.totalMemory()));
|
102
|
res.setMaxMemory(bytesToMegaBytes(runtime.maxMemory()));
|
103
|
res.setUsedMemory(bytesToMegaBytes(runtime.totalMemory() - runtime.freeMemory()));
|
104
|
|
105
|
res.setNumberOfProcessors(osBean.getAvailableProcessors());
|
106
|
res.setSysLoadAvg(osBean.getSystemLoadAverage());
|
107
|
|
108
|
return res;
|
109
|
}
|
110
|
|
111
|
public String getProfileId() {
|
112
|
return profileId;
|
113
|
}
|
114
|
|
115
|
public void setProfileId(final String profileId) {
|
116
|
this.profileId = profileId;
|
117
|
}
|
118
|
|
119
|
public Map<String, String> getServiceProperties() {
|
120
|
return serviceProperties;
|
121
|
}
|
122
|
|
123
|
public void setServiceProperties(final Map<String, String> serviceProperties) {
|
124
|
this.serviceProperties = serviceProperties;
|
125
|
}
|
126
|
|
127
|
public Map<String, String> getExtraProtocols() {
|
128
|
return extraProtocols;
|
129
|
}
|
130
|
|
131
|
public void setExtraProtocols(final Map<String, String> extraProtocols) {
|
132
|
this.extraProtocols = extraProtocols;
|
133
|
}
|
134
|
|
135
|
public DnetServiceType getServiceType() {
|
136
|
return serviceType;
|
137
|
}
|
138
|
|
139
|
public void setServiceType(final DnetServiceType serviceType) {
|
140
|
this.serviceType = serviceType;
|
141
|
}
|
142
|
|
143
|
public String getBaseUrl() {
|
144
|
return baseUrl;
|
145
|
}
|
146
|
|
147
|
public void setBaseUrl(final String baseUrl) {
|
148
|
this.baseUrl = baseUrl;
|
149
|
}
|
150
|
|
151
|
private static final long bytesToMegaBytes(final long n) {
|
152
|
return n / 1_048_576;
|
153
|
}
|
154
|
|
155
|
}
|