Project

General

Profile

1
package eu.dnetlib.enabling.tools.registration;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5

    
6
import javax.annotation.PostConstruct;
7

    
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Required;
12

    
13
import eu.dnetlib.clients.ISLookUpClient;
14
import eu.dnetlib.conf.ContainerConfiguration;
15
import eu.dnetlib.services.BaseService;
16

    
17
public class ServiceRegistrationManager {
18

    
19
	private static final Log log = LogFactory.getLog(ServiceRegistrationManager.class);
20

    
21
	@Autowired
22
	private ISLookUpClient isLookup;
23

    
24
	private ServiceRegistrator registrator;
25

    
26
	private boolean disabled = false;
27

    
28
	@Autowired(required = false)
29
	private List<BaseService> services = new ArrayList<>();
30

    
31
	@Autowired
32
	private ContainerConfiguration containerConfiguration;
33

    
34
	@PostConstruct
35
	public void registerAllServices() {
36

    
37
		if (disabled) { return; }
38

    
39
		disabled = true;
40

    
41
		services.stream()
42
				.filter(this::filterService)
43
				.forEach(this::registerService);
44
	}
45

    
46
	/**
47
	 * check if we have already a registered service profile for this service.
48
	 *
49
	 * @throws
50
	 */
51
	private boolean filterService(final BaseService service) {
52

    
53
		try {
54

    
55
			final String query = "for $x in //RESOURCE_PROFILE[.//RESOURCE_URI/@value='" + containerConfiguration.getUrl() + "']"
56
					+ " where contains($x//RESOURCE_TYPE/@value/string(), 'Service') return $x//RESOURCE_IDENTIFIER/@value/string()";
57

    
58
			final List<String> profIds = isLookup.find(query);
59

    
60
			if (profIds.isEmpty()) {
61
				log.debug("there is no service registered for service: " + service.getServiceType());
62
				return true;
63
			} else {
64
				service.setProfileId(profIds.get(0));
65
				return false;
66
			}
67
		} catch (final Throwable e) {
68
			log.error("Error filtering endpoint", e);
69
			disabled = false;
70
			return false;
71
		}
72
	}
73

    
74
	private void registerService(final BaseService service) {
75
		try {
76
			final String profId = registrator.registerService(service, containerConfiguration.getUrl());
77
			service.setProfileId(profId);
78
		} catch (final Throwable e) {
79
			log.error("Error registering endpoint", e);
80
			disabled = false;
81
		}
82
	}
83

    
84
	public ServiceRegistrator getRegistrator() {
85
		return registrator;
86
	}
87

    
88
	@Required
89
	public void setRegistrator(final ServiceRegistrator registrator) {
90
		this.registrator = registrator;
91
	}
92

    
93
	public boolean isDisabled() {
94
		return disabled;
95
	}
96

    
97
}
(1-1/2)