1
|
package eu.dnetlib.enabling.is;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import java.util.UUID;
|
6
|
|
7
|
import javax.annotation.PostConstruct;
|
8
|
import javax.annotation.Resource;
|
9
|
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.dom4j.Document;
|
13
|
import org.dom4j.Element;
|
14
|
import org.dom4j.io.SAXReader;
|
15
|
|
16
|
import eu.dnetlib.enabling.is.dao.DnetServiceDao;
|
17
|
import eu.dnetlib.enabling.is.jdbc.DatabaseUtils;
|
18
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
19
|
import eu.dnetlib.enabling.is.rmi.DnetService;
|
20
|
import eu.dnetlib.enabling.is.rmi.InformationService;
|
21
|
import eu.dnetlib.enabling.is.rmi.InformationServiceException;
|
22
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
23
|
import eu.dnetlib.enabling.tools.AbstractBaseService;
|
24
|
|
25
|
public class InformationServiceImpl extends AbstractBaseService implements InformationService {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(InformationServiceImpl.class);
|
28
|
|
29
|
@Resource
|
30
|
private DnetServiceDao dnetServiceDao;
|
31
|
|
32
|
@Resource
|
33
|
private DatabaseUtils dbUtils;
|
34
|
|
35
|
@Resource
|
36
|
private UniqueServiceLocator serviceLocator;
|
37
|
|
38
|
@PostConstruct
|
39
|
public void init() throws Exception {
|
40
|
log.info("Registering services...");
|
41
|
|
42
|
final SAXReader reader = new SAXReader();
|
43
|
|
44
|
final ISLookUpService lookup = serviceLocator.getService(ISLookUpService.class);
|
45
|
|
46
|
final List<String> list = lookup.quickSearchProfile("for $x in collection('/db/DRIVER/ServiceResources') return $x");
|
47
|
for (String s : list) {
|
48
|
final Document doc = reader.read(new StringReader(s));
|
49
|
final DnetService service = new DnetService();
|
50
|
service.setName(doc.valueOf("//RESOURCE_TYPE/@value"));
|
51
|
|
52
|
for (Object o : doc.selectNodes("//SERVICE_PROPERTIES/PROPERTY")) {
|
53
|
final Element n = (Element) o;
|
54
|
service.getProperties().put(n.valueOf("@key"), n.valueOf("@value"));
|
55
|
}
|
56
|
for (Object o : doc.selectNodes("//PROTOCOLS/PROTOCOL")) {
|
57
|
final Element n = (Element) o;
|
58
|
service.getProtocols().put(n.valueOf("@name"), n.valueOf("@address"));
|
59
|
}
|
60
|
|
61
|
registerService(service);
|
62
|
}
|
63
|
log.info("Done, total: " + list.size());
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public List<DnetService> listServices() throws InformationServiceException {
|
68
|
return dnetServiceDao.list();
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public String registerService(final DnetService service) throws InformationServiceException {
|
73
|
final String id = "service:" + UUID.randomUUID();
|
74
|
service.setId(id);
|
75
|
|
76
|
dnetServiceDao.save(service);
|
77
|
|
78
|
log.info("Service " + service + " saved");
|
79
|
|
80
|
return id;
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
public List<String> search(final String sql) throws InformationServiceException {
|
85
|
return dbUtils.search(sql);
|
86
|
}
|
87
|
}
|