Skip to content

Commit 5a6b1a6

Browse files
author
Lloyd Watkin
committed
Shift file configuration loading to its own class
1 parent 3cd0349 commit 5a6b1a6

5 files changed

Lines changed: 92 additions & 31 deletions

File tree

src/main/java/org/buddycloud/channelserver/Configuration.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileInputStream;
55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.io.Reader;
78
import java.sql.Connection;
89
import java.sql.DriverManager;
910
import java.sql.PreparedStatement;
@@ -19,7 +20,10 @@
1920

2021
import org.apache.log4j.Logger;
2122
import org.buddycloud.channelserver.channel.LocalDomainChecker;
23+
import org.buddycloud.channelserver.utils.configuration.ConfigurationException;
2224
import org.buddycloud.channelserver.utils.configuration.DatabaseLoader;
25+
import org.buddycloud.channelserver.utils.configuration.FileLoader;
26+
import org.buddycloud.channelserver.utils.configuration.Loader;
2327
import org.xmpp.packet.JID;
2428

2529
public class Configuration extends Properties {
@@ -56,9 +60,7 @@ public class Configuration extends Properties {
5660

5761
public static final String NOTIFICATIONS_SENDTO = "notifications.sendTo";
5862
public static final String NOTIFICATIONS_CONNECTED = "notifications.connected";
59-
60-
private static final String CONFIGURATION_FILE = "configuration.properties";
61-
63+
6264
public static final String PURGE_REMOTE_ON_START = "sync.purge-on-start";
6365

6466
public static final String XMPP_PORT = "xmpp.port";
@@ -78,47 +80,44 @@ public class Configuration extends Properties {
7880

7981
private Properties conf;
8082

83+
private boolean collectionsSetup = false;
84+
8185
private Configuration() {
8286
try {
8387
conf = new Properties();
8488
String databaseConnectionString = System.getenv(DATABASE_ENV);
89+
Loader loader = null;
8590

8691
if (null == databaseConnectionString) {
87-
loadConfigurationFromFile();
92+
loader = new FileLoader(this);
8893
} else {
89-
DatabaseLoader loader = new DatabaseLoader(this, databaseConnectionString);
90-
loader.load();
94+
loader = new DatabaseLoader(this, databaseConnectionString);
9195
}
96+
loader.load();
9297

93-
} catch (Exception e) {
94-
LOGGER.error("Could not load configuration");
98+
} catch (ConfigurationException e) {
99+
LOGGER.error(e.getMessage());
95100
System.exit(1);
96101
}
97102
setupCollections();
98103
}
99104

100-
private void loadConfigurationFromFile() throws IOException {
101-
InputStream confFile = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
102-
if (confFile != null) {
103-
load(confFile);
104-
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from classpath.");
105-
} else {
106-
File f = new File(CONFIGURATION_FILE);
107-
load(new FileInputStream(f));
108-
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from working directory.");
109-
}
110-
}
111-
112105
private void setupCollections() {
106+
if (true == collectionsSetup) {
107+
return;
108+
}
113109
adminUsers = getJIDArrayProperty(CONFIGURATION_ADMIN_USERS);
114110
autosubscribeChannels = getJIDArrayProperty(CONFIGURATION_CHANNELS_AUTOSUBSCRIBE);
111+
collectionsSetup = true;
115112
}
116113

117114
public Collection<JID> getAdminUsers() {
115+
setupCollections();
118116
return adminUsers;
119117
}
120118

121119
public Collection<JID> getAutosubscribeChannels() {
120+
setupCollections();
122121
return autosubscribeChannels;
123122
}
124123

@@ -154,10 +153,6 @@ public void putProperty(String key, String value) {
154153
conf.put(key, value);
155154
}
156155

157-
public void load(InputStream inputStream) throws IOException {
158-
conf.load(inputStream);
159-
}
160-
161156
private Collection<String> getStringArrayProperty(String key) {
162157
String prop = getProperty(key);
163158

@@ -277,4 +272,9 @@ public Object setProperty(String key, String value) {
277272
public void removeKey(String key) {
278273
conf.remove(key);
279274
}
275+
276+
public void load(InputStream stream) throws IOException {
277+
collectionsSetup = false;
278+
conf.load(stream);
279+
}
280280
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.buddycloud.channelserver.utils.configuration;
2+
3+
public class ConfigurationException extends Exception {
4+
5+
public ConfigurationException(String message) {
6+
super(message);
7+
}
8+
9+
}

src/main/java/org/buddycloud/channelserver/utils/configuration/DatabaseLoader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.apache.log4j.Logger;
1010
import org.buddycloud.channelserver.Configuration;
1111

12-
public class DatabaseLoader {
12+
public class DatabaseLoader implements Loader {
1313

1414
private static final Logger LOGGER = Logger.getLogger(DatabaseLoader.class);
1515
private Configuration conf;
@@ -20,7 +20,7 @@ public DatabaseLoader(Configuration conf, String connectionString) {
2020
this.conf = conf;
2121
}
2222

23-
public void load() throws SQLException {
23+
public void load() throws ConfigurationException {
2424

2525
Connection connection = null;
2626
LOGGER.info("Loading configuration from database");
@@ -36,13 +36,15 @@ public void load() throws SQLException {
3636
this.conf.removeKey(Configuration.JDBC_USER);
3737
this.conf.removeKey(Configuration.JDBC_PASSWORD);
3838
} catch (SQLException e) {
39-
e.printStackTrace();
40-
e.getMessage();
41-
LOGGER.error("Could not get configuration from database");
42-
System.exit(1);
39+
throw new ConfigurationException("Could not get configuration from database");
4340
} finally {
44-
if (null != connection) {
41+
if (null == connection) {
42+
return;
43+
}
44+
try {
4545
connection.close();
46+
} catch (SQLException e) {
47+
throw new ConfigurationException("Could not get configuration from database");
4648
}
4749
}
4850
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.buddycloud.channelserver.utils.configuration;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
import org.apache.log4j.Logger;
9+
import org.buddycloud.channelserver.Configuration;
10+
11+
public class FileLoader implements Loader {
12+
13+
private static final Logger LOGGER = Logger.getLogger(FileLoader.class);
14+
15+
private static final String CONFIGURATION_FILE = "configuration.properties";
16+
17+
private Configuration conf;
18+
19+
public FileLoader(Configuration conf) {
20+
this.conf = conf;
21+
}
22+
23+
public void load() throws ConfigurationException {
24+
InputStream confFile = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
25+
try {
26+
if (confFile != null) {
27+
readFile(confFile);
28+
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from classpath.");
29+
} else {
30+
File f = new File(CONFIGURATION_FILE);
31+
readFile(new FileInputStream(f));
32+
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from working directory.");
33+
}
34+
} catch (IOException e) {
35+
throw new ConfigurationException("Could not load configuraton from file " + CONFIGURATION_FILE);
36+
}
37+
}
38+
39+
private void readFile(InputStream inputStream) throws IOException {
40+
this.conf.load(inputStream);
41+
}
42+
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.buddycloud.channelserver.utils.configuration;
2+
3+
public interface Loader {
4+
5+
public void load() throws ConfigurationException;
6+
7+
}

0 commit comments

Comments
 (0)