diff options
Diffstat (limited to 'libjava/classpath/gnu/CORBA/SocketRepository.java')
-rw-r--r-- | libjava/classpath/gnu/CORBA/SocketRepository.java | 86 |
1 files changed, 66 insertions, 20 deletions
diff --git a/libjava/classpath/gnu/CORBA/SocketRepository.java b/libjava/classpath/gnu/CORBA/SocketRepository.java index a6b99200a48..e48a9a5da17 100644 --- a/libjava/classpath/gnu/CORBA/SocketRepository.java +++ b/libjava/classpath/gnu/CORBA/SocketRepository.java @@ -40,8 +40,9 @@ package gnu.CORBA; import java.net.Socket; import java.net.SocketException; - import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; /** * This class caches the opened sockets that are reused during the @@ -56,9 +57,10 @@ public class SocketRepository * The socket map. */ private static HashMap sockets = new HashMap(); - + /** - * Put a socket. + * Put a socket. This method also discards all not reusable sockets from + * the map. * * @param key as socket key. * @@ -66,7 +68,41 @@ public class SocketRepository */ public static void put_socket(Object key, Socket s) { - sockets.put(key, s); + synchronized (sockets) + { + sockets.put(key, s); + gc(); + } + } + + /** + * Removes all non reusable sockets. As it is private, + * we know we call from the synchronized code already. + */ + private static void gc() + { + Iterator iter = sockets.entrySet().iterator(); + + Map.Entry e; + Socket sx; + + while (iter.hasNext()) + { + e = (Map.Entry) iter.next(); + sx = (Socket) e.getValue(); + + if (not_reusable(sx)) + iter.remove(); + } + } + + /** + * Return true if the socket is no longer reusable. + */ + static boolean not_reusable(Socket s) + { + return (s.isClosed() || !s.isBound() || !s.isConnected() || + s.isInputShutdown() || s.isOutputShutdown()); } /** @@ -75,31 +111,41 @@ public class SocketRepository * @param key a socket key. * * @return an opened socket for reuse, null if no such available or it is - * closed. + * closed, its input or output has been shutown or otherwise the socket is not + * reuseable. */ public static Socket get_socket(Object key) { - Socket s = (Socket) sockets.get(key); - if (s == null) + if (true) return null; - else if (s.isClosed()) - { - sockets.remove(key); - return null; - } - else + + synchronized (sockets) { - sockets.remove(key); - try + Socket s = (Socket) sockets.get(key); + if (s == null) + return null; + + // Ensure that the socket is fully reusable. + else if (not_reusable(s)) { - // Set one minute time out that will be changed later. - s.setSoTimeout(60*1000); + sockets.remove(key); + return null; } - catch (SocketException e) + else { - s = null; + try + { + // Set one minute time out that will be changed later. + s.setSoTimeout(60 * 1000); + } + catch (SocketException e) + { + s = null; + } + + sockets.remove(key); + return s; } - return s; } } }
\ No newline at end of file |