package org.gcube.common.clients.builders; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; import javax.xml.ws.wsaddressing.W3CEndpointReference; import org.gcube.common.clients.cache.EndpointCache; import org.gcube.common.clients.config.DiscoveryConfig; import org.gcube.common.clients.config.EndpointConfig; import org.gcube.common.clients.config.Property; import org.gcube.common.clients.delegates.DirectDelegate; import org.gcube.common.clients.delegates.DiscoveryDelegate; import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyPlugin; import org.gcube.common.clients.queries.Query; /** * Partial implementation of proxy builders. * * @author Fabio Simeoni * * @param the type of service addresses * @param the type of service stubs * @param

the type of service proxies */ public abstract class AbstractBuilder { /** * Default proxy timeout. */ public static final int defaultTimeout = (int)TimeUnit.SECONDS.toMillis(10); private final ProxyPlugin plugin; private Query query; private W3CEndpointReference address; private final EndpointCache cache; private final Map properties = new HashMap(); /** * Constructs an instance with a given {@link ProxyPlugin}, and {@link EndpointCache}, and zero or more default {@link Property}s. * @param plugin the plugin * @param cache the cache * @param properties the properties */ protected AbstractBuilder(ProxyPlugin plugin,EndpointCache cache,Property ... properties) { this.plugin=plugin; this.cache=cache; //sets default timeout, may be overridden by custom properties below setTimeout(defaultTimeout); //add custom properties for (Property property : properties) addProperty(property); } /** * Returns the {@link ProxyPlugin}. * @return the plugin */ protected ProxyPlugin plugin() { return plugin; } /** * Sets the query. * @param query the query */ protected void setQuery(Query query) { this.query = query; } /** * Sets the timeout. * @param timeout the timout */ protected void setTimeout(int timeout) { addProperty(Property.timeout(timeout)); } /** * Sets the address. * @param address the address */ protected void setAddress(W3CEndpointReference address) { this.address=address; } /** * Adds a custom property. * @param property the property */ protected void addProperty(Property property) { properties.put(property.name(),property.value()); } //shared among subclasses public P build() { ProxyDelegate delegate = null; if (address==null) { DiscoveryConfig config = new DiscoveryConfig(plugin,query,cache); for (Entry prop : properties.entrySet()) config.addProperty(prop.getKey(),prop.getValue()); delegate = new DiscoveryDelegate(config); } else { EndpointConfig config = new EndpointConfig(plugin,convertAddress(address)); for (Entry prop : properties.entrySet()) config.addProperty(prop.getKey(),prop.getValue()); delegate = new DirectDelegate(config); } return plugin.newProxy(delegate); } /** * Converts a {@link W3CEndpointReference} in a service address. * @param address the address as a {@link W3CEndpointReference} * @return the converted address */ protected abstract A convertAddress(W3CEndpointReference address); protected abstract String contextPath(); }