Use a connection factory instead of a connection builder.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Nov 2014 19:52:12 +0000 (20:52 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Nov 2014 19:52:12 +0000 (20:52 +0100)
src/main/java/net/pterodactylus/irc/ConnectionBuilder.java [deleted file]
src/main/java/net/pterodactylus/irc/ConnectionFactory.java [new file with mode: 0644]
src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java [new file with mode: 0644]
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/main/Main.java

diff --git a/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java b/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java
deleted file mode 100644 (file)
index feefe1f..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * XdccDownloader - ConnectionBuilder.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.irc;
-
-import javax.net.SocketFactory;
-import javax.net.ssl.SSLSocketFactory;
-
-import com.google.common.base.Optional;
-import com.google.common.eventbus.EventBus;
-import com.google.inject.Inject;
-
-/**
- * Builder for {@link Connection}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class ConnectionBuilder {
-
-       /** The event bus. */
-       private final EventBus eventBus;
-
-       /** The hostname to connect to. */
-       private Optional<String> hostname = Optional.absent();
-
-       /** The port number to connect to. */
-       private int port = 6666;
-
-       /** Whether to use an SSL connection. */
-       private boolean secure = false;
-
-       /**
-        * Creates a new connection builder.
-        *
-        * @param eventBus
-        *              The event bus
-        */
-       @Inject
-       public ConnectionBuilder(EventBus eventBus) {
-               this.eventBus = eventBus;
-       }
-
-       /**
-        * Configures this builder to use the given hostname for the connection.
-        *
-        * @param hostname
-        *              The hostname of the IRC server
-        * @return This builder
-        */
-       public ConnectionBuilder connect(String hostname) {
-               this.hostname = Optional.fromNullable(hostname);
-               return this;
-       }
-
-       /**
-        * Configures this builder to use the given port number for the connection.
-        *
-        * @param port
-        *              The port number of the IRC server
-        * @return This builder
-        */
-       public ConnectionBuilder port(int port) {
-               this.port = port;
-               return this;
-       }
-
-       /**
-        * Configures this builder to use an SSL connection.
-        *
-        * @return This builder
-        */
-       public ConnectionBuilder secure() {
-               this.secure = true;
-               return this;
-       }
-
-       /**
-        * Builds the connection.
-        *
-        * @return The created connection
-        * @throws IllegalStateException
-        *              if the hostname has not been set
-        */
-       public Connection build() throws IllegalStateException {
-               if (!hostname.isPresent()) {
-                       throw new IllegalStateException("hostname must not be empty");
-               }
-
-               SocketFactory socketFactory = secure ? SSLSocketFactory.getDefault() : SocketFactory.getDefault();
-               return new Connection(eventBus, socketFactory, hostname.get(), port);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/irc/ConnectionFactory.java b/src/main/java/net/pterodactylus/irc/ConnectionFactory.java
new file mode 100644 (file)
index 0000000..51993a7
--- /dev/null
@@ -0,0 +1,12 @@
+package net.pterodactylus.irc;
+
+/**
+ * Creates {@link Connection}s to IRC servers.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ConnectionFactory {
+
+       Connection createConnection(String hostname, int port);
+
+}
diff --git a/src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java b/src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java
new file mode 100644 (file)
index 0000000..6a37394
--- /dev/null
@@ -0,0 +1,25 @@
+package net.pterodactylus.irc;
+
+import javax.net.SocketFactory;
+
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Default {@link ConnectionFactory} implementation that uses plain sockets to create connections.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultConnectionFactory implements ConnectionFactory {
+
+       private final EventBus eventBus;
+
+       public DefaultConnectionFactory(EventBus eventBus) {
+               this.eventBus = eventBus;
+       }
+
+       @Override
+       public Connection createConnection(String hostname, int port) {
+               return new Connection(eventBus, SocketFactory.getDefault(), hostname, port);
+       }
+
+}
index 3414d18..ab9804a 100644 (file)
@@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 import net.pterodactylus.irc.Connection;
-import net.pterodactylus.irc.ConnectionBuilder;
+import net.pterodactylus.irc.ConnectionFactory;
 import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
@@ -106,6 +106,7 @@ public class Core extends AbstractExecutionThreadService {
 
        /** The event bus. */
        private final EventBus eventBus;
+       private final ConnectionFactory connectionFactory;
        private final ChannelBanManager channelBanManager =
                        new ChannelBanManager();
 
@@ -147,8 +148,9 @@ public class Core extends AbstractExecutionThreadService {
         *              The directory to move finished files to
         */
        @Inject
-       public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
+       public Core(EventBus eventBus, ConnectionFactory connectionFactory, String temporaryDirectory, String finalDirectory) {
                this.eventBus = eventBus;
+               this.connectionFactory = connectionFactory;
                this.temporaryDirectory = temporaryDirectory;
                this.finalDirectory = finalDirectory;
        }
@@ -428,7 +430,7 @@ public class Core extends AbstractExecutionThreadService {
                                return;
                        }
                        Server server = servers.get((int) (Math.random() * servers.size()));
-                       Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build();
+                       Connection connection = connectionFactory.createConnection(server.hostname(), server.unencryptedPorts().iterator().next());
                        connection.username(RandomNickname.get()).realName(RandomNickname.get());
                        networkConnections.put(network, connection);
                        connection.start();
index 3272736..c9b7c5b 100644 (file)
@@ -25,6 +25,8 @@ import java.util.HashSet;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.Executors;
 
+import net.pterodactylus.irc.ConnectionFactory;
+import net.pterodactylus.irc.DefaultConnectionFactory;
 import net.pterodactylus.xdcc.core.Core;
 import net.pterodactylus.xdcc.data.Channel;
 import net.pterodactylus.xdcc.data.Download;
@@ -58,8 +60,9 @@ public class Main {
 
                EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
                Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
+               ConnectionFactory connectionFactory = new DefaultConnectionFactory(eventBus);
 
-               Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
+               Core core = new Core(eventBus, connectionFactory, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
                eventBus.register(core);
 
                for (Configuration.Network network : configuration.getNetworks()) {