KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > webapp > parser > ImplementationUtil


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.webapp.parser;
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.servlet.jsp.PageContext JavaDoc;
42 import java.util.List JavaDoc;
43
44 import com.icesoft.faces.webapp.parser.ComponentRuleSet;
45
46 /**
47  * For ICEfaces to support JSF-RI, MyFaces, or any other future JSF
48  * implementations, it may require some logic specific to the implementation.
49  * Obviously this is not a good thing but it may be unavoidable if we need to
50  * access something under the hood that isn't available through public API
51  * calls. This class is available to encapsulate implementation specific
52  * anomalies.
53  */

54 public class ImplementationUtil {
55
56     /**
57      * Logging instance for this class.
58      */

59     protected static Log log = LogFactory.getLog(ImplementationUtil.class);
60
61
62     /**
63      * Boolean values to track which implementation we are running under.
64      */

65     private static boolean isRI = false;
66     private static boolean isMyFaces = false;
67
68     /**
69      * Marker classes whose presence we used to detect which implementation we
70      * are running under.
71      */

72     private static String JavaDoc RI_MARKER =
73             "com.sun.faces.application.ApplicationImpl";
74     private static String JavaDoc MYFACES_MARKER =
75             "org.apache.myfaces.application.ApplicationImpl";
76
77     /**
78      * In a couple of places, we need to access the component stack from the
79      * PageContext and the key used to do this is implemenation dependent. So
80      * here is where we track the keys and provide appropriate value depending
81      * on the implementation we are running under.
82      */

83     private static String JavaDoc RI_COMPONENT_STACK_KEY =
84             "javax.faces.webapp.COMPONENT_TAG_STACK";
85
86     private static String JavaDoc MYFACES_COMPONENT_STACK_KEY =
87             "javax.faces.webapp.UIComponentTag.COMPONENT_STACK";
88
89
90     static {
91         try {
92             Class.forName(RI_MARKER);
93             isRI = true;
94         } catch (ClassNotFoundException JavaDoc e) {
95         }
96
97         try {
98             Class.forName(MYFACES_MARKER);
99             isMyFaces = true;
100         } catch (ClassNotFoundException JavaDoc e) {
101         }
102
103         if (log.isTraceEnabled()) {
104             log.trace("JSF-RI: " + isRI + " MyFaces: " + isMyFaces);
105         }
106
107     }
108
109     /**
110      * Identifies if the JSF implementation we are running in is Sun's JSF
111      * reference implemenation (RI).
112      *
113      * @return true if the JSF implementation is Sun's JSF reference
114      * implemenation
115      */

116     public static boolean isRI() {
117         return isRI;
118     }
119
120     /**
121      * Identifies if the JSF implementation we are running in is Apache's
122      * MyFaces implementation.
123      *
124      * @return true if the JSF implementation is Apache MyFaces.
125      */

126     public static boolean isMyFaces() {
127         return isMyFaces;
128     }
129
130     /**
131      * Identifies if the JSF implementation we are running in is JSF 1.2
132      *
133      * @return true if the JSF implementation is JSF 1.2
134      */

135     public static boolean isJSF12() {
136         return ComponentRuleSet.isJSF12();
137     }
138
139
140     /**
141      * Returns the key used to get the component stack from the PageContext. The
142      * key is a private member of UIComponentTag class but differs between the
143      * known JSF implementations. We detect the correct implementation in this
144      * class and provide the proper key.
145      *
146      * @return String
147      */

148     public static String JavaDoc getComponentStackKey() {
149         String JavaDoc key = null;
150         if (isRI) {
151             key = RI_COMPONENT_STACK_KEY;
152         } else if (isMyFaces) {
153             key = MYFACES_COMPONENT_STACK_KEY;
154         }
155
156         if (key != null) {
157             return key;
158         }
159
160         if (log.isFatalEnabled()) {
161             log.fatal(
162                     "cannot detect JSF implementation so cannot determine component stack key");
163         }
164
165         throw new UnknownJSFImplementationException(
166                 "cannot determine component stack key");
167     }
168
169     /**
170      * Returns the component tag stack, including checking in the current
171      * request as is the strategy of JSF 1.1_02
172      * @param pageContext
173      * @return list being the component tag stack
174      */

175     public static List JavaDoc getComponentStack(PageContext JavaDoc pageContext) {
176         List JavaDoc list = (List JavaDoc) pageContext.getAttribute(
177                 getComponentStackKey(), PageContext.REQUEST_SCOPE);
178         if (null == list) {
179             list = (List JavaDoc) FacesContext.getCurrentInstance()
180                     .getExternalContext().getRequestMap().get(
181                     getComponentStackKey());
182         }
183         return list;
184
185     }
186 }
187
188 class UnknownJSFImplementationException extends FacesException {
189
190     public UnknownJSFImplementationException() {
191     }
192
193     public UnknownJSFImplementationException(Throwable JavaDoc cause) {
194         super(cause);
195     }
196
197     public UnknownJSFImplementationException(String JavaDoc message) {
198         super(message);
199     }
200
201     public UnknownJSFImplementationException(String JavaDoc message, Throwable JavaDoc cause) {
202         super(message, cause);
203     }
204
205 }
206
Popular Tags