1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.lifecycle;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import javax.faces.FacesException;
27 import javax.faces.application.Application;
28 import javax.faces.application.FacesMessage;
29 import javax.faces.application.ProjectStage;
30 import javax.faces.application.ViewHandler;
31 import javax.faces.component.UIViewRoot;
32 import javax.faces.context.FacesContext;
33 import javax.faces.event.PhaseId;
34 import javax.faces.event.PostRenderViewEvent;
35 import javax.faces.event.PreRenderViewEvent;
36 import javax.faces.view.ViewDeclarationLanguage;
37
38 import org.apache.myfaces.shared.config.MyfacesConfig;
39
40
41
42
43
44
45
46 class RenderResponseExecutor extends PhaseExecutor
47 {
48
49 private static final Logger log = Logger.getLogger(RenderResponseExecutor.class.getName());
50 private MyfacesConfig _myfacesConfig;
51
52 public boolean execute(FacesContext facesContext)
53 {
54 Application application = facesContext.getApplication();
55 ViewHandler viewHandler = application.getViewHandler();
56 UIViewRoot root;
57 UIViewRoot previousRoot;
58 String viewId;
59 String newViewId;
60 boolean isNotSameRoot;
61 int loops = 0;
62 int maxLoops = 15;
63
64 if (facesContext.getViewRoot() == null)
65 {
66 throw new ViewNotFoundException("A view is required to execute "+facesContext.getCurrentPhaseId());
67 }
68
69 forceSessionCreation(facesContext);
70
71 try
72 {
73
74 do
75 {
76 root = facesContext.getViewRoot();
77 previousRoot = root;
78 viewId = root.getViewId();
79
80 ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(
81 facesContext, viewId);
82 if (vdl != null)
83 {
84 vdl.buildView(facesContext, root);
85 }
86
87
88
89
90 application.publishEvent(facesContext, PreRenderViewEvent.class, root);
91
92
93 if (facesContext.getResponseComplete())
94 {
95 return false;
96 }
97
98 root = facesContext.getViewRoot();
99
100 newViewId = root.getViewId();
101
102 isNotSameRoot = !( (newViewId == null ? newViewId == viewId : newViewId.equals(viewId) ) &&
103 previousRoot.equals(root) );
104
105 loops++;
106 }
107 while ((newViewId == null && viewId != null)
108 || (newViewId != null && (!newViewId.equals(viewId) || isNotSameRoot ) ) && loops < maxLoops);
109
110 if (loops == maxLoops)
111 {
112
113 boolean production = facesContext.isProjectStage(ProjectStage.Production);
114 Level level = production ? Level.FINE : Level.WARNING;
115 if (log.isLoggable(level))
116 {
117 log.log(level, "Cicle over buildView-PreRenderViewEvent on RENDER_RESPONSE phase "
118 + "reaches maximal limit, please check listeners for infinite recursion.");
119 }
120 }
121
122 viewHandler.renderView(facesContext, root);
123
124 application.publishEvent(facesContext, PostRenderViewEvent.class, root);
125
126
127
128
129
130 List<FacesMessage> messageList = facesContext.getMessageList();
131 if (!messageList.isEmpty())
132 {
133 StringBuilder builder = new StringBuilder();
134 boolean shouldLog = false;
135 for (int i = 0, size = messageList.size(); i < size; i++)
136 {
137 FacesMessage message = messageList.get(i);
138 if (!message.isRendered())
139 {
140 builder.append("\n- ");
141 builder.append(message.getDetail());
142
143 shouldLog = true;
144 }
145 }
146 if (shouldLog)
147 {
148 log.log(Level.WARNING, "There are some unhandled FacesMessages, " +
149 "this means not every FacesMessage had a chance to be rendered.\n" +
150 "These unhandled FacesMessages are: " + builder.toString());
151 }
152 }
153 }
154 catch (IOException e)
155 {
156 throw new FacesException(e.getMessage(), e);
157 }
158 return false;
159 }
160
161 public PhaseId getPhase()
162 {
163 return PhaseId.RENDER_RESPONSE;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 private void forceSessionCreation(FacesContext context)
181 {
182 if (context.getExternalContext().getSession(false) == null)
183 {
184 if (_myfacesConfig == null)
185 {
186 _myfacesConfig = MyfacesConfig.getCurrentInstance(context.getExternalContext());
187 }
188 if (_myfacesConfig.isAlwaysForceSessionCreation()
189 || (!context.getViewRoot().isTransient()
190 && !context.getApplication().getStateManager().isSavingStateInClient(context)))
191 {
192 context.getExternalContext().getSession(true);
193 }
194 }
195 }
196 }