#3882: Reduce lock boilerplate by using lomboks @Locked
This commit is contained in:
@@ -30,6 +30,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import lombok.Locked;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.CommandSender;
|
import net.md_5.bungee.api.CommandSender;
|
||||||
@@ -98,21 +99,15 @@ public final class PluginManager
|
|||||||
* @param plugin the plugin owning this command
|
* @param plugin the plugin owning this command
|
||||||
* @param command the command to register
|
* @param command the command to register
|
||||||
*/
|
*/
|
||||||
|
@Locked.Write("commandsLock")
|
||||||
public void registerCommand(Plugin plugin, Command command)
|
public void registerCommand(Plugin plugin, Command command)
|
||||||
{
|
{
|
||||||
commandsLock.writeLock().lock();
|
commandMap.put( command.getName().toLowerCase( Locale.ROOT ), command );
|
||||||
try
|
for ( String alias : command.getAliases() )
|
||||||
{
|
{
|
||||||
commandMap.put( command.getName().toLowerCase( Locale.ROOT ), command );
|
commandMap.put( alias.toLowerCase( Locale.ROOT ), command );
|
||||||
for ( String alias : command.getAliases() )
|
|
||||||
{
|
|
||||||
commandMap.put( alias.toLowerCase( Locale.ROOT ), command );
|
|
||||||
}
|
|
||||||
commandsByPlugin.put( plugin, command );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
commandsLock.writeLock().unlock();
|
|
||||||
}
|
}
|
||||||
|
commandsByPlugin.put( plugin, command );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,17 +115,11 @@ public final class PluginManager
|
|||||||
*
|
*
|
||||||
* @param command the command to unregister
|
* @param command the command to unregister
|
||||||
*/
|
*/
|
||||||
|
@Locked.Write("commandsLock")
|
||||||
public void unregisterCommand(Command command)
|
public void unregisterCommand(Command command)
|
||||||
{
|
{
|
||||||
commandsLock.writeLock().lock();
|
while ( commandMap.values().remove( command ) );
|
||||||
try
|
commandsByPlugin.values().remove( command );
|
||||||
{
|
|
||||||
while ( commandMap.values().remove( command ) );
|
|
||||||
commandsByPlugin.values().remove( command );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
commandsLock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,20 +127,14 @@ public final class PluginManager
|
|||||||
*
|
*
|
||||||
* @param plugin the plugin to register the commands of
|
* @param plugin the plugin to register the commands of
|
||||||
*/
|
*/
|
||||||
|
@Locked.Write("commandsLock")
|
||||||
public void unregisterCommands(Plugin plugin)
|
public void unregisterCommands(Plugin plugin)
|
||||||
{
|
{
|
||||||
commandsLock.writeLock().lock();
|
for ( Iterator<Command> it = commandsByPlugin.get( plugin ).iterator(); it.hasNext(); )
|
||||||
try
|
|
||||||
{
|
{
|
||||||
for ( Iterator<Command> it = commandsByPlugin.get( plugin ).iterator(); it.hasNext(); )
|
Command command = it.next();
|
||||||
{
|
while ( commandMap.values().remove( command ) );
|
||||||
Command command = it.next();
|
it.remove();
|
||||||
while ( commandMap.values().remove( command ) );
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
commandsLock.writeLock().unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,21 +450,15 @@ public final class PluginManager
|
|||||||
* @param plugin the owning plugin
|
* @param plugin the owning plugin
|
||||||
* @param listener the listener to register events for
|
* @param listener the listener to register events for
|
||||||
*/
|
*/
|
||||||
|
@Locked("listenersLock")
|
||||||
public void registerListener(Plugin plugin, Listener listener)
|
public void registerListener(Plugin plugin, Listener listener)
|
||||||
{
|
{
|
||||||
listenersLock.lock();
|
for ( Method method : listener.getClass().getDeclaredMethods() )
|
||||||
try
|
|
||||||
{
|
{
|
||||||
for ( Method method : listener.getClass().getDeclaredMethods() )
|
Preconditions.checkArgument( !method.isAnnotationPresent( Subscribe.class ), "Listener %s has registered using deprecated subscribe annotation! Please update to @EventHandler.", listener );
|
||||||
{
|
|
||||||
Preconditions.checkArgument( !method.isAnnotationPresent( Subscribe.class ), "Listener %s has registered using deprecated subscribe annotation! Please update to @EventHandler.", listener );
|
|
||||||
}
|
|
||||||
eventBus.register( listener );
|
|
||||||
listenersByPlugin.put( plugin, listener );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
listenersLock.unlock();
|
|
||||||
}
|
}
|
||||||
|
eventBus.register( listener );
|
||||||
|
listenersByPlugin.put( plugin, listener );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -489,17 +466,11 @@ public final class PluginManager
|
|||||||
*
|
*
|
||||||
* @param listener the listener to unregister
|
* @param listener the listener to unregister
|
||||||
*/
|
*/
|
||||||
|
@Locked("listenersLock")
|
||||||
public void unregisterListener(Listener listener)
|
public void unregisterListener(Listener listener)
|
||||||
{
|
{
|
||||||
listenersLock.lock();
|
eventBus.unregister( listener );
|
||||||
try
|
listenersByPlugin.values().remove( listener );
|
||||||
{
|
|
||||||
eventBus.unregister( listener );
|
|
||||||
listenersByPlugin.values().remove( listener );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
listenersLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -507,19 +478,13 @@ public final class PluginManager
|
|||||||
*
|
*
|
||||||
* @param plugin target plugin
|
* @param plugin target plugin
|
||||||
*/
|
*/
|
||||||
|
@Locked("listenersLock")
|
||||||
public void unregisterListeners(Plugin plugin)
|
public void unregisterListeners(Plugin plugin)
|
||||||
{
|
{
|
||||||
listenersLock.lock();
|
for ( Iterator<Listener> it = listenersByPlugin.get( plugin ).iterator(); it.hasNext(); )
|
||||||
try
|
|
||||||
{
|
{
|
||||||
for ( Iterator<Listener> it = listenersByPlugin.get( plugin ).iterator(); it.hasNext(); )
|
eventBus.unregister( it.next() );
|
||||||
{
|
it.remove();
|
||||||
eventBus.unregister( it.next() );
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
listenersLock.unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,16 +493,10 @@ public final class PluginManager
|
|||||||
*
|
*
|
||||||
* @return commands
|
* @return commands
|
||||||
*/
|
*/
|
||||||
|
@Locked.Read("commandsLock")
|
||||||
public Collection<Map.Entry<String, Command>> getCommands()
|
public Collection<Map.Entry<String, Command>> getCommands()
|
||||||
{
|
{
|
||||||
commandsLock.readLock().lock();
|
return Collections.unmodifiableCollection( commandMap.entrySet() );
|
||||||
try
|
|
||||||
{
|
|
||||||
return Collections.unmodifiableCollection( commandMap.entrySet() );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
commandsLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isTransitiveDepend(PluginDescription plugin, PluginDescription depend)
|
boolean isTransitiveDepend(PluginDescription plugin, PluginDescription depend)
|
||||||
|
@@ -10,6 +10,7 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.locks.ReadWriteLock;
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import lombok.Locked;
|
||||||
import net.md_5.bungee.api.AbstractReconnectHandler;
|
import net.md_5.bungee.api.AbstractReconnectHandler;
|
||||||
import net.md_5.bungee.api.ProxyServer;
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
import net.md_5.bungee.api.config.ServerInfo;
|
import net.md_5.bungee.api.config.ServerInfo;
|
||||||
@@ -53,31 +54,17 @@ public class YamlReconnectHandler extends AbstractReconnectHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Locked.Read("lock")
|
||||||
protected ServerInfo getStoredServer(ProxiedPlayer player)
|
protected ServerInfo getStoredServer(ProxiedPlayer player)
|
||||||
{
|
{
|
||||||
ServerInfo server = null;
|
return ProxyServer.getInstance().getServerInfo( data.get( key( player ) ) );
|
||||||
lock.readLock().lock();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
server = ProxyServer.getInstance().getServerInfo( data.get( key( player ) ) );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
lock.readLock().unlock();
|
|
||||||
}
|
|
||||||
return server;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Locked.Write("lock")
|
||||||
public void setServer(ProxiedPlayer player)
|
public void setServer(ProxiedPlayer player)
|
||||||
{
|
{
|
||||||
lock.writeLock().lock();
|
data.put( key( player ), ( player.getReconnectServer() != null ) ? player.getReconnectServer().getName() : player.getServer().getInfo().getName() );
|
||||||
try
|
|
||||||
{
|
|
||||||
data.put( key( player ), ( player.getReconnectServer() != null ) ? player.getReconnectServer().getName() : player.getServer().getInfo().getName() );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
lock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String key(ProxiedPlayer player)
|
private String key(ProxiedPlayer player)
|
||||||
|
@@ -45,6 +45,7 @@ import java.util.logging.Handler;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Locked;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.Synchronized;
|
import lombok.Synchronized;
|
||||||
import net.md_5.bungee.api.CommandSender;
|
import net.md_5.bungee.api.CommandSender;
|
||||||
@@ -525,18 +526,12 @@ public class BungeeCord extends ProxyServer
|
|||||||
*
|
*
|
||||||
* @param packet the packet to send
|
* @param packet the packet to send
|
||||||
*/
|
*/
|
||||||
|
@Locked.Read("connectionLock")
|
||||||
public void broadcast(DefinedPacket packet)
|
public void broadcast(DefinedPacket packet)
|
||||||
{
|
{
|
||||||
connectionLock.readLock().lock();
|
for ( UserConnection con : connections.values() )
|
||||||
try
|
|
||||||
{
|
{
|
||||||
for ( UserConnection con : connections.values() )
|
con.unsafe().sendPacket( packet );
|
||||||
{
|
|
||||||
con.unsafe().sendPacket( packet );
|
|
||||||
}
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
connectionLock.readLock().unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,17 +593,11 @@ public class BungeeCord extends ProxyServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Locked.Read("connectionLock")
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Collection<ProxiedPlayer> getPlayers()
|
public Collection<ProxiedPlayer> getPlayers()
|
||||||
{
|
{
|
||||||
connectionLock.readLock().lock();
|
return Collections.unmodifiableCollection( new HashSet( connections.values() ) );
|
||||||
try
|
|
||||||
{
|
|
||||||
return Collections.unmodifiableCollection( new HashSet( connections.values() ) );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
connectionLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -618,16 +607,10 @@ public class BungeeCord extends ProxyServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Locked.Read("connectionLock")
|
||||||
public ProxiedPlayer getPlayer(String name)
|
public ProxiedPlayer getPlayer(String name)
|
||||||
{
|
{
|
||||||
connectionLock.readLock().lock();
|
return connections.get( name );
|
||||||
try
|
|
||||||
{
|
|
||||||
return connections.get( name );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
connectionLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserConnection getPlayerByOfflineUUID(UUID uuid)
|
public UserConnection getPlayerByOfflineUUID(UUID uuid)
|
||||||
@@ -647,16 +630,10 @@ public class BungeeCord extends ProxyServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Locked.Read("connectionLock")
|
||||||
public ProxiedPlayer getPlayer(UUID uuid)
|
public ProxiedPlayer getPlayer(UUID uuid)
|
||||||
{
|
{
|
||||||
connectionLock.readLock().lock();
|
return connectionsByUUID.get( uuid );
|
||||||
try
|
|
||||||
{
|
|
||||||
return connectionsByUUID.get( uuid );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
connectionLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -782,21 +759,15 @@ public class BungeeCord extends ProxyServer
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Locked.Write("connectionLock")
|
||||||
public void removeConnection(UserConnection con)
|
public void removeConnection(UserConnection con)
|
||||||
{
|
{
|
||||||
connectionLock.writeLock().lock();
|
// TODO See #1218
|
||||||
try
|
if ( connections.get( con.getName() ) == con )
|
||||||
{
|
{
|
||||||
// TODO See #1218
|
connections.remove( con.getName() );
|
||||||
if ( connections.get( con.getName() ) == con )
|
connectionsByUUID.remove( con.getUniqueId() );
|
||||||
{
|
connectionsByOfflineUUID.remove( con.getPendingConnection().getOfflineId() );
|
||||||
connections.remove( con.getName() );
|
|
||||||
connectionsByUUID.remove( con.getUniqueId() );
|
|
||||||
connectionsByOfflineUUID.remove( con.getPendingConnection().getOfflineId() );
|
|
||||||
}
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
connectionLock.writeLock().unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user