Skip to content

Commit

Permalink
Add abstraction for reading and writing to account files with differe…
Browse files Browse the repository at this point in the history
…nt formats
  • Loading branch information
Adiras committed Feb 24, 2024
1 parent 0bb8e6a commit f14191b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 194 deletions.
25 changes: 25 additions & 0 deletions application/accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"username": "test2",
"homeDirectory": "C:\\Users\\wkacp\\Desktop"
},
{
"username": "admin",
"homeDirectory": "test",
"hashedPassword": "7ee737c83ee689c96ef37d3a029068c390ebc8f8"
},
{
"username": "admin2",
"homeDirectory": "test",
"hashedPassword": "7ee737c83ee689c96ef37d3a029068c390ebc8f8"
},
{
"username": "admin3",
"homeDirectory": "test",
"hashedPassword": "7ee737c83ee689c96ef37d3a029068c390ebc8f8"
},
{
"username": "sd",
"homeDirectory": ""
}
]
11 changes: 0 additions & 11 deletions application/users.ftprx

This file was deleted.

3 changes: 2 additions & 1 deletion server/src/main/java/com/ftprx/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.ftprx.server;

import com.ftprx.server.account.AccountFileFormat;
import com.ftprx.server.account.ObservableAccountRepository;
import com.ftprx.server.channel.Client;
import com.ftprx.server.repository.FileAccountRepository;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class Server {

private Server() {
this.clients = new CopyOnWriteArrayList<>();
this.accountRepository = new FileAccountRepository("users.ftprx");
this.accountRepository = new FileAccountRepository("accounts.json", AccountFileFormat.JSON);
this.status = ServerStatus.STOPPED;
this.config = ConfigFactory.create(ServerConfig.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ftprx.server.account;

public enum AccountFileFormat {
JSON
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ftprx.server.account;

import com.ftprx.server.account.Account;

import java.io.Reader;
import java.io.Writer;
import java.util.List;

/**
* An interface for reading from and writing to a file containing account list.
*/
public interface AccountFileManager {
void write(Writer writer, List<Account> accounts);
List<Account> read(Reader reader);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ftprx.server.account;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.Reader;
import java.io.Writer;
import java.util.List;

public class JsonAccountFileManager implements AccountFileManager {
private final Gson gson;

public JsonAccountFileManager() {
this.gson = new GsonBuilder().setPrettyPrinting().create();
}

@Override
public void write(Writer writer, List<Account> accounts) {
gson.toJson(accounts, writer);
}

@Override
public List<Account> read(Reader reader) {
return List.of(gson.fromJson(reader, Account[].class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@
package com.ftprx.server.repository;

import com.ftprx.server.account.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tinylog.Logger;

import java.io.*;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import static com.ftprx.server.account.AccountInsertException.ACCOUNT_ALREADY_EXISTS;
import static java.nio.file.Files.*;
import static java.util.Collections.emptyList;

public class FileAccountRepository implements ObservableAccountRepository {
private final Gson gson;
private final Path repositoryFile;
private final Path accountsFile;
private final Set<AccountRepositoryChangeListener> listeners;
private final AccountFileManager fileManager;

public FileAccountRepository(String filename) {
this.repositoryFile = Paths.get(filename);
this.gson = new GsonBuilder()
.setPrettyPrinting()
.create();
public FileAccountRepository(String filename, AccountFileFormat fileFormat) {
this.accountsFile = Paths.get(filename);

switch (fileFormat) {
case JSON -> fileManager = new JsonAccountFileManager();
default -> throw new IllegalStateException("Unexpected value: " + fileFormat);
}

listeners = Collections.newSetFromMap(new ConcurrentHashMap<>());

Expand All @@ -50,9 +52,7 @@ public FileAccountRepository(String filename) {
insert(new Account("admin", "test", "dir"));
insert(new Account("admin2", "test", "dir"));
insert(new Account("admin3", "test", "dir"));
} catch (AccountInsertException ignore) {} catch (AccountCreateException e) {
e.printStackTrace();
}
} catch (AccountInsertException | AccountCreateException ignore) {}
}

@Override
Expand Down Expand Up @@ -119,32 +119,32 @@ private void delete(String username) {
saveFile(toSave);
}

private void createRepositoryFileIfNotExists() {
if (!exists(repositoryFile)) {
private void createAccountsFileIfNotExists() {
if (!exists(accountsFile)) {
Logger.debug("Account file not found!");
try {
createFile(repositoryFile);
createFile(accountsFile);
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
}

private void saveFile(List<Account> accounts) {
createRepositoryFileIfNotExists();
try (Writer writer = newBufferedWriter(repositoryFile)) {
gson.toJson(accounts, writer);
createAccountsFileIfNotExists();
try (Writer writer = newBufferedWriter(accountsFile)) {
fileManager.write(writer, accounts);
} catch (Exception e) {
Logger.error(e.getMessage());
}
}

public List<Account> readFile() {
createRepositoryFileIfNotExists();
try (Reader reader = newBufferedReader(repositoryFile)) {
return Arrays.asList(gson.fromJson(reader, Account[].class));
createAccountsFileIfNotExists();
try (Reader reader = newBufferedReader(accountsFile)) {
return fileManager.read(reader);
} catch (Exception e) {
return Collections.emptyList();
return emptyList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

public class InMemoryAccountRepository implements AccountRepository {
private List<Account> accounts = new ArrayList<>();
private final List<Account> accounts = new ArrayList<>();

public InMemoryAccountRepository() {
try {
Expand Down

0 comments on commit f14191b

Please sign in to comment.