Commit fa5140b5cee2f26820e610f72b01705bb692cff0

Authored by Danilo Costa Viana
1 parent 077b05aa

Corrigindo problema de vazamento de memória em FacesViewContextImpl

(Jira FWK-205).
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java
... ... @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.context;
39 39 import java.util.concurrent.ConcurrentHashMap;
40 40 import java.util.concurrent.atomic.AtomicLong;
41 41  
  42 +import javax.enterprise.event.Observes;
42 43 import javax.enterprise.inject.Alternative;
43 44 import javax.faces.component.UIViewRoot;
44 45 import javax.faces.context.FacesContext;
... ... @@ -48,6 +49,7 @@ import javax.servlet.http.HttpSession;
48 49 import br.gov.frameworkdemoiselle.annotation.Priority;
49 50 import br.gov.frameworkdemoiselle.annotation.ViewScoped;
50 51 import br.gov.frameworkdemoiselle.context.ViewContext;
  52 +import br.gov.frameworkdemoiselle.lifecycle.BeforeSessionDestroyed;
51 53 import br.gov.frameworkdemoiselle.util.Faces;
52 54  
53 55 /**
... ... @@ -80,8 +82,6 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC
80 82  
81 83 @Override
82 84 protected BeanStore getStore() {
83   - clearInvalidatedSession();
84   -
85 85 final String sessionId = getSessionId();
86 86  
87 87 if (sessionId==null){
... ... @@ -113,13 +113,13 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC
113 113 return currentStore.getStore(viewId, this);
114 114 }
115 115  
116   - private synchronized void clearInvalidatedSession(){
117   - if (wasSessionInvalidated()){
118   - final String requestedSessionId = getRequestedSessionId();
119   - final SessionBeanStore store = sessionBeanStore.get(requestedSessionId);
  116 + protected void clearInvalidatedSession(@Observes BeforeSessionDestroyed event){
  117 + String sessionId = event.getSessionId();
  118 + if (sessionId != null){
  119 + final SessionBeanStore store = sessionBeanStore.get(sessionId);
120 120 if (store!=null){
121 121 store.clear(this);
122   - sessionBeanStore.remove(requestedSessionId);
  122 + sessionBeanStore.remove(sessionId);
123 123 }
124 124 }
125 125 }
... ... @@ -128,14 +128,4 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC
128 128 final HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
129 129 return session!=null ? session.getId() : null;
130 130 }
131   -
132   - private String getRequestedSessionId(){
133   - final HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
134   - return request!=null ? request.getRequestedSessionId() : null;
135   - }
136   -
137   - private boolean wasSessionInvalidated(){
138   - final HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
139   - return request!=null && request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid();
140   - }
141 131 }
... ...
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/lifecycle/AfterSessionCreated.java 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +package br.gov.frameworkdemoiselle.lifecycle;
  2 +
  3 +/**
  4 + * This interface represents an event fired before a new HTTP session is created.
  5 + *
  6 + * @author serpro
  7 + *
  8 + */
  9 +public interface AfterSessionCreated {
  10 +
  11 + public String getSessionId();
  12 +
  13 +}
... ...
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/lifecycle/BeforeSessionDestroyed.java 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +package br.gov.frameworkdemoiselle.lifecycle;
  2 +
  3 +/**
  4 + * This interface represents an event fired after a new HTTP session is destroyed.
  5 + *
  6 + * @author serpro
  7 + *
  8 + */
  9 +public interface BeforeSessionDestroyed {
  10 +
  11 + public String getSessionId();
  12 +
  13 +}
... ...
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/SessionListener.java 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +package br.gov.frameworkdemoiselle.util;
  2 +
  3 +import javax.servlet.http.HttpSession;
  4 +import javax.servlet.http.HttpSessionEvent;
  5 +import javax.servlet.http.HttpSessionListener;
  6 +
  7 +import br.gov.frameworkdemoiselle.lifecycle.BeforeSessionDestroyed;
  8 +import br.gov.frameworkdemoiselle.lifecycle.AfterSessionCreated;
  9 +
  10 +/**
  11 + * <p>Implements the {@link HttpSessionListener} interface and fires two events.</p>
  12 + *
  13 + * <ul>
  14 + * <li><strong>{@link AfterSessionCreated}</strong>: Just after a new HTTP session is created</li>
  15 + * <li><strong>{@link BeforeSessionDestroyed}</strong>: Just before an HTTP session is invalidated</li>
  16 + * </ul>
  17 + *
  18 + * @author serpro
  19 + *
  20 + */
  21 +public class SessionListener implements HttpSessionListener {
  22 +
  23 + @Override
  24 + public void sessionCreated(final HttpSessionEvent sessionEvent) {
  25 + Beans.getBeanManager().fireEvent(new AfterSessionCreated() {
  26 + @Override
  27 + public String getSessionId() {
  28 + HttpSession session = sessionEvent.getSession();
  29 + return session != null ? session.getId() : null;
  30 + }
  31 + });
  32 + }
  33 +
  34 + @Override
  35 + public void sessionDestroyed(final HttpSessionEvent sessionEvent) {
  36 + Beans.getBeanManager().fireEvent(new BeforeSessionDestroyed() {
  37 + @Override
  38 + public String getSessionId() {
  39 + HttpSession session = sessionEvent.getSession();
  40 + return session != null ? session.getId() : null;
  41 + }
  42 + });
  43 + }
  44 +}
... ...
impl/extension/servlet/src/main/resources/META-INF/web-fragment.xml
... ... @@ -42,6 +42,10 @@
42 42 <listener>
43 43 <listener-class>br.gov.frameworkdemoiselle.util.ServletListener</listener-class>
44 44 </listener>
  45 +
  46 + <listener>
  47 + <listener-class>br.gov.frameworkdemoiselle.util.SessionListener</listener-class>
  48 + </listener>
45 49  
46 50 <filter>
47 51 <filter-name>Demoiselle Servlet Filter</filter-name>
... ...