package org.gcube.common.clients.config; import java.util.HashMap; import java.util.Map; import org.gcube.common.clients.delegates.ProxyPlugin; /** * Partial implementation of {@link ProxyConfig}. * * @author Fabio Simeoni * * @param the type of service addresses * @param the type of service stubs */ public abstract class AbstractConfig implements ProxyConfig { private final ProxyPlugin plugin; private final Map properties = new HashMap(); /** * Creates an instance with a given {@link ProxyPlugin}. * @param plugin the plugin */ protected AbstractConfig(ProxyPlugin plugin) { this.plugin = plugin; } @Override public ProxyPlugin plugin() { return plugin; } @Override public long timeout() throws IllegalArgumentException { if (!hasProperty(Property.timeout)) throw new IllegalArgumentException("timeout property is undefined"); else return property(Property.timeout,Long.class); } @Override public void addProperty(String name, T value) { properties.put(name, value); } @Override public void addProperty(Property property) { properties.put(property.name(),property.value()); } @Override public boolean hasProperty(String property) { return properties.containsKey(property); } @Override public T property(String property, Class clazz) throws IllegalStateException, IllegalArgumentException { if (!hasProperty(property)) throw new IllegalStateException(property+" is unknown"); try { return clazz.cast(properties.get(property)); } catch(Exception e) { throw new IllegalArgumentException("could not retrieve "+property+" as "+clazz,e); } } }