Skip to content

Commit 4454dd4

Browse files
committed
Refactor app out into spiked factories in preparation for TDD
1 parent 020ad75 commit 4454dd4

File tree

6 files changed

+91
-25
lines changed

6 files changed

+91
-25
lines changed

src/main/java/httpserver/App.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
package httpserver;
22

3-
import httpserver.file.FileOperator;
43
import httpserver.file.PathExaminer;
54

65
import java.io.IOException;
7-
import java.net.ServerSocket;
8-
import java.net.Socket;
9-
import java.nio.file.Path;
10-
import java.nio.file.Paths;
116
import java.util.concurrent.ExecutorService;
127
import java.util.concurrent.Executors;
138

149
public class App {
10+
11+
private static ExecutorService threadPool;
12+
private static ServerFactory serverFactory;
13+
private static SocketHandlerFactory socketHandlerFactory;
14+
1515
public static void main(String[] args) throws IOException {
16+
threadPool = Executors.newFixedThreadPool(16);
17+
serverFactory = new ServerFactory(new PathExaminer(), new ServerSocketFactory());
18+
socketHandlerFactory = new SocketHandlerFactory();
19+
1620
int port = Integer.parseInt(args[1]);
1721
String fileDirectory = args[3];
1822

19-
Path root = new PathExaminer().getPath(fileDirectory);
20-
ServerSocket serverSocket = new ServerSocket(port);
21-
Path logPath = Paths.get(root.toString(), "logs");
22-
23-
ExecutorService pool = Executors.newFixedThreadPool(16);
24-
25-
while (true) {
26-
Socket clientSocket = serverSocket.accept();
27-
SocketHandler socketHandler = new SocketHandler(root,
28-
new FileLogger(logPath, new FileOperator()),
29-
clientSocket.getInputStream(),
30-
clientSocket.getOutputStream());
31-
pool.execute(socketHandler);
32-
}
23+
Server server = serverFactory.makeServer(port, fileDirectory);
24+
server.start(threadPool, socketHandlerFactory);
3325
}
3426
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package httpserver;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
import java.nio.file.Path;
7+
import java.util.concurrent.ExecutorService;
8+
9+
public class Server {
10+
private final Path logPath;
11+
private final Path root;
12+
private final ServerSocket serverSocket;
13+
14+
public Server(ServerSocket serverSocket, Path root, Path logPath) {
15+
this.serverSocket = serverSocket;
16+
this.root = root;
17+
this.logPath = logPath;
18+
}
19+
20+
public void start(ExecutorService threadPool, SocketHandlerFactory socketHandlerFactory) throws IOException {
21+
while (true) {
22+
Socket clientSocket = serverSocket.accept();
23+
SocketHandler socketHandler = socketHandlerFactory.newSocketHandler(root, logPath, clientSocket);
24+
threadPool.execute(socketHandler);
25+
}
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package httpserver;
2+
3+
import httpserver.file.PathExaminer;
4+
5+
import java.io.IOException;
6+
import java.net.ServerSocket;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
10+
public class ServerFactory {
11+
private ServerSocketFactory serverSocketFactory;
12+
private PathExaminer pathExaminer;
13+
14+
public ServerFactory(PathExaminer pathExaminer, ServerSocketFactory serverSocketFactory) {
15+
this.serverSocketFactory = serverSocketFactory;
16+
this.pathExaminer = pathExaminer;
17+
}
18+
public Server makeServer(int port, String fileDirectory) throws IOException {
19+
Path root = pathExaminer.getPath(fileDirectory);
20+
Path logPath = Paths.get(root.toString(), "logs");
21+
ServerSocket serverSocket = serverSocketFactory.newServerSocket(port);
22+
23+
return new Server(serverSocket, root, logPath);
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package httpserver;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
6+
public class ServerSocketFactory {
7+
public ServerSocket newServerSocket(int port) throws IOException {
8+
return new ServerSocket(port);
9+
}
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package httpserver;
2+
3+
import httpserver.file.FileOperator;
4+
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import java.nio.file.Path;
8+
9+
public class SocketHandlerFactory {
10+
public SocketHandler newSocketHandler(Path root, Path logPath, Socket clientSocket) throws IOException {
11+
return new SocketHandler(root,
12+
new FileLogger(logPath, new FileOperator()),
13+
clientSocket.getInputStream(),
14+
clientSocket.getOutputStream());
15+
}
16+
}

src/main/java/httpserver/file/FileOperator.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ public void appendToFile(Path path, byte[] contents) throws IOException {
2121
write(path, contents, StandardOpenOption.APPEND);
2222
}
2323

24-
public void replaceContents(Path path, byte[] contents) {
25-
try {
26-
write(path, contents);
27-
} catch (IOException e) {
28-
e.printStackTrace();
29-
}
24+
public void replaceContents(Path path, byte[] contents) throws IOException {
25+
write(path, contents);
3026
}
3127
}

0 commit comments

Comments
 (0)