From bbdf17d17b00e6aae4378f9110bcdee7603d2d40 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 13 Nov 2019 05:59:51 +0100 Subject: [PATCH 1/4] Loading config from properties before command line args --- src/main/java/envoy/client/Config.java | 4 +-- src/main/java/envoy/client/ui/Startup.java | 34 ++++++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index c31c972..545df2f 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -32,7 +32,7 @@ public class Config { * * @param properties a {@link Properties} object containing information about * the server and port, as well as the path to the local - * database + * database and the synchronization timeout * @since Envoy v0.1-alpha */ public void load(Properties properties) { @@ -64,8 +64,6 @@ public class Config { case "-db": localDB = new File(args[++i]); } - if (localDB == null) localDB = new File(".\\localDB"); - if (syncTimeout == 0) syncTimeout = 1000; } /** diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index c3a3baa..5a2d92f 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -26,21 +26,24 @@ public class Startup { public static void main(String[] args) { Config config = Config.getInstance(); - if (args.length > 0) { - config.load(args); - } else { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - try { - Properties configProperties = new Properties(); - configProperties.load(loader.getResourceAsStream("client.properties")); - config.load(configProperties); - } catch (IOException e) { - e.printStackTrace(); - } + + // Load the configuration from client.properties first + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + try { + Properties configProperties = new Properties(); + configProperties.load(loader.getResourceAsStream("client.properties")); + config.load(configProperties); + } catch (IOException e) { + e.printStackTrace(); } + // Override configuration values with command line arguments + if (args.length > 0) + config.load(args); + if (!config.isInitialized()) { - System.err.println("Server or port are not defined. Exiting..."); + JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", + JOptionPane.ERROR_MESSAGE); System.exit(1); } @@ -49,16 +52,15 @@ public class Startup { System.err.println("User name is not set or empty. Exiting..."); System.exit(1); } - Client client = new Client(config, userName); - LocalDB localDB = new LocalDB(client.getSender()); + Client client = new Client(config, userName); + LocalDB localDB = new LocalDB(client.getSender()); try { localDB.initializeDBFile(config.getLocalDB()); } catch (EnvoyException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error while loading local database: " + e.toString() + "\nChats will not be stored locally.", - "Local DB error", - JOptionPane.WARNING_MESSAGE); + "Local DB error", JOptionPane.WARNING_MESSAGE); } EventQueue.invokeLater(() -> { From 30d380857c7c2fab772995ce1978b9d228cf047a Mon Sep 17 00:00:00 2001 From: derharry333 Date: Wed, 27 Nov 2019 17:07:25 +0100 Subject: [PATCH 2/4] Replaced print statements with logger statements. --- src/main/java/envoy/client/Client.java | 10 ++++++--- src/main/java/envoy/client/LocalDB.java | 21 +++++++++++-------- src/main/java/envoy/client/ui/ChatWindow.java | 5 ++++- src/main/java/envoy/client/ui/Startup.java | 10 +++++++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 522d44f..5b9a2b8 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,5 +1,7 @@ package envoy.client; +import java.util.logging.Logger; + import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -28,16 +30,18 @@ public class Client { private Config config; private User sender, recipient; + private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); + public Client(Config config, String username) { this.config = config; sender = getUser(username); - System.out.println("ID: " + sender.getID()); + + logger.info("ID: " + sender.getID()); } private R post(String uri, T body, Class responseBodyClass) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); WebTarget target = client.target(uri); - Response response = target.request().post(Entity.entity(body, "application/xml")); R responseBody = response.readEntity(responseBodyClass); response.close(); @@ -92,7 +96,7 @@ public class Client { if (returnSenderSync.getUsers().size() == 1) { returnSender = returnSenderSync.getUsers().get(0); } else { - System.out.println("ERROR exiting..."); + logger.warning("ERROR exiting..."); } return returnSender; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 142b687..8af3094 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -9,6 +9,7 @@ import java.io.ObjectOutputStream; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; @@ -36,6 +37,8 @@ public class LocalDB { private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; + private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + /** * Constructs an empty local database. * @@ -80,13 +83,13 @@ public class LocalDB { localDB.createNewFile(); } catch (IOException e) { e.printStackTrace(); - System.err.println("unable to save the messages"); + logger.warning("unable to save the messages"); } try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { out.writeObject(chats); } catch (IOException ex) { ex.printStackTrace(); - System.err.println("unable to save the messages"); + logger.warning("unable to save the messages"); } } @@ -144,7 +147,7 @@ public class LocalDB { } readMessages.getMessages().clear(); - System.out.println(sync.getMessages().size()); + logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size())); return sync; } @@ -207,20 +210,20 @@ public class LocalDB { if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { // Update local Messages to state READ - System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + "was initialized to be set to READ in localDB."); for (int j = 0; j < getChats().size(); j++) { if (getChats().get(j) .getRecipient() .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { - System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); + logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync .getMessages() .get(i) .getMetadata() .getMessageId()) { - System.out.println("Message with ID: " + logger.info("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); getChats().get(j) @@ -228,7 +231,7 @@ public class LocalDB { .get(k) .getMetadata() .setState(returnSync.getMessages().get(i).getMetadata().getState()); - System.out.println("Message State is now: " + logger.info("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); } } @@ -244,7 +247,7 @@ public class LocalDB { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); - System.out.println(getChats().get(k).getRecipient().getStatus().toString()); + logger.info(getChats().get(k).getRecipient().getStatus().toString()); } } } @@ -342,7 +345,7 @@ public class LocalDB { for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { // addMessageToSync(localDB.getChats().get(i).getModel().get(j)); - System.out.println("Got Waiting Message"); + logger.info("Got Waiting Message"); sync.getMessages().add(0, getChats().get(i).getModel().get(j)); } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2a7ae11..c6fce27 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -56,6 +57,8 @@ public class ChatWindow extends JFrame { private Chat currentChat; private JTextArea messageEnterTextArea; + + private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; @@ -186,7 +189,7 @@ public class ChatWindow extends JFrame { SettingsScreen.open(localDB.getUser().getName()); } catch (Exception e) { SettingsScreen.open(); - System.err.println("An error occured while opening the settings screen: " + e); + logger.warning("An error occured while opening the settings screen: " + e); e.printStackTrace(); } }); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index c3a3baa..0fdd76f 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -3,6 +3,8 @@ package envoy.client.ui; import java.awt.EventQueue; import java.io.IOException; import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JOptionPane; @@ -24,7 +26,11 @@ import envoy.exception.EnvoyException; */ public class Startup { + private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); + public static void main(String[] args) { + logger.setLevel(Level.ALL); + Config config = Config.getInstance(); if (args.length > 0) { config.load(args); @@ -40,13 +46,13 @@ public class Startup { } if (!config.isInitialized()) { - System.err.println("Server or port are not defined. Exiting..."); + logger.warning("Server or port are not defined. Exiting..."); System.exit(1); } String userName = JOptionPane.showInputDialog("Please enter your username"); if (userName == null || userName.isEmpty()) { - System.err.println("User name is not set or empty. Exiting..."); + logger.warning("User name is not set or empty. Exiting..."); System.exit(1); } Client client = new Client(config, userName); From f3078bf7f3ca0ce19d4255f4ec8c2edd0a217721 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 29 Nov 2019 20:48:21 +0100 Subject: [PATCH 3/4] Fixed minor Javadoc errors --- src/main/java/envoy/client/LocalDB.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index a07ba8b..44f0f19 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -47,7 +47,7 @@ public class LocalDB { /** * Constructs an empty local database. * - * @param client the user that is logged in with this client + * @param sender the user that is logged in with this client * @since Envoy v0.1-alpha */ @@ -117,7 +117,9 @@ public class LocalDB { * Creates a {@link Message} object serializable to XML. * * @param textContent The content (text) of the message + * @param recipient The recipient of the message * @return prepared {@link Message} object + * @since Envoy v0.1-alpha */ public Message createMessage(String textContent, User recipient) { Message.Metadata metaData = objectFactory.createMessageMetadata(); From b3befc351456fcb32c9a8f752bfbda68c9446b6b Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 29 Nov 2019 20:54:33 +0100 Subject: [PATCH 4/4] Deleted unnecessary blank line --- src/main/java/envoy/client/LocalDB.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 44f0f19..b1668ad 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -50,7 +50,6 @@ public class LocalDB { * @param sender the user that is logged in with this client * @since Envoy v0.1-alpha */ - public LocalDB(User sender) { this.sender = sender; try {