|
View:
New views
1 Messages
—
Rating Filter:
Alert me
|
|
|
[HippoCMS-svn] [20627] ecm/site-toolkit: HSTTWO-922 add subsite session pool specializationRevision: 20627
Author: aschrijvers Date: 2009-11-11 13:45:31 +0100 (Wed, 11 Nov 2009) Log Message: ----------- HSTTWO-922 add subsite session pool specialization Modified Paths: -------------- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/HSTHttpAttributes.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/base/HstRequestContext.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/domain/DomainMappingFilter.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/jcrsession/JcrSessionFilter.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/mapping/UrlUtilities.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/BinariesServlet.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/StatusServlet.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPool.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPoolManager.java ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/ReadOnlyPooledSession.java Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/HSTHttpAttributes.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/HSTHttpAttributes.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/HSTHttpAttributes.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -29,9 +29,6 @@ @Deprecated public static final String URL_MAPPING_ATTR = "hst.urlmapping"; - @Deprecated - public static final String JCRSESSION_MAPPING_ATTR = "hst.jcrsession.attr"; - //request attributes @Deprecated public static final String CURRENT_PAGE_CONTAINER_NAME_REQ_ATTRIBUTE = "currentPageContainer"; Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/base/HstRequestContext.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/base/HstRequestContext.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/base/HstRequestContext.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -24,6 +24,7 @@ import org.hippoecm.hst.core.mapping.URLMappingManager; import org.hippoecm.hst.core.template.node.PageNode; import org.hippoecm.hst.core.template.node.content.ContentRewriter; +import org.hippoecm.hst.jcr.JcrSessionPoolManager; public class HstRequestContext { @@ -155,5 +156,4 @@ this.contentRewriter = contentRewriter; } - } Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/domain/DomainMappingFilter.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/domain/DomainMappingFilter.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/domain/DomainMappingFilter.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -17,6 +17,7 @@ import java.io.IOException; +import javax.jcr.Session; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -44,6 +45,8 @@ import org.hippoecm.hst.core.mapping.UrlUtilities; import org.hippoecm.hst.core.timers.DomainMappingTimer; import org.hippoecm.hst.core.timers.GeneralTimer; +import org.hippoecm.hst.jcr.JcrSessionPoolManager; +import org.hippoecm.hst.jcr.ReadOnlyPooledSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -163,36 +166,50 @@ * the repository mapping has knowledge of the two main context's that are needed in the hst. One context for the 'hst:configuration' * and one context for the 'hst content'. We'll create these context here and put them on the hstRequestContext */ - + Session session = null; try { - ContextBase contentContextBase = new ContextBase(repositoryMapping.getContentPath(), hstRequestContext.getJcrSession()); - ContextBase hstConfigurationContextBase = new ContextBase(repositoryMapping.getHstConfigPath(), hstRequestContext.getJcrSession()); - hstRequestContext.setContentContextBase(contentContextBase); - hstRequestContext.setHstConfigurationContextBase(hstConfigurationContextBase); - // TODO for backwards compatability for now put the context ctx base also seperately on the request attr. Classes needing the - // content ctx base should not use this anymore but access it through the hstRequestContext - req.setAttribute(HSTHttpAttributes.CURRENT_CONTENT_CONTEXTBASE_REQ_ATTRIBUTE, contentContextBase); - } catch (ContextBaseException e) { - log.warn("Unable to create context base : {}. Cannot compleet request.", e.getMessage()); - ((HttpServletResponse)response).sendError(HttpServletResponse.SC_NOT_FOUND); - return; + session = JcrSessionPoolManager.getInstance().getSession(req, hstRequestContext, repositoryMapping.getPath()); + hstRequestContext.setJcrSession(session); + + try { + ContextBase contentContextBase = new ContextBase(repositoryMapping.getContentPath(), hstRequestContext.getJcrSession()); + ContextBase hstConfigurationContextBase = new ContextBase(repositoryMapping.getHstConfigPath(), hstRequestContext.getJcrSession()); + hstRequestContext.setContentContextBase(contentContextBase); + hstRequestContext.setHstConfigurationContextBase(hstConfigurationContextBase); + + // TODO for backwards compatability for now put the context ctx base also seperately on the request attr. Classes needing the + // content ctx base should not use this anymore but access it through the hstRequestContext + req.setAttribute(HSTHttpAttributes.CURRENT_CONTENT_CONTEXTBASE_REQ_ATTRIBUTE, contentContextBase); + } catch (ContextBaseException e) { + log.warn("Unable to create context base : {}. Cannot compleet request.", e.getMessage()); + ((HttpServletResponse)response).sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + + // set original request on the HstRequestContext and the request uri that matters for the hst + String hstRequestUri = getHstRequestUri(req.getRequestURI(), repositoryMapping, req.getContextPath()); + hstRequestContext.setOriginalRequestUri(req.getRequestURI()); + hstRequestContext.setHstRequestUri(hstRequestUri); + hstRequestContext.setRequest(req); + + if(hstRequestUri.startsWith("/binaries")) { + HttpServletRequest request = new BinariesRequestWrapper(req, repositoryMapping); + log.debug("Forwarding orginal request to binaries servlet: '{}' --> '{}'", req.getRequestURI(),request.getRequestURI()); + RequestDispatcher dispatcher = request.getRequestDispatcher(request.getRequestURI()); + dispatcher.forward(request, response); + return; + } + chain.doFilter(req, response); + + } finally { + if (session != null && session instanceof ReadOnlyPooledSession) { + ((ReadOnlyPooledSession) session).getJcrSessionPool().release((req).getSession()); + } else if( session != null) { + session.logout(); + } } - // set original request on the HstRequestContext and the request uri that matters for the hst - String hstRequestUri = getHstRequestUri(req.getRequestURI(), repositoryMapping, req.getContextPath()); - hstRequestContext.setOriginalRequestUri(req.getRequestURI()); - hstRequestContext.setHstRequestUri(hstRequestUri); - hstRequestContext.setRequest(req); - - if(hstRequestUri.startsWith("/binaries")) { - HttpServletRequest request = new BinariesRequestWrapper(req, repositoryMapping); - log.debug("Forwarding orginal request to binaries servlet: '{}' --> '{}'", req.getRequestURI(),request.getRequestURI()); - RequestDispatcher dispatcher = request.getRequestDispatcher(request.getRequestURI()); - dispatcher.forward(request, response); - return; - } - chain.doFilter(req, response); } } Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/jcrsession/JcrSessionFilter.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/jcrsession/JcrSessionFilter.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/filters/jcrsession/JcrSessionFilter.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -17,7 +17,6 @@ import java.io.IOException; -import javax.jcr.Session; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -26,39 +25,33 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import org.hippoecm.hst.core.HSTHttpAttributes; import org.hippoecm.hst.core.filters.base.HstBaseFilter; import org.hippoecm.hst.core.filters.base.HstRequestContext; -import org.hippoecm.hst.core.timers.GeneralTimer; import org.hippoecm.hst.jcr.JcrSessionPoolManager; -import org.hippoecm.hst.jcr.ReadOnlyPooledSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class JcrSessionFilter extends HstBaseFilter implements Filter { private static final Logger log = LoggerFactory.getLogger(JcrSessionFilter.class); - public static final String JCR_SESSION_POOL_MANAGER_ATTR = JcrSessionFilter.class.getName() + "_ctxAttr"; - + private static final String REFRESH_INTERVAL_MINUTE = "refresh_interval_minutes"; - private final JcrSessionPoolManager jcrSessionPoolManager = new JcrSessionPoolManager(); - + public void init(FilterConfig filterConfig) throws ServletException { String refreshIntervalStr = filterConfig.getInitParameter(REFRESH_INTERVAL_MINUTE); if(refreshIntervalStr != null) { try { int refreshInterval = Integer.parseInt(refreshIntervalStr); - jcrSessionPoolManager.setRefreshInterval(refreshInterval); + JcrSessionPoolManager.getInstance().setRefreshInterval(refreshInterval); } catch(NumberFormatException e) { log.error("Ignoring {} parameter because cannot be parsed to an integer", REFRESH_INTERVAL_MINUTE); } } - filterConfig.getServletContext().setAttribute(JCR_SESSION_POOL_MANAGER_ATTR, jcrSessionPoolManager); } public void destroy() { // dispose all pools and logs out all session in all pools - jcrSessionPoolManager.dispose(); + JcrSessionPoolManager.getInstance().dispose(); } public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, @@ -77,28 +70,7 @@ chain.doFilter(request, response); return; } - GeneralTimer.log.debug("<------ START REQUEST: {} ------> ", request.getRequestURI()); - long requesttime = System.currentTimeMillis(); - Session session = null; - try { - session = jcrSessionPoolManager.getSession(request, hstRequestContext); - - hstRequestContext.setJcrSession(session); - - // TODO remove below: below deprecated, kept for now for - - request.setAttribute(HSTHttpAttributes.JCRSESSION_MAPPING_ATTR, session); - - // ** // - - chain.doFilter(request, response); - } finally { - if (session != null && session instanceof ReadOnlyPooledSession) { - ((ReadOnlyPooledSession) session).getJcrSessionPool().release((request).getSession()); - } - GeneralTimer.log.debug("Total request took " + (System.currentTimeMillis() - requesttime) + " ms."); - GeneralTimer.log.debug("<------ END REQUEST: {} ------> ", request.getRequestURI()); - } + chain.doFilter(request, response); } Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/mapping/UrlUtilities.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/mapping/UrlUtilities.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/mapping/UrlUtilities.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -144,7 +144,7 @@ * @return the jcr decoded and url encoded name */ private static String encodePart(String part) { - if(part == null || "".equals("")) { + if(part == null || "".equals(part)) { return null; } String name = NodeNameCodec.decode(part); Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/BinariesServlet.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/BinariesServlet.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/BinariesServlet.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -47,9 +47,7 @@ private static final long serialVersionUID = 1L; public static final Logger log = LoggerFactory.getLogger(BinariesServlet.class); - - private final JcrSessionPoolManager jcrSessionPoolManager = new JcrSessionPoolManager(); - + public void init(ServletConfig config) throws ServletException { super.init(config); } @@ -75,7 +73,7 @@ path = UrlUtilities.decodeUrl(path); Session session = null; try { - session = jcrSessionPoolManager.getSessionForBinaries(req); + session = JcrSessionPoolManager.getInstance().getSessionForBinaries(req); Item item = session.getItem(path); Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/StatusServlet.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/StatusServlet.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/core/servlets/StatusServlet.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -29,6 +29,8 @@ import org.hippoecm.hst.caching.Cache; import org.hippoecm.hst.caching.CacheManagerImpl; +import org.hippoecm.hst.jcr.JcrSessionPool; +import org.hippoecm.hst.jcr.JcrSessionPoolManager; public class StatusServlet extends HttpServlet { @@ -80,17 +82,24 @@ writer.println(" <span STYLE=\"font-size: 18pt;margin-right:25px;\">Cache Manager</span>"); writer.println(" <a href=\"?view=system\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">System Properties</span></a>"); writer.println(" <a href=\"?view=request\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Request Headers</span></a>"); + writer.println(" <a href=\"?view=sessionview\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">JCR Session stats</span></a>"); } else if(view.equals("system")) { writer.println(" <a href=\"?view=cache\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Cache Manager</span></a>"); writer.println(" <span STYLE=\"font-size: 18pt;margin-right:25px;\">System Properties</span>"); writer.println(" <a href=\"?view=request\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Request Headers</span></a>"); + writer.println(" <a href=\"?view=sessionview\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">JCR Session stats</span></a>"); } else if(view.equals("request")) { writer.println(" <a href=\"?view=cache\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Cache Manager</span></a>"); writer.println(" <a href=\"?view=system\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">System Properties</span></a>"); writer.println(" <span STYLE=\"font-size: 18pt;margin-right:25px;\">Request Headers</span>"); + writer.println(" <a href=\"?view=sessionview\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">JCR Session stats</span></a>"); + } else if(view.equals("sessionview")) { + writer.println(" <a href=\"?view=cache\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Cache Manager</span></a>"); + writer.println(" <a href=\"?view=system\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">System Properties</span></a>"); + writer.println(" <a href=\"?view=request\"><span STYLE=\"font-size: 18pt;margin-right:25px;\">Request Headers</span></a>"); + writer.println(" <span STYLE=\"font-size: 18pt;margin-right:25px;\">JCR Session stats</span>"); } - if(view == null || view.equals("cache")) { writer.println(" <table cellpadding=\"5px;\" style=\"background-color:grey\" >"); @@ -171,7 +180,7 @@ writer.println("<tr><td>"+prop.getKey()+"</td><td>"+prop.getValue()+"</td></tr>"); } - writer.println("</<table>"); + writer.println("</table>"); } else if( view.equals("request")) { writer.println(" <table cellpadding=\"5px;\" >"); @@ -207,8 +216,39 @@ } - writer.println("</<table>"); - } + writer.println("</table>"); + } else if( view.equals("sessionview")) { + + JcrSessionPoolManager jcrSessionPoolManager = JcrSessionPoolManager.getInstance(); + String dirty = req.getParameter("dirty"); + if(dirty != null) { + if("all".equals(dirty)) { + jcrSessionPoolManager.markAllDirty(); + } else { + jcrSessionPoolManager.markDirty(dirty); + } + } + writer.println("<p>Marking dirty means that all existing sessions, idle & active will be marked as dirty and cleaned when the session pool manager cleanup thread runs (every 60 seconds)</p>"); + writer.println(" <table cellpadding=\"5px;\" style=\"background-color:grey\" >"); + writer.println ( "<tr><td valign=\"top\"><b>Session Pool Name</b></td><td><b>Active</b></td><td><b>Idle</b></td>" + + "<td> mark dirty: <a href=?view=sessionview&dirty=all style=color:white>dirty all </td>" ); + + int i = 0; + int totalActive = 0; + int totalIdle = 0; + for(Entry<String,JcrSessionPool> entry : jcrSessionPoolManager.getJcrSessionPools().entrySet()) { + totalActive += entry.getValue().getActiveCount(); + totalIdle += entry.getValue().getIdleCount(); + writer.println ( "<tr><td style="+getColor(i%2, true)+">"+entry.getKey()+"</td><td style="+getColor(i%2, true)+">"+entry.getValue().getActiveCount()+"</td><td style="+getColor(i%2, true)+">"+entry.getValue().getIdleCount()+"</td>" + + "<td style="+getColor(i%2, true)+"> <a href=?view=sessionview&dirty="+entry.getKey()+" style=color:black>mark dirty</a> </td>" ); + i++; + } + + writer.println ( "<tr><td valign=\"top\"><b>TOTAL </b></td><td><b>"+totalActive+"</b></td><td><b>"+totalIdle+"</b></td>" + + "<td></td>" ); + + writer.println("<table>"); + } writer.println("</body>"); writer.println("</head>"); writer.println("</html>"); Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPool.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPool.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPool.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -15,10 +15,12 @@ */ package org.hippoecm.hst.jcr; +import java.util.ArrayList; import java.util.Collections; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import java.util.Map; import javax.jcr.LoginException; @@ -42,13 +44,15 @@ private Map<HttpSession, ReadOnlyPooledSession> activeSessions = Collections.synchronizedMap(new IdentityHashMap<HttpSession, ReadOnlyPooledSession>()); private boolean isSessionInterValRefresh; private int refreshInterval; + private String poolName; - public JcrSessionPool(SimpleCredentials simpleCredentials, String repositoryLocation, boolean isSessionInterValRefresh, int refreshInterval) { + public JcrSessionPool(SimpleCredentials simpleCredentials, String repositoryLocation, boolean isSessionInterValRefresh, int refreshInterval, String poolName) { log.debug("Create a new jcr session pool for '" + simpleCredentials.getUserID() + "'"); this.simpleCredentials = simpleCredentials; this.repositoryLocation = repositoryLocation; this.isSessionInterValRefresh = isSessionInterValRefresh; this.refreshInterval = refreshInterval; + this.poolName = poolName; } @@ -92,8 +96,10 @@ return session; } else { // try next in the idleSessions untill none left - log.debug("Found idle session is expired. Remove this one, and try next idle"); - session.getDelegatee().logout(); + log.debug("Found idle session is expired or dirty. Remove this one, and try next idle"); + if(session.isLive()) { + session.getDelegatee().logout(); + } session = null; } @@ -143,13 +149,14 @@ if(finishedSession.getRefCount() == 0) { this.activeSessions.remove(httpSession); if (!finishedSession.isLive()) { - log.debug("Used session is not live anymore: log out"); - finishedSession.getDelegatee().logout(); + log.warn("Used session is not live anymore: log out"); return; } if (finishedSession.isValid()) { log.debug("Return the used session to the pool"); - idleSessions.add(finishedSession); + // put at the beginning of the list + idleSessions.add(0,finishedSession); + finishedSession.setIdleSince(System.currentTimeMillis()); } else { log.debug("Used session is expired. Log out"); finishedSession.getDelegatee().logout(); @@ -181,6 +188,29 @@ } + public void cleanupPool(){ + synchronized (this) { + List<ReadOnlyPooledSession> remove = new ArrayList<ReadOnlyPooledSession>(); + for(ReadOnlyPooledSession idle : this.idleSessions) { + if(!idle.isLive()) { + remove.add(idle); + } else if(!idle.isValid()){ + idle.getDelegatee().logout(); + remove.add(idle); + + } else if( (System.currentTimeMillis() - idle.getIdleSince()) > (1 * 60 * 1000) ) { + // if a session is 10 minutes idle, let's remove it as well (where we keep at least one session) + if(this.activeSessions.size() == 0 && (this.idleSessions.size() - remove.size() ) >1 ) { + idle.getDelegatee().logout(); + remove.add(idle); + } + } + } + log.debug("Cleaning up '{}' idle sessions from '{}'", remove.size() , poolName ); + this.idleSessions.removeAll(remove); + } + } + public void dispose() { synchronized(this) { for( Iterator<ReadOnlyPooledSession> si = this.activeSessions.values().iterator(); si.hasNext() ; ){ @@ -199,4 +229,26 @@ public int getRefreshInterval(){ return this.refreshInterval; } + + public int getActiveCount() { + synchronized(this) { + return this.activeSessions.size(); + } + } + + public int getIdleCount() { + return this.idleSessions.size(); + } + + public void markDirty() { + synchronized(this) { + for(ReadOnlyPooledSession session : this.idleSessions) { + session.setDirty(true); + } + for(ReadOnlyPooledSession session : this.activeSessions.values()) { + session.setDirty(true); + } + } + } + } Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPoolManager.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPoolManager.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/JcrSessionPoolManager.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -35,16 +35,28 @@ private static long lastEventTick = System.currentTimeMillis(); private int refreshInterval = 0; + private JcrSessionPoolManagerChecker jcrSessionPoolManagerChecker; + private static final JcrSessionPoolManager jcrSessionPoolManager = new JcrSessionPoolManager(); + public static long getLastEventTick() { return lastEventTick; } + private JcrSessionPoolManager(){ + this.jcrSessionPoolManagerChecker = new JcrSessionPoolManagerChecker(); + jcrSessionPoolManagerChecker.start(); + } + + public static JcrSessionPoolManager getInstance(){ + return jcrSessionPoolManager; + } + public static void setLastEventTick(long lastEventTick) { JcrSessionPoolManager.lastEventTick = lastEventTick; } - public ReadOnlyPooledSession getSession(HttpServletRequest request, HstRequestContext hstRequestContext){ + public ReadOnlyPooledSession getSession(HttpServletRequest request, HstRequestContext hstRequestContext, String entryPath){ // TODO improve speed of getting a session from the pool. ServletContext sc = request.getSession().getServletContext(); @@ -54,22 +66,28 @@ SimpleCredentials simpleCredentials = new SimpleCredentials(username, (password != null ? password.toCharArray() : null)); // TODO a less blocking synronization - String poolKey = simpleCredentials.getUserID(); - if(poolKey == null) { - poolKey = "anonymous"; + String poolKey = ""; + if(entryPath != null) { + poolKey += entryPath+":"; } + if(simpleCredentials.getUserID() != null) { + poolKey += simpleCredentials.getUserID(); + } + else{ + poolKey += "anonymous"; + } boolean isSessionInterValRefresh = true; synchronized(jcrSessionPools) { if(hstRequestContext != null) { - poolKey += "_"+hstRequestContext.isPreview(); + poolKey += ":preview="+hstRequestContext.isPreview(); // the preview does not do interval refresh isSessionInterValRefresh = !hstRequestContext.isPreview(); } JcrSessionPool jcrSessionPool = jcrSessionPools.get(poolKey); if(jcrSessionPool == null) { - log.debug("No session pool present for user '" +username+ "'. Create one" ); - jcrSessionPool = new JcrSessionPool(simpleCredentials, repositoryLocation, isSessionInterValRefresh, refreshInterval); + log.debug("No session pool present for key '" +poolKey+ "'. Create one" ); + jcrSessionPool = new JcrSessionPool(simpleCredentials, repositoryLocation, isSessionInterValRefresh, refreshInterval, poolKey); jcrSessionPools.put(poolKey,jcrSessionPool); return jcrSessionPool.getSession(request.getSession()); } @@ -79,10 +97,19 @@ } public ReadOnlyPooledSession getSessionForBinaries(HttpServletRequest request){ - return getSession(request, null); + return getSession(request, null, null); } public void dispose() { + if (this.jcrSessionPoolManagerChecker != null) { + try { + this.jcrSessionPoolManagerChecker.interrupt(); + } catch (Exception e) { + log.warn("Exception occurred during interrupting jcrSessionPoolManagerChecker thread. {}", e.toString()); + } + this.jcrSessionPoolManagerChecker = null; + } + // logout *all* jcr sessions. synchronized(jcrSessionPools) { for(Iterator<JcrSessionPool> si = jcrSessionPools.values().iterator(); si.hasNext(); ) { @@ -90,10 +117,56 @@ jcrSessionPool.dispose(); } } + + } public void setRefreshInterval(int refreshInterval) { this.refreshInterval = refreshInterval; } + private class JcrSessionPoolManagerChecker extends Thread { + + private JcrSessionPoolManagerChecker() { + super("JcrSessionPoolChecker"); + } + + public void run() { + while (true) { + synchronized (this) { + try { + Thread.sleep(5000); + for(JcrSessionPool pool: jcrSessionPools.values()) { + pool.cleanupPool(); + } + } catch (InterruptedException e) { + log.error("JcrSessionPoolManagerChecker thread died"); + } + } + } + } + } + + public Map<String, JcrSessionPool> getJcrSessionPools() { + return jcrSessionPools; + } + + public void markAllDirty() { + synchronized(this) { + for(JcrSessionPool pool: jcrSessionPools.values()) { + pool.markDirty(); + } + } + } + + public void markDirty(String dirty) { + synchronized(this) { + JcrSessionPool pool = jcrSessionPools.get(dirty); + if(pool != null) { + pool.markDirty(); + } + } + } + + } Modified: ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/ReadOnlyPooledSession.java =================================================================== --- ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/ReadOnlyPooledSession.java 2009-11-11 11:34:38 UTC (rev 20626) +++ ecm/site-toolkit/branches/Branch-HSTTWO-v2_02_xx/core/src/main/java/org/hippoecm/hst/jcr/ReadOnlyPooledSession.java 2009-11-11 12:45:31 UTC (rev 20627) @@ -62,6 +62,8 @@ private int refresh_interval_minutes = REFRESH_MINUTE_INTERVAL; private int refCount; private long lastRefresTick; + private long idleSince; + private boolean dirty; public ReadOnlyPooledSession(Session session, JcrSessionPool jcrSessionPool) { this.delegatee = session; @@ -85,6 +87,9 @@ } public boolean isValid(){ + if(dirty) { + return false; + } return ( (System.currentTimeMillis() - creationTime) < TIME_TO_LIVE_SECONDS*1000); } @@ -253,5 +258,15 @@ return delegatee.itemExists(absPath); } + public void setIdleSince(long idleSince) { + this.idleSince = idleSince; + } + public long getIdleSince(){ + return this.idleSince; + } + public void setDirty(boolean dirty) { + this.dirty = dirty; + } + } _______________________________________________ Hippocms-svn mailing list Hippocms-svn@... http://lists.hippo.nl/mailman/listinfo/hippocms-svn |
| Free embeddable forum powered by Nabble | Forum Help |