Project

General

Profile

1
package eu.dnetlib.clients.utils.ws;
2

    
3
//import deltix.qsrv.comm.xml.ThrowableTransientAnnotationReaderBuilder;
4

    
5
import eu.dnetlib.api.DriverService;
6
import eu.dnetlib.api.enabling.A2Service;
7
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
8
import eu.dnetlib.enabling.tools.ServiceResolver;
9
import eu.dnetlib.enabling.tools.registration.ServiceNameResolver;
10
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder;
11
import eu.dnetlib.utils.resolver.ServiceClientFactory;
12
import eu.dnetlib.utils.resolver.TransportConfiguration;
13
import gr.uoa.di.driver.util.FallBackServiceLocator;
14
import gr.uoa.di.driver.util.RefreshingServiceLocator;
15
import gr.uoa.di.driver.util.ServiceLocator;
16
import gr.uoa.di.driver.util.ServiceLocatorFactory;
17
import org.apache.log4j.Logger;
18

    
19
import java.util.Map;
20
import java.util.concurrent.Executors;
21
import java.util.concurrent.ScheduledExecutorService;
22

    
23
/**
24
 * This is an implementation of ServiceLocatorFactory for web service transport
25
 * layer. This implementation assumes that all service interfaces are part of
26
 * the DriverService hierarchy, but does not require that the web service
27
 * interfaces belong to the DriverWebService hierarchy.
28
 * 
29
 * This implementation is created for dnet45, where there is no common API for
30
 * all partners and the only transport layer is SOAP based for all services.
31
 * 
32
 * @author <a href="mailto:antleb@di.uoa.gr">Antonis Lempesis</a>
33
 * 
34
 */
35
public class CompatibilityServiceLocatorFactory implements
36
		ServiceLocatorFactory {
37
	private static Logger logger = Logger.getLogger(CompatibilityServiceLocatorFactory.class);
38
	
39
	private TransportConfiguration configuration = null;
40
	private Map<Class<?>, ServiceClientFactory<?>> clientMap = null;
41
	private CompatibilityEndpointResolver endpointResolver = null;
42
	private StandaloneCxfEndpointReferenceBuilder eprBuilder = null;
43
	private ServiceNameResolver serviceNameResolver = null;
44
	private ServiceResolver serviceResolver = null;
45
	private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
46
	private long locatorRefreshPeriod = 5*60*1000; // 5 minutes
47

    
48
	private UniqueServiceLocator serviceLocator = null;
49

    
50
	public void init() {
51
		logger.debug("Init...");
52
	}
53
	@Override
54
	public <S extends DriverService> ServiceLocator<S> newServiceLocator(
55
			Class<S> serviceClass) {
56
		if (serviceClass.equals(A2Service.class))
57
			throw new IllegalArgumentException("Cannot create " +
58
				"dynamic locator for A2Service");
59

    
60
		return newRefreshingServiceLocator(newDynamicServiceLocator(serviceClass));
61
	}
62

    
63
	@Override
64
	@SuppressWarnings("unchecked")
65
	public <S extends DriverService> ServiceLocator<S> newServiceLocator(
66
			Class<S> serviceClass, String serviceUrl) {
67

    
68
//		if (serviceClass.equals(A2Service.class))
69
//			return (ServiceLocator<S>) newA2ServiceLocator(serviceUrl);
70
//		else 
71
//		if (serviceClass.equals(StoreService.class))
72
//			return (ServiceLocator<S>) newStoreServiceClient(serviceUrl);
73
//		else
74
			return new FallBackServiceLocator<S>(
75
				newRefreshingServiceLocator(newDynamicServiceLocator(serviceClass)),
76
				newUrlServiceLocator(serviceClass, serviceUrl));
77
	}
78
	
79
	private <S extends DriverService> ServiceLocator<S> newRefreshingServiceLocator(
80
			ServiceLocator<S> locator) {
81
		RefreshingServiceLocator<S> rLocator = new RefreshingServiceLocator<S>();
82
		
83
		rLocator.setExecutor(executor);
84
		rLocator.setLocator(locator);
85
		rLocator.setPeriod(this.locatorRefreshPeriod);
86
		
87
		return rLocator;
88
	}
89

    
90
	private <S extends DriverService> ServiceLocator<S> newUrlServiceLocator(
91
			Class<S> serviceClass, String serviceUrl) {
92
		JaxwsServiceLocator<S> locator = new JaxwsServiceLocator<S>();
93
		
94
		locator.setEprBuilder(eprBuilder);
95
		locator.setServiceClass(serviceClass);
96
		locator.setUrl(serviceUrl);
97
		locator.setResolver(endpointResolver);
98
		
99
		return locator;
100
	}
101

    
102
	@SuppressWarnings("unchecked")
103
	private <S extends DriverService> ServiceLocator<S> newDynamicServiceLocator(
104
			Class<S> serviceClass) {
105
		CompatibilityServiceLocator<S> locator = new CompatibilityServiceLocator<S>();
106
		ServiceClientFactory<S> clientFactory = (ServiceClientFactory<S>) clientMap.get(serviceClass);
107

    
108
		locator.setClientFactory(clientFactory);
109
		locator.setServiceClass(serviceClass);
110
		locator.setServiceLocator(this.serviceLocator);
111
		locator.setTransportConfiguration(this.configuration);
112

    
113
		return locator;
114
	}
115

    
116
	public StandaloneCxfEndpointReferenceBuilder getEprBuilder() {
117
		return eprBuilder;
118
	}
119

    
120
	public void setEprBuilder(StandaloneCxfEndpointReferenceBuilder eprBuilder) {
121
		this.eprBuilder = eprBuilder;
122
	}
123

    
124

    
125
	public ServiceNameResolver getServiceNameResolver() {
126
		return serviceNameResolver;
127
	}
128

    
129
	public void setServiceNameResolver(ServiceNameResolver serviceNameResolver) {
130
		this.serviceNameResolver = serviceNameResolver;
131
	}
132

    
133
	public ServiceResolver getServiceResolver() {
134
		return serviceResolver;
135
	}
136

    
137
	public void setServiceResolver(ServiceResolver serviceResolver) {
138
		this.serviceResolver = serviceResolver;
139
	}
140

    
141
	public TransportConfiguration getConfiguration() {
142
		return configuration;
143
	}
144

    
145
	public void setConfiguration(TransportConfiguration configuration) {
146
		this.configuration = configuration;
147
	}
148

    
149
	public CompatibilityEndpointResolver getEndpointResolver() {
150
		return endpointResolver;
151
	}
152

    
153
	public void setEndpointResolver(CompatibilityEndpointResolver endpointResolver) {
154
		this.endpointResolver = endpointResolver;
155
	}
156

    
157
	public Map<Class<?>, ServiceClientFactory<?>> getClientMap() {
158
		return clientMap;
159
	}
160

    
161
	public void setClientMap(Map<Class<?>, ServiceClientFactory<?>> clientMap) {
162
		this.clientMap = clientMap;
163
	}
164

    
165
	public UniqueServiceLocator getServiceLocator() {
166
		return serviceLocator;
167
	}
168

    
169
	public void setServiceLocator(UniqueServiceLocator serviceLocator) {
170
		this.serviceLocator = serviceLocator;
171
	}
172
}
(4-4/7)