1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.lib.jsf;
20
21 import java.util.List;
22 import java.util.ListIterator;
23
24 import javax.faces.context.ExternalContext;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.FacesContextWrapper;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
31
32
33
34
35
36
37
38
39
40 public class _PortletFacesContextWrapper extends FacesContextWrapper
41 {
42 private final static String REQUEST_ADAPTER = "org.apache.myfaces.orchestra.REQUEST_ADAPTER";
43
44
45
46 private final FacesContext _facesContext;
47 private final ExternalContext externalContextDelegate;
48 private final RequestHandler contextLockHandler;
49 private final List _handlers;
50 private final String _nextToken;
51
52 private final Log log = LogFactory
53 .getLog(_PortletFacesContextWrapper.class);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public _PortletFacesContextWrapper(final FacesContext facesContext,
70 final boolean install, boolean finit, String fnextToken, List fhandlers,
71 final RequestHandler fcontextLockHandler )
72 {
73 if (log.isDebugEnabled())
74 {
75 log.debug("getFacesContext: running inner constructor");
76 }
77
78 _facesContext = facesContext;
79
80 if (install)
81 {
82 FacesContext.setCurrentInstance(this);
83 }
84
85 externalContextDelegate = new PortletExternalContextWrapper(
86 _facesContext.getExternalContext());
87
88 _handlers = fhandlers;
89 _nextToken = fnextToken;
90 contextLockHandler = fcontextLockHandler;
91 if (finit)
92 {
93 ListIterator i = fhandlers.listIterator();
94 try
95 {
96 contextLockHandler.init(facesContext);
97 while (i.hasNext())
98 {
99 RequestHandler h = (RequestHandler) i.next();
100
101 if (log.isDebugEnabled())
102 {
103 log.debug("Running inithandler of type "
104 + h.getClass().getName());
105 }
106
107 h.init(facesContext);
108 }
109 }
110 catch (RuntimeException e)
111 {
112 log.error("Problem initialising RequestHandler", e);
113 _release(i);
114 contextLockHandler.deinit();
115 throw e;
116 }
117 }
118 else
119 {
120 try
121 {
122 contextLockHandler.init(facesContext);
123 }
124 catch (RuntimeException e)
125 {
126 contextLockHandler.deinit();
127 }
128
129 RequestType type = ExternalContextUtils.getRequestType(facesContext
130 .getExternalContext());
131
132 if (RequestType.RENDER.equals(type))
133 {
134 String handlersKey = (String) fnextToken;
135 FrameworkAdapter adapter = (FrameworkAdapter) getExternalContext()
136 .getApplicationMap().remove(
137 REQUEST_ADAPTER + handlersKey);
138 if (FrameworkAdapter.getCurrentInstance() == null)
139 {
140 FrameworkAdapter.setCurrentInstance(adapter);
141 }
142 }
143 }
144 }
145
146
147
148 public void release()
149 {
150 if (log.isDebugEnabled())
151 {
152 log.debug("Running release");
153 }
154 RequestType type = ExternalContextUtils
155 .getRequestType(getExternalContext());
156 if (RequestType.RENDER.equals(type) ||
157 RequestType.EVENT.equals(type) ||
158 RequestType.RESOURCE.equals(type) ||
159 this.getResponseComplete())
160 {
161 ListIterator i = _handlers.listIterator();
162 while (i.hasNext())
163 {
164 i.next();
165 }
166 _release(i);
167 }
168 if (RequestType.ACTION.equals(type))
169 {
170 if (this.getResponseComplete())
171 {
172
173
174
175 getExternalContext().getApplicationMap().remove(
176 PortletOrchestraFacesContextFactory.REQUEST_HANDLERS+_nextToken);
177 }
178 else
179 {
180
181
182 FrameworkAdapter adapter = FrameworkAdapter.getCurrentInstance();
183 getExternalContext().getApplicationMap().put(
184 REQUEST_ADAPTER + _nextToken, adapter);
185
186
187
188
189
190 FrameworkAdapter.setCurrentInstance(null);
191 }
192 }
193
194 try
195 {
196
197
198
199 contextLockHandler.deinit();
200 }
201 catch (Exception e)
202 {
203 log.error("Problem deinitialising RequestHandler", e);
204 }
205 if (log.isDebugEnabled())
206 {
207 log.debug("Release completed");
208 }
209 getWrapped().release();
210 }
211
212 @Override
213 public FacesContext getWrapped()
214 {
215 return _facesContext;
216 }
217
218 private void _release(ListIterator i)
219 {
220 while (i.hasPrevious())
221 {
222 try
223 {
224 RequestHandler h = (RequestHandler) i.previous();
225 if (log.isDebugEnabled())
226 {
227 log.debug("Running deinithandler of type "
228 + h.getClass().getName());
229 }
230 h.deinit();
231 }
232 catch (Exception e)
233 {
234 log.error("Problem deinitialising RequestHandler", e);
235 }
236 }
237 }
238
239
240
241 public ExternalContext getExternalContext()
242 {
243 return externalContextDelegate == null ? getWrapped()
244 .getExternalContext() : externalContextDelegate;
245 }
246 }