diff --git a/src/helma/servlet/AbstractServletClient.java b/src/helma/servlet/AbstractServletClient.java index 563381c4..91e36ed9 100644 --- a/src/helma/servlet/AbstractServletClient.java +++ b/src/helma/servlet/AbstractServletClient.java @@ -85,7 +85,7 @@ public abstract class AbstractServletClient extends HttpServlet { RequestTrans reqtrans = new RequestTrans (method); reqtrans.path = getRequestPath (pathInfo); - try { + try { if (cookies != null) { for (int i=0; i < cookies.length;i++) try { // get Cookies @@ -102,10 +102,10 @@ public abstract class AbstractServletClient extends HttpServlet { if (reqtrans.session == null) { reqtrans.session = Long.toString (Math.round (Math.random ()*Long.MAX_VALUE), 16); reqtrans.session += "@"+Long.toString (System.currentTimeMillis (), 16); - Cookie c = new Cookie("HopSession", reqtrans.session); + Cookie c = new Cookie("HopSession", reqtrans.session); c.setPath ("/"); if (cookieDomain != null) - c.setDomain (cookieDomain); + c.setDomain (cookieDomain); response.addCookie(c); } @@ -136,7 +136,7 @@ public abstract class AbstractServletClient extends HttpServlet { if (paramValues.length > 1) reqtrans.set (nextKey+"_array", paramValues); // set string array } - } + } String contentType = request.getContentType(); if (contentType != null && contentType.indexOf("multipart/form-data")==0) { @@ -168,7 +168,7 @@ public abstract class AbstractServletClient extends HttpServlet { invalidateApp (appID); app = getApp (appID); app.ping (); - restrans = app.execute (reqtrans); + restrans = app.execute (reqtrans); } writeResponse (response, restrans, cookies, protocol); @@ -177,9 +177,9 @@ public abstract class AbstractServletClient extends HttpServlet { try { response.setContentType ("text/html"); Writer out = response.getWriter (); - if (debug) + if (debug) out.write ("Error:
" +x); - else + else out.write ("This server is temporarily unavailable. Please check back later."); out.flush (); } catch (Exception io_e) {} @@ -187,21 +187,21 @@ public abstract class AbstractServletClient extends HttpServlet { } - private void writeResponse (HttpServletResponse res, ResponseTrans trans, Cookie[] cookies, String protocol) { + void writeResponse (HttpServletResponse res, ResponseTrans trans, Cookie[] cookies, String protocol) { for (int i = 0; i < trans.countCookies(); i++) try { - Cookie c = new Cookie(trans.getKeyAt(i), trans.getValueAt(i)); + Cookie c = new Cookie(trans.getKeyAt(i), trans.getValueAt(i)); c.setPath ("/"); if (cookieDomain != null) - c.setDomain (cookieDomain); + c.setDomain (cookieDomain); int expires = trans.getDaysAt(i); if (expires > 0) - c.setMaxAge(expires * 60*60*24); // Cookie time to live, days -> seconds - res.addCookie(c); + c.setMaxAge(expires * 60*60*24); // Cookie time to live, days -> seconds + res.addCookie(c); } catch (Exception ign) {} if (trans.getRedirect () != null) { - try { + try { res.sendRedirect(trans.getRedirect ()); } catch(Exception io_e) {} diff --git a/src/helma/servlet/StandaloneServletClient.java b/src/helma/servlet/StandaloneServletClient.java index e81cb0ef..a81b3329 100644 --- a/src/helma/servlet/StandaloneServletClient.java +++ b/src/helma/servlet/StandaloneServletClient.java @@ -15,35 +15,61 @@ import helma.util.*; /** * This is a standalone Hop servlet client, running a Hop application by itself. */ - -public class StandaloneServletClient extends AbstractServletClient { - + +public final class StandaloneServletClient extends AbstractServletClient { + private Application app = null; private String appName; private String serverProps; + public void init (ServletConfig init) throws ServletException { super.init (init); appName = init.getInitParameter ("application"); serverProps = init.getInitParameter ("serverprops"); - - super.init (init); } - synchronized IRemoteApp getApp (String appID) throws Exception { + IRemoteApp getApp (String appID) { + if (app == null) + createApp (); + return app; + } + + /** + * Create the application. Since we are synchronized only here, we + * do another check if the app already exists and immediately return if it does. + */ + synchronized void createApp () { if (app != null) - return app; + return; try { File propfile = new File (serverProps); File hopHome = new File (propfile.getParent()); SystemProperties sysProps = new SystemProperties (propfile.getAbsolutePath()); app = new Application (appName, hopHome, sysProps, null); + app.init (); app.start (); } catch (Exception x) { System.err.println ("Error starting Application "+appName+": "+x); x.printStackTrace (); } - return app; + } + + + /** + * The servlet is being destroyed. Close and release the application if + * it does exist. + */ + public void destroy () { + if (app != null) { + try { + app.stop (); + } catch (Exception x) { + System.err.println ("Error shutting down app "+app.getName()+": "); + x.printStackTrace (); + } + } + app = null; } void invalidateApp (String appID) { @@ -57,7 +83,7 @@ public class StandaloneServletClient extends AbstractServletClient { String getRequestPath (String path) { // get request path if (path != null) - return trim (path); + return trim (path); else return ""; } @@ -85,23 +111,8 @@ public class StandaloneServletClient extends AbstractServletClient { } + } - - - - - - - - - - - - - - - -