1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.webapp;
20
21 import javax.faces.FacesException;
22 import javax.servlet.ServletContext;
23
24 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
25 import org.apache.myfaces.shared.config.MyfacesConfig;
26 import org.apache.myfaces.shared.util.ClassUtils;
27 import org.apache.myfaces.util.ContainerUtils;
28
29 /**
30 * Simple Factory to get a FacesInitializer implementation either from a web.xml
31 * parameter or from a list of default implementations.
32 *
33 * @author Jakob Korherr (latest modification by $Author: lu4242 $)
34 * @version $Revision: 1296049 $ $Date: 2012-03-01 23:48:16 -0500 (Thu, 01 Mar 2012) $
35 */
36 public class FacesInitializerFactory
37 {
38
39 /**
40 * Indicate the class implementing FacesInitializer interface that will
41 * be used to setup MyFaces Core contexts.
42 * <p>This is used when some custom task must be done specifically when
43 * a myfaces web context is initialized or destroyed, or when MyFaces should
44 * be initialized in some custom environment.
45 * </p>
46 */
47 @JSFWebConfigParam(since = "2.0.1", desc = "Class name of a custom FacesInitializer implementation.")
48 private static final String FACES_INITIALIZER_PARAM = "org.apache.myfaces.FACES_INITIALIZER";
49
50 /**
51 * Gets the FacesInitializer for the system.
52 * @param context
53 * @return
54 */
55 public static FacesInitializer getFacesInitializer(ServletContext context)
56 {
57 FacesInitializer initializer = _getFacesInitializerFromInitParam(context);
58 if (initializer == null)
59 {
60 initializer = _getDefaultFacesInitializer(context);
61 }
62 return initializer;
63 }
64
65 /**
66 * Gets a FacesInitializer from the web.xml config param.
67 * @param context
68 * @return
69 */
70 private static FacesInitializer _getFacesInitializerFromInitParam(ServletContext context)
71 {
72 String initializerClassName = context.getInitParameter(FACES_INITIALIZER_PARAM);
73 if (initializerClassName != null)
74 {
75 try
76 {
77 // get Class object
78 Class<?> clazz = ClassUtils.classForName(initializerClassName);
79 if (!FacesInitializer.class.isAssignableFrom(clazz))
80 {
81 throw new FacesException("Class " + clazz
82 + " does not implement FacesInitializer");
83 }
84
85 // create instance and return it
86 return (FacesInitializer) ClassUtils.newInstance(clazz);
87 }
88 catch (ClassNotFoundException cnfe)
89 {
90 throw new FacesException("Could not find class of specified FacesInitializer", cnfe);
91 }
92 }
93 return null;
94 }
95
96 /**
97 * Returns a FacesInitializer that fits for the current environment (JSP 2.0 or 2.1).
98 * @param context
99 * @return
100 */
101 private static FacesInitializer _getDefaultFacesInitializer(ServletContext context)
102 {
103 // No MyfacesConfig available yet, we must read the parameter directly:
104 String initParameter = context.getInitParameter(MyfacesConfig.INIT_PARAM_SUPPORT_JSP_AND_FACES_EL);
105 if (Boolean.FALSE.toString().equals(initParameter))
106 {
107 return new FaceletsInitilializer();
108 }
109 else if (ContainerUtils.isJsp21(context))
110 {
111 return new Jsp21FacesInitializer();
112 }
113 else
114 {
115 return new Jsp20FacesInitializer();
116 }
117 }
118
119 }