Skip to content

Commit ddb39d7

Browse files
committed
created a functional configuration system
1 parent 71dca26 commit ddb39d7

File tree

5 files changed

+365
-377
lines changed

5 files changed

+365
-377
lines changed

src/de/sveh/simpleserverclient/client/Client.java

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import java.io.*;
44
import java.net.Socket;
5-
import java.util.Properties;
65

76
import de.sveh.simpleserverclient.annotations.Authorized;
7+
import de.sveh.simpleserverclient.config.IConfiguration;
8+
import de.sveh.simpleserverclient.config.PropertyConfiguration;
89
import de.sveh.simpleserverclient.datapackage.AbstractDataPackage;
910
import de.sveh.simpleserverclient.datapackage.MessageDataPackage;
1011
import de.sveh.simpleserverclient.encoding.AESEncoding;
@@ -29,42 +30,40 @@ public abstract class Client {
2930
private ClientState clientState;
3031
private AESEncoding aesEncoding;
3132

32-
private Properties properties;
33+
private IConfiguration properties;
3334

3435
public Client(String host, int port) {
3536
createClient(host, port);
3637
}
3738

3839
public Client(String host, int port, String propertyFilename) {
39-
boolean read = readProperties(propertyFilename);
40-
if (!read)
41-
ILogger.log(LogType.ERROR, "Could not read the property file");
40+
properties = new PropertyConfiguration(propertyFilename);
4241

4342
createClient(host, port);
4443
}
4544

4645
public Client(String propertyFilename) {
47-
boolean read = readProperties(propertyFilename);
48-
if (read) {
49-
String hostProp = properties.getProperty("host");
50-
String portProp = properties.getProperty("port");
51-
52-
if (hostProp == null) {
53-
ILogger.log(LogType.ERROR, "No host property found");
54-
return;
55-
} else if (portProp == null) {
56-
ILogger.log(LogType.ERROR, "No port property found");
57-
return;
58-
}
46+
properties = new PropertyConfiguration(propertyFilename);
5947

60-
try {
61-
int portNo = Integer.parseInt(portProp);
62-
createClient(hostProp, portNo);
63-
} catch (Exception e) {
64-
ILogger.log(LogType.ERROR, "Port property is not a number");
65-
}
48+
String hostProp = properties.getProperty("host");
49+
String portProp = properties.getProperty("port");
6650

51+
if (hostProp == null) {
52+
ILogger.log(LogType.ERROR, "No host property found");
53+
return;
54+
} else if (portProp == null) {
55+
ILogger.log(LogType.ERROR, "No port property found");
56+
return;
6757
}
58+
59+
try {
60+
int portNo = Integer.parseInt(portProp);
61+
createClient(hostProp, portNo);
62+
} catch (Exception e) {
63+
ILogger.log(LogType.ERROR, "Port property is not a number");
64+
}
65+
66+
6867
}
6968

7069
private void createClient(String host, int port) {
@@ -76,25 +75,10 @@ private void createClient(String host, int port) {
7675
init();
7776
}
7877

79-
80-
private boolean readProperties(String propertyFilename) {
81-
properties = new Properties();
82-
File propertyFile = new File(propertyFilename);
83-
if (!propertyFile.exists() || propertyFile.isDirectory()) return false;
84-
85-
try {
86-
properties.load(new FileInputStream(propertyFile));
87-
} catch (IOException e) {
88-
return false;
89-
}
90-
return true;
91-
}
92-
9378
public String getProperty(String key) {
9479
return properties.getProperty(key);
9580
}
9681

97-
9882
public abstract void onConnected();
9983

10084
public abstract void onDisconnected();
@@ -121,7 +105,7 @@ public void connect() {
121105
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
122106
out = new PrintWriter(socket.getOutputStream(), true);
123107

124-
new Thread(() -> onConnected()).start();
108+
new Thread(this::onConnected).start();
125109

126110
String line;
127111
while ((line = in.readLine()) != null) {
@@ -143,7 +127,7 @@ public void connect() {
143127
System.out.println("Could not connect to the server!");
144128
}
145129

146-
new Thread(() -> onDisconnected()).start();
130+
new Thread(this::onDisconnected).start();
147131
}
148132
}).start();
149133

src/de/sveh/simpleserverclient/config/AbstractFileConfiguration.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.sveh.simpleserverclient.config;
2+
3+
public interface IConfiguration {
4+
boolean save();
5+
boolean addProperty(String key, String value);
6+
String getProperty(String key);
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package de.sveh.simpleserverclient.config;
2+
3+
import java.io.*;
4+
import java.util.Properties;
5+
6+
public class PropertyConfiguration implements IConfiguration {
7+
8+
private Properties properties;
9+
private File file;
10+
11+
public PropertyConfiguration(String fileName) {
12+
this.file = new File(fileName);
13+
properties = new Properties();
14+
InputStream in;
15+
try {
16+
in = new FileInputStream(file);
17+
properties.load(in);
18+
in.close();
19+
} catch (IOException e) {
20+
e.printStackTrace();
21+
}
22+
23+
}
24+
25+
@Override
26+
public boolean addProperty(String key, String value) {
27+
if(properties.contains(key)) return false;
28+
29+
properties.put(key, value);
30+
return true;
31+
}
32+
33+
@Override
34+
public boolean save(){
35+
try {
36+
properties.store(new FileOutputStream(file), null);
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
return false;
40+
}
41+
42+
return true;
43+
}
44+
45+
@Override
46+
public String getProperty(String key) {
47+
return properties.getProperty(key);
48+
}
49+
}

0 commit comments

Comments
 (0)