View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  /**
38   * Implements the render response phase (JSF Spec 2.2.6)
39   * 
40   * @author Nikolay Petrov (latest modification by $Author: lu4242 $)
41   * @version $Revision: 1296362 $ $Date: 2012-03-02 13:27:53 -0500 (Fri, 02 Mar 2012) $
42   */
43  class RenderResponseExecutor extends PhaseExecutor
44  {
45      
46      private static final Logger log = Logger.getLogger(RenderResponseExecutor.class.getName());
47      
48      public boolean execute(FacesContext facesContext)
49      {
50          Application application = facesContext.getApplication();
51          ViewHandler viewHandler = application.getViewHandler();
52          UIViewRoot root;
53          UIViewRoot previousRoot;
54          String viewId;
55          String newViewId;
56          boolean isNotSameRoot;
57          int loops = 0;
58          int maxLoops = 15;
59          
60          if (facesContext.getViewRoot() == null)
61          {
62              throw new ViewNotFoundException("A view is required to execute "+facesContext.getCurrentPhaseId());
63          }
64          
65          try
66          {
67              // do-while, because the view might change in PreRenderViewEvent-listeners
68              do
69              {
70                  root = facesContext.getViewRoot();
71                  previousRoot = root;
72                  viewId = root.getViewId();
73                  
74                  ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(
75                          facesContext, viewId);
76                  if (vdl != null)
77                  {
78                      vdl.buildView(facesContext, root);
79                  }
80                  
81                  // publish a PreRenderViewEvent: note that the event listeners
82                  // of this event can change the view, so we have to perform the algorithm 
83                  // until the viewId does not change when publishing this event.
84                  application.publishEvent(facesContext, PreRenderViewEvent.class, root);
85                  
86                  // was the response marked as complete by an event listener?
87                  if (facesContext.getResponseComplete())
88                  {
89                      return false;
90                  }
91  
92                  root = facesContext.getViewRoot();
93                  
94                  newViewId = root.getViewId();
95                  
96                  isNotSameRoot = !( (newViewId == null ? newViewId == viewId : newViewId.equals(viewId) ) && 
97                          previousRoot.equals(root) ); 
98                  
99                  loops++;
100             }
101             while ((newViewId == null && viewId != null) 
102                     || (newViewId != null && (!newViewId.equals(viewId) || isNotSameRoot ) ) && loops < maxLoops);
103             
104             if (loops == maxLoops)
105             {
106                 // PreRenderView reach maxLoops - probably a infinitive recursion:
107                 boolean production = facesContext.isProjectStage(ProjectStage.Production);
108                 Level level = production ? Level.FINE : Level.WARNING;
109                 if (log.isLoggable(level))
110                 {
111                     log.log(level, "Cicle over buildView-PreRenderViewEvent on RENDER_RESPONSE phase reaches maximal limit, please check " +
112                             "listeners for infinite recursion.");
113                 }
114             }
115 
116             viewHandler.renderView(facesContext, root);
117             
118             // log all unhandled FacesMessages, don't swallow them
119             // perf: org.apache.myfaces.context.servlet.FacesContextImpl.getMessageList() creates
120             // new Collections.unmodifiableList with every invocation->  call it only once
121             // and messageList is RandomAccess -> use index based loop
122             List<FacesMessage> messageList = facesContext.getMessageList();
123             if (!messageList.isEmpty())
124             {
125                 StringBuilder builder = new StringBuilder();
126                 boolean shouldLog = false;
127                 for (int i = 0, size = messageList.size(); i < size; i++)
128                 {
129                     FacesMessage message = messageList.get(i);
130                     if (!message.isRendered())
131                     {
132                         builder.append("\n- ");
133                         builder.append(message.getDetail());
134                         
135                         shouldLog = true;
136                     }
137                 }
138                 if (shouldLog)
139                 {
140                     log.log(Level.WARNING, "There are some unhandled FacesMessages, " +
141                             "this means not every FacesMessage had a chance to be rendered.\n" +
142                             "These unhandled FacesMessages are: " + builder.toString());
143                 }
144             }
145         }
146         catch (IOException e)
147         {
148             throw new FacesException(e.getMessage(), e);
149         }
150         return false;
151     }
152 
153     public PhaseId getPhase()
154     {
155         return PhaseId.RENDER_RESPONSE;
156     }
157 }