| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.application.impl; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
|
| 20 | |
import javax.faces.component.UIViewRoot; |
| 21 | |
import javax.faces.context.ExternalContext; |
| 22 | |
import javax.faces.context.FacesContext; |
| 23 | |
import javax.faces.render.ResponseStateManager; |
| 24 | |
|
| 25 | |
import org.seasar.teeda.core.application.TeedaStateManager; |
| 26 | |
import org.seasar.teeda.core.application.TreeStructure; |
| 27 | |
import org.seasar.teeda.core.util.ResponseStateManagerUtil; |
| 28 | |
import org.seasar.teeda.core.util.StateManagerUtil; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class TeedaSessionStateManagerImpl extends TeedaStateManager { |
| 37 | |
|
| 38 | |
static final long serialVersionUID = 0L; |
| 39 | |
|
| 40 | 0 | public TeedaSessionStateManagerImpl() { |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public UIViewRoot restoreView(FacesContext context, String viewId, |
| 44 | |
String renderKitId) { |
| 45 | 0 | assertRenderKitIdNotNull(renderKitId); |
| 46 | 0 | UIViewRoot viewRoot = restoreTreeStructure(context, viewId, renderKitId); |
| 47 | 0 | if (viewRoot != null) { |
| 48 | 0 | viewRoot.setViewId(viewId); |
| 49 | 0 | restoreComponentState(context, viewRoot, renderKitId); |
| 50 | 0 | if (!isSavingStateInClient(context)) { |
| 51 | 0 | ExternalContext extContext = context.getExternalContext(); |
| 52 | 0 | removeSerializedViewFromServer(extContext, viewId); |
| 53 | |
} |
| 54 | |
} |
| 55 | 0 | return viewRoot; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public SerializedView saveSerializedView(FacesContext context) |
| 59 | |
throws IllegalStateException { |
| 60 | 0 | UIViewRoot viewRoot = context.getViewRoot(); |
| 61 | 0 | StateManagerUtil.assertComponentNoDuplicateId(viewRoot); |
| 62 | 0 | SerializedView serializedView = createSerializedView(context); |
| 63 | 0 | if (isSavingStateInClient(context)) { |
| 64 | 0 | return serializedView; |
| 65 | |
} |
| 66 | 0 | saveSerializedViewToServer(context, viewRoot.getViewId(), |
| 67 | |
serializedView); |
| 68 | 0 | return null; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void writeState(FacesContext context, SerializedView serializedView) |
| 72 | |
throws IOException { |
| 73 | 0 | if (isSavingStateInClient(context)) { |
| 74 | 0 | UIViewRoot viewRoot = context.getViewRoot(); |
| 75 | 0 | ResponseStateManager responseStateManager = ResponseStateManagerUtil |
| 76 | |
.getResponseStateManager(context, viewRoot.getRenderKitId()); |
| 77 | 0 | responseStateManager.writeState(context, serializedView); |
| 78 | |
} |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
protected Object getComponentStateToSave(FacesContext context) { |
| 82 | 0 | UIViewRoot viewRoot = context.getViewRoot(); |
| 83 | 0 | if (viewRoot.isTransient()) { |
| 84 | 0 | return null; |
| 85 | |
} |
| 86 | 0 | return viewRoot.processSaveState(context); |
| 87 | |
} |
| 88 | |
|
| 89 | |
protected Object getTreeStructureToSave(FacesContext context) { |
| 90 | 0 | UIViewRoot viewRoot = context.getViewRoot(); |
| 91 | 0 | if (viewRoot.isTransient()) { |
| 92 | 0 | return null; |
| 93 | |
} |
| 94 | 0 | return getTreeStructureManager().buildTreeStructure(viewRoot); |
| 95 | |
} |
| 96 | |
|
| 97 | |
protected void restoreComponentState(FacesContext context, |
| 98 | |
UIViewRoot viewRoot, String renderKitId) { |
| 99 | 0 | assertRenderKitIdNotNull(renderKitId); |
| 100 | 0 | if (viewRoot.getRenderKitId() == null) { |
| 101 | 0 | viewRoot.setRenderKitId(renderKitId); |
| 102 | |
} |
| 103 | 0 | if (isSavingStateInClient(context)) { |
| 104 | 0 | restoreComponentStateFromClient(context, viewRoot, renderKitId); |
| 105 | |
} else { |
| 106 | 0 | restoreComponentStateFromServer(context, viewRoot); |
| 107 | |
} |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
protected UIViewRoot restoreTreeStructure(FacesContext context, |
| 111 | |
String viewId, String renderKitId) { |
| 112 | 0 | assertRenderKitIdNotNull(renderKitId); |
| 113 | 0 | if (isSavingStateInClient(context)) { |
| 114 | 0 | return restoreTreeStructureFromClient(context, viewId, renderKitId); |
| 115 | |
} |
| 116 | 0 | return restoreTreeStructureFromServer(context, viewId); |
| 117 | |
} |
| 118 | |
|
| 119 | |
protected void restoreComponentStateFromClient(FacesContext context, |
| 120 | |
UIViewRoot viewRoot, String renderKitId) { |
| 121 | 0 | ResponseStateManager responseStateManager = ResponseStateManagerUtil |
| 122 | |
.getResponseStateManager(context, renderKitId); |
| 123 | 0 | Object state = responseStateManager.getComponentStateToRestore(context); |
| 124 | 0 | viewRoot.processRestoreState(context, state); |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
protected void restoreComponentStateFromServer(FacesContext context, |
| 128 | |
UIViewRoot viewRoot) { |
| 129 | 0 | SerializedView serializedView = getSerializedViewFromServer(context |
| 130 | |
.getExternalContext(), viewRoot.getViewId()); |
| 131 | 0 | if (serializedView == null) { |
| 132 | 0 | return; |
| 133 | |
} |
| 134 | 0 | Object state = serializedView.getState(); |
| 135 | 0 | if (state == null) { |
| 136 | 0 | return; |
| 137 | |
} |
| 138 | 0 | viewRoot.processRestoreState(context, state); |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
protected UIViewRoot restoreTreeStructureFromClient(FacesContext context, |
| 142 | |
String viewId, String renderKitId) { |
| 143 | 0 | ResponseStateManager responseStateManager = ResponseStateManagerUtil |
| 144 | |
.getResponseStateManager(context, renderKitId); |
| 145 | 0 | TreeStructure struct = (TreeStructure) responseStateManager |
| 146 | |
.getTreeStructureToRestore(context, viewId); |
| 147 | 0 | if (struct == null) { |
| 148 | 0 | return null; |
| 149 | |
} |
| 150 | 0 | return (UIViewRoot) getTreeStructureManager().restoreTreeStructure( |
| 151 | |
struct); |
| 152 | |
} |
| 153 | |
|
| 154 | |
protected UIViewRoot restoreTreeStructureFromServer(FacesContext context, |
| 155 | |
String viewId) { |
| 156 | 0 | SerializedView serializedView = getSerializedViewFromServer(context |
| 157 | |
.getExternalContext(), viewId); |
| 158 | 0 | if (serializedView == null) { |
| 159 | 0 | return null; |
| 160 | |
} |
| 161 | 0 | TreeStructure struct = (TreeStructure) serializedView.getStructure(); |
| 162 | 0 | return (UIViewRoot) getTreeStructureManager().restoreTreeStructure( |
| 163 | |
struct); |
| 164 | |
} |
| 165 | |
|
| 166 | |
private static void assertRenderKitIdNotNull(String renderKitId) { |
| 167 | 0 | if (renderKitId == null) { |
| 168 | 0 | throw new IllegalArgumentException(); |
| 169 | |
} |
| 170 | 0 | } |
| 171 | |
|
| 172 | |
protected SerializedView getSerializedViewFromServer( |
| 173 | |
ExternalContext externalContext, String viewId) { |
| 174 | 0 | return (SerializedView) externalContext.getSessionMap().get( |
| 175 | |
SERIALIZED_VIEW_ATTR + "-" + viewId); |
| 176 | |
} |
| 177 | |
|
| 178 | |
protected void saveSerializedViewToServer(FacesContext context, |
| 179 | |
String viewId, SerializedView serializedView) { |
| 180 | 0 | ExternalContext externalContext = context.getExternalContext(); |
| 181 | 0 | externalContext.getSessionMap().put( |
| 182 | |
SERIALIZED_VIEW_ATTR + "-" + viewId, serializedView); |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
protected void removeSerializedViewFromServer( |
| 186 | |
ExternalContext externalContext, String viewId) { |
| 187 | 0 | externalContext.getSessionMap().remove( |
| 188 | |
SERIALIZED_VIEW_ATTR + "-" + viewId); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
public void removeSerializedView(String viewId) { |
| 192 | 0 | FacesContext.getCurrentInstance().getExternalContext().getSessionMap() |
| 193 | |
.remove(viewId); |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
public boolean hasSerializedView(final FacesContext context, |
| 197 | |
final String viewId) { |
| 198 | 0 | if (isSavingStateInClient(context)) { |
| 199 | 0 | return false; |
| 200 | |
} |
| 201 | 0 | final ExternalContext externalContext = context.getExternalContext(); |
| 202 | 0 | SerializedView serializedView = getSerializedViewFromServer( |
| 203 | |
externalContext, viewId); |
| 204 | 0 | return (serializedView != null); |
| 205 | |
} |
| 206 | |
|
| 207 | |
} |