1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.lifecycle.impl; |
17 | |
|
18 | |
import java.util.Collections; |
19 | |
import java.util.Locale; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import javax.faces.FacesException; |
23 | |
import javax.faces.application.ViewHandler; |
24 | |
import javax.faces.component.UIViewRoot; |
25 | |
import javax.faces.context.ExternalContext; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.event.PhaseId; |
28 | |
import javax.faces.internal.SubApplicationUtil; |
29 | |
import javax.faces.internal.WindowIdUtil; |
30 | |
import javax.faces.internal.scope.PageScope; |
31 | |
import javax.faces.internal.scope.SubApplicationScope; |
32 | |
|
33 | |
import org.seasar.framework.util.LruHashMap; |
34 | |
import org.seasar.teeda.core.JsfConstants; |
35 | |
import org.seasar.teeda.core.lifecycle.AbstractPhase; |
36 | |
import org.seasar.teeda.core.util.ExternalContextUtil; |
37 | |
import org.seasar.teeda.core.util.FacesContextUtil; |
38 | |
import org.seasar.teeda.core.util.PortletUtil; |
39 | |
import org.seasar.teeda.core.util.PostbackUtil; |
40 | |
import org.seasar.teeda.core.util.ServletExternalContextUtil; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 4 | public class RestoreViewPhase extends AbstractPhase { |
47 | |
|
48 | 2 | private static final String VIEW_ID_LRU_ATTR = RestoreViewPhase.class |
49 | |
.getName() + |
50 | |
".VIEW_ID_LRU"; |
51 | |
|
52 | 8 | private int viewIdLruSize = 16; |
53 | |
|
54 | 8 | public RestoreViewPhase() { |
55 | 8 | } |
56 | |
|
57 | |
public int getViewIdLruSize() { |
58 | 0 | return viewIdLruSize; |
59 | |
} |
60 | |
|
61 | |
public void setViewIdLruSize(int viewIdLruSize) { |
62 | 2 | this.viewIdLruSize = viewIdLruSize; |
63 | 2 | } |
64 | |
|
65 | |
protected void executePhase(final FacesContext context) |
66 | |
throws FacesException { |
67 | 4 | final RestoreValueHolder holder = setUpRestoreViewPhase(context); |
68 | 4 | final String viewId = holder.getCurrentViewId(); |
69 | 4 | final String wid = holder.getWid(); |
70 | 4 | final String previousViewId = holder.getPreviousViewId(); |
71 | 4 | if (previousViewId != null && !previousViewId.equals(viewId)) { |
72 | 1 | PageScope.removeContext(context, wid); |
73 | |
} |
74 | 4 | String subAppPath = SubApplicationUtil.getSubApplicationPath(viewId); |
75 | 4 | String previousSubAppPath = SubApplicationUtil |
76 | |
.getSubApplicationPath(previousViewId); |
77 | 4 | if (previousSubAppPath != null && |
78 | |
!previousSubAppPath.equals(subAppPath)) { |
79 | 1 | SubApplicationScope.removeContext(context, wid); |
80 | |
} |
81 | 4 | final UIViewRoot viewRoot = composeViewRoot(context, viewId); |
82 | 4 | context.setViewRoot(viewRoot); |
83 | |
|
84 | 4 | final ExternalContext externalContext = context.getExternalContext(); |
85 | 4 | saveViewIdToSession(externalContext.getSessionMap(), wid, viewId); |
86 | 4 | initializeChildren(context, viewRoot); |
87 | 4 | Map requestParameterMap = externalContext.getRequestParameterMap(); |
88 | 4 | if (requestParameterMap.isEmpty()) { |
89 | 3 | context.renderResponse(); |
90 | |
} |
91 | 4 | } |
92 | |
|
93 | |
protected RestoreValueHolder setUpRestoreViewPhase( |
94 | |
final FacesContext context) { |
95 | 4 | RestoreValueHolder holder = new RestoreValueHolder(context); |
96 | 4 | final ExternalContext externalContext = context.getExternalContext(); |
97 | 4 | final String previousViewId = holder.getPreviousViewId(); |
98 | 4 | Map requestMap = externalContext.getRequestMap(); |
99 | 4 | requestMap.put(JsfConstants.PREVIOUS_VIEW_ID, previousViewId); |
100 | 4 | boolean isPost = true; |
101 | |
|
102 | 4 | if (!PortletUtil.isPortlet(context)) { |
103 | 4 | isPost = ServletExternalContextUtil.isPost(externalContext); |
104 | |
} else { |
105 | 0 | isPost = !PortletUtil.isRender(context); |
106 | |
} |
107 | 4 | PostbackUtil.setPostback(requestMap, isPost); |
108 | 4 | return holder; |
109 | |
} |
110 | |
|
111 | |
private UIViewRoot composeViewRoot(final FacesContext context, |
112 | |
final String viewId) { |
113 | 4 | final ViewHandler viewHandler = FacesContextUtil |
114 | |
.getViewHandler(context); |
115 | 4 | UIViewRoot viewRoot = viewHandler.restoreView(context, viewId); |
116 | 4 | if (viewRoot != null) { |
117 | 1 | final Locale locale = viewHandler.calculateLocale(context); |
118 | 1 | if (locale != null) { |
119 | 1 | viewRoot.setLocale(locale); |
120 | |
} |
121 | |
} else { |
122 | 3 | viewRoot = viewHandler.createView(context, viewId); |
123 | |
} |
124 | 4 | return viewRoot; |
125 | |
} |
126 | |
|
127 | |
private String getViewId(final FacesContext context, |
128 | |
final ExternalContext externalContext) { |
129 | 4 | return ExternalContextUtil.getViewId(externalContext); |
130 | |
} |
131 | |
|
132 | |
protected String getViewIdFromSession(final Map sessionMap, |
133 | |
final String windowId) { |
134 | 3 | Map lru = getViewIdLruFromSession(sessionMap); |
135 | 3 | return (String) lru.get(windowId); |
136 | |
} |
137 | |
|
138 | |
protected synchronized Map getViewIdLruFromSession(final Map sessionMap) { |
139 | 10 | Map lru = (Map) sessionMap.get(VIEW_ID_LRU_ATTR); |
140 | 10 | if (lru == null) { |
141 | 6 | lru = Collections.synchronizedMap(new LruHashMap(viewIdLruSize)); |
142 | 6 | sessionMap.put(VIEW_ID_LRU_ATTR, lru); |
143 | |
} |
144 | 10 | return lru; |
145 | |
} |
146 | |
|
147 | |
protected void saveViewIdToSession(final Map sessionMap, |
148 | |
final String windowId, final String viewId) { |
149 | 6 | Map lru = getViewIdLruFromSession(sessionMap); |
150 | 6 | lru.put(windowId, viewId); |
151 | 6 | } |
152 | |
|
153 | |
protected PhaseId getCurrentPhaseId() { |
154 | 1 | return PhaseId.RESTORE_VIEW; |
155 | |
} |
156 | |
|
157 | |
private class RestoreValueHolder { |
158 | |
|
159 | |
private String currentViewId; |
160 | |
|
161 | |
private String wid; |
162 | |
|
163 | |
private String previousViewId; |
164 | |
|
165 | 4 | public RestoreValueHolder(FacesContext context) { |
166 | 4 | final ExternalContext externalContext = context |
167 | |
.getExternalContext(); |
168 | 4 | this.currentViewId = getViewId(context, externalContext); |
169 | 4 | this.wid = WindowIdUtil.getWindowId(externalContext); |
170 | 4 | this.previousViewId = getViewIdFromSession(externalContext |
171 | |
.getSessionMap(), this.wid); |
172 | 4 | } |
173 | |
|
174 | |
public String getCurrentViewId() { |
175 | 4 | return currentViewId; |
176 | |
} |
177 | |
|
178 | |
public String getWid() { |
179 | 4 | return wid; |
180 | |
} |
181 | |
|
182 | |
public String getPreviousViewId() { |
183 | 8 | return previousViewId; |
184 | |
} |
185 | |
|
186 | |
} |
187 | |
|
188 | |
} |