propagate i/o errors to the futures returned by the dao

This commit is contained in:
Luck 2017-12-10 01:47:00 +00:00
parent 9dd4f71526
commit 322b522a52
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
13 changed files with 408 additions and 529 deletions

View File

@ -299,17 +299,21 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
// Load any online users (in the case of a reload)
for (Player player : getServer().getOnlinePlayers()) {
scheduler.doAsync(() -> {
LoginHelper.loadUser(this, player.getUniqueId(), player.getName(), false);
User user = getUserManager().getIfLoaded(getUuidCache().getUUID(player.getUniqueId()));
if (user != null) {
scheduler.doSync(() -> {
try {
LPPermissible lpPermissible = new LPPermissible(player, user, this);
PermissibleInjector.inject(player, lpPermissible);
} catch (Throwable t) {
t.printStackTrace();
}
});
try {
LoginHelper.loadUser(this, player.getUniqueId(), player.getName(), false);
User user = getUserManager().getIfLoaded(getUuidCache().getUUID(player.getUniqueId()));
if (user != null) {
scheduler.doSync(() -> {
try {
LPPermissible lpPermissible = new LPPermissible(player, user, this);
PermissibleInjector.inject(player, lpPermissible);
} catch (Throwable t) {
t.printStackTrace();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
});
}

View File

@ -323,7 +323,7 @@ data:
# This setting controls the maximum number of milliseconds that the plugin will wait for a
# connection from the pool, before timing out.
connection-timeout: 15000 # 15 seconds
connection-timeout: 5000 # 5 seconds
# This setting allows you to define extra properties for connections.
properties:

View File

@ -269,7 +269,7 @@ data:
# This setting controls the maximum number of milliseconds that the plugin will wait for a
# connection from the pool, before timing out.
connection-timeout: 15000 # 15 seconds
connection-timeout: 5000 # 5 seconds
# This setting allows you to define extra properties for connections.
properties:

View File

@ -367,7 +367,7 @@ public class ConfigKeys {
int maxPoolSize = c.getInt("data.pool-settings.maximum-pool-size", c.getInt("data.pool-size", 10));
int minIdle = c.getInt("data.pool-settings.minimum-idle", maxPoolSize);
int maxLifetime = c.getInt("data.pool-settings.maximum-lifetime", 1800000);
int connectionTimeout = c.getInt("data.pool-settings.connection-timeout", 15000);
int connectionTimeout = c.getInt("data.pool-settings.connection-timeout", 5000);
Map<String, String> props = ImmutableMap.copyOf(c.getMap("data.pool-settings.properties", ImmutableMap.of("useUnicode", "true", "characterEncoding", "utf8")));
return new StorageCredentials(

View File

@ -46,6 +46,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@ -86,7 +87,7 @@ public class DependencyManager {
public DependencyManager(LuckPermsPlugin plugin) {
this.plugin = plugin;
try {
this.digest = MessageDigest.getInstance("SHA-1");
this.digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
@ -168,8 +169,11 @@ public class DependencyManager {
}
byte[] hash = this.digest.digest(bytes);
plugin.getLog().info("Successfully downloaded '" + fileName + "' with checksum: " + Base64.getEncoder().encodeToString(hash));
if (!Arrays.equals(hash, dependency.getChecksum())) {
throw new RuntimeException("Downloaded file had an invalid hash.");
throw new RuntimeException("Downloaded file had an invalid hash. Expected: " + Base64.getEncoder().encodeToString(dependency.getChecksum()));
}
Files.write(file.toPath(), bytes);
@ -178,7 +182,6 @@ public class DependencyManager {
if (!file.exists()) {
throw new IllegalStateException("File not present. - " + file.toString());
} else {
plugin.getLog().info("Dependency '" + fileName + "' successfully downloaded.");
return file;
}
}

View File

@ -29,6 +29,8 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import com.google.common.base.Throwables;
import me.lucko.luckperms.api.HeldPermission;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.api.event.cause.CreationCause;
@ -48,8 +50,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.concurrent.CompletionException;
/**
* Converts a {@link AbstractDao} to use {@link CompletableFuture}s
@ -74,8 +77,15 @@ public class AbstractStorage implements Storage {
this.delegate = new ApiStorage(plugin, this);
}
private <T> CompletableFuture<T> makeFuture(Supplier<T> supplier) {
return CompletableFuture.supplyAsync(supplier, dao.getPlugin().getScheduler().async());
private <T> CompletableFuture<T> makeFuture(Callable<T> supplier) {
return CompletableFuture.supplyAsync(() -> {
try {
return supplier.call();
} catch (Exception e) {
Throwables.propagateIfPossible(e);
throw new CompletionException(e);
}
}, dao.getPlugin().getScheduler().async());
}
@Override

View File

@ -61,46 +61,46 @@ public abstract class AbstractDao {
return Collections.emptyMap();
}
public abstract boolean logAction(LogEntry entry);
public abstract boolean logAction(LogEntry entry) throws Exception;
public abstract Log getLog();
public abstract Log getLog() throws Exception;
public abstract boolean applyBulkUpdate(BulkUpdate bulkUpdate);
public abstract boolean applyBulkUpdate(BulkUpdate bulkUpdate) throws Exception;
public abstract boolean loadUser(UUID uuid, String username);
public abstract boolean loadUser(UUID uuid, String username) throws Exception;
public abstract boolean saveUser(User user);
public abstract boolean saveUser(User user) throws Exception;
public abstract Set<UUID> getUniqueUsers();
public abstract Set<UUID> getUniqueUsers() throws Exception;
public abstract List<HeldPermission<UUID>> getUsersWithPermission(String permission);
public abstract List<HeldPermission<UUID>> getUsersWithPermission(String permission) throws Exception;
public abstract boolean createAndLoadGroup(String name);
public abstract boolean createAndLoadGroup(String name) throws Exception;
public abstract boolean loadGroup(String name);
public abstract boolean loadGroup(String name) throws Exception;
public abstract boolean loadAllGroups();
public abstract boolean loadAllGroups() throws Exception;
public abstract boolean saveGroup(Group group);
public abstract boolean saveGroup(Group group) throws Exception;
public abstract boolean deleteGroup(Group group);
public abstract boolean deleteGroup(Group group) throws Exception;
public abstract List<HeldPermission<String>> getGroupsWithPermission(String permission);
public abstract List<HeldPermission<String>> getGroupsWithPermission(String permission) throws Exception;
public abstract boolean createAndLoadTrack(String name);
public abstract boolean createAndLoadTrack(String name) throws Exception;
public abstract boolean loadTrack(String name);
public abstract boolean loadTrack(String name) throws Exception;
public abstract boolean loadAllTracks();
public abstract boolean loadAllTracks() throws Exception;
public abstract boolean saveTrack(Track track);
public abstract boolean saveTrack(Track track) throws Exception;
public abstract boolean deleteTrack(Track track);
public abstract boolean deleteTrack(Track track) throws Exception;
public abstract boolean saveUUIDData(UUID uuid, String username);
public abstract boolean saveUUIDData(UUID uuid, String username) throws Exception;
public abstract UUID getUUID(String username);
public abstract UUID getUUID(String username) throws Exception;
public abstract String getName(UUID uuid);
public abstract String getName(UUID uuid) throws Exception;
}

View File

@ -70,7 +70,13 @@ public class SplitStorageDao extends AbstractDao {
@Override
public void shutdown() {
backing.values().forEach(AbstractDao::shutdown);
for (AbstractDao ds : backing.values()) {
try {
ds.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
@ -84,17 +90,17 @@ public class SplitStorageDao extends AbstractDao {
}
@Override
public boolean logAction(LogEntry entry) {
public boolean logAction(LogEntry entry) throws Exception {
return backing.get(types.get("log")).logAction(entry);
}
@Override
public Log getLog() {
public Log getLog() throws Exception {
return backing.get(types.get("log")).getLog();
}
@Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) throws Exception {
String userType = types.get("user");
String groupType = types.get("group");
@ -108,92 +114,92 @@ public class SplitStorageDao extends AbstractDao {
}
@Override
public boolean loadUser(UUID uuid, String username) {
public boolean loadUser(UUID uuid, String username) throws Exception {
return backing.get(types.get("user")).loadUser(uuid, username);
}
@Override
public boolean saveUser(User user) {
public boolean saveUser(User user) throws Exception {
return backing.get(types.get("user")).saveUser(user);
}
@Override
public Set<UUID> getUniqueUsers() {
public Set<UUID> getUniqueUsers() throws Exception {
return backing.get(types.get("user")).getUniqueUsers();
}
@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) throws Exception {
return backing.get(types.get("user")).getUsersWithPermission(permission);
}
@Override
public boolean createAndLoadGroup(String name) {
public boolean createAndLoadGroup(String name) throws Exception {
return backing.get(types.get("group")).createAndLoadGroup(name);
}
@Override
public boolean loadGroup(String name) {
public boolean loadGroup(String name) throws Exception {
return backing.get(types.get("group")).loadGroup(name);
}
@Override
public boolean loadAllGroups() {
public boolean loadAllGroups() throws Exception {
return backing.get(types.get("group")).loadAllGroups();
}
@Override
public boolean saveGroup(Group group) {
public boolean saveGroup(Group group) throws Exception {
return backing.get(types.get("group")).saveGroup(group);
}
@Override
public boolean deleteGroup(Group group) {
public boolean deleteGroup(Group group) throws Exception {
return backing.get(types.get("group")).deleteGroup(group);
}
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
public List<HeldPermission<String>> getGroupsWithPermission(String permission) throws Exception {
return backing.get(types.get("group")).getGroupsWithPermission(permission);
}
@Override
public boolean createAndLoadTrack(String name) {
public boolean createAndLoadTrack(String name) throws Exception {
return backing.get(types.get("track")).createAndLoadTrack(name);
}
@Override
public boolean loadTrack(String name) {
public boolean loadTrack(String name) throws Exception {
return backing.get(types.get("track")).loadTrack(name);
}
@Override
public boolean loadAllTracks() {
public boolean loadAllTracks() throws Exception {
return backing.get(types.get("track")).loadAllTracks();
}
@Override
public boolean saveTrack(Track track) {
public boolean saveTrack(Track track) throws Exception {
return backing.get(types.get("track")).saveTrack(track);
}
@Override
public boolean deleteTrack(Track track) {
public boolean deleteTrack(Track track) throws Exception {
return backing.get(types.get("track")).deleteTrack(track);
}
@Override
public boolean saveUUIDData(UUID uuid, String username) {
public boolean saveUUIDData(UUID uuid, String username) throws Exception {
return backing.get(types.get("uuid")).saveUUIDData(uuid, username);
}
@Override
public UUID getUUID(String username) {
public UUID getUUID(String username) throws Exception {
return backing.get(types.get("uuid")).getUUID(username);
}
@Override
public String getName(UUID uuid) {
public String getName(UUID uuid) throws Exception {
return backing.get(types.get("uuid")).getName(uuid);
}
}

View File

@ -156,10 +156,10 @@ public abstract class ConfigurateDao extends AbstractDao {
return (dir, name) -> name.endsWith(fileExtension);
}
private boolean reportException(String file, Exception ex) {
private Exception reportException(String file, Exception ex) throws Exception {
plugin.getLog().warn("Exception thrown whilst performing i/o: " + file);
ex.printStackTrace();
return false;
throw ex;
}
private void registerFileAction(StorageLocation type, File file) {
@ -322,68 +322,63 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
try {
if (bulkUpdate.getDataType().isIncludingUsers()) {
File[] files = getDirectory(StorageLocation.USER).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Users directory matched no files.");
}
for (File file : files) {
try {
registerFileAction(StorageLocation.USER, file);
ConfigurationNode object = readFile(file);
Set<NodeModel> nodes = readNodes(object);
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!nodes.equals(results)) {
writeNodes(object, results);
saveFile(file, object);
}
} catch (Exception e) {
reportException(file.getName(), e);
}
}
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) throws Exception {
if (bulkUpdate.getDataType().isIncludingUsers()) {
File[] files = getDirectory(StorageLocation.USER).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Users directory matched no files.");
}
if (bulkUpdate.getDataType().isIncludingGroups()) {
File[] files = getDirectory(StorageLocation.GROUP).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Groups directory matched no files.");
}
for (File file : files) {
try {
registerFileAction(StorageLocation.USER, file);
ConfigurationNode object = readFile(file);
Set<NodeModel> nodes = readNodes(object);
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
for (File file : files) {
try {
registerFileAction(StorageLocation.GROUP, file);
ConfigurationNode object = readFile(file);
Set<NodeModel> nodes = readNodes(object);
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!nodes.equals(results)) {
writeNodes(object, results);
saveFile(file, object);
}
} catch (Exception e) {
reportException(file.getName(), e);
if (!nodes.equals(results)) {
writeNodes(object, results);
saveFile(file, object);
}
} catch (Exception e) {
throw reportException(file.getName(), e);
}
}
}
if (bulkUpdate.getDataType().isIncludingGroups()) {
File[] files = getDirectory(StorageLocation.GROUP).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Groups directory matched no files.");
}
for (File file : files) {
try {
registerFileAction(StorageLocation.GROUP, file);
ConfigurationNode object = readFile(file);
Set<NodeModel> nodes = readNodes(object);
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!nodes.equals(results)) {
writeNodes(object, results);
saveFile(file, object);
}
} catch (Exception e) {
throw reportException(file.getName(), e);
}
}
} catch (Exception e) {
reportException("bulk update", e);
return false;
}
return true;
}
@Override
public boolean loadUser(UUID uuid, String username) {
public boolean loadUser(UUID uuid, String username) throws Exception {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
user.getIoLock().lock();
try {
@ -412,7 +407,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
}
} catch (Exception e) {
return reportException(uuid.toString(), e);
throw reportException(uuid.toString(), e);
} finally {
user.getIoLock().unlock();
}
@ -421,7 +416,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean saveUser(User user) {
public boolean saveUser(User user) throws Exception {
user.getIoLock().lock();
try {
if (!GenericUserManager.shouldSave(user)) {
@ -438,7 +433,7 @@ public abstract class ConfigurateDao extends AbstractDao {
saveFile(StorageLocation.USER, user.getUuid().toString(), data);
}
} catch (Exception e) {
return reportException(user.getUuid().toString(), e);
throw reportException(user.getUuid().toString(), e);
} finally {
user.getIoLock().unlock();
}
@ -456,39 +451,34 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) throws Exception {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
try {
File[] files = getDirectory(StorageLocation.USER).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Users directory matched no files.");
}
File[] files = getDirectory(StorageLocation.USER).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Users directory matched no files.");
}
for (File file : files) {
try {
registerFileAction(StorageLocation.USER, file);
ConfigurationNode object = readFile(file);
UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - fileExtension.length()));
Set<NodeModel> nodes = readNodes(object);
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
for (File file : files) {
try {
registerFileAction(StorageLocation.USER, file);
ConfigurationNode object = readFile(file);
UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - fileExtension.length()));
Set<NodeModel> nodes = readNodes(object);
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
} catch (Exception e) {
reportException(file.getName(), e);
held.add(NodeHeldPermission.of(holder, e));
}
} catch (Exception e) {
throw reportException(file.getName(), e);
}
} catch (Exception e) {
reportException("users", e);
return null;
}
return held.build();
}
@Override
public boolean createAndLoadGroup(String name) {
public boolean createAndLoadGroup(String name) throws Exception {
Group group = plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock();
try {
@ -507,7 +497,7 @@ public abstract class ConfigurateDao extends AbstractDao {
saveFile(StorageLocation.GROUP, name, data);
}
} catch (Exception e) {
return reportException(name, e);
throw reportException(name, e);
} finally {
group.getIoLock().unlock();
}
@ -516,7 +506,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean loadGroup(String name) {
public boolean loadGroup(String name) throws Exception {
Group group = plugin.getGroupManager().getIfLoaded(name);
if (group != null) {
group.getIoLock().lock();
@ -539,7 +529,7 @@ public abstract class ConfigurateDao extends AbstractDao {
group.setEnduringNodes(nodes);
} catch (Exception e) {
return reportException(name, e);
throw reportException(name, e);
} finally {
if (group != null) {
group.getIoLock().unlock();
@ -557,17 +547,30 @@ public abstract class ConfigurateDao extends AbstractDao {
.map(s -> s.substring(0, s.length() - fileExtension.length()))
.collect(Collectors.toList());
groups.forEach(this::loadGroup);
boolean success = true;
for (String g : groups) {
try {
loadGroup(g);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a group");
}
GroupManager gm = plugin.getGroupManager();
gm.getAll().values().stream()
.filter(g -> !groups.contains(g.getName()))
.forEach(gm::unload);
return true;
}
@Override
public boolean saveGroup(Group group) {
public boolean saveGroup(Group group) throws Exception {
group.getIoLock().lock();
try {
ConfigurationNode data = SimpleConfigurationNode.root();
@ -578,7 +581,7 @@ public abstract class ConfigurateDao extends AbstractDao {
saveFile(StorageLocation.GROUP, group.getName(), data);
} catch (Exception e) {
return reportException(group.getName(), e);
throw reportException(group.getName(), e);
} finally {
group.getIoLock().unlock();
}
@ -586,7 +589,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean deleteGroup(Group group) {
public boolean deleteGroup(Group group) throws Exception {
group.getIoLock().lock();
try {
File groupFile = new File(groupsDirectory, group.getName() + fileExtension);
@ -596,7 +599,7 @@ public abstract class ConfigurateDao extends AbstractDao {
groupFile.delete();
}
} catch (Exception e) {
return reportException(group.getName(), e);
throw reportException(group.getName(), e);
} finally {
group.getIoLock().unlock();
}
@ -604,39 +607,34 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
public List<HeldPermission<String>> getGroupsWithPermission(String permission) throws Exception {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
try {
File[] files = getDirectory(StorageLocation.GROUP).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Groups directory matched no files.");
}
File[] files = getDirectory(StorageLocation.GROUP).listFiles(getFileTypeFilter());
if (files == null) {
throw new IllegalStateException("Groups directory matched no files.");
}
for (File file : files) {
try {
registerFileAction(StorageLocation.GROUP, file);
ConfigurationNode object = readFile(file);
String holder = file.getName().substring(0, file.getName().length() - fileExtension.length());
Set<NodeModel> nodes = readNodes(object);
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
for (File file : files) {
try {
registerFileAction(StorageLocation.GROUP, file);
ConfigurationNode object = readFile(file);
String holder = file.getName().substring(0, file.getName().length() - fileExtension.length());
Set<NodeModel> nodes = readNodes(object);
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
} catch (Exception e) {
reportException(file.getName(), e);
held.add(NodeHeldPermission.of(holder, e));
}
} catch (Exception e) {
throw reportException(file.getName(), e);
}
} catch (Exception e) {
reportException("groups", e);
return null;
}
return held.build();
}
@Override
public boolean createAndLoadTrack(String name) {
public boolean createAndLoadTrack(String name) throws Exception {
Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock();
try {
@ -656,7 +654,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
} catch (Exception e) {
return reportException(name, e);
throw reportException(name, e);
} finally {
track.getIoLock().unlock();
}
@ -664,7 +662,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean loadTrack(String name) {
public boolean loadTrack(String name) throws Exception {
Track track = plugin.getTrackManager().getIfLoaded(name);
if (track != null) {
track.getIoLock().lock();
@ -689,7 +687,7 @@ public abstract class ConfigurateDao extends AbstractDao {
track.setGroups(groups);
} catch (Exception e) {
return reportException(name, e);
throw reportException(name, e);
} finally {
if (track != null) {
track.getIoLock().unlock();
@ -706,17 +704,30 @@ public abstract class ConfigurateDao extends AbstractDao {
.map(s -> s.substring(0, s.length() - fileExtension.length()))
.collect(Collectors.toList());
tracks.forEach(this::loadTrack);
boolean success = true;
for (String t : tracks) {
try {
loadTrack(t);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a track");
}
TrackManager tm = plugin.getTrackManager();
tm.getAll().values().stream()
.filter(t -> !tracks.contains(t.getName()))
.forEach(tm::unload);
return true;
}
@Override
public boolean saveTrack(Track track) {
public boolean saveTrack(Track track) throws Exception {
track.getIoLock().lock();
try {
ConfigurationNode data = SimpleConfigurationNode.root();
@ -724,7 +735,7 @@ public abstract class ConfigurateDao extends AbstractDao {
data.getNode("groups").setValue(track.getGroups());
saveFile(StorageLocation.TRACK, track.getName(), data);
} catch (Exception e) {
return reportException(track.getName(), e);
throw reportException(track.getName(), e);
} finally {
track.getIoLock().unlock();
}
@ -732,7 +743,7 @@ public abstract class ConfigurateDao extends AbstractDao {
}
@Override
public boolean deleteTrack(Track track) {
public boolean deleteTrack(Track track) throws Exception {
track.getIoLock().lock();
try {
File trackFile = new File(tracksDirectory, track.getName() + fileExtension);
@ -742,7 +753,7 @@ public abstract class ConfigurateDao extends AbstractDao {
trackFile.delete();
}
} catch (Exception e) {
return reportException(track.getName(), e);
throw reportException(track.getName(), e);
} finally {
track.getIoLock().unlock();
}

View File

@ -88,12 +88,6 @@ public class MongoDao extends AbstractDao {
this.prefix = prefix;
}
private boolean reportException(Exception ex) {
plugin.getLog().warn("Exception thrown whilst performing i/o: ");
ex.printStackTrace();
return false;
}
@Override
public void init() {
MongoCredential credential = null;
@ -151,116 +145,102 @@ public class MongoDao extends AbstractDao {
@Override
public boolean logAction(LogEntry entry) {
try {
MongoCollection<Document> c = database.getCollection(prefix + "action");
Document doc = new Document()
.append("timestamp", entry.getTimestamp())
.append("actor", entry.getActor())
.append("actorName", entry.getActorName())
.append("type", Character.toString(entry.getType().getCode()))
.append("actedName", entry.getActedName())
.append("action", entry.getAction());
MongoCollection<Document> c = database.getCollection(prefix + "action");
Document doc = new Document()
.append("timestamp", entry.getTimestamp())
.append("actor", entry.getActor())
.append("actorName", entry.getActorName())
.append("type", Character.toString(entry.getType().getCode()))
.append("actedName", entry.getActedName())
.append("action", entry.getAction());
if (entry.getActed().isPresent()) {
doc.append("acted", entry.getActed().get());
}
c.insertOne(doc);
} catch (Exception e) {
return reportException(e);
if (entry.getActed().isPresent()) {
doc.append("acted", entry.getActed().get());
}
c.insertOne(doc);
return true;
}
@Override
public Log getLog() {
Log.Builder log = Log.builder();
try {
MongoCollection<Document> c = database.getCollection(prefix + "action");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
MongoCollection<Document> c = database.getCollection(prefix + "action");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
UUID actedUuid = null;
if (d.containsKey("acted")) {
actedUuid = d.get("acted", UUID.class);
}
ExtendedLogEntry e = ExtendedLogEntry.build()
.timestamp(d.getLong("timestamp"))
.actor(d.get("actor", UUID.class))
.actorName(d.getString("actorName"))
.type(LogEntry.Type.valueOf(d.getString("type").toCharArray()[0]))
.acted(actedUuid)
.actedName(d.getString("actedName"))
.action(d.getString("action"))
.build();
log.add(e);
UUID actedUuid = null;
if (d.containsKey("acted")) {
actedUuid = d.get("acted", UUID.class);
}
ExtendedLogEntry e = ExtendedLogEntry.build()
.timestamp(d.getLong("timestamp"))
.actor(d.get("actor", UUID.class))
.actorName(d.getString("actorName"))
.type(LogEntry.Type.valueOf(d.getString("type").toCharArray()[0]))
.acted(actedUuid)
.actedName(d.getString("actedName"))
.action(d.getString("action"))
.build();
log.add(e);
}
} catch (Exception e) {
reportException(e);
return null;
}
return log.build();
}
@Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
try {
if (bulkUpdate.getDataType().isIncludingUsers()) {
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
if (bulkUpdate.getDataType().isIncludingUsers()) {
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
UUID uuid = d.get("_id", UUID.class);
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
UUID uuid = d.get("_id", UUID.class);
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!nodes.equals(results)) {
List<Document> newNodes = results.stream()
.map(MongoDao::nodeToDoc)
.collect(Collectors.toList());
if (!nodes.equals(results)) {
List<Document> newNodes = results.stream()
.map(MongoDao::nodeToDoc)
.collect(Collectors.toList());
d.append("permissions", newNodes).remove("perms");
c.replaceOne(new Document("_id", uuid), d);
}
d.append("permissions", newNodes).remove("perms");
c.replaceOne(new Document("_id", uuid), d);
}
}
}
}
if (bulkUpdate.getDataType().isIncludingGroups()) {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
if (bulkUpdate.getDataType().isIncludingGroups()) {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
String holder = d.getString("_id");
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
String holder = d.getString("_id");
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
Set<NodeModel> results = nodes.stream()
.map(bulkUpdate::apply)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (!nodes.equals(results)) {
List<Document> newNodes = results.stream()
.map(MongoDao::nodeToDoc)
.collect(Collectors.toList());
if (!nodes.equals(results)) {
List<Document> newNodes = results.stream()
.map(MongoDao::nodeToDoc)
.collect(Collectors.toList());
d.append("permissions", newNodes).remove("perms");
c.replaceOne(new Document("_id", holder), d);
}
d.append("permissions", newNodes).remove("perms");
c.replaceOne(new Document("_id", holder), d);
}
}
}
} catch (Exception e) {
reportException(e);
return false;
}
return true;
}
@ -299,8 +279,6 @@ public class MongoDao extends AbstractDao {
}
}
}
} catch (Exception e) {
return reportException(e);
} finally {
user.getIoLock().unlock();
}
@ -318,8 +296,6 @@ public class MongoDao extends AbstractDao {
} else {
return c.replaceOne(new Document("_id", user.getUuid()), userToDoc(user), new UpdateOptions().upsert(true)).wasAcknowledged();
}
} catch (Exception e) {
return reportException(e);
} finally {
user.getIoLock().unlock();
}
@ -328,17 +304,12 @@ public class MongoDao extends AbstractDao {
@Override
public Set<UUID> getUniqueUsers() {
Set<UUID> uuids = new HashSet<>();
try {
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
uuids.add(d.get("_id", UUID.class));
}
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
uuids.add(d.get("_id", UUID.class));
}
} catch (Exception e) {
reportException(e);
return null;
}
return uuids;
}
@ -346,25 +317,20 @@ public class MongoDao extends AbstractDao {
@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
try {
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
UUID holder = d.get("_id", UUID.class);
MongoCollection<Document> c = database.getCollection(prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
UUID holder = d.get("_id", UUID.class);
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
}
}
} catch (Exception e) {
reportException(e);
return null;
}
return held.build();
}
@ -384,8 +350,6 @@ public class MongoDao extends AbstractDao {
c.insertOne(groupToDoc(group));
}
}
} catch (Exception e) {
return reportException(e);
} finally {
group.getIoLock().unlock();
}
@ -408,8 +372,6 @@ public class MongoDao extends AbstractDao {
Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
group.setEnduringNodes(nodes);
}
} catch (Exception e) {
return reportException(e);
} finally {
group.getIoLock().unlock();
}
@ -420,25 +382,33 @@ public class MongoDao extends AbstractDao {
@Override
public boolean loadAllGroups() {
List<String> groups = new ArrayList<>();
try {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
String name = cursor.next().getString("_id");
groups.add(name);
}
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
String name = cursor.next().getString("_id");
groups.add(name);
}
} catch (Exception e) {
reportException(e);
return false;
}
groups.forEach(this::loadGroup);
boolean success = true;
for (String g : groups) {
try {
loadGroup(g);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a group");
}
GroupManager gm = plugin.getGroupManager();
gm.getAll().values().stream()
.filter(g -> !groups.contains(g.getName()))
.forEach(gm::unload);
return true;
}
@ -448,8 +418,6 @@ public class MongoDao extends AbstractDao {
try {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
return c.replaceOne(new Document("_id", group.getName()), groupToDoc(group), new UpdateOptions().upsert(true)).wasAcknowledged();
} catch (Exception e) {
return reportException(e);
} finally {
group.getIoLock().unlock();
}
@ -461,8 +429,6 @@ public class MongoDao extends AbstractDao {
try {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
return c.deleteOne(new Document("_id", group.getName())).wasAcknowledged();
} catch (Exception e) {
return reportException(e);
} finally {
group.getIoLock().unlock();
}
@ -471,25 +437,20 @@ public class MongoDao extends AbstractDao {
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
try {
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
MongoCollection<Document> c = database.getCollection(prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
String holder = d.getString("_id");
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
String holder = d.getString("_id");
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
}
}
} catch (Exception e) {
reportException(e);
return null;
}
return held.build();
}
@ -509,8 +470,6 @@ public class MongoDao extends AbstractDao {
track.setGroups((List<String>) d.get("groups"));
}
}
} catch (Exception e) {
return reportException(e);
} finally {
track.getIoLock().unlock();
}
@ -532,8 +491,6 @@ public class MongoDao extends AbstractDao {
}
return false;
}
} catch (Exception e) {
return reportException(e);
} finally {
track.getIoLock().unlock();
}
@ -542,25 +499,33 @@ public class MongoDao extends AbstractDao {
@Override
public boolean loadAllTracks() {
List<String> tracks = new ArrayList<>();
try {
MongoCollection<Document> c = database.getCollection(prefix + "tracks");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
String name = cursor.next().getString("_id");
tracks.add(name);
}
MongoCollection<Document> c = database.getCollection(prefix + "tracks");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
String name = cursor.next().getString("_id");
tracks.add(name);
}
} catch (Exception e) {
reportException(e);
return false;
}
tracks.forEach(this::loadTrack);
boolean success = true;
for (String t : tracks) {
try {
loadTrack(t);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a track");
}
TrackManager tm = plugin.getTrackManager();
tm.getAll().values().stream()
.filter(t -> !tracks.contains(t.getName()))
.forEach(tm::unload);
return true;
}
@ -570,8 +535,6 @@ public class MongoDao extends AbstractDao {
try {
MongoCollection<Document> c = database.getCollection(prefix + "tracks");
return c.replaceOne(new Document("_id", track.getName()), trackToDoc(track)).wasAcknowledged();
} catch (Exception e) {
return reportException(e);
} finally {
track.getIoLock().unlock();
}
@ -583,8 +546,6 @@ public class MongoDao extends AbstractDao {
try {
MongoCollection<Document> c = database.getCollection(prefix + "tracks");
return c.deleteOne(new Document("_id", track.getName())).wasAcknowledged();
} catch (Exception e) {
return reportException(e);
} finally {
track.getIoLock().unlock();
}
@ -592,45 +553,31 @@ public class MongoDao extends AbstractDao {
@Override
public boolean saveUUIDData(UUID uuid, String username) {
try {
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username.toLowerCase()), new UpdateOptions().upsert(true));
} catch (Exception e) {
return reportException(e);
}
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username.toLowerCase()), new UpdateOptions().upsert(true));
return true;
}
@Override
public UUID getUUID(String username) {
try {
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
try (MongoCursor<Document> cursor = c.find(new Document("name", username.toLowerCase())).iterator()) {
if (cursor.hasNext()) {
return cursor.next().get("_id", UUID.class);
}
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
try (MongoCursor<Document> cursor = c.find(new Document("name", username.toLowerCase())).iterator()) {
if (cursor.hasNext()) {
return cursor.next().get("_id", UUID.class);
}
return null;
} catch (Exception e) {
reportException(e);
return null;
}
return null;
}
@Override
public String getName(UUID uuid) {
try {
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) {
if (cursor.hasNext()) {
return cursor.next().get("name", String.class);
}
MongoCollection<Document> c = database.getCollection(prefix + "uuid");
try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) {
if (cursor.hasNext()) {
return cursor.next().get("name", String.class);
}
return null;
} catch (Exception e) {
reportException(e);
return null;
}
return null;
}
private static Document userToDoc(User user) {

View File

@ -210,7 +210,6 @@ public class SqlDao extends AbstractDao {
} catch (Exception e) {
plugin.getLog().severe("Error occurred whilst initialising the database.");
e.printStackTrace();
shutdown();
}
}
@ -229,7 +228,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean logAction(LogEntry entry) {
public boolean logAction(LogEntry entry) throws SQLException {
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(ACTION_INSERT))) {
ps.setLong(1, entry.getTimestamp());
@ -240,16 +239,13 @@ public class SqlDao extends AbstractDao {
ps.setString(6, entry.getActedName());
ps.setString(7, entry.getAction());
ps.execute();
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public Log getLog() {
public Log getLog() throws SQLException {
final Log.Builder log = Log.builder();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(ACTION_SELECT_ALL))) {
@ -270,51 +266,35 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return log.build();
}
@Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
boolean success = true;
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) throws SQLException {
String queryString = bulkUpdate.buildAsSql();
try (Connection c = provider.getConnection()) {
if (bulkUpdate.getDataType().isIncludingUsers()) {
String table = prefix.apply("{prefix}user_permissions");
try (Statement s = c.createStatement()) {
s.execute(queryString.replace("{table}", table));
} catch (SQLException e) {
e.printStackTrace();
success = false;
}
}
if (bulkUpdate.getDataType().isIncludingGroups()) {
String table = prefix.apply("{prefix}group_permissions");
try (Statement s = c.createStatement()) {
s.execute(queryString.replace("{table}", table));
} catch (SQLException e) {
e.printStackTrace();
success = false;
}
}
} catch (SQLException e) {
e.printStackTrace();
success = false;
}
return success;
return true;
}
@Override
public boolean loadUser(UUID uuid, String username) {
public boolean loadUser(UUID uuid, String username) throws SQLException {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
user.getIoLock().lock();
try {
@ -339,9 +319,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
// Collect user meta (username & primary group)
@ -356,9 +333,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
// update username & primary group
@ -398,7 +372,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean saveUser(User user) {
public boolean saveUser(User user) throws SQLException {
user.getIoLock().lock();
try {
// Empty data - just delete from the DB.
@ -413,9 +387,6 @@ public class SqlDao extends AbstractDao {
ps.setString(2, user.getUuid().toString());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
@ -438,8 +409,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
return false;
}
Set<NodeModel> local = user.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
@ -464,9 +433,6 @@ public class SqlDao extends AbstractDao {
}
ps.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@ -485,9 +451,6 @@ public class SqlDao extends AbstractDao {
}
ps.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@ -518,9 +481,6 @@ public class SqlDao extends AbstractDao {
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
@ -530,7 +490,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public Set<UUID> getUniqueUsers() {
public Set<UUID> getUniqueUsers() throws SQLException {
Set<UUID> uuids = new HashSet<>();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(USER_PERMISSIONS_SELECT_DISTINCT))) {
@ -541,15 +501,12 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return uuids;
}
@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) throws SQLException {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(USER_PERMISSIONS_SELECT_PERMISSION))) {
@ -568,15 +525,12 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return held.build();
}
@Override
public boolean createAndLoadGroup(String name) {
public boolean createAndLoadGroup(String name) throws SQLException {
String query;
switch (provider.getName()) {
case "H2":
@ -598,16 +552,13 @@ public class SqlDao extends AbstractDao {
ps.setString(1, name);
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return loadGroup(name);
}
@Override
public boolean loadGroup(String name) {
public boolean loadGroup(String name) throws SQLException {
// Check the group actually exists
List<String> groups = new ArrayList<>();
try (Connection c = provider.getConnection()) {
@ -619,9 +570,6 @@ public class SqlDao extends AbstractDao {
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (!groups.contains(name)) {
@ -649,9 +597,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (!data.isEmpty()) {
@ -668,7 +613,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean loadAllGroups() {
public boolean loadAllGroups() throws SQLException {
List<String> groups = new ArrayList<>();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_SELECT_ALL))) {
@ -678,30 +623,32 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
boolean success = true;
for (String g : groups) {
if (!loadGroup(g)) {
try {
loadGroup(g);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (success) {
GroupManager gm = plugin.getGroupManager();
gm.getAll().values().stream()
.filter(g -> !groups.contains(g.getName()))
.forEach(gm::unload);
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a group");
}
return success;
GroupManager gm = plugin.getGroupManager();
gm.getAll().values().stream()
.filter(g -> !groups.contains(g.getName()))
.forEach(gm::unload);
return true;
}
@Override
public boolean saveGroup(Group group) {
public boolean saveGroup(Group group) throws SQLException {
group.getIoLock().lock();
try {
// Empty data, just delete.
@ -711,9 +658,6 @@ public class SqlDao extends AbstractDao {
ps.setString(1, group.getName());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
@ -736,9 +680,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
Set<NodeModel> local = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
@ -763,9 +704,6 @@ public class SqlDao extends AbstractDao {
}
ps.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@ -784,9 +722,6 @@ public class SqlDao extends AbstractDao {
}
ps.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@ -797,7 +732,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean deleteGroup(Group group) {
public boolean deleteGroup(Group group) throws SQLException {
group.getIoLock().lock();
try {
try (Connection c = provider.getConnection()) {
@ -810,20 +745,15 @@ public class SqlDao extends AbstractDao {
ps.setString(1, group.getName());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
} finally {
group.getIoLock().unlock();
}
}
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
public List<HeldPermission<String>> getGroupsWithPermission(String permission) throws SQLException {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(GROUP_PERMISSIONS_SELECT_PERMISSION))) {
@ -842,15 +772,12 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return held.build();
}
@Override
public boolean createAndLoadTrack(String name) {
public boolean createAndLoadTrack(String name) throws SQLException {
Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock();
try {
@ -867,9 +794,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (exists.get()) {
@ -884,9 +808,6 @@ public class SqlDao extends AbstractDao {
ps.setString(2, json);
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
@ -897,7 +818,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean loadTrack(String name) {
public boolean loadTrack(String name) throws SQLException {
Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock();
try {
@ -913,9 +834,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
track.setGroups(gson.fromJson(groups.get(), LIST_STRING_TYPE));
@ -927,7 +845,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean loadAllTracks() {
public boolean loadAllTracks() throws SQLException {
List<String> tracks = new ArrayList<>();
try (Connection c = provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(prefix.apply(TRACK_SELECT_ALL))) {
@ -937,29 +855,32 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
boolean success = true;
for (String t : tracks) {
if (!loadTrack(t)) {
try {
loadTrack(t);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
}
if (success) {
TrackManager tm = plugin.getTrackManager();
tm.getAll().values().stream()
.filter(t -> !tracks.contains(t.getName()))
.forEach(tm::unload);
if (!success) {
throw new RuntimeException("Exception occurred whilst loading a track");
}
return success;
TrackManager tm = plugin.getTrackManager();
tm.getAll().values().stream()
.filter(t -> !tracks.contains(t.getName()))
.forEach(tm::unload);
return true;
}
@Override
public boolean saveTrack(Track track) {
public boolean saveTrack(Track track) throws SQLException {
track.getIoLock().lock();
try {
String s = gson.toJson(track.getGroups());
@ -969,9 +890,6 @@ public class SqlDao extends AbstractDao {
ps.setString(2, track.getName());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
} finally {
@ -980,7 +898,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean deleteTrack(Track track) {
public boolean deleteTrack(Track track) throws SQLException {
track.getIoLock().lock();
try {
try (Connection c = provider.getConnection()) {
@ -988,9 +906,6 @@ public class SqlDao extends AbstractDao {
ps.setString(1, track.getName());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
} finally {
track.getIoLock().unlock();
@ -1001,7 +916,7 @@ public class SqlDao extends AbstractDao {
}
@Override
public boolean saveUUIDData(UUID uuid, String username) {
public boolean saveUUIDData(UUID uuid, String username) throws SQLException {
final String u = username.toLowerCase();
AtomicReference<String> remoteUserName = new AtomicReference<>(null);
@ -1012,8 +927,6 @@ public class SqlDao extends AbstractDao {
ps.setString(2, uuid.toString());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
}
try (Connection c = provider.getConnection()) {
@ -1025,9 +938,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (remoteUserName.get() != null) {
@ -1042,9 +952,6 @@ public class SqlDao extends AbstractDao {
ps.setString(2, uuid.toString());
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
} else {
@ -1056,16 +963,13 @@ public class SqlDao extends AbstractDao {
ps.setString(3, "default");
ps.execute();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
}
@Override
public UUID getUUID(String username) {
public UUID getUUID(String username) throws SQLException {
final String u = username.toLowerCase();
final AtomicReference<UUID> uuid = new AtomicReference<>(null);
@ -1078,16 +982,13 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return uuid.get();
}
@Override
public String getName(UUID uuid) {
public String getName(UUID uuid) throws SQLException {
final AtomicReference<String> name = new AtomicReference<>(null);
try (Connection c = provider.getConnection()) {
@ -1099,9 +1000,6 @@ public class SqlDao extends AbstractDao {
}
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return name.get();

View File

@ -41,7 +41,7 @@ import java.util.concurrent.CompletableFuture;
@UtilityClass
public class LoginHelper {
public static User loadUser(LuckPermsPlugin plugin, UUID u, String username, boolean joinUuidSave) {
public static User loadUser(LuckPermsPlugin plugin, UUID u, String username, boolean joinUuidSave) throws Exception {
final long startTime = System.currentTimeMillis();
final UuidCache cache = plugin.getUuidCache();

View File

@ -273,7 +273,7 @@ data {
# This setting controls the maximum number of milliseconds that the plugin will wait for a
# connection from the pool, before timing out.
connection-timeout=15000 # 15 seconds
connection-timeout=5000 # 5 seconds
# This setting allows you to define extra properties for connections.
properties {