Merge branch 'develop' into f/message_notification

This commit is contained in:
Kai S. K. Engelbart 2019-12-05 16:11:28 +01:00 committed by GitHub
commit e4249919ad
5 changed files with 341 additions and 327 deletions

View File

@ -1,5 +1,7 @@
package envoy.client; package envoy.client;
import java.util.logging.Logger;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
@ -28,16 +30,18 @@ public class Client {
private Config config; private Config config;
private User sender, recipient; private User sender, recipient;
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
public Client(Config config, String username) { public Client(Config config, String username) {
this.config = config; this.config = config;
sender = getUser(username); sender = getUser(username);
System.out.println("ID: " + sender.getID());
logger.info("ID: " + sender.getID());
} }
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) { private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
javax.ws.rs.client.Client client = ClientBuilder.newClient(); javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri); WebTarget target = client.target(uri);
Response response = target.request().post(Entity.entity(body, "application/xml")); Response response = target.request().post(Entity.entity(body, "application/xml"));
R responseBody = response.readEntity(responseBodyClass); R responseBody = response.readEntity(responseBodyClass);
response.close(); response.close();
@ -92,7 +96,7 @@ public class Client {
if (returnSenderSync.getUsers().size() == 1) { if (returnSenderSync.getUsers().size() == 1) {
returnSender = returnSenderSync.getUsers().get(0); returnSender = returnSenderSync.getUsers().get(0);
} else { } else {
System.out.println("ERROR exiting..."); logger.warning("ERROR exiting...");
} }
return returnSender; return returnSender;

View File

@ -32,7 +32,7 @@ public class Config {
* *
* @param properties a {@link Properties} object containing information about * @param properties a {@link Properties} object containing information about
* the server and port, as well as the path to the local * the server and port, as well as the path to the local
* database * database and the synchronization timeout
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void load(Properties properties) { public void load(Properties properties) {
@ -64,8 +64,6 @@ public class Config {
case "-db": case "-db":
localDB = new File(args[++i]); localDB = new File(args[++i]);
} }
if (localDB == null) localDB = new File(".\\localDB");
if (syncTimeout == 0) syncTimeout = 1000;
} }
/** /**

View File

@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.logging.Logger;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
@ -57,6 +58,8 @@ public class ChatWindow extends JFrame {
private JTextArea messageEnterTextArea; private JTextArea messageEnterTextArea;
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
public ChatWindow(Client client, LocalDB localDB) { public ChatWindow(Client client, LocalDB localDB) {
this.client = client; this.client = client;
this.localDB = localDB; this.localDB = localDB;
@ -184,7 +187,7 @@ public class ChatWindow extends JFrame {
SettingsScreen.open(localDB.getUser().getName()); SettingsScreen.open(localDB.getUser().getName());
} catch (Exception e) { } catch (Exception e) {
SettingsScreen.open(); 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(); e.printStackTrace();
} }
}); });

View File

@ -3,6 +3,8 @@ package envoy.client.ui;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -24,41 +26,48 @@ import envoy.exception.EnvoyException;
*/ */
public class Startup { public class Startup {
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
public static void main(String[] args) { public static void main(String[] args) {
logger.setLevel(Level.ALL);
Config config = Config.getInstance(); Config config = Config.getInstance();
if (args.length > 0) {
config.load(args); // Load the configuration from client.properties first
} else { ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = Thread.currentThread().getContextClassLoader(); try {
try { Properties configProperties = new Properties();
Properties configProperties = new Properties(); configProperties.load(loader.getResourceAsStream("client.properties"));
configProperties.load(loader.getResourceAsStream("client.properties")); config.load(configProperties);
config.load(configProperties); } catch (IOException e) {
} catch (IOException e) { e.printStackTrace();
e.printStackTrace();
}
} }
// Override configuration values with command line arguments
if (args.length > 0)
config.load(args);
if (!config.isInitialized()) { if (!config.isInitialized()) {
System.err.println("Server or port are not defined. Exiting..."); logger.warning("Server or port are not defined. Exiting...");
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error",
JOptionPane.ERROR_MESSAGE);
System.exit(1); System.exit(1);
} }
String userName = JOptionPane.showInputDialog("Please enter your username"); String userName = JOptionPane.showInputDialog("Please enter your username");
if (userName == null || userName.isEmpty()) { 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); System.exit(1);
} }
Client client = new Client(config, userName); Client client = new Client(config, userName);
LocalDB localDB = new LocalDB(client.getSender()); LocalDB localDB = new LocalDB(client.getSender());
try { try {
localDB.initializeDBFile(config.getLocalDB()); localDB.initializeDBFile(config.getLocalDB());
} catch (EnvoyException e) { } catch (EnvoyException e) {
e.printStackTrace(); e.printStackTrace();
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.", "Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
"Local DB error", "Local DB error", JOptionPane.WARNING_MESSAGE);
JOptionPane.WARNING_MESSAGE);
} }
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {