package net.sf.briar.plugins; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Executor; import net.sf.briar.api.ContactId; import net.sf.briar.api.transport.InvalidConfigException; import net.sf.briar.api.transport.InvalidPropertiesException; import net.sf.briar.api.transport.TransportPlugin; import net.sf.briar.api.transport.batch.BatchTransportCallback; public abstract class AbstractPlugin implements TransportPlugin { protected final Executor executor; // These fields should be accessed with this's lock held protected Map localProperties = null; protected Map> remoteProperties = null; protected Map config = null; protected BatchTransportCallback callback = null; protected boolean started = false; protected AbstractPlugin(Executor executor) { this.executor = executor; } protected synchronized void start(Map localProperties, Map> remoteProperties, Map config) throws InvalidPropertiesException, InvalidConfigException { if(started) throw new IllegalStateException(); started = true; this.localProperties = Collections.unmodifiableMap(localProperties); // Copy the remoteProperties map to make its values unmodifiable // Copy the remoteProperties map to make its values unmodifiable int size = remoteProperties.size(); Map> m = new HashMap>(size); for(Entry> e : remoteProperties.entrySet()) { m.put(e.getKey(), Collections.unmodifiableMap(e.getValue())); } this.remoteProperties = m; this.config = Collections.unmodifiableMap(config); } public synchronized void stop() throws IOException { if(!started) throw new IllegalStateException(); started = false; } public synchronized void setLocalProperties(Map properties) throws InvalidPropertiesException { if(!started) throw new IllegalStateException(); localProperties = Collections.unmodifiableMap(properties); } public synchronized void setRemoteProperties(ContactId c, Map properties) throws InvalidPropertiesException { if(!started) throw new IllegalStateException(); remoteProperties.put(c, Collections.unmodifiableMap(properties)); } public synchronized void setConfig(Map config) throws InvalidConfigException { if(!started) throw new IllegalStateException(); this.config = Collections.unmodifiableMap(config); } }