Added documentation, refined exception handling

This commit is contained in:
Kai S. K. Engelbart 2019-12-14 11:30:00 +01:00
parent fb3b53efd8
commit f044c5cc8d
4 changed files with 65 additions and 32 deletions

View File

@ -33,8 +33,23 @@ public class Client {
private User sender, recipient;
private boolean online = false;
/**
* Initializes the client. At this state, the client user has yet to be
* initialized, which can be done by calling {@link Client#onlineInit(String).
*
* @param config The {@link Config} instance to use in this client
* @since Envoy v0.2-alpha
*/
public Client(Config config) { this.config = config; }
/**
* Enters the online mode by acquiring a user ID from the server.
*
* @param userName the name of the client user
* @throws EnvoyException if the online mode could not be entered or the request
* failed for some other reason
* @since Envoy v0.2-alpha
*/
public void onlineInit(String userName) throws EnvoyException {
sender = getUser(userName);
online = true;
@ -52,10 +67,8 @@ public class Client {
}
/**
* Returns a {@code Map<String, User>} of all users on the server with their
* user names as keys.
*
* @return Sync - List of all users on the server.
* @return a {@code Map<String, User>} of all users on the server with their
* user names as keys
* @since Envoy v0.2-alpha
*/
public Map<String, User> getUsers() {
@ -138,9 +151,12 @@ public class Client {
* @param sync the sync object (yet to be converted from java class to
* sync.xml)
* @return a returnSync.xml file
* @throws EnvoyException if the client is not in online mode
* @since Envoy v0.1-alpha
*/
public Sync sendSync(long userId, Sync sync) {
public Sync sendSync(long userId, Sync sync) throws EnvoyException {
if(!isOnline())
throw new EnvoyException("Client is not in online mode");
// Print sync XML to console
JAXBContext jc;
try {
@ -162,6 +178,12 @@ public class Client {
*/
public User getSender() { return sender; }
/**
* Sets the client user which is used to send messages.
*
* @param sender the client user to set
* @since Envoy v0.2-alpha
*/
public void setSender(User sender) { this.sender = sender; }
/**
@ -173,7 +195,7 @@ public class Client {
/**
* Sets the recipient.
*
* @param recipient - the recipient to set
* @param recipient the recipient to set
* @since Envoy v0.1-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
@ -186,6 +208,7 @@ public class Client {
/**
* @return {@code true} if a connection to the server could be established
* @since Envoy v0.2-alpha
*/
public boolean isOnline() { return online; }
}

View File

@ -36,10 +36,10 @@ import envoy.schema.User;
*/
public class LocalDB {
private File localDBDir, localDBFile, usersFile;
private User user;
private File localDBDir, localDBFile, usersFile;
private User user;
private Map<String, User> users = new HashMap<>();
private List<Chat> chats = new ArrayList<>();
private List<Chat> chats = new ArrayList<>();
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
@ -54,6 +54,7 @@ public class LocalDB {
* Constructs an empty local database. To serialize any chats to the file
* system, call {@link LocalDB#initializeDBFile(File)}.
*
* @param localDBDir the directory in which to store users and chats
* @since Envoy v0.1-alpha
*/
public LocalDB(File localDBDir) throws IOException {
@ -68,24 +69,23 @@ public class LocalDB {
// Initialize local database directory
if (localDBDir.exists() && !localDBDir.isDirectory())
throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
usersFile = new File(localDBDir, "users.db");
usersFile = new File(localDBDir, "users.db");
}
/**
* Initializes the local database and fills it with values
* if the user has already sent or received messages.
* Creates a database file for a user-specific list of chats.
*
* @param localDBDir the directory where we wish to save/load the database from.
* @throws EnvoyException if the directory selected is not an actual directory.
* @throws NullPointerException if the client user is not yet specified
* @since Envoy v0.1-alpha
*/
public void initializeDBFile() throws EnvoyException {
public void initializeDBFile() {
if (user == null) throw new NullPointerException("Client user is null");
localDBFile = new File(localDBDir, user.getID() + ".db");
localDBFile = new File(localDBDir, user.getID() + ".db");
}
/**
* Saves the database into a file for future use.
* Stores all users to the local database. If the client user is specified, the
* chats related to this user are stored as well.
*
* @throws IOException if something went wrong during saving
* @since Envoy v0.1-alpha
@ -98,30 +98,36 @@ public class LocalDB {
write(localDBFile, chats);
}
/**
* Loads all users that are stored in the local database.
*
* @throws EnvoyException if the loading process failed
* @since Envoy v0.2-alpha
*/
@SuppressWarnings("unchecked")
public void loadUsers() throws ClassNotFoundException, IOException { users = read(usersFile, HashMap.class); }
public void loadUsers() throws EnvoyException { users = read(usersFile, HashMap.class); }
/**
* Loads all chats saved by Envoy for the client user.
*
* @throws EnvoyException if something fails while loading.
* @throws IOException
* @throws ClassNotFoundException
* @throws EnvoyException if the loading process failed
* @since Envoy v0.1-alpha
*/
@SuppressWarnings("unchecked")
public void loadChats() throws ClassNotFoundException, IOException { chats = read(localDBFile, ArrayList.class); }
public void loadChats() throws EnvoyException { chats = read(localDBFile, ArrayList.class); }
private <T> T read(File file, Class<T> serializedClass) throws ClassNotFoundException, IOException {
private <T> T read(File file, Class<T> serializedClass) throws EnvoyException {
if (file == null) throw new NullPointerException("File is null");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
return serializedClass.cast(in.readObject());
} catch (ClassNotFoundException | IOException e) {
throw e;
throw new EnvoyException("Could not load serialized object", e);
}
}
private <T> void write(File file, T obj) throws IOException {
if (file == null) throw new NullPointerException("File is null");
if (obj == null) throw new NullPointerException("Object to serialize is null");
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
@ -306,7 +312,8 @@ public class LocalDB {
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
/**
* @return the users
* @return a {@code Map<String, User>} of all users stored locally with their user names as keys
* @since Envoy v0.2-alpha
*/
public Map<String, User> getUsers() { return users; }

View File

@ -239,8 +239,7 @@ public class ChatWindow extends JFrame {
loadUsersAndChats();
if(client.isOnline())
startSyncThread(Config.getInstance().getSyncTimeout());
if (client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
contentPane.revalidate();
}
@ -342,7 +341,11 @@ public class ChatWindow extends JFrame {
new Thread(() -> {
// Synchronize
localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
try {
localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not perform sync", e);
}
// Process unread messages
localDB.addUnreadMessagesToLocalDB();

View File

@ -97,7 +97,7 @@ public class Startup {
try {
localDB.initializeDBFile();
localDB.loadChats();
} catch (EnvoyException | ClassNotFoundException | IOException e) {
} catch (EnvoyException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",