Blocker and Critical level bugfixes throughout the project (#4648)

* fixed https://sonarcloud.io/project/issues?id=org.xmage%3Amage-root&issues=AWIlv32RgrzAwlaaQ7rP&open=AWIlv32RgrzAwlaaQ7rP

* ensure closing of scanner if it was opened

* Refactored method in EmpyrialArchAngel to not always return same value.

* Refactored method in FalkenrathAristocrat to not always return same value.

* Refactored method in GilderBairn to not always return the same value.

* fixed left open resources, ensured quiet closing of the streams

* Refactored method in IceCave to not always return same value.

* Refactored method in KjeldoranRoyalGuard to not always return same value.

* Refactored method in LegionsInitiative to not always return same value.

* Refactored method in NaturesWill to not always return same value.

* added quiet closing method in new streamutils class, used to clean up the connectdialog

* Fix small typo

* added quiet closing to saveobjectutil

* closed resources in savegame method of gamecontroller

* properly close resources in loadGame method of GameReplay class

* further proper resource closing in ServerMessagesUtil

* fixed unclosed resources in copy method in mage framework Copier

* closed unclosed resources in copyCompressed method in Copier

* ensure closing of filewriter in manasymbols

* ensure proper closing of Stream in arcane UI

* ensure closing of datagram socket in arcane Util

* ensure resource closing in deckimport from clipboard

* ensure closing of plugin classloader

* ensured closing of zipinputstream resource

* ensure closing of fileoutputstream in ScryfallSymbolsSource

* ensure closing resources after finishing/canceling download of pictures

* remove commented code

* move locks to try block to ensure unlocking along all execution paths

* remove dangerous instance of double-checked locking

* removed dangerous instance of double checked locking in settingsmanager

* Removed dangerous instance of double-checked locking in ThemePluginImpl

* close resource which did not happen certainly

* close another stream

* ensure closing of inputstream
This commit is contained in:
ArcadeMode 2018-03-22 14:13:13 +01:00 committed by Jeff Wadsworth
parent ec77cecbf6
commit 5ac975c52e
31 changed files with 288 additions and 276 deletions

View file

@ -90,8 +90,8 @@ public class ChatSession {
String userName = clients.get(userId);
if (reason != DisconnectReason.LostConnection) { // for lost connection the user will be reconnected or session expire so no removeUserFromAllTablesAndChat of chat yet
final Lock w = lock.writeLock();
w.lock();
try {
w.lock();
clients.remove(userId);
} finally {
w.unlock();

View file

@ -29,6 +29,7 @@
package mage.server;
import mage.server.util.PluginClassLoader;
import mage.util.StreamUtils;
import java.io.File;
import java.io.IOException;
@ -54,10 +55,12 @@ public final class ExtensionPackageLoader {
String entryPoint = entryPointReader.nextLine().trim();
entryPointReader.close();
PluginClassLoader classLoader = new PluginClassLoader();
for(File f : packagesDirectory.listFiles()) classLoader.addURL(f.toURI().toURL());
PluginClassLoader classLoader = null;
try {
classLoader = new PluginClassLoader();
for(File f : packagesDirectory.listFiles()) {
classLoader.addURL(f.toURI().toURL());
}
return (ExtensionPackage) Class.forName(entryPoint, false, classLoader).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
@ -65,6 +68,8 @@ public final class ExtensionPackageLoader {
throw new RuntimeException("Entry point class not found!", e);
} catch (ClassCastException e) {
throw new RuntimeException("Entry point not an instance of ExtensionPackage.", e);
} finally {
StreamUtils.closeQuietly(classLoader);
}
}
}

View file

@ -231,8 +231,8 @@ public enum UserManager {
}
logger.debug("Users to remove " + toRemove.size());
final Lock w = lock.readLock();
w.lock();
try {
w.lock();
for (User user : toRemove) {
users.remove(user.getId());
}

View file

@ -64,6 +64,7 @@ import mage.server.util.ConfigSettings;
import mage.server.util.Splitter;
import mage.server.util.SystemUtil;
import mage.server.util.ThreadExecutor;
import mage.utils.StreamUtils;
import mage.utils.timer.PriorityTimer;
import mage.view.*;
import mage.view.ChatMessage.MessageColor;
@ -902,17 +903,23 @@ public class GameController implements GameCallback {
}
public boolean saveGame() {
OutputStream file = null;
ObjectOutput output = null;
OutputStream buffer = null;
try {
OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
OutputStream buffer = new BufferedOutputStream(file);
try (ObjectOutput output = new ObjectOutputStream(new GZIPOutputStream(buffer))) {
output.writeObject(game);
output.writeObject(game.getGameStates());
}
file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
buffer = new BufferedOutputStream(file);
output = new ObjectOutputStream(new GZIPOutputStream(buffer));
output.writeObject(game);
output.writeObject(game.getGameStates());
logger.debug("Saved game:" + game.getId());
return true;
} catch (IOException ex) {
logger.fatal("Cannot save game.", ex);
} finally {
StreamUtils.closeQuietly(file);
StreamUtils.closeQuietly(output);
StreamUtils.closeQuietly(buffer);
}
return false;
}

View file

@ -40,6 +40,7 @@ import mage.game.GameState;
import mage.game.GameStates;
import mage.server.Main;
import mage.util.CopierObjectInputStream;
import mage.utils.StreamUtils;
import org.apache.log4j.Logger;
@ -84,21 +85,31 @@ public class GameReplay {
}
private Game loadGame(UUID gameId) {
InputStream file = null;
InputStream buffer = null;
InputStream gzip = null;
ObjectInput input = null;
try{
InputStream file = new FileInputStream("saved/" + gameId.toString() + ".game");
InputStream buffer = new BufferedInputStream(file);
try (ObjectInput input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer))) {
Game loadGame = (Game) input.readObject();
GameStates states = (GameStates) input.readObject();
loadGame.loadGameStates(states);
return loadGame;
}
file = new FileInputStream("saved/" + gameId.toString() + ".game");
buffer = new BufferedInputStream(file);
gzip = new GZIPInputStream(buffer);
input = new CopierObjectInputStream(Main.classLoader, gzip);
Game loadGame = (Game) input.readObject();
GameStates states = (GameStates) input.readObject();
loadGame.loadGameStates(states);
return loadGame;
}
catch(ClassNotFoundException ex) {
logger.fatal("Cannot load game. Class not found.", ex);
}
catch(IOException ex) {
logger.fatal("Cannot load game:" + gameId, ex);
} finally {
StreamUtils.closeQuietly(file);
StreamUtils.closeQuietly(buffer);
StreamUtils.closeQuietly(input);
StreamUtils.closeQuietly(gzip);
}
return null;
}

View file

@ -27,6 +27,7 @@
*/
package mage.server.util;
import mage.utils.StreamUtils;
import org.apache.log4j.Logger;
import java.io.File;
@ -49,7 +50,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
* @author nantuko
*/
public enum ServerMessagesUtil {
instance;
instance;
private static final Logger log = Logger.getLogger(ServerMessagesUtil.class);
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
private ScheduledExecutorService updateExecutor;
@ -147,13 +148,22 @@ instance;
log.warn("Couldn't find server.msg");
return null;
}
Scanner scanner = new Scanner(is);
Scanner scanner = null;
List<String> newMessages = new ArrayList<>();
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
if (!message.trim().isEmpty()) {
newMessages.add(message.trim());
try {
scanner = new Scanner(is);
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
if (!message.trim().isEmpty()) {
newMessages.add(message.trim());
}
}
} catch(Exception e) {
log.error(e,e);
} finally {
StreamUtils.closeQuietly(scanner);
StreamUtils.closeQuietly(is);
}
return newMessages;
}