From 0db8fd2bbd1b53a18d8d74b30c475370b4d99d5b Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 16 Oct 2002 15:28:12 +0000 Subject: [PATCH] Fixed bugs 131: "No retry code in ServletClient and MultiServletClient" and 132: "helma.serlvet.ServletClient stops working after *some* time" And also did some cleanup work. --- src/helma/servlet/MultiServletClient.java | 52 ++++++++++++++--------- src/helma/servlet/ServletClient.java | 26 ++++++++---- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/helma/servlet/MultiServletClient.java b/src/helma/servlet/MultiServletClient.java index 43b635f8..113fff80 100644 --- a/src/helma/servlet/MultiServletClient.java +++ b/src/helma/servlet/MultiServletClient.java @@ -9,7 +9,7 @@ import javax.servlet.http.*; import java.io.*; import java.rmi.Naming; import java.rmi.RemoteException; -import java.util.HashMap; +import java.util.Hashtable; import helma.framework.*; /** @@ -19,12 +19,12 @@ import helma.framework.*; */ public class MultiServletClient extends AbstractServletClient { - - private HashMap apps = null; + + private Hashtable apps; public void init (ServletConfig init) throws ServletException { super.init (init); - apps = new HashMap (); + apps = new Hashtable (); host = init.getInitParameter ("host"); if (host == null) host = "localhost"; @@ -33,25 +33,37 @@ public class MultiServletClient extends AbstractServletClient { hopUrl = "//" + host + ":" + port + "/"; } - ResponseTrans execute (RequestTrans req, String reqPath) throws Exception { - String appID = getAppID (reqPath); - IRemoteApp app = getApp (appID); - req.path = getRequestPath (reqPath); - return app.execute (req); - } - - IRemoteApp getApp (String appID) throws Exception { - IRemoteApp retval = (IRemoteApp) apps.get (appID); - if (retval != null) { - return retval; + public void destroy () { + if (apps != null) { + apps.clear (); + apps = null; } - retval = (IRemoteApp) Naming.lookup (hopUrl + appID); - apps.put (appID, retval); - return retval; } - void invalidateApp (String appID) { - apps.remove (appID); + ResponseTrans execute (RequestTrans req, String reqPath) throws Exception { + String appId = getAppID (reqPath); + IRemoteApp app = getApp (appId); + req.path = getRequestPath (reqPath); + try { + return app.execute (req); + } catch (Exception x) { + invalidateApp (appId); + app = getApp (appId); + return app.execute (req); + } + } + + IRemoteApp getApp (String appId) throws Exception { + IRemoteApp app = (IRemoteApp) apps.get (appId); + if (app != null) + return app; + app = (IRemoteApp) Naming.lookup (hopUrl + appId); + apps.put (appId, app); + return app; + } + + void invalidateApp (String appId) { + apps.remove (appId); } String getAppID (String path) { diff --git a/src/helma/servlet/ServletClient.java b/src/helma/servlet/ServletClient.java index 53f492ff..ed8d8075 100644 --- a/src/helma/servlet/ServletClient.java +++ b/src/helma/servlet/ServletClient.java @@ -30,21 +30,31 @@ public class ServletClient extends AbstractServletClient { String portstr = init.getInitParameter ("port"); port = portstr == null ? 5055 : Integer.parseInt (portstr); hopUrl = "//" + host + ":" + port + "/"; + if (appName == null) + throw new ServletException ("Application name not specified for helma.servlet.ServletClient"); } + public void destroy () { + if (app != null) { + app = null; + } + } + + ResponseTrans execute (RequestTrans req, String reqPath) throws Exception { - IRemoteApp app = getApp (); req.path = getRequestPath (reqPath); - return app.execute (req); + if (app == null) + initApp (); + try { + return app.execute (req); + } catch (Exception x) { + initApp (); + return app.execute (req); + } } - IRemoteApp getApp () throws Exception { - if (app != null) - return app; - if (appName == null) - throw new ServletException ("Helma application name not specified for helma.servlet.ServletClient"); + synchronized void initApp () throws Exception { app = (IRemoteApp) Naming.lookup (hopUrl + appName); - return app; }