Project

General

Profile

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

    
3
import java.lang.reflect.InvocationTargetException;
4

    
5
import org.apache.log4j.Logger;
6

    
7
import eu.dnetlib.api.DriverService;
8
import eu.dnetlib.utils.resolver.ServiceClientFactory;
9

    
10
/**
11
 * This is an implementation of ServiceClientFactory for web service transport
12
 * layer. This implementation assumes that all service interfaces are part of
13
 * the DriverService hierarchy, but does not require that the web service
14
 * interfaces belong to the DriverWebService hierarchy.
15
 * 
16
 * This implementation is created for dnet1.1, where there is no common API for
17
 * all partners and the only transport layer is SOAP based for all services.
18
 * 
19
 * @author <a href="mailto:antleb@di.uoa.gr">Antonis Lempesis</a>
20
 * 
21
 */
22
public class CompatibilityServiceClientFactory<S extends DriverService> implements ServiceClientFactory<S> {
23
	private CompatibilityTransportConfiguration config = null;
24
	private static Logger logger = Logger.getLogger(CompatibilityServiceClientFactory.class);
25
	
26
	private Class<S> serviceClass = null;
27

    
28
	/**
29
	 * Creates a client for a service that does not provide a DriverWebService
30
	 * endpoint. The assumption made here is that the service client provides a
31
	 * default constructor and a setWebService(Object webService) method.
32
	 * 
33
	 * @param <T>
34
	 *            The service type.
35
	 * @param serviceClass
36
	 *            The service class.
37
	 * @param endpoint
38
	 *            The web service endpoint.
39
	 * 
40
	 * @return A client for this web service.
41
	 */
42
	public S newClient(Object endpoint) {
43
		if (logger.isDebugEnabled())
44
			logger.debug("Creating client for service "
45
					+ serviceClass.getName() + " and endpoint of class "
46
					+ endpoint.getClass().getName());
47

    
48
		try {
49
			Class<S> clientClass = config.getServiceClientClass(serviceClass);
50

    
51
			if (logger.isDebugEnabled())
52
				logger.debug("client class: " + clientClass.getName());
53

    
54
			S client = clientClass.newInstance();
55

    
56
			clientClass
57
					.getMethod("setWebService", new Class[] { Object.class })
58
					.invoke(client, endpoint);
59

    
60
			return client;
61
		} catch (InstantiationException e) {
62
			logger.error("Error creating service client", e);
63
		} catch (IllegalAccessException e) {
64
			logger.error("Error creating service client", e);
65
		} catch (IllegalArgumentException e) {
66
			logger.error("Error creating service client", e);
67
		} catch (SecurityException e) {
68
			logger.error("Error creating service client", e);
69
		} catch (InvocationTargetException e) {
70
			logger.error("Error creating service client", e);
71
		} catch (NoSuchMethodException e) {
72
			logger.error("Error creating service client", e);
73
		}
74

    
75
		return null;
76
	}
77

    
78
	public CompatibilityTransportConfiguration getConfig() {
79
		return config;
80
	}
81

    
82
	public void setConfig(CompatibilityTransportConfiguration config) {
83
		this.config = config;
84
	}
85

    
86
	public Class<S> getServiceClass() {
87
		return serviceClass;
88
	}
89

    
90
	public void setServiceClass(Class<S> serviceClass) {
91
		this.serviceClass = serviceClass;
92
	}
93
}
(2-2/7)