mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-11 11:49:12 +00:00
Prevent some rare cases of NPE and Deadlocks, better error handling on yaml load
This commit is contained in:
parent
019b49ef11
commit
51390a9698
8 changed files with 145 additions and 105 deletions
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
|||
import java.util.logging.Level;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.error.YAMLException;
|
||||
|
||||
|
||||
public abstract class AbstractDelayedYamlFileReader<T extends StorageObject> implements Runnable
|
||||
|
@ -33,9 +34,17 @@ public abstract class AbstractDelayedYamlFileReader<T extends StorageObject> imp
|
|||
try
|
||||
{
|
||||
onStart();
|
||||
try
|
||||
{
|
||||
reader = new FileReader(file);
|
||||
final T object = new YamlStorageReader(reader, plugin).load(clazz);
|
||||
onFinish(object);
|
||||
T object = new YamlStorageReader(reader, plugin).load(clazz);
|
||||
onSuccess(object);
|
||||
}
|
||||
catch (ObjectLoadException ex)
|
||||
{
|
||||
onException();
|
||||
Bukkit.getLogger().log(Level.SEVERE, "File broken: " + file.toString(), ex.getCause());
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
|
@ -57,5 +66,7 @@ public abstract class AbstractDelayedYamlFileReader<T extends StorageObject> imp
|
|||
}
|
||||
}
|
||||
|
||||
public abstract void onFinish(T object);
|
||||
public abstract void onSuccess(T object);
|
||||
|
||||
public abstract void onException();
|
||||
}
|
||||
|
|
|
@ -3,5 +3,5 @@ package com.earth2me.essentials.storage;
|
|||
|
||||
public interface IStorageReader
|
||||
{
|
||||
<T extends StorageObject> T load(final Class<? extends T> clazz);
|
||||
<T extends StorageObject> T load(final Class<? extends T> clazz) throws ObjectLoadException;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.earth2me.essentials.storage;
|
||||
|
||||
|
||||
public class ObjectLoadException extends Exception
|
||||
{
|
||||
public ObjectLoadException(Throwable thrwbl)
|
||||
{
|
||||
super(thrwbl);
|
||||
}
|
||||
}
|
|
@ -4,8 +4,6 @@ import java.io.Reader;
|
|||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.TypeDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
@ -14,8 +12,8 @@ import org.yaml.snakeyaml.constructor.Constructor;
|
|||
|
||||
public class YamlStorageReader implements IStorageReader
|
||||
{
|
||||
private transient static Map<Class, Yaml> preparedYamls = Collections.synchronizedMap(new HashMap<Class, Yaml>());
|
||||
private transient static Map<Class, ReentrantLock> locks = new HashMap<Class, ReentrantLock>();
|
||||
private transient static final Map<Class, Yaml> PREPARED_YAMLS = Collections.synchronizedMap(new HashMap<Class, Yaml>());
|
||||
private transient static final Map<Class, ReentrantLock> LOCKS = new HashMap<Class, ReentrantLock>();
|
||||
private transient final Reader reader;
|
||||
private transient final Plugin plugin;
|
||||
|
||||
|
@ -26,49 +24,40 @@ public class YamlStorageReader implements IStorageReader
|
|||
}
|
||||
|
||||
@Override
|
||||
public <T extends StorageObject> T load(final Class<? extends T> clazz)
|
||||
public <T extends StorageObject> T load(final Class<? extends T> clazz) throws ObjectLoadException
|
||||
{
|
||||
Yaml yaml = preparedYamls.get(clazz);
|
||||
Yaml yaml = PREPARED_YAMLS.get(clazz);
|
||||
if (yaml == null)
|
||||
{
|
||||
yaml = new Yaml(prepareConstructor(clazz));
|
||||
preparedYamls.put(clazz, yaml);
|
||||
PREPARED_YAMLS.put(clazz, yaml);
|
||||
}
|
||||
ReentrantLock lock;
|
||||
synchronized (locks)
|
||||
synchronized (LOCKS)
|
||||
{
|
||||
lock = locks.get(clazz);
|
||||
lock = LOCKS.get(clazz);
|
||||
if (lock == null)
|
||||
{
|
||||
lock = new ReentrantLock();
|
||||
}
|
||||
}
|
||||
T ret;
|
||||
lock.lock();
|
||||
try
|
||||
{
|
||||
ret = (T)yaml.load(reader);
|
||||
T object = (T)yaml.load(reader);
|
||||
if (object == null) {
|
||||
object = clazz.newInstance();
|
||||
}
|
||||
return object;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new ObjectLoadException(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
lock.unlock();
|
||||
}
|
||||
if (ret == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
ret = (T)clazz.newInstance();
|
||||
}
|
||||
catch (InstantiationException ex)
|
||||
{
|
||||
Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
catch (IllegalAccessException ex)
|
||||
{
|
||||
Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private Constructor prepareConstructor(final Class<?> clazz)
|
||||
|
|
|
@ -81,7 +81,6 @@ public class User extends UserBase implements IOfflineUser
|
|||
new UserDataWriter();
|
||||
}
|
||||
|
||||
|
||||
private class UserDataWriter extends AbstractDelayedYamlFileWriter
|
||||
{
|
||||
public UserDataWriter()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.earth2me.essentials;
|
||||
|
||||
import com.earth2me.essentials.settings.Settings;
|
||||
import com.earth2me.essentials.storage.ObjectLoadException;
|
||||
import com.earth2me.essentials.storage.StorageObject;
|
||||
import com.earth2me.essentials.storage.YamlStorageReader;
|
||||
import com.earth2me.essentials.storage.YamlStorageWriter;
|
||||
|
@ -11,7 +12,6 @@ import org.bukkit.World;
|
|||
import org.bukkit.World.Environment;
|
||||
import org.bukkit.plugin.InvalidDescriptionException;
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
|
||||
public class StorageTest extends TestCase
|
||||
|
@ -41,6 +41,8 @@ public class StorageTest extends TestCase
|
|||
|
||||
@Test
|
||||
public void testSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
assertTrue(StorageObject.class.isAssignableFrom(Settings.class));
|
||||
ExecuteTimer ext = new ExecuteTimer();
|
||||
|
@ -70,9 +72,16 @@ public class StorageTest extends TestCase
|
|||
//assertEquals("Default and rewritten config should be equal", settings, settings2);
|
||||
//that assertion fails, because empty list and maps return as null
|
||||
}
|
||||
catch (ObjectLoadException ex)
|
||||
{
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserdata()
|
||||
{
|
||||
try
|
||||
{
|
||||
FakeServer server = new FakeServer();
|
||||
World world = server.createWorld("testWorld", Environment.NORMAL);
|
||||
|
@ -116,8 +125,11 @@ public class StorageTest extends TestCase
|
|||
System.out.println(userdata.toString());
|
||||
System.out.println(userdata2.toString());
|
||||
System.out.println(ext.end());
|
||||
com.earth2me.essentials.user.User test = new com.earth2me.essentials.user.User(null, ess);
|
||||
test.example();
|
||||
}
|
||||
catch (ObjectLoadException ex)
|
||||
{
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ public class EssentialsSpawn extends JavaPlugin
|
|||
}
|
||||
|
||||
spawns = new SpawnStorage(ess);
|
||||
ess.addReloadListener(spawns);
|
||||
|
||||
final EssentialsSpawnPlayerListener playerListener = new EssentialsSpawnPlayerListener(ess, spawns);
|
||||
pluginManager.registerEvent(Type.PLAYER_RESPAWN, playerListener, Priority.Low, this);
|
||||
|
|
|
@ -6,12 +6,14 @@ import com.earth2me.essentials.IEssentialsModule;
|
|||
import com.earth2me.essentials.settings.Spawns;
|
||||
import com.earth2me.essentials.storage.AbstractDelayedYamlFileReader;
|
||||
import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter;
|
||||
import com.earth2me.essentials.storage.ObjectLoadException;
|
||||
import com.earth2me.essentials.storage.StorageObject;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
|
@ -35,6 +37,10 @@ public class SpawnStorage implements IConf, IEssentialsModule
|
|||
rwl.writeLock().lock();
|
||||
try
|
||||
{
|
||||
if (spawns == null)
|
||||
{
|
||||
spawns = new Spawns();
|
||||
}
|
||||
if (spawns.getSpawns() == null)
|
||||
{
|
||||
spawns.setSpawns(new HashMap<String, Location>());
|
||||
|
@ -136,9 +142,21 @@ public class SpawnStorage implements IConf, IEssentialsModule
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onFinish(final Spawns object)
|
||||
public void onSuccess(final Spawns object)
|
||||
{
|
||||
if (object != null)
|
||||
{
|
||||
spawns = object;
|
||||
}
|
||||
rwl.writeLock().unlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException()
|
||||
{
|
||||
if (spawns == null) {
|
||||
spawns = new Spawns();
|
||||
}
|
||||
rwl.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue