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 if (!_initialized)
72 {
73 initialize();
74 }
75
76 for (ViewDeclarationLanguageStrategy strategy : _supportedLanguages)
77 {
78 if (strategy.handles(viewId))
79 {
80 return strategy.getViewDeclarationLanguage();
81 }
82 }
83
84 return null;
85 }
86
87
88
89
90 private synchronized void initialize()
91 {
92 if (!_initialized)
93 {
94 FacesContext context = FacesContext.getCurrentInstance();
95
96 if (isFacelets2Enabled(context))
97 {
98 logWarningIfLegacyFaceletViewHandlerIsPresent(context);
99
100 if (MyfacesConfig.getCurrentInstance(
101 context.getExternalContext()).isSupportJSPAndFacesEL())
102 {
103 _supportedLanguages = new ViewDeclarationLanguageStrategy[2];
104 _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
105 _supportedLanguages[1] = new JspViewDeclarationLanguageStrategy();
106 }
107 else
108 {
109 _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
110 _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
111 }
112 }
113 else
114 {
115
116 _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
117 _supportedLanguages[0] = new JspViewDeclarationLanguageStrategy();
118 }
119
120 _initialized = true;
121 }
122 }
123
124
125
126
127
128
129
130
131
132 private boolean isFacelets2Enabled(FacesContext context)
133 {
134 String param = context.getExternalContext().getInitParameter(PARAM_DISABLE_JSF_FACELET);
135 boolean facelets2ParamDisabled = (param != null && Boolean.parseBoolean(param.toLowerCase()));
136
137 return !facelets2ParamDisabled;
138 }
139
140
141
142
143
144
145
146 private void logWarningIfLegacyFaceletViewHandlerIsPresent(FacesContext context)
147 {
148 boolean facelets1ViewHandlerPresent
149 = context.getApplication().getViewHandler().getClass().getName().equals(FACELETS_1_VIEW_HANDLER);
150
151 if (facelets1ViewHandlerPresent)
152 {
153 if (LOGGER.isLoggable(Level.WARNING))
154 {
155 LOGGER.log(Level.WARNING, "Your faces-config.xml contains the " + FACELETS_1_VIEW_HANDLER + " class."
156 + "\nYou need to remove it since you have not disabled the \"new\" Facelets-2 version with the "
157 + PARAM_DISABLE_JSF_FACELET + " context parameter");
158 }
159 }
160 }
161 }