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

    
112
		return locator;
113
	}
114

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

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

    
123

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

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

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

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

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

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

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

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

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

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

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

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