1
|
package eu.dnetlib;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
|
5
|
import javax.sql.DataSource;
|
6
|
|
7
|
import com.mongodb.MongoClient;
|
8
|
import com.mongodb.MongoClientOptions;
|
9
|
import com.mongodb.ServerAddress;
|
10
|
import eu.dnetlib.OpenaireExporterConfig.Jdbc;
|
11
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreService;
|
12
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
13
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
14
|
import io.prometheus.client.exporter.MetricsServlet;
|
15
|
import io.prometheus.client.hotspot.DefaultExports;
|
16
|
import io.prometheus.client.spring.boot.SpringBootMetricsCollector;
|
17
|
import org.apache.commons.dbcp2.BasicDataSource;
|
18
|
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Value;
|
21
|
import org.springframework.boot.actuate.endpoint.PublicMetrics;
|
22
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
23
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
24
|
import org.springframework.context.annotation.Bean;
|
25
|
import org.springframework.context.annotation.Configuration;
|
26
|
|
27
|
/**
|
28
|
* Created by claudio on 07/07/2017.
|
29
|
*/
|
30
|
@Configuration
|
31
|
public class DNetOpenaireExporterBeanFactory {
|
32
|
|
33
|
@Autowired
|
34
|
private OpenaireExporterConfig config;
|
35
|
|
36
|
@Bean
|
37
|
public ISLookUpService getLookUpService() {
|
38
|
return getServiceStub(ISLookUpService.class, config.getIsLookupUrl());
|
39
|
}
|
40
|
|
41
|
@Bean
|
42
|
public ObjectStoreService getObjectStoreService() {
|
43
|
return getServiceStub(ObjectStoreService.class, config.getObjectStoreServiceUrl());
|
44
|
}
|
45
|
|
46
|
@Bean
|
47
|
public ISRegistryService getRegistryService() {
|
48
|
return getServiceStub(ISRegistryService.class, config.getIsRegistryServiceUrl());
|
49
|
}
|
50
|
|
51
|
@SuppressWarnings("unchecked")
|
52
|
private <T> T getServiceStub(final Class<T> clazz, final String endpoint) {
|
53
|
final JaxWsProxyFactoryBean jaxWsProxyFactory = new JaxWsProxyFactoryBean();
|
54
|
jaxWsProxyFactory.setServiceClass(clazz);
|
55
|
jaxWsProxyFactory.setAddress(endpoint);
|
56
|
return (T) jaxWsProxyFactory.create();
|
57
|
}
|
58
|
|
59
|
@Bean
|
60
|
public DataSource getSqlDataSource() {
|
61
|
final Jdbc jdbc = config.getJdbc();
|
62
|
return getDatasource(
|
63
|
jdbc.getDriverClassName(),
|
64
|
jdbc.getUrl(),
|
65
|
jdbc.getUser(),
|
66
|
jdbc.getPwd(),
|
67
|
jdbc.getMinIdle(),
|
68
|
jdbc.getMaxRows());
|
69
|
}
|
70
|
|
71
|
private BasicDataSource getDatasource(String driverClassName, String jdbcUrl, String jdbcUser, String jdbcPwd, int jdbcMinIdle, int jdbcMaxIdle) {
|
72
|
final BasicDataSource d = new BasicDataSource();
|
73
|
d.setDriverClassName(driverClassName);
|
74
|
d.setUrl(jdbcUrl);
|
75
|
d.setUsername(jdbcUser);
|
76
|
d.setPassword(jdbcPwd);
|
77
|
d.setMinIdle(jdbcMinIdle);
|
78
|
d.setMaxIdle(jdbcMaxIdle);
|
79
|
return d;
|
80
|
}
|
81
|
|
82
|
@Bean
|
83
|
public MongoClient getMongoClient() {
|
84
|
return new MongoClient(
|
85
|
new ServerAddress(config.getDatasource().getMongoHost(), config.getDatasource().getMongoPort()),
|
86
|
MongoClientOptions.builder().connectionsPerHost(config.getDatasource().getMongoConnectionsPerHost()).build());
|
87
|
}
|
88
|
|
89
|
|
90
|
@Bean
|
91
|
@ConditionalOnMissingBean(SpringBootMetricsCollector.class)
|
92
|
SpringBootMetricsCollector springBootMetricsCollector(final Collection<PublicMetrics> publicMetrics) {
|
93
|
|
94
|
final SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
|
95
|
springBootMetricsCollector.register();
|
96
|
|
97
|
return springBootMetricsCollector;
|
98
|
}
|
99
|
|
100
|
@Bean
|
101
|
@ConditionalOnMissingBean(name = "prometheusMetricsServletRegistrationBean")
|
102
|
ServletRegistrationBean prometheusMetricsServletRegistrationBean(@Value("${prometheus.metrics.path:/prometheus}") final String metricsPath) {
|
103
|
DefaultExports.initialize();
|
104
|
return new ServletRegistrationBean(new MetricsServlet(), metricsPath);
|
105
|
}
|
106
|
|
107
|
|
108
|
}
|