summaryrefslogtreecommitdiff
path: root/sources/shared/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shared/Main.java')
-rw-r--r--sources/shared/Main.java125
1 files changed, 125 insertions, 0 deletions
diff --git a/sources/shared/Main.java b/sources/shared/Main.java
new file mode 100644
index 0000000..37ebc03
--- /dev/null
+++ b/sources/shared/Main.java
@@ -0,0 +1,125 @@
+package shared;
+
+import java.io.*;
+
+import javax.swing.JFrame;
+
+import server.*;
+
+import client.*;
+
+/**
+ * This class contains the static main method to start the program from.
+ * This class is also responsible for handling info ans status messages and
+ * the logging option of those messages.
+ * @author buetow
+ *
+ */
+public final class Main {
+ private static NetCalendarClient netCalendarClient = null;
+ private static BufferedWriter bufferedLogfileWriter = null;
+
+ /**
+ * This method is the start point of the whole program. Its initializing the static Config object
+ * and starts a calendar server and/or a calendar client thread depending on the "server_run" and
+ * "client_run" configuration options.
+ * @param args Specifies the program arguments (not used yet by the program)
+ */
+ public static void main(String[] args) {
+ Config.initialize(args);
+
+ try {
+ FileWriter fileWriter = new FileWriter(Config.getStringValue("logfile"));
+ bufferedLogfileWriter = new BufferedWriter(fileWriter);
+
+ } catch (IOException e) {
+ infoMessage("Error: Could not open logfile: " + e.getMessage());
+ }
+
+
+ if (Config.getBooleanValue("server_run")) {
+ new NetCalendarServer(
+ Config.getIntValue("server_port"),
+ Config.getStringValue("server_database_dir"));
+ }
+
+ if (Config.getBooleanValue("client_run")) {
+ new Thread(new SplashScreen()).start();
+ //Make sure we have nice window decorations.
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ netCalendarClient = new NetCalendarClient();
+ }
+ }
+
+ /**
+ * All info messages of the calendar client and the calendar server go through this method.
+ * They are not using System.out.* directly.
+ * @param sMessage Specifies the program info message.
+ */
+ public static void infoMessage(String sMessage) {
+ // Later: Add logging to a logfile!
+ System.out.println(sMessage);
+ logMessage(sMessage);
+ }
+
+ /**
+ * This method is for various messages. All messages will show up in the status bar of the client's main window.
+ * If there is no main window, the infoMessage method will be used instead.
+ * @param sMessage Specifies the message to be displayed in the status bar.
+ */
+ public static void statusMessage(String sMessage) {
+ if (netCalendarClient != null)
+ netCalendarClient.statusMessage(sMessage);
+ else
+ infoMessage(sMessage);
+ }
+
+ /**
+ * This method writes a specific message string into a logfile which is specified in the config.txt.
+ * @param sMessage Specified the message string to write into the logfile.
+ */
+ private static void logMessage(String sMessage) {
+ if (bufferedLogfileWriter != null) {
+ try {
+ bufferedLogfileWriter.write(sMessage + "\n");
+ // bufferedLogfileWriter.flush();
+
+ } catch (IOException e) {
+ Main.infoMessage("Error: Could not write to logfile: " + e.toString());
+ }
+ }
+ }
+
+ /**
+ * This method is called thenever the server or the client process wants to exit.
+ * @param iCode Specifies the exit code to use for the System.exit call.
+ */
+ public static void exit(int iCode) {
+ infoMessage("Shutting down the current process");
+
+ try {
+ bufferedLogfileWriter.close();
+ } catch (IOException e) {
+ System.err.println("Error: Could not close logfile: " + e.toString());
+ }
+
+ System.exit(iCode);
+ }
+
+
+ /**
+ * This method executes an external program.
+ * @param sCommand Specifies the command string to execute.
+ */
+ public static void execExternalCommand(String sCommand) {
+ if (sCommand.equals("none") || sCommand.equals(""))
+ return;
+
+ try {
+ Runtime.getRuntime().exec(sCommand);
+
+ } catch (IOException e) {
+ Main.infoMessage("Error while executing external command: " + e.getMessage());
+ }
+ }
+}