Coverage Report - javax.faces.context.FacesContext
 
Classes in this File Line Coverage Branch Coverage Complexity
FacesContext
14%
14/96
13%
6/46
2.1
 
 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 javax.faces.context;
 20  
 
 21  
 import java.util.Collections;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.el.ELContext;
 27  
 import javax.faces.application.Application;
 28  
 import javax.faces.application.FacesMessage;
 29  
 import javax.faces.application.ProjectStage;
 30  
 import javax.faces.component.UINamingContainer;
 31  
 import javax.faces.component.UIViewRoot;
 32  
 import javax.faces.event.PhaseId;
 33  
 import javax.faces.render.RenderKit;
 34  
 
 35  
 /**
 36  
  * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
 37  
  */
 38  550
 public abstract class FacesContext
 39  
 {
 40  2
     private static ThreadLocal<FacesContext> currentInstance = new ThreadLocal<FacesContext>();
 41  
 
 42  2
     private static ThreadLocal<FacesContext> firstInstance = new ThreadLocal<FacesContext>();
 43  
 
 44  
     public abstract void addMessage(String clientId, FacesMessage message);
 45  
 
 46  
     public abstract Application getApplication();
 47  
 
 48  
     /**
 49  
      * 
 50  
      * @return
 51  
      * 
 52  
      * @since 2.0
 53  
      */
 54  
     public Map<Object, Object> getAttributes()
 55  
     {
 56  0
         FacesContext ctx = firstInstance.get();
 57  
         
 58  0
         if (ctx == null)
 59  
         {
 60  0
             throw new UnsupportedOperationException();
 61  
         }
 62  
         
 63  0
         return ctx.getAttributes();
 64  
     }
 65  
 
 66  
     public abstract Iterator<String> getClientIdsWithMessages();
 67  
 
 68  
     public static FacesContext getCurrentInstance()
 69  
     {
 70  23926
         return currentInstance.get();
 71  
     }
 72  
 
 73  
     /**
 74  
      * 
 75  
      * @return
 76  
      * 
 77  
      * @since 2.0
 78  
      */
 79  
     public PhaseId getCurrentPhaseId()
 80  
     {
 81  0
         FacesContext ctx = firstInstance.get();
 82  
         
 83  0
         if (ctx == null)
 84  
         {
 85  0
             throw new UnsupportedOperationException();
 86  
         }
 87  
         
 88  0
         return ctx.getCurrentPhaseId();
 89  
     }
 90  
     
 91  
     /**
 92  
      * Return the context within which all EL-expressions are evaluated.
 93  
      * <p>
 94  
      * A JSF implementation is expected to provide a full implementation of this class. However JSF also explicitly
 95  
      * allows user code to apply the "decorator" pattern to this type, by overriding the FacesContextFactory class. In
 96  
      * that pattern, the decorating class has a reference to an "underlying" implementation and forward calls to it,
 97  
      * possibly after taking other related actions.
 98  
      * <p>
 99  
      * The decorator pattern does have difficulties with backwards-compatibility when new methods are added to the class
 100  
      * being decorated, as with this method which was added in JSF1.2. Decorator classes that were written for JSF1.1
 101  
      * will subclass this class, but will not override this method to pass the call on to the "underlying" instance.
 102  
      * This base implementation therefore must do that for it.
 103  
      * <p>
 104  
      * Unfortunately the JSF designers stuffed up the design; this base class has no way of knowing what the
 105  
      * "underlying" instance is! The current implementation here is therefore to delegate directly to the very
 106  
      * <i>first</i> FacesContext instance registered within this request (via setCurrentInstance). This instance should
 107  
      * be the "full" implementation provided by the JSF framework. The drawback is that when any decorator class is
 108  
      * present which defaults to this base implementation, then any following decorator instances that do override this
 109  
      * method do not get it invoked.
 110  
      * <p>
 111  
      * It is believed that the Sun JSF implementation (Mojarra) does something similar.
 112  
      * 
 113  
      * @since 1.2
 114  
      */
 115  
     public ELContext getELContext()
 116  
     {
 117  
         // Do NOT use getCurrentInstance here. For FacesContext decorators that
 118  
         // register themselves as "the current instance" that will cause an
 119  
         // infinite loop. For FacesContext decorators that do not register
 120  
         // themselves as "the current instance", if they are themselves wrapped
 121  
         // by a decorator that *does* register itself, then an infinite loop
 122  
         // also occurs.
 123  
         //
 124  
         // As noted above, we really want to do something like
 125  
         // ctx = getWrappedInstance();
 126  
         // where the subclass can return the object it is delegating to.
 127  
         // As there is no such method, however, the best we can do is pass the
 128  
         // method call on to the first-registered FacesContext instance. That
 129  
         // instance will never be "this", as the real original FacesContext
 130  
         // object will provide a proper implementation of this method.
 131  0
         FacesContext ctx = firstInstance.get();
 132  
 
 133  0
         if (ctx == null)
 134  
         {
 135  0
             throw new NullPointerException(FacesContext.class.getName());
 136  
         }
 137  
 
 138  0
         ELContext elctx = ctx.getELContext();
 139  0
         if (elctx == null)
 140  
         {
 141  0
             throw new UnsupportedOperationException();
 142  
         }
 143  
 
 144  0
         return elctx;
 145  
     }
 146  
 
 147  
     /**
 148  
      * 
 149  
      * @return
 150  
      * 
 151  
      * @since 2.0
 152  
      */
 153  
     public ExceptionHandler getExceptionHandler()
 154  
     {
 155  0
         FacesContext ctx = firstInstance.get();
 156  
         
 157  0
         if (ctx == null)
 158  
         {
 159  0
             throw new UnsupportedOperationException();
 160  
         }
 161  
         
 162  0
         return ctx.getExceptionHandler();
 163  
     }
 164  
 
 165  
     public abstract ExternalContext getExternalContext();
 166  
 
 167  
     public abstract FacesMessage.Severity getMaximumSeverity();
 168  
 
 169  
     /**
 170  
      * 
 171  
      * @return
 172  
      * 
 173  
      * @since 2.0
 174  
      */
 175  
     public List<FacesMessage> getMessageList()
 176  
     {
 177  0
         FacesContext ctx = firstInstance.get();
 178  
         
 179  0
         if (ctx == null)
 180  
         {
 181  0
             throw new UnsupportedOperationException();
 182  
         }
 183  
         
 184  0
         return ctx.getMessageList();
 185  
     }
 186  
     
 187  
     /**
 188  
      * 
 189  
      * @param clientId
 190  
      * @return
 191  
      * 
 192  
      * @since 2.0
 193  
      */
 194  
     public List<FacesMessage> getMessageList(String clientId)
 195  
     {
 196  0
         FacesContext ctx = firstInstance.get();
 197  
         
 198  0
         if (ctx == null)
 199  
         {
 200  0
             throw new UnsupportedOperationException();
 201  
         }
 202  
         
 203  0
         return ctx.getMessageList(clientId);
 204  
     }
 205  
 
 206  
     public abstract Iterator<FacesMessage> getMessages();
 207  
 
 208  
     public abstract Iterator<FacesMessage> getMessages(String clientId);
 209  
 
 210  
     /**
 211  
      * <p>
 212  
      * Return the PartialViewContext for this request. The PartialViewContext is used to control the processing of
 213  
      * specified components during the execute portion of the request processing lifecycle (known as partial processing)
 214  
      * and the rendering of specified components (known as partial rendering). This method must return a new
 215  
      * PartialViewContext if one does not already exist.
 216  
      * </p>
 217  
      * 
 218  
      * @return The PartialViewContext
 219  
      * @throws IllegalStateException
 220  
      *             if this method is called after this instance has been released
 221  
      * 
 222  
      * @since 2.0
 223  
      */
 224  
     public PartialViewContext getPartialViewContext()
 225  
     {
 226  0
         FacesContext ctx = firstInstance.get();
 227  
         
 228  0
         if (ctx == null)
 229  
         {
 230  0
             throw new UnsupportedOperationException();
 231  
         }
 232  
         
 233  0
         return ctx.getPartialViewContext();
 234  
     }
 235  
 
 236  
     public abstract RenderKit getRenderKit();
 237  
 
 238  
     public abstract boolean getRenderResponse();
 239  
 
 240  
     public abstract boolean getResponseComplete();
 241  
 
 242  
     public abstract ResponseStream getResponseStream();
 243  
 
 244  
     public abstract ResponseWriter getResponseWriter();
 245  
     
 246  
     /**
 247  
      * 
 248  
      * @return
 249  
      * 
 250  
      * @since 2.0
 251  
      */
 252  
     public boolean isValidationFailed()
 253  
     {
 254  0
         FacesContext ctx = firstInstance.get();
 255  
         
 256  0
         if (ctx == null)
 257  
         {
 258  0
             throw new UnsupportedOperationException();
 259  
         }
 260  
         
 261  0
         return ctx.isValidationFailed();
 262  
     }
 263  
 
 264  
     public abstract UIViewRoot getViewRoot();
 265  
 
 266  
     /**
 267  
      * 
 268  
      * @return
 269  
      * 
 270  
      * @since 2.0
 271  
      */
 272  
     public boolean isPostback()
 273  
     {
 274  0
         FacesContext ctx = firstInstance.get();
 275  
         
 276  0
         if (ctx == null)
 277  
         {
 278  0
             throw new UnsupportedOperationException();
 279  
         }
 280  
         
 281  0
         return ctx.isPostback();
 282  
     }
 283  
     
 284  
     /**
 285  
      * 
 286  
      * @return
 287  
      * 
 288  
      * @since 2.0
 289  
      */
 290  
     public boolean isProcessingEvents()
 291  
     {
 292  0
         FacesContext ctx = firstInstance.get();
 293  
         
 294  0
         if (ctx == null)
 295  
         {
 296  0
             throw new UnsupportedOperationException();
 297  
         }
 298  
         
 299  0
         return ctx.isProcessingEvents();
 300  
     }
 301  
 
 302  
     public abstract void release();
 303  
 
 304  
     public abstract void renderResponse();
 305  
 
 306  
     public abstract void responseComplete();
 307  
 
 308  
     protected static void setCurrentInstance(FacesContext context)
 309  
     {
 310  1088
         if (context == null)
 311  
         {
 312  520
             currentInstance.remove();
 313  520
             firstInstance.remove();
 314  
         }
 315  
         else
 316  
         {
 317  568
             currentInstance.set(context);
 318  
 
 319  568
             if (firstInstance.get() == null)
 320  
             {
 321  522
                 firstInstance.set(context);
 322  
             }
 323  
         }
 324  1088
     }
 325  
 
 326  
     /**
 327  
      * 
 328  
      * @return
 329  
      * 
 330  
      * @since 2.0
 331  
      */
 332  
     public void setCurrentPhaseId(PhaseId currentPhaseId)
 333  
     {
 334  0
         FacesContext ctx = firstInstance.get();
 335  
         
 336  0
         if (ctx == null)
 337  
         {
 338  0
             throw new UnsupportedOperationException();
 339  
         }
 340  
         
 341  0
         ctx.setCurrentPhaseId(currentPhaseId);
 342  0
     }
 343  
 
 344  
     /**
 345  
      * 
 346  
      * @return
 347  
      * 
 348  
      * @since 2.0
 349  
      */
 350  
     public void setExceptionHandler(ExceptionHandler exceptionHandler)
 351  
     {
 352  0
         FacesContext ctx = firstInstance.get();
 353  
         
 354  0
         if (ctx == null)
 355  
         {
 356  0
             throw new UnsupportedOperationException();
 357  
         }
 358  
         
 359  0
         ctx.setExceptionHandler(exceptionHandler);
 360  0
     }
 361  
     
 362  
     /**
 363  
      * 
 364  
      * @param processingEvents
 365  
      * 
 366  
      * @since 2.0
 367  
      */
 368  
     public void setProcessingEvents(boolean processingEvents)
 369  
     {
 370  0
         FacesContext ctx = firstInstance.get();
 371  
         
 372  0
         if (ctx == null)
 373  
         {
 374  0
             throw new UnsupportedOperationException();
 375  
         }
 376  
         
 377  0
         ctx.setProcessingEvents(processingEvents);
 378  0
     }
 379  
 
 380  
     public abstract void setResponseStream(ResponseStream responseStream);
 381  
 
 382  
     public abstract void setResponseWriter(ResponseWriter responseWriter);
 383  
 
 384  
     public abstract void setViewRoot(UIViewRoot root);
 385  
     
 386  
     /**
 387  
      * 
 388  
      * 
 389  
      * @since 2.0
 390  
      */
 391  
     public void validationFailed()
 392  
     {
 393  0
         FacesContext ctx = firstInstance.get();
 394  
         
 395  0
         if (ctx == null)
 396  
         {
 397  0
             throw new UnsupportedOperationException();
 398  
         }
 399  
         
 400  0
         ctx.validationFailed();
 401  0
     }
 402  
     
 403  
     public boolean isProjectStage(ProjectStage stage)
 404  
     {
 405  300
         if (stage == null)
 406  
         {
 407  0
             throw new NullPointerException();
 408  
         }
 409  
         
 410  300
         if (stage.equals(getApplication().getProjectStage()))
 411  
         {
 412  0
             return true;
 413  
         }
 414  300
         return false;
 415  
     }
 416  
     
 417  
     /**
 418  
      * 
 419  
      * @since 2.1
 420  
      * @return
 421  
      */
 422  
     public boolean isReleased()
 423  
     {
 424  0
         FacesContext ctx = firstInstance.get();
 425  
         
 426  0
         if (ctx == null)
 427  
         {
 428  0
             throw new UnsupportedOperationException();
 429  
         }
 430  
 
 431  0
         return ctx.isReleased();
 432  
     }
 433  
     
 434  
     /**
 435  
      * @since 2.2
 436  
      * @return 
 437  
      */
 438  
     public List<String> getResourceLibraryContracts()
 439  
     {
 440  0
         FacesContext ctx = firstInstance.get();
 441  
         
 442  0
         if (ctx == null)
 443  
         {
 444  0
             return Collections.emptyList();
 445  
         }        
 446  
         
 447  0
         return ctx.getResourceLibraryContracts();
 448  
     }
 449  
     
 450  
     /**
 451  
      * @since 2.2
 452  
      * @param contracts 
 453  
      */
 454  
     public void setResourceLibraryContracts(List<String> contracts)
 455  
     {
 456  0
         FacesContext ctx = firstInstance.get();
 457  
         
 458  0
         if (ctx == null)
 459  
         {
 460  0
             return;
 461  
         }
 462  0
         ctx.setResourceLibraryContracts(contracts);
 463  0
     }
 464  
     
 465  
     /**
 466  
      * @since 2.2
 467  
      * @return 
 468  
      */
 469  
     public char getNamingContainerSeparatorChar()
 470  
     {
 471  0
         FacesContext ctx = firstInstance.get();
 472  
         
 473  0
         if (ctx == null)
 474  
         {
 475  0
             return UINamingContainer.getSeparatorChar(this);
 476  
         }
 477  0
         return ctx.getNamingContainerSeparatorChar();
 478  
     }
 479  
 }