KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > context > FacesContextFactoryImpl


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.context;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import javax.faces.FacesException;
40 import javax.faces.context.FacesContext;
41 import javax.faces.context.FacesContextFactory;
42 import javax.faces.lifecycle.Lifecycle;
43 import javax.servlet.ServletContext JavaDoc;
44 import javax.servlet.ServletRequest JavaDoc;
45 import java.lang.reflect.Method JavaDoc;
46
47 /**
48  * This is the ICEfaces implementation of the FacesContextFactory. We take
49  * advantage of the delegation design provided by the JSF spec to create our
50  * factory with a delegate factory. We then check to see if requests are being
51  * handled by one of our PersistentFaces classes. This is accomplished by
52  * inserting a known attribute into the request. If the attribute is present,
53  * then we know that the request for a FacesContext originated from an ICEfaces
54  * servlet (or portlet). If not, we delegate the call to the "parent" factory.
55  * This allows us to run in "plain" Faces mode while still making use of our own
56  * ViewHandler and renders.
57  */

58 public class FacesContextFactoryImpl extends FacesContextFactory {
59
60     protected static Log log = LogFactory.getLog(FacesContextFactoryImpl.class);
61
62     public static final String JavaDoc SERVLET_KEY = "servletkey";
63     public static final String JavaDoc PERSISTENT = "persistent";
64
65     private FacesContextFactory delegate;
66
67     public FacesContextFactoryImpl() {
68
69     }
70
71     public FacesContextFactoryImpl(FacesContextFactory delegate) {
72         this.delegate = delegate;
73     }
74
75     public FacesContext getFacesContext(Object JavaDoc context, Object JavaDoc request,
76                                         Object JavaDoc response,
77                                         Lifecycle lifecycle)
78             throws FacesException {
79
80         // If anything is null, we're not good to go.
81
if (context == null || request == null ||
82                 response == null || lifecycle == null) {
83             throw new NullPointerException JavaDoc();
84         }
85
86         //If ICEfaces should not be handling this, then delegate the responsibility of
87
//providing the FacesContext to the delegated FacesContextFactory.
88
if (shouldDelegate(request)) {
89             return delegate
90                     .getFacesContext(context, request, response, lifecycle);
91         }
92
93         // Create a new external context that wraps up the context for the environment that
94
// we're currently running in as well as the request and response objects. The
95
// BridgeExternalContext is responsible for differentiating the type of environment
96
// and delegating the calls appropriately.
97
if (context instanceof ServletContext JavaDoc) {
98             return null;
99         } else {
100             throw new IllegalStateException JavaDoc("Unknown environment");
101         }
102     }
103
104     private boolean shouldDelegate(Object JavaDoc request) {
105         if (delegate == null) {
106             return false;
107         }
108         //We must run without portlet.jar being present, so cannot use
109
//instanceof PortletRequest
110
//if (request instanceof PortletRequest) {
111
Method JavaDoc getAttributeMethod = null;
112         Class JavaDoc portletRequestClass = null;
113         try {
114             portletRequestClass =
115                     Class.forName("javax.portlet.PortletRequest");
116             getAttributeMethod = portletRequestClass
117                     .getMethod("getAttribute", new Class JavaDoc[]{String JavaDoc.class});
118         } catch (Throwable JavaDoc t) {
119             if (log.isDebugEnabled()) {
120                 log.debug("Portlet classes not available");
121             }
122         }
123
124         if ((null != portletRequestClass) &&
125                 (portletRequestClass.isInstance(request))) {
126             //now use our reflectively obtained method to get another attribute
127
String JavaDoc portletType = null;
128             try {
129                 portletType = (String JavaDoc) getAttributeMethod
130                         .invoke(request, new Object JavaDoc[]{SERVLET_KEY});
131             } catch (Exception JavaDoc e) {
132                 if (log.isWarnEnabled()) {
133                     log.warn("Reflection failure: request.getAttribute", e);
134                 }
135             }
136
137             if (portletType != null &&
138                     portletType.equalsIgnoreCase(PERSISTENT)) {
139                 return false;
140             }
141         } else if (request instanceof ServletRequest JavaDoc) {
142             String JavaDoc servletType = (String JavaDoc) ((ServletRequest JavaDoc) request)
143                     .getAttribute(SERVLET_KEY);
144             if (servletType != null &&
145                     servletType.equalsIgnoreCase(PERSISTENT)) {
146                 return false;
147             }
148         }
149
150         return true;
151     }
152
153 }
154
Popular Tags