Project

General

Profile

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

    
3
import java.util.List;
4
import javax.xml.ws.Endpoint;
5

    
6
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
7
import eu.dnetlib.enabling.tools.AbstractBaseService;
8
import eu.dnetlib.rmi.common.BaseService;
9
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
10
import eu.dnetlib.rmi.enabling.ISLookUpService;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
public class ServiceRegistrationManager {
17

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

    
20
	@Autowired
21
	private UniqueServiceLocator serviceLocator;
22

    
23
	private ServiceRegistrator registrator;
24

    
25
	private boolean disabled = false;
26

    
27
	@Autowired
28
	private List<Endpoint> services;
29

    
30
	public void registerAllServices() {
31

    
32
		if (this.disabled) { return; }
33

    
34
		this.disabled = true;
35

    
36
		getServices()
37
				.stream()
38
				.filter(this::filterEndpoint)
39
				.forEach(this::registerEndpoint);
40
	}
41

    
42
	/**
43
	 * check if we have already a registered service profile for this service.
44
	 *
45
	 * @throws
46
	 */
47
	private boolean filterEndpoint(final Endpoint endpoint) {
48
		if (!(endpoint.getImplementor() instanceof BaseService)) { return false; }
49

    
50
		final String uri = this.registrator.getEprBuilder().getAddress(endpoint) + "?wsdl";
51

    
52
		try {
53
			final String query = "for $x in //RESOURCE_PROFILE[.//RESOURCE_URI/@value='" + uri + "']"
54
					+ " where contains($x//RESOURCE_TYPE/@value/string(), 'Service') return $x//RESOURCE_IDENTIFIER/@value/string()";
55

    
56
			final String profId = this.serviceLocator.getService(ISLookUpService.class, true).getResourceProfileByQuery(query);
57

    
58
			((AbstractBaseService) endpoint.getImplementor()).setProfileId(profId);
59

    
60
			return false;
61
		} catch (final ISLookUpDocumentNotFoundException e) {
62
			log.debug("there is no service registered for uri: " + uri);
63
			return true;
64
		} catch (final Throwable e) {
65
			log.error("Error filtering endpoint", e);
66
			this.disabled = false;
67
			return false;
68
		}
69
	}
70

    
71
	private void registerEndpoint(final Endpoint endpoint) {
72
		try {
73
			final AbstractBaseService service = (AbstractBaseService) endpoint.getImplementor();
74
			final String profId = this.registrator.registerService(service, endpoint);
75
			service.setProfileId(profId);
76
		} catch (final Throwable e) {
77
			log.error("Error registering endpoint", e);
78
			this.disabled = false;
79
		}
80
	}
81

    
82
	public ServiceRegistrator getRegistrator() {
83
		return this.registrator;
84
	}
85

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

    
91
	public boolean isDisabled() {
92
		return this.disabled;
93
	}
94

    
95
	public UniqueServiceLocator getServiceLocator() {
96
		return this.serviceLocator;
97
	}
98

    
99
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
100
		this.serviceLocator = serviceLocator;
101
	}
102

    
103
	public List<Endpoint> getServices() {
104
		return this.services;
105
	}
106

    
107
	public void setServices(final List<Endpoint> services) {
108
		this.services = services;
109
	}
110

    
111
}
(1-1/2)