From 42e586abc4c9c72bd4eecc976f85906bf5cc6689 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Thu, 6 Oct 2011 18:07:58 +0100
Subject: [PATCH] Merged code from FilePlugin and SocketPlugin into a
 superclass.

---
 .../net/sf/briar/plugins/AbstractPlugin.java  | 73 +++++++++++++++++++
 .../net/sf/briar/plugins/file/FilePlugin.java | 65 +++--------------
 .../sf/briar/plugins/socket/SocketPlugin.java | 51 ++-----------
 3 files changed, 91 insertions(+), 98 deletions(-)
 create mode 100644 components/net/sf/briar/plugins/AbstractPlugin.java

diff --git a/components/net/sf/briar/plugins/AbstractPlugin.java b/components/net/sf/briar/plugins/AbstractPlugin.java
new file mode 100644
index 0000000000..babc90c0f6
--- /dev/null
+++ b/components/net/sf/briar/plugins/AbstractPlugin.java
@@ -0,0 +1,73 @@
+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;
+
+	protected Map<String, String> localProperties = null;
+	protected Map<ContactId, Map<String, String>> remoteProperties = null;
+	protected Map<String, String> config = null;
+	protected BatchTransportCallback callback = null;
+	protected boolean started = false;
+
+	protected AbstractPlugin(Executor executor) {
+		this.executor = executor;
+	}
+
+	protected void start(Map<String, String> localProperties,
+			Map<ContactId, Map<String, String>> remoteProperties,
+			Map<String, String> 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<ContactId, Map<String, String>> m =
+			new HashMap<ContactId, Map<String, String>>(size);
+		for(Entry<ContactId, Map<String, String>> 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<String, String> properties)
+	throws InvalidPropertiesException {
+		if(!started) throw new IllegalStateException();
+		localProperties = Collections.unmodifiableMap(properties);
+	}
+
+	public synchronized void setRemoteProperties(ContactId c,
+			Map<String, String> properties)
+	throws InvalidPropertiesException {
+		if(!started) throw new IllegalStateException();
+		remoteProperties.put(c, Collections.unmodifiableMap(properties));
+	}
+
+	public synchronized void setConfig(Map<String, String> config)
+	throws InvalidConfigException {
+		if(!started) throw new IllegalStateException();
+		this.config = Collections.unmodifiableMap(config);
+	}
+}
diff --git a/components/net/sf/briar/plugins/file/FilePlugin.java b/components/net/sf/briar/plugins/file/FilePlugin.java
index f16796ace7..9cb3cd726d 100644
--- a/components/net/sf/briar/plugins/file/FilePlugin.java
+++ b/components/net/sf/briar/plugins/file/FilePlugin.java
@@ -5,10 +5,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-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;
@@ -19,79 +16,37 @@ import net.sf.briar.api.transport.batch.BatchTransportCallback;
 import net.sf.briar.api.transport.batch.BatchTransportPlugin;
 import net.sf.briar.api.transport.batch.BatchTransportReader;
 import net.sf.briar.api.transport.batch.BatchTransportWriter;
+import net.sf.briar.plugins.AbstractPlugin;
 
 import org.apache.commons.io.FileSystemUtils;
 
-abstract class FilePlugin implements BatchTransportPlugin {
-
-	private final Executor executor;
-
-	protected Map<String, String> localProperties = null;
-	protected Map<ContactId, Map<String, String>> remoteProperties = null;
-	protected Map<String, String> config = null;
-	protected BatchTransportCallback callback = null;
-
-	private volatile boolean started = false;
+abstract class FilePlugin extends AbstractPlugin
+implements BatchTransportPlugin {
 
 	protected abstract File chooseOutputDirectory();
 	protected abstract void writerFinished(File f);
 	protected abstract void readerFinished(File f);
 
-	FilePlugin(Executor executor) {
-		this.executor = executor;
+	protected FilePlugin(Executor executor) {
+		super(executor);
 	}
 
 	public synchronized void start(Map<String, String> localProperties,
 			Map<ContactId, Map<String, String>> remoteProperties,
 			Map<String, String> config, BatchTransportCallback callback)
 	throws InvalidPropertiesException, InvalidConfigException, IOException {
-		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<ContactId, Map<String, String>> m =
-			new HashMap<ContactId, Map<String, String>>(size);
-		for(Entry<ContactId, Map<String, String>> e
-				: remoteProperties.entrySet()) {
-			m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
-		}
-		this.remoteProperties = m;
-		this.config = Collections.unmodifiableMap(config);
+		super.start(localProperties, remoteProperties, config);
 		this.callback = callback;
 	}
 
-	public synchronized void stop() throws IOException {
-		if(!started) throw new IllegalStateException();
-		started = false;
-	}
-
-	public synchronized void setLocalProperties(Map<String, String> properties)
-	throws InvalidPropertiesException {
-		if(!started) throw new IllegalStateException();
-		localProperties = Collections.unmodifiableMap(properties);
-	}
-
-	public synchronized void setRemoteProperties(ContactId c,
-			Map<String, String> properties)
-	throws InvalidPropertiesException {
-		if(!started) throw new IllegalStateException();
-		remoteProperties.put(c, Collections.unmodifiableMap(properties));
-	}
-
-	public synchronized void setConfig(Map<String, String> config)
-	throws InvalidConfigException {
-		if(!started) throw new IllegalStateException();
-		this.config = Collections.unmodifiableMap(config);
-	}
-
 	public BatchTransportReader createReader(ContactId c) {
 		return null;
 	}
 
 	public BatchTransportWriter createWriter(ContactId c) {
-		if(!started) return null;
+		synchronized(this) {
+			if(!started) return null;
+		}
 		File dir = chooseOutputDirectory();
 		if(dir == null || !dir.exists() || !dir.isDirectory()) return null;
 		File f = new File(dir, createFilename());
@@ -117,7 +72,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
 		return FileSystemUtils.freeSpaceKb(path) * 1024L;
 	}
 
-	protected void createReaderFromFile(final File f) {
+	protected synchronized void createReaderFromFile(final File f) {
 		if(!started) return;
 		executor.execute(new ReaderCreator(f));
 	}
diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java
index fadd7c41cf..ad6560069a 100644
--- a/components/net/sf/briar/plugins/socket/SocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java
@@ -4,10 +4,7 @@ import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
-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;
@@ -16,19 +13,14 @@ import net.sf.briar.api.transport.InvalidPropertiesException;
 import net.sf.briar.api.transport.stream.StreamTransportCallback;
 import net.sf.briar.api.transport.stream.StreamTransportConnection;
 import net.sf.briar.api.transport.stream.StreamTransportPlugin;
+import net.sf.briar.plugins.AbstractPlugin;
 
-abstract class SocketPlugin implements StreamTransportPlugin {
+abstract class SocketPlugin extends AbstractPlugin
+implements StreamTransportPlugin {
 
-	private final Executor executor;
-
-	protected Map<String, String> localProperties = null;
-	protected Map<ContactId, Map<String, String>> remoteProperties = null;
-	protected Map<String, String> config = null;
 	protected StreamTransportCallback callback = null;
 	protected ServerSocket socket = null;
 
-	private volatile boolean started = false;
-
 	// These methods should be called with this's lock held and started == true
 	protected abstract SocketAddress getLocalSocketAddress();
 	protected abstract SocketAddress getSocketAddress(ContactId c);
@@ -36,27 +28,15 @@ abstract class SocketPlugin implements StreamTransportPlugin {
 	protected abstract Socket createClientSocket() throws IOException;
 	protected abstract ServerSocket createServerSocket() throws IOException;
 
-	SocketPlugin(Executor executor) {
-		this.executor = executor;
+	protected SocketPlugin(Executor executor) {
+		super(executor);
 	}
 
 	public synchronized void start(Map<String, String> localProperties,
 			Map<ContactId, Map<String, String>> remoteProperties,
 			Map<String, String> config, StreamTransportCallback callback)
 	throws InvalidPropertiesException, InvalidConfigException {
-		if(started) throw new IllegalStateException();
-		started = true;
-		this.localProperties = Collections.unmodifiableMap(localProperties);
-		// Copy the remoteProperties map to make its values unmodifiable
-		int size = remoteProperties.size();
-		Map<ContactId, Map<String, String>> m =
-			new HashMap<ContactId, Map<String, String>>(size);
-		for(Entry<ContactId, Map<String, String>> e
-				: remoteProperties.entrySet()) {
-			m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
-		}
-		this.remoteProperties = m;
-		this.config = Collections.unmodifiableMap(config);
+		super.start(localProperties, remoteProperties, config);
 		this.callback = callback;
 		executor.execute(createBinder());
 	}
@@ -132,15 +112,13 @@ abstract class SocketPlugin implements StreamTransportPlugin {
 	}
 
 	public synchronized void stop() throws IOException {
-		if(!started) throw new IllegalStateException();
-		started = false;
+		super.stop();
 		if(socket != null) socket.close();
 	}
 
 	public synchronized void setLocalProperties(Map<String, String> properties)
 	throws InvalidPropertiesException {
-		if(!started) throw new IllegalStateException();
-		localProperties = Collections.unmodifiableMap(properties);
+		super.setLocalProperties(properties);
 		// Close and reopen the socket if its address has changed
 		if(socket != null) {
 			SocketAddress addr = socket.getLocalSocketAddress();
@@ -155,19 +133,6 @@ abstract class SocketPlugin implements StreamTransportPlugin {
 		}
 	}
 
-	public synchronized void setRemoteProperties(ContactId c,
-			Map<String, String> properties)
-	throws InvalidPropertiesException {
-		if(!started) throw new IllegalStateException();
-		remoteProperties.put(c, Collections.unmodifiableMap(properties));
-	}
-
-	public synchronized void setConfig(Map<String, String> config)
-	throws InvalidConfigException {
-		if(!started) throw new IllegalStateException();
-		this.config = Collections.unmodifiableMap(config);
-	}
-
 	public synchronized void poll() {
 		if(!shouldPoll()) throw new UnsupportedOperationException();
 		if(!started) throw new IllegalStateException();
-- 
GitLab