View Javadoc

1   /*
2    * $Id: ViewSessionPatchStateManager.java,v 1.18 2011/06/16 09:29:42 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.util;
6   
7   import java.io.IOException;
8   import java.util.List;
9   import java.util.Map;
10  
11  import javax.faces.application.StateManager;
12  import javax.faces.component.UIViewRoot;
13  import javax.faces.context.FacesContext;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.rcfaces.core.internal.Constants;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
22   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:42 $
23   */
24  public class ViewSessionPatchStateManager extends StateManager {
25      private static final String REVISION = "$Revision: 1.18 $";
26  
27      private static final String FACES_VIEW_LIST = "com.sun.faces.VIEW_LIST";
28  
29      private static final String VIEW_PATCH_PARAMETER = Constants
30              .getPackagePrefix()
31              + ".VIEW_SESSION_PATCH";
32  
33      private static final Log LOG = LogFactory
34              .getLog(ViewSessionPatchStateManager.class);
35  
36      private final StateManager parent;
37  
38      private Boolean enabled;
39  
40      public ViewSessionPatchStateManager(StateManager stateManager) {
41          this.parent = stateManager;
42      }
43  
44      public SerializedView saveSerializedView(FacesContext context) {
45          synchronized (this) {
46              if (enabled == null) {
47                  if ("true".equalsIgnoreCase(context.getExternalContext()
48                          .getInitParameter(VIEW_PATCH_PARAMETER))) {
49                      enabled = Boolean.TRUE;
50                      if (LOG.isInfoEnabled()) {
51                          LOG.info("Enable view-session patch.");
52                      }
53                  } else {
54                      enabled = Boolean.FALSE;
55                  }
56              }
57          }
58  
59          if (enabled == Boolean.TRUE) {
60              Map sessionMap = context.getExternalContext().getSessionMap();
61  
62              synchronized (this) {
63                  if (sessionMap != null) {
64                      List viewList = (List) sessionMap.get(FACES_VIEW_LIST);
65                      if (viewList != null) {
66                          viewList.remove(context.getViewRoot().getViewId());
67  
68                          if (LOG.isDebugEnabled()) {
69                              LOG.debug("View list size=" + viewList.size());
70                          }
71                      }
72                  }
73              }
74          }
75  
76          return parent.saveSerializedView(context);
77      }
78  
79      public void writeState(FacesContext context, SerializedView state)
80              throws IOException {
81          parent.writeState(context, state);
82      }
83  
84      public UIViewRoot restoreView(FacesContext context, String viewId,
85              String renderKitId) {
86  
87          return parent.restoreView(context, viewId, renderKitId);
88      }
89  
90      /* ---- */
91  
92      protected Object getTreeStructureToSave(FacesContext context) {
93          return null;
94      }
95  
96      protected Object getComponentStateToSave(FacesContext context) {
97          return null;
98      }
99  
100     protected UIViewRoot restoreTreeStructure(FacesContext context,
101             String viewId, String renderKitId) {
102         return null;
103     }
104 
105     protected void restoreComponentState(FacesContext context,
106             UIViewRoot viewRoot, String renderKitId) {
107     }
108 }