FacesContextUtils.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.web.jsf;
  17. import javax.faces.context.FacesContext;
  18. import org.springframework.web.context.WebApplicationContext;
  19. /**
  20.  * Convenience methods to retrieve the root WebApplicationContext for a given
  21.  * FacesContext. This is e.g. useful for accessing a Spring context from
  22.  * custom JSF code.
  23.  *
  24.  * <p>Analogous to Spring's WebApplicationContextUtils for the ServletContext.
  25.  *
  26.  * @author Juergen Hoeller
  27.  * @since 02.08.2004
  28.  * @see org.springframework.web.context.ContextLoader
  29.  * @see org.springframework.web.context.support.WebApplicationContextUtils
  30.  */
  31. public abstract class FacesContextUtils {
  32. /**
  33.  * Find the root WebApplicationContext for this web app, which is
  34.  * typically loaded via ContextLoaderListener or ContextLoaderServlet.
  35.  * <p>Will rethrow an exception that happened on root context startup,
  36.  * to differentiate between a failed context startup and no context at all.
  37.  * @param fc FacesContext to find the web application context for
  38.  * @return the root WebApplicationContext for this web app, or null if none
  39.  * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  40.  */
  41. public static WebApplicationContext getWebApplicationContext(FacesContext fc) {
  42. Object attr = fc.getExternalContext().getApplicationMap().get(
  43. WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  44. if (attr == null) {
  45. return null;
  46. }
  47. if (attr instanceof RuntimeException) {
  48. throw (RuntimeException) attr;
  49. }
  50. if (attr instanceof Error) {
  51. throw (Error) attr;
  52. }
  53. if (!(attr instanceof WebApplicationContext)) {
  54. throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
  55. }
  56. return (WebApplicationContext) attr;
  57. }
  58. /**
  59.  * Find the root WebApplicationContext for this web app, which is
  60.  * typically loaded via ContextLoaderListener or ContextLoaderServlet.
  61.  * <p>Will rethrow an exception that happened on root context startup,
  62.  * to differentiate between a failed context startup and no context at all.
  63.  * @param fc FacesContext to find the web application context for
  64.  * @return the root WebApplicationContext for this web app
  65.  * @throws IllegalStateException if the root WebApplicationContext could not be found
  66.  * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  67.  */
  68. public static WebApplicationContext getRequiredWebApplicationContext(FacesContext fc)
  69.     throws IllegalStateException {
  70. WebApplicationContext wac = getWebApplicationContext(fc);
  71. if (wac == null) {
  72. throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
  73. }
  74. return wac;
  75. }
  76. }