1
|
package eu.dnetlib;
|
2
|
|
3
|
import javax.sql.DataSource;
|
4
|
|
5
|
import com.mongodb.MongoClient;
|
6
|
import com.mongodb.MongoClientOptions;
|
7
|
import com.mongodb.ServerAddress;
|
8
|
import eu.dnetlib.OpenaireExporterConfig.Jdbc;
|
9
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreService;
|
10
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
11
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
12
|
import org.apache.commons.dbcp2.BasicDataSource;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.context.annotation.Bean;
|
18
|
import org.springframework.context.annotation.Configuration;
|
19
|
|
20
|
/**
|
21
|
* Created by claudio on 07/07/2017.
|
22
|
*/
|
23
|
@Configuration
|
24
|
public class DNetOpenaireExporterBeanFactory {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(DNetOpenaireExporterBeanFactory.class);
|
27
|
|
28
|
@Autowired
|
29
|
private OpenaireExporterConfig config;
|
30
|
|
31
|
@Bean
|
32
|
public ISLookUpService getLookUpService() {
|
33
|
return getServiceStub(ISLookUpService.class, config.getIsLookupUrl());
|
34
|
}
|
35
|
|
36
|
@Bean
|
37
|
public ObjectStoreService getObjectStoreService() {
|
38
|
return getServiceStub(ObjectStoreService.class, config.getObjectStoreServiceUrl());
|
39
|
}
|
40
|
|
41
|
@Bean
|
42
|
public ISRegistryService getRegistryService() {
|
43
|
return getServiceStub(ISRegistryService.class, config.getIsRegistryServiceUrl());
|
44
|
}
|
45
|
|
46
|
@SuppressWarnings("unchecked")
|
47
|
private <T> T getServiceStub(final Class<T> clazz, final String endpoint) {
|
48
|
log.info(String.format("Initializing service stub %s, endpoint %s", clazz.toString(),endpoint));
|
49
|
final JaxWsProxyFactoryBean jaxWsProxyFactory = new JaxWsProxyFactoryBean();
|
50
|
jaxWsProxyFactory.setServiceClass(clazz);
|
51
|
jaxWsProxyFactory.setAddress(endpoint);
|
52
|
return (T) jaxWsProxyFactory.create();
|
53
|
}
|
54
|
|
55
|
@Bean
|
56
|
public DataSource getSqlDataSource() {
|
57
|
final Jdbc jdbc = config.getJdbc();
|
58
|
return getDatasource(
|
59
|
jdbc.getDriverClassName(),
|
60
|
jdbc.getUrl(),
|
61
|
jdbc.getUser(),
|
62
|
jdbc.getPwd(),
|
63
|
jdbc.getMinIdle(),
|
64
|
jdbc.getMaxRows());
|
65
|
}
|
66
|
|
67
|
private BasicDataSource getDatasource(String driverClassName, String jdbcUrl, String jdbcUser, String jdbcPwd, int jdbcMinIdle, int jdbcMaxIdle) {
|
68
|
final BasicDataSource d = new BasicDataSource();
|
69
|
d.setDriverClassName(driverClassName);
|
70
|
d.setUrl(jdbcUrl);
|
71
|
d.setUsername(jdbcUser);
|
72
|
d.setPassword(jdbcPwd);
|
73
|
d.setMinIdle(jdbcMinIdle);
|
74
|
d.setMaxIdle(jdbcMaxIdle);
|
75
|
return d;
|
76
|
}
|
77
|
|
78
|
@Bean
|
79
|
public MongoClient getMongoClient() {
|
80
|
return new MongoClient(
|
81
|
new ServerAddress(config.getDatasource().getMongoHost(), config.getDatasource().getMongoPort()),
|
82
|
MongoClientOptions.builder().connectionsPerHost(config.getDatasource().getMongoConnectionsPerHost()).build());
|
83
|
}
|
84
|
|
85
|
}
|