1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view;
20
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import javax.faces.context.FacesContext;
25 import javax.faces.view.ViewDeclarationLanguage;
26 import javax.faces.view.ViewDeclarationLanguageFactory;
27
28 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
29 import org.apache.myfaces.shared.config.MyfacesConfig;
30 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageStrategy;
31 import org.apache.myfaces.view.jsp.JspViewDeclarationLanguageStrategy;
32
33
34
35
36
37
38
39
40
41
42 public class ViewDeclarationLanguageFactoryImpl extends ViewDeclarationLanguageFactory
43 {
44
45
46
47 @JSFWebConfigParam(since="2.0", defaultValue="false", expectedValues="true,false", group="viewhandler")
48 public static final String PARAM_DISABLE_JSF_FACELET = "javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER";
49
50 private static final String FACELETS_1_VIEW_HANDLER = "com.sun.facelets.FaceletViewHandler";
51
52 private static final Logger LOGGER = Logger.getLogger(ViewDeclarationLanguageFactoryImpl.class.getName());
53
54 private volatile boolean _initialized;
55 private volatile ViewDeclarationLanguageStrategy[] _supportedLanguages;
56
57
58
59
60 public ViewDeclarationLanguageFactoryImpl()
61 {
62 _initialized = false;
63 }
64
65
66
67
68 @Override
69 public ViewDeclarationLanguage getViewDeclarationLanguage(String viewId)
70 {
71
72
73
74
75
76
77
78
79 if (!_initialized)
80 {
81 initialize();
82 }
83
84 for (ViewDeclarationLanguageStrategy strategy : _supportedLanguages)
85 {
86 if (strategy.handles(viewId))
87 {
88 return strategy.getViewDeclarationLanguage();
89 }
90 }
91
92
93
94
95
96
97
98 return null;
99 }
100
101
102
103
104 private synchronized void initialize()
105 {
106 if (!_initialized)
107 {
108 FacesContext context = FacesContext.getCurrentInstance();
109
110 if (isFacelets2Enabled(context))
111 {
112 logWarningIfLegacyFaceletViewHandlerIsPresent(context);
113
114 if (MyfacesConfig.getCurrentInstance(
115 context.getExternalContext()).isSupportJSPAndFacesEL())
116 {
117 _supportedLanguages = new ViewDeclarationLanguageStrategy[2];
118 _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
119 _supportedLanguages[1] = new JspViewDeclarationLanguageStrategy();
120 }
121 else
122 {
123 _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
124 _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
125 }
126 }
127 else
128 {
129
130 _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
131 _supportedLanguages[0] = new JspViewDeclarationLanguageStrategy();
132 }
133
134 _initialized = true;
135 }
136 }
137
138
139
140
141
142
143
144
145
146 private boolean isFacelets2Enabled(FacesContext context)
147 {
148 String param = context.getExternalContext().getInitParameter(PARAM_DISABLE_JSF_FACELET);
149 boolean facelets2ParamDisabled = (param != null && Boolean.parseBoolean(param.toLowerCase()));
150
151 return !facelets2ParamDisabled;
152 }
153
154
155
156
157
158
159
160 private void logWarningIfLegacyFaceletViewHandlerIsPresent(FacesContext context)
161 {
162 boolean facelets1ViewHandlerPresent
163 = context.getApplication().getViewHandler().getClass().getName().equals(FACELETS_1_VIEW_HANDLER);
164
165 if (facelets1ViewHandlerPresent)
166 {
167 if (LOGGER.isLoggable(Level.WARNING))
168 {
169 LOGGER.log(Level.WARNING, "Your faces-config.xml contains the " + FACELETS_1_VIEW_HANDLER + " class."
170 + "\nYou need to remove it since you have not disabled the \"new\" Facelets-2 version with the "
171 + PARAM_DISABLE_JSF_FACELET + " context parameter");
172 }
173 }
174 }
175 }