private void cacheController(String key, JspController cont) { try { if (!_cacheControllers) return; if (cont == null) return; synchronized (_controllerCache) { if (_controllerCache.containsKey(key)) return; ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream p = new ObjectOutputStream(out); p.writeObject(cont); p.flush(); byte b[] = out.toByteArray(); _controllerCache.put(key, b); out.close(); } } catch (IOException e) { MessageLog.writeErrorMessage( "Error caching controller:" + cont.getClass().getName(), e, this); } }
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { setUpApplicationContext(getServletConfig().getServletContext(), (HttpServletRequest) request); JspController cont = null; boolean sessionKeepAlive = true; try { long time = System.currentTimeMillis(); request.setAttribute(SALMON_SERVLET_KEY, this); sessionKeepAlive = request.getParameter("sessionKeepAlive") != null; if (!_replaceFactoryInit) { Props p = Props.getSystemProps(); _replaceFactoryInit = true; _replaceFactory = p.getBooleanProperty(Props.SYS_REPLACE_JSP_FACTORY, true); _cacheControllers = p.getBooleanProperty(Props.SYS_CACHE_CONTROLLERS, false); MessageLog.writeInfoMessage( "***JspServlet initialized with properties: " + Props.SYS_REPLACE_JSP_FACTORY + "=" + _replaceFactory + ", " + Props.SYS_CACHE_CONTROLLERS + "=" + _cacheControllers + ". To reset, change the System.properties file and restart the server.***", this); } // if (_replaceFactory) { // JspFactory fact = JspFactory.getDefaultFactory(); // if (fact == null || // !fact.getClass().getName().equals("com.salmonllc.jsp.engine.JspFactoryImpl")) // JspFactory.setDefaultFactory(new com.salmonllc.jsp.engine.JspFactoryImpl(fact)); // } HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; if (sessionKeepAlive) com.salmonllc.util.MessageLog.writeInfoMessage( "JspServlet.service() keepAlive - URI=" + req.getRequestURI(), Props.LOG_LEVEL_10, this); else { notifyListeners((HttpServletRequest) request, (HttpServletResponse) response, true); com.salmonllc.util.MessageLog.writeInfoMessage( "JspServlet.service() start - URI=" + req.getRequestURI(), Props.LOG_LEVEL_10, this); } String sessID = req.getParameter(PageTag.getSessionIdentifier()); HttpSession sess = PageTag.getSession(sessID); boolean sessValid = true; if (sess == null) { sessID = req.getRequestedSessionId(); sessValid = req.isRequestedSessionIdValid(); if (!sessValid && sessionKeepAlive) return; sess = req.getSession(true); } boolean onSession = false; boolean sessExp = false; if (sessID != null && !sessValid) sess.setAttribute("AppServer_SessExp", new Boolean(true)); boolean createPage = (req.getHeader(SALMON_CREATE_PAGE_HEADER) != null); if (_replaceFactory) { Object sessToken = sess.getAttribute("AppServer_SessionToken"); if (sessToken == null) { sess.setAttribute("AppServer_SessionToken", new String("tok")); sessToken = sess.getAttribute("AppServer_SessionToken"); } synchronized (sessToken) { String sessName = "$jsp$" + com.salmonllc.jsp.tags.PageTag.generateSessionName(req); loadCachedController(sessName, sess); onSession = sess.getAttribute(sessName) != null; if (!onSession) _jspService(req, new HttpServletResponseDummy(res, null)); cont = (JspController) sess.getAttribute(sessName); generateExpireResponseHeaders(res, cont.getAddExpireHeaders()); cacheController(sessName, cont); cont.setSessionExpired(sessExp); cont.setDoPostRedirected(false); _jspService(req, res); } } else { String sessName = "$jsp$" + com.salmonllc.jsp.tags.PageTag.generateSessionName(req); String token = sessName + "$pageToken$"; try { if (!createPage) { if (sess.getAttribute(token) != null) { /* * srufle : Jun 25, 2004 4 : 26 : 38 PM * This was put in to solve a thread deadlocking issue is was encountering * * Possible enhancements include * - making vars get their values from system parameters */ int index = 0; int indexLimit = 1024; int sleepCount = 0; int sleepCountLimit = 4096; int sleepTime = 10; while (sess.getAttribute(token) != null) { index++; Thread.yield(); if (index >= (indexLimit)) { Thread.sleep(sleepTime); index = 0; sleepCount++; if (sleepCount >= sleepCountLimit) { throw (new ServletException("Thread Locked:Throwing to unlock")); } } } } sess.setAttribute(token, token); } loadCachedController(sessName, sess); onSession = sess.getAttribute(sessName) != null; if (!onSession && !createPage) { createPage(req, res, sess, getPageURL(req, res), sess.getId()); cont = (JspController) sess.getAttribute(sessName); cacheController(sessName, cont); } else cont = (JspController) sess.getAttribute(sessName); if (cont != null) { generateExpireResponseHeaders(res, cont.getAddExpireHeaders()); cont.setSessionExpired(sessExp); cont.setDoPostRedirected(false); synchronized (cont) { _jspService(req, res); } } else { String contextToken = req.getHeader(SALMON_CONTEXT_TOKEN); _jspService(req, res); if (contextToken != null) { TagContext t = (TagContext) req.getAttribute(TagContext.TAG_CONTEXT_REQ_KEY); if (t != null) sess.setAttribute(contextToken, t); } } } catch (Exception e) { if (cont == null || cont.getPortletException() == null) { if (e instanceof SocketException) { // ignore java.net.SocketException MessageLog.writeInfoMessage("SocketException would have been thrown", this); } else { MessageLog.writeErrorMessage("service", e, this); throw (new ServletException(e.getMessage())); } } } finally { if (!createPage) sess.removeAttribute(token); } } if (!sessionKeepAlive) { time = (System.currentTimeMillis() - time); if (!createPage) addPageHit(time); if (Props.getSystemProps().getBooleanProperty(Props.SYS_RECORD_PAGE_TIMERS)) recordTimerActivity(req.getRequestURI(), time, this, false); com.salmonllc.util.MessageLog.writeInfoMessage( "JspServlet.service() end - URI=" + req.getRequestURI() + " Time=" + time + " Init=" + (!onSession), Props.LOG_LEVEL_10, this); } } catch (java.net.SocketException e) { // ignore java.net.SocketException MessageLog.writeInfoMessage("SocketException would have been thrown", this); } catch (ServletException e) { if (cont == null || cont.getPortletException() == null) { com.salmonllc.util.MessageLog.writeErrorMessage("JspServlet.service()", e, this); throw (e); } } catch (IOException e) { com.salmonllc.util.MessageLog.writeErrorMessage("JspServlet.service()", e, this); throw (e); } catch (Exception e) { com.salmonllc.util.MessageLog.writeErrorMessage("JspServlet.service()", e, this); throw (new ServletException(e)); } finally { try { if (!sessionKeepAlive) notifyListeners((HttpServletRequest) request, (HttpServletResponse) response, false); } catch (Exception e) { com.salmonllc.util.MessageLog.writeErrorMessage("JspServlet.service()", e, this); throw (new ServletException(e)); } } }
/** Generates a stub controller for a view. */ public static void generateCode( java.io.PrintWriter p, Hashtable hashFields, JspController cont, String controllerName) { try { PrintWriter pw = p; println(pw, "<BR>"); println(pw, ""); String cName = controllerName; String packageName = ""; if (cName == null || cName.equals("") || cName.equals("com.salmonllc.jsp.JspController")) cName = "GeneratedController"; int pos = cName.lastIndexOf("."); if (pos > 0) { packageName = cName.substring(0, pos); cName = cName.substring(pos + 1); } // create the package statement println(pw, "//package statement"); if (packageName != null) println(pw, PACKAGE + SPACE + packageName + ";"); // create some standard import statements println(pw, ""); println(pw, "//Salmon import statements"); println(pw, IMPORT + SPACE + "com.salmonllc.jsp.*;"); println(pw, IMPORT + SPACE + "com.salmonllc.html.events.*;"); println(pw, ""); println(pw, ""); // Start the Class definition println(pw, "/**"); println(pw, " * " + cName + ": a SOFIA generated controller"); println(pw, " */"); pw.print(CLASSDEF + SPACE + cName + SPACE); pw.print(EXTENDS + SPACE + BASECLASS + SPACE); if (INTERFACES != null && INTERFACES.size() > 0) { pw.print(IMPLEMENTS + SPACE); pw.print(INTERFACES.elementAt(0)); for (int i = 1; i < INTERFACES.size(); i++) { pw.print(COMMA + SPACE); pw.print(INTERFACES.elementAt(i)); } } println(pw, " {"); // Add the variables cont.printVars(pw); // add the Initialize Method println(pw, SPACE); println(pw, "/**"); println( pw, " * Initialize the page. Set up listeners and perform other initialization activities."); println(pw, " */"); println(pw, INITMETHOD + "{"); // For Page Listeners if (INTERFACES.contains(new String("PageListener"))) println(pw, TAB + "addPageListener(this);"); if (hashFields != null) { Enumeration en = hashFields.keys(); while (en.hasMoreElements() && INTERFACES != null) { String key = (String) en.nextElement(); Object obj = hashFields.get(key); // For Buttons if (INTERFACES.contains(new String("SubmitListener"))) { if (obj instanceof com.salmonllc.html.HtmlSubmitButton || obj instanceof com.salmonllc.html.HtmlSubmitImage) println(pw, TAB + computeFieldName(key) + ".addSubmitListener(this);"); } // For Value Change Listeners if (INTERFACES.contains(new String("ValueChangedListener"))) { if (obj instanceof com.salmonllc.html.HtmlCheckBox || obj instanceof com.salmonllc.html.HtmlRadioButton || obj instanceof com.salmonllc.html.HtmlDropDownList) println(pw, TAB + computeFieldName(key) + ".addValueChangeListener(this);"); } // For Tree Listeners if (INTERFACES.contains(new String("TreeListener"))) { if (obj instanceof com.salmonllc.html.HtmlTreeControl) println(pw, TAB + computeFieldName(key) + ".addTreeListener(this);"); } } // while loop } // hashatable not null println(pw, "}"); // add the listeners addListeners(pw); // End the class definition println(pw, ""); println(pw, "}"); pw.flush(); } catch (java.io.IOException e) { System.out.println("Cannot create Java File."); e.printStackTrace(); } }