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