Skip to content

Commit 1b2933c

Browse files
committed
Update storage providers to return futures when initialising them.
1 parent ec0c6a7 commit 1b2933c

2 files changed

Lines changed: 49 additions & 52 deletions

File tree

src/me/armar/plugins/autorank/storage/flatfile/FlatFileStorageProvider.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.time.LocalDate;
1818
import java.time.ZoneId;
1919
import java.util.*;
20+
import java.util.concurrent.CompletableFuture;
2021
import java.util.concurrent.atomic.AtomicInteger;
2122
import java.util.stream.Collectors;
2223
import java.util.stream.Stream;
@@ -36,14 +37,6 @@ public class FlatFileStorageProvider extends StorageProvider {
3637

3738
public FlatFileStorageProvider(Autorank instance) {
3839
super(instance);
39-
40-
// Initialise provider to make it ready for use.
41-
if (!this.initialiseProvider()) {
42-
plugin.debugMessage("There was an error loading storage provider '" + getName() + "'.");
43-
isLoaded = false;
44-
}
45-
46-
isLoaded = true;
4740
}
4841

4942
@Override
@@ -111,18 +104,21 @@ public String getName() {
111104
}
112105

113106
@Override
114-
public boolean initialiseProvider() {
107+
public CompletableFuture<Boolean> initialiseProvider() {
108+
return CompletableFuture.supplyAsync(() -> {
109+
// Load data files
110+
FlatFileStorageProvider.this.loadDataFiles();
115111

116-
// Load data files
117-
this.loadDataFiles();
112+
// Register task for saving data files.
113+
FlatFileStorageProvider.this.registerTasks();
118114

119-
// Register task for saving data files.
120-
this.registerTasks();
115+
// Do calendar check to reset storage files that are outdated.
116+
FlatFileStorageProvider.this.doCalendarCheck();
121117

122-
// Do calendar check to reset storage files that are outdated.
123-
this.doCalendarCheck();
118+
isLoaded = true;
124119

125-
return true;
120+
return true;
121+
});
126122
}
127123

128124
@Override

src/me/armar/plugins/autorank/storage/mysql/MySQLStorageProvider.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,8 @@ public class MySQLStorageProvider extends StorageProvider {
4040

4141
private boolean isLoaded = false;
4242

43-
// TODO implement MySQL provider
4443
public MySQLStorageProvider(Autorank instance) {
4544
super(instance);
46-
47-
// Initialise provider to make it ready for use.
48-
if (!this.initialiseProvider()) {
49-
plugin.debugMessage("There was an error loading storage provider '" + getName() + "'.");
50-
isLoaded = false;
51-
}
52-
53-
isLoaded = true;
5445
}
5546

5647
@Override
@@ -124,23 +115,33 @@ public String getName() {
124115
}
125116

126117
@Override
127-
public boolean initialiseProvider() {
118+
public CompletableFuture<Boolean> initialiseProvider() {
128119

129-
// Load table names
130-
loadTableNames();
120+
return CompletableFuture.supplyAsync(() -> {
121+
// Load table names
122+
loadTableNames();
131123

132-
// Load settings from settings file.
133-
loadMySQLVariables();
124+
// Load settings from settings file and initialize MySQL connection .
125+
try {
126+
if (!loadMySQLVariables().get()) {
127+
return false;
128+
}
129+
} catch (InterruptedException | ExecutionException e) {
130+
e.printStackTrace();
131+
return false;
132+
}
134133

135-
// Check whether loading was successful.
136-
if (mysqlLibrary == null) {
137-
return false;
138-
}
134+
// Check whether loading was successful.
135+
if (mysqlLibrary == null) {
136+
return false;
137+
}
139138

140-
// Load and create tables
141-
createTables();
139+
// Load and create tables
140+
createTables();
142141

143-
return true;
142+
isLoaded = true;
143+
return true;
144+
});
144145
}
145146

146147
@Override
@@ -313,34 +314,34 @@ private void loadTableNames() {
313314
/**
314315
* Grab the credentials defined in the Setting config and initialise connection to MySQL database.
315316
*/
316-
private void loadMySQLVariables() {
317+
private CompletableFuture<Boolean> loadMySQLVariables() {
317318

318-
final SettingsConfig configHandler = plugin.getSettingsConfig();
319+
return CompletableFuture.supplyAsync(() -> {
320+
final SettingsConfig configHandler = plugin.getSettingsConfig();
319321

322+
if (!configHandler.useMySQL()) {
323+
plugin.getLogger().warning("Autorank is trying to register a MySQL storage provider, but MySQL is " +
324+
"disabled in the settings file!");
325+
return false;
326+
}
320327

321-
if (!configHandler.useMySQL()) {
322-
plugin.getLogger().warning("Autorank is trying to register a MySQL storage provider, but MySQL is " +
323-
"disabled in the settings file!");
324-
return;
325-
}
326-
327-
hostname = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.HOSTNAME);
328-
username = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.USERNAME);
329-
password = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.PASSWORD);
330-
database = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.DATABASE);
328+
hostname = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.HOSTNAME);
329+
username = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.USERNAME);
330+
password = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.PASSWORD);
331+
database = configHandler.getMySQLCredentials(SettingsConfig.MySQLCredentials.DATABASE);
331332

332-
mysqlLibrary = new SQLDataStorage(hostname, username, password, database);
333+
mysqlLibrary = new SQLDataStorage(hostname, username, password, database);
333334

334-
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
335335
if (!mysqlLibrary.connect()) {
336336
mysqlLibrary = null;
337337
plugin.getLogger().severe("Could not connect to " + hostname);
338338
plugin.debugMessage(ChatColor.RED + "Could not connect to MYSQL!");
339+
return false;
339340
} else {
340341
plugin.debugMessage(ChatColor.RED + "Successfully established connection to " + hostname);
342+
return true;
341343
}
342344
});
343-
344345
}
345346

346347
/**

0 commit comments

Comments
 (0)