From d1f202f65353b4ea52f8347e32424dbdc8c35ca6 Mon Sep 17 00:00:00 2001 From: Alejandro Revilla Date: Tue, 30 Jul 2024 21:43:28 -0300 Subject: [PATCH] use synchronizedMap to prevent concurrent modificatrion on channels --- .../src/main/java/org/jpos/iso/ISOServer.java | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOServer.java b/jpos/src/main/java/org/jpos/iso/ISOServer.java index 0ee1af4323..06d6e17fe3 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOServer.java +++ b/jpos/src/main/java/org/jpos/iso/ISOServer.java @@ -29,17 +29,7 @@ import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.EventObject; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Observable; -import java.util.Observer; -import java.util.Random; -import java.util.Vector; +import java.util.*; import org.jpos.core.Configurable; import org.jpos.core.Configuration; @@ -95,7 +85,7 @@ private enum PermLogPolicy { protected Configuration cfg; private boolean shutdown = false; private ServerSocket serverSocket; - private Map channels; + private Map> channels; protected boolean ignoreISOExceptions; protected List serverListeners = null; @@ -118,7 +108,7 @@ public ISOServer(int port, ServerChannel clientSide, ThreadPool pool) { new ThreadPool (1, DEFAULT_MAX_THREADS) : pool; listeners = new Vector(); name = ""; - channels = new HashMap(); + channels = Collections.synchronizedMap(new HashMap<>()); cnt = new int[SIZEOF_CNT]; serverListeners = new ArrayList(); } @@ -266,18 +256,13 @@ private void shutdownChannels () { } } private void purgeChannels () { - Iterator iter = channels.entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry entry = (Map.Entry) iter.next(); - WeakReference ref = (WeakReference) entry.getValue(); - ISOChannel c = (ISOChannel) ref.get (); - if (c == null || !c.isConnected()) { - iter.remove (); - } - } + channels.entrySet().removeIf(entry -> { + WeakReference ref = entry.getValue(); + ISOChannel c = ref.get(); + return c == null || !c.isConnected(); + }); } - @Override public ServerSocket createServerSocket(int port) throws IOException { ServerSocket ss = new ServerSocket();