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