blob: 36edd81d58156e9916ff77e25a4bed1fece91ed7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow
* http://netcalendar.buetow.org - netcalendar@dev.buetow.org
*/
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 Paul C. 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"),
checkDatabaseDir(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();
}
}
/**
* For checking if the database dir exists. If not, use one upper level
* directory.
* @param sDatabaseDir The database dir
* @return The database dir if exists, else one upper level directory
*/
private static String checkDatabaseDir(String sDatabaseDir) {
if (new File(sDatabaseDir).exists())
return sDatabaseDir;
return "../" + sDatabaseDir;
}
/**
* 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 netcalendar.conf.
* @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());
}
}
}
|