KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > servlet > FacesHelper


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app.servlet;
18
19 import javax.faces.FactoryFinder;
20 import javax.faces.component.UIViewRoot;
21 import javax.faces.context.FacesContext;
22 import javax.faces.context.FacesContextFactory;
23 import javax.faces.el.ValueBinding;
24 import javax.faces.lifecycle.Lifecycle;
25 import javax.faces.lifecycle.LifecycleFactory;
26 import javax.portlet.PortletContext;
27 import javax.portlet.PortletRequest;
28 import javax.portlet.PortletResponse;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.ServletResponse JavaDoc;
32
33 /**
34  * @author Kevin Roast
35  */

36 public final class FacesHelper
37 {
38    /**
39     * Private constructor
40     */

41    private FacesHelper()
42    {
43    }
44    
45    /**
46     * Return a valid FacesContext for the specific context, request and response.
47     * The FacesContext can be constructor for Servlet use.
48     *
49     * @param context ServletContext
50     * @param request ServletRequest
51     * @param response ServletReponse
52     *
53     * @return FacesContext
54     */

55    public static FacesContext getFacesContext(ServletRequest JavaDoc request, ServletResponse JavaDoc response, ServletContext JavaDoc context)
56    {
57       return getFacesContextImpl(request, response, context);
58    }
59    
60    /**
61     * Return a valid FacesContext for the specific context, request and response.
62     * The FacesContext can be constructor for Servlet use.
63     *
64     * @param context ServletContext
65     * @param request ServletRequest
66     * @param response ServletReponse
67     *
68     * @return FacesContext
69     */

70    public static FacesContext getFacesContext(PortletRequest request, PortletResponse response, PortletContext context)
71    {
72       return getFacesContextImpl(request, response, context);
73    }
74    
75    /**
76     * Return a valid FacesContext for the specific context, request and response.
77     * The FacesContext can be constructor for Servlet and Portlet use.
78     *
79     * @param context ServletContext or PortletContext
80     * @param request ServletRequest or PortletRequest
81     * @param response ServletReponse or PortletResponse
82     *
83     * @return FacesContext
84     */

85    private static FacesContext getFacesContextImpl(Object JavaDoc request, Object JavaDoc response, Object JavaDoc context)
86    {
87       FacesContext facesContext = FacesContext.getCurrentInstance();
88       if (facesContext != null) return facesContext;
89       
90       FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
91       LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
92       Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
93       
94       // Doesn't set this instance as the current instance of FacesContext.getCurrentInstance
95
facesContext = contextFactory.getFacesContext(context, request, response, lifecycle);
96       
97       // Set using our inner class
98
InnerFacesContext.setFacesContextAsCurrent(facesContext);
99       
100       // set a new viewRoot, otherwise context.getViewRoot returns null
101
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "/jsp/root");
102       facesContext.setViewRoot(view);
103       
104       return facesContext;
105    }
106    
107    /**
108     * Return a JSF managed bean reference.
109     *
110     * @param fc FacesContext
111     * @param name Name of the managed bean to return
112     *
113     * @return the managed bean or null if not found
114     */

115    public static Object JavaDoc getManagedBean(FacesContext fc, String JavaDoc name)
116    {
117       ValueBinding vb = fc.getApplication().createValueBinding("#{" + name + "}");
118       return vb.getValue(fc);
119    }
120    
121    /**
122     * We need an inner class to be able to call FacesContext.setCurrentInstance
123     * since it's a protected method
124     */

125    private abstract static class InnerFacesContext extends FacesContext
126    {
127       protected static void setFacesContextAsCurrent(FacesContext facesContext)
128       {
129          FacesContext.setCurrentInstance(facesContext);
130       }
131    }
132 }
133
Popular Tags