KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > jsf > FacesContextUtils


1 /*
2  * Copyright 2002-2006 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
17 package org.springframework.web.jsf;
18
19 import javax.faces.context.ExternalContext;
20 import javax.faces.context.FacesContext;
21
22 import org.springframework.util.Assert;
23 import org.springframework.web.context.WebApplicationContext;
24 import org.springframework.web.util.WebUtils;
25
26 /**
27  * Convenience methods to retrieve the root WebApplicationContext for a given
28  * FacesContext. This is e.g. useful for accessing a Spring context from
29  * custom JSF code.
30  *
31  * <p>Analogous to Spring's WebApplicationContextUtils for the ServletContext.
32  *
33  * @author Juergen Hoeller
34  * @since 1.1
35  * @see org.springframework.web.context.ContextLoader
36  * @see org.springframework.web.context.support.WebApplicationContextUtils
37  */

38 public abstract class FacesContextUtils {
39
40     /**
41      * Find the root WebApplicationContext for this web app, which is
42      * typically loaded via ContextLoaderListener or ContextLoaderServlet.
43      * <p>Will rethrow an exception that happened on root context startup,
44      * to differentiate between a failed context startup and no context at all.
45      * @param fc the FacesContext to find the web application context for
46      * @return the root WebApplicationContext for this web app, or <code>null</code> if none
47      * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
48      */

49     public static WebApplicationContext getWebApplicationContext(FacesContext fc) {
50         Assert.notNull(fc, "FacesContext must not be null");
51         Object JavaDoc attr = fc.getExternalContext().getApplicationMap().get(
52                 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
53         if (attr == null) {
54             return null;
55         }
56         if (attr instanceof RuntimeException JavaDoc) {
57             throw (RuntimeException JavaDoc) attr;
58         }
59         if (attr instanceof Error JavaDoc) {
60             throw (Error JavaDoc) attr;
61         }
62         if (!(attr instanceof WebApplicationContext)) {
63             throw new IllegalStateException JavaDoc("Root context attribute is not of type WebApplicationContext: " + attr);
64         }
65         return (WebApplicationContext) attr;
66     }
67
68     /**
69      * Find the root WebApplicationContext for this web app, which is
70      * typically loaded via ContextLoaderListener or ContextLoaderServlet.
71      * <p>Will rethrow an exception that happened on root context startup,
72      * to differentiate between a failed context startup and no context at all.
73      * @param fc the FacesContext to find the web application context for
74      * @return the root WebApplicationContext for this web app
75      * @throws IllegalStateException if the root WebApplicationContext could not be found
76      * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
77      */

78     public static WebApplicationContext getRequiredWebApplicationContext(FacesContext fc)
79         throws IllegalStateException JavaDoc {
80
81         WebApplicationContext wac = getWebApplicationContext(fc);
82         if (wac == null) {
83             throw new IllegalStateException JavaDoc("No WebApplicationContext found: no ContextLoaderListener registered?");
84         }
85         return wac;
86     }
87
88     /**
89      * Return the best available mutex for the given session:
90      * that is, an object to synchronize on for the given session.
91      * <p>Returns the session mutex attribute if available; usually,
92      * this means that the HttpSessionMutexListener needs to be defined
93      * in <code>web.xml</code>. Falls back to the Session reference itself
94      * if no mutex attribute found.
95      * <p>The session mutex is guaranteed to be the same object during
96      * the entire lifetime of the session, available under the key defined
97      * by the <code>SESSION_MUTEX_ATTRIBUTE</code> constant. It serves as a
98      * safe reference to synchronize on for locking on the current session.
99      * <p>In many cases, the Session reference itself is a safe mutex
100      * as well, since it will always be the same object reference for the
101      * same active logical session. However, this is not guaranteed across
102      * different servlet containers; the only 100% safe way is a session mutex.
103      * @param fc the FacesContext to find the session mutex for
104      * @return the mutex object (never <code>null</code>)
105      * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE
106      * @see org.springframework.web.util.HttpSessionMutexListener
107      */

108     public static Object JavaDoc getSessionMutex(FacesContext fc) {
109         Assert.notNull(fc, "FacesContext must not be null");
110         ExternalContext ec = fc.getExternalContext();
111         Object JavaDoc mutex = ec.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE);
112         if (mutex == null) {
113             mutex = ec.getSession(true);
114         }
115         return mutex;
116     }
117
118 }
119
Popular Tags