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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
package shared;
import java.util.*;
import java.net.*;
import java.io.*;
/**
* This class makes all the configuration options available to all the other classes and objects
* of this program. Its reading and writing back the values from and to the netcalendar.conf file.
* @author buetow
*
*/
public final class Config {
public static final String VERSION = "NetCalendar 1.1 SVN";
private static final String CONFIG_FILE = "netcalendar.conf";
private static HashMap mapConfigVals;
/**
* Initializes all the config values.
* @param args The start parameters.
*/
public static void initialize(String[] args) {
if (args.length > 0) {
if (args[0].equals("--help")) {
System.out.println(Config.VERSION + " help");
System.out.println("(c) 2006, 2009 by Dipl.-Inform. (FH) Paul C. Buetow [netcalendar@dev.buetow.org]");
System.out.println("This is free software, see the LICENSE.txt for copying conditions. There is NO");
System.out.println("warranty; not even for MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE.");
System.out.println();
System.out.println("Possible start parameters are:");
System.out.println("\t--help Shows this help and exists");
System.out.println("\t--client-only Runs the NetCalendar client only");
System.out.println("\t--server-address=host Specifies the server host to connect to");
System.out.println("\t--server-only Runs the NetCalendar server only");
System.out.println("\t--client-and-server Runs the NetCalendar server and client");
System.out.println("\t within the same process!");
System.out.println();
System.exit(0);
}
}
mapConfigVals = new HashMap();
readConfig(CONFIG_FILE);
for (int i = 0; i < args.length; ++i) {
if (args[i].equals("--server-only")) {
mapConfigVals.put("server_run", "true");
mapConfigVals.put("client_run", "false");
} else if (args[i].equals("--client-only")) {
mapConfigVals.put("server_run", "false");
mapConfigVals.put("client_run", "true");
} else if (args[i].equals("--client-and-server")) {
mapConfigVals.put("server_run", "true");
mapConfigVals.put("client_run", "true");
mapConfigVals.put("server_address", "localhost");
} else if (args[i].startsWith("--server-address=")) {
mapConfigVals.put("server_address", args[i].substring(17));
}
}
if (args.length > 0)
writeConfigToFile();
}
/**
* This methods reads the options from the calendar config file 'netcalendar.conf'.
* @param sConfigFile Specifies the relative or absolute path to the config file.
*/
public static void readConfig(String sConfigFile) {
Main.infoMessage("Config: Using config file " + sConfigFile);
File file = new File(sConfigFile);
if (!file.isFile()) {
Main.infoMessage("Config: " + sConfigFile + " is not a file!");
System.exit(1);
}
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String sLine;
while ((sLine = in.readLine()) != null) {
int iSeperator = sLine.indexOf('=');
if (iSeperator > -1) {
int iEnd = sLine.indexOf(';');
if (iEnd > -1) {
String sKey = sLine.substring(0, iSeperator).trim();
String sVal = sLine.substring(iSeperator+1, iEnd).trim();
mapConfigVals.put(sKey, sVal);
}
}
}
} catch (Exception e) {
Main.infoMessage("Config: " + e.getMessage());
System.exit(1);
}
}
/**
* This method returns the inet address of the calendar server.
* @return Returns the calendar server's address .
*/
public static InetAddress getServerAddress() {
InetAddress serverAddress = null;
try {
serverAddress = InetAddress.getByName(Config.getStringValue("server_address"));
} catch (Exception e) {
Main.infoMessage("Server address error: " + e.getMessage());
}
return serverAddress;
}
/**
* This method returns a specific string config value.
* @param sKey Specifies the key of the config value to return.
* @return Returns the requested config value.
*/
public static String getStringValue(String sKey) {
return getStringValue(sKey, true);
}
/**
* This method returns a specific string config value.
* @param sKey Specifies the key of the config value to return.
* @param bLogMessages Specifies if this action should be logged or not.
* @return Returns the requested config value.
*/
public static String getStringValue(String sKey, boolean bLogMessage) {
String sRet = (String) mapConfigVals.get(sKey);
if (sRet != null)
sRet = sRet.trim();
return sRet;
}
/**
* This method returns a specific integer config value.
* @param sKey Specifies the key of the config value to return.
* @return Returns the requested config value.
*/
public static int getIntValue(String sKey) {
String sVal = (String) mapConfigVals.get(sKey);
if (sVal != null)
sVal = sVal.trim();
return Integer.parseInt(sVal);
}
/**
* This method returns a specific boolean config value.
* @param sKey Specifies the key of the config value to return.
* @return Returns the requested config value.
*/
public static boolean getBooleanValue(String sKey) {
String sVal = (String) mapConfigVals.get(sKey);
if (sVal != null)
sVal = sVal.trim();
boolean bRet = false;
if (sVal != null)
bRet = sVal.equals("true");
return bRet;
}
/**
* This method returns a set of keys of the config hash map.
* @return Returns a Set object containing all keys of the Config's hash map.
*/
public static Set getKeySet() {
return mapConfigVals.keySet();
}
/**
* This method changes or adds a specific config value.
* @param sKey Specifies the config key to set or update.
* @param sVal Specifies the value of the key to set or update.
*/
public static void setValue(String sKey, String sVal) {
mapConfigVals.put(sKey, sVal);
}
/**
* This method returns a sorted String array of all available config keys.
* @return Returns a sorted String array of all available config keys.
*/
public static String [] getSortedKeyArray() {
Set keySet = getKeySet();
Object [] keys = keySet.toArray();
int iNumPairs = keys.length;
String [] sKeys = new String[iNumPairs];
for (int i = 0; i < iNumPairs; ++i)
sKeys[i] = (String) keys[i];
Arrays.sort(sKeys);
return sKeys;
}
/**
* This method writes the current configuration to the config file "netcalendar.conf".
*/
public static void writeConfigToFile() {
String [] sKeys = getSortedKeyArray();
int iNumKeys = sKeys.length;
try {
FileWriter fileWriter = new FileWriter(CONFIG_FILE);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < iNumKeys; ++i) {
String sKey = sKeys[i];
String sVal = getStringValue(sKey);
bufferedWriter.write(sKey + "=" + sVal + ";\n");
}
bufferedWriter.close();
} catch (IOException e) {
Main.infoMessage("Error: " + e.toString());
}
}
}
|