1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.config.annotation; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.security.AccessController; |
23 | |
import java.security.PrivilegedActionException; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.logging.Level; |
27 | |
import java.util.logging.Logger; |
28 | |
|
29 | |
import javax.faces.FacesException; |
30 | |
import javax.faces.context.ExternalContext; |
31 | |
import javax.naming.Context; |
32 | |
import javax.naming.InitialContext; |
33 | |
import javax.naming.NamingException; |
34 | |
|
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
36 | |
import org.apache.myfaces.shared.util.ClassUtils; |
37 | |
import org.apache.myfaces.spi.ServiceProviderFinderFactory; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class DefaultLifecycleProviderFactory extends LifecycleProviderFactory |
44 | |
{ |
45 | |
|
46 | 0 | private static Logger log = Logger.getLogger(DefaultLifecycleProviderFactory.class.getName()); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public static final String LIFECYCLE_PROVIDER_INSTANCE_KEY |
55 | |
= LifecycleProvider.class.getName() + ".LIFECYCLE_PROVIDER_INSTANCE"; |
56 | |
|
57 | |
@JSFWebConfigParam(name="org.apache.myfaces.config.annotation.LifecycleProvider", since="1.1") |
58 | 0 | public static final String LIFECYCLE_PROVIDER = LifecycleProvider.class.getName(); |
59 | |
|
60 | |
|
61 | |
public DefaultLifecycleProviderFactory() |
62 | 0 | { |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public LifecycleProvider getLifecycleProvider(ExternalContext externalContext) |
67 | |
{ |
68 | 0 | LifecycleProvider lifecycleProvider = null; |
69 | 0 | if (externalContext == null) |
70 | |
{ |
71 | |
|
72 | |
|
73 | |
|
74 | 0 | log.info("No ExternalContext using fallback LifecycleProvider."); |
75 | 0 | lifecycleProvider = resolveFallbackLifecycleProvider(); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | lifecycleProvider = (LifecycleProvider) |
80 | |
externalContext.getApplicationMap().get(LIFECYCLE_PROVIDER_INSTANCE_KEY); |
81 | |
} |
82 | 0 | if (lifecycleProvider == null) |
83 | |
{ |
84 | 0 | if (!resolveLifecycleProviderFromExternalContext(externalContext)) |
85 | |
{ |
86 | 0 | if (!resolveLifecycleProviderFromService(externalContext)) |
87 | |
{ |
88 | 0 | lifecycleProvider = resolveFallbackLifecycleProvider(); |
89 | 0 | externalContext.getApplicationMap().put(LIFECYCLE_PROVIDER_INSTANCE_KEY, lifecycleProvider); |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | |
|
94 | 0 | lifecycleProvider = (LifecycleProvider) |
95 | |
externalContext.getApplicationMap().get(LIFECYCLE_PROVIDER_INSTANCE_KEY); |
96 | |
} |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | |
|
101 | 0 | lifecycleProvider = (LifecycleProvider) |
102 | |
externalContext.getApplicationMap().get(LIFECYCLE_PROVIDER_INSTANCE_KEY); |
103 | |
} |
104 | 0 | log.info("Using LifecycleProvider "+ lifecycleProvider.getClass().getName()); |
105 | |
} |
106 | 0 | return lifecycleProvider; |
107 | |
} |
108 | |
|
109 | |
@Override |
110 | |
public void release() |
111 | |
{ |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
private boolean resolveLifecycleProviderFromExternalContext(ExternalContext externalContext) |
117 | |
{ |
118 | |
try |
119 | |
{ |
120 | 0 | String lifecycleProvider = externalContext.getInitParameter(LIFECYCLE_PROVIDER); |
121 | 0 | if (lifecycleProvider != null) |
122 | |
{ |
123 | |
|
124 | 0 | Object obj = createClass(lifecycleProvider, externalContext); |
125 | |
|
126 | 0 | if (obj instanceof LifecycleProvider) |
127 | |
{ |
128 | 0 | externalContext.getApplicationMap().put(LIFECYCLE_PROVIDER_INSTANCE_KEY, obj); |
129 | 0 | return true; |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 0 | catch (ClassNotFoundException e) |
134 | |
{ |
135 | 0 | log.log(Level.SEVERE, "", e); |
136 | |
} |
137 | 0 | catch (InstantiationException e) |
138 | |
{ |
139 | 0 | log.log(Level.SEVERE, "", e); |
140 | |
} |
141 | 0 | catch (IllegalAccessException e) |
142 | |
{ |
143 | 0 | log.log(Level.SEVERE, "", e); |
144 | |
} |
145 | 0 | catch (InvocationTargetException e) |
146 | |
{ |
147 | 0 | log.log(Level.SEVERE, "", e); |
148 | 0 | } |
149 | 0 | return false; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
private boolean resolveLifecycleProviderFromService( |
154 | |
ExternalContext externalContext) |
155 | |
{ |
156 | 0 | boolean returnValue = false; |
157 | 0 | final ExternalContext extContext = externalContext; |
158 | |
try |
159 | |
{ |
160 | 0 | if (System.getSecurityManager() != null) |
161 | |
{ |
162 | 0 | returnValue = AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Boolean>() |
163 | 0 | { |
164 | |
public Boolean run() throws ClassNotFoundException, |
165 | |
NoClassDefFoundError, |
166 | |
InstantiationException, |
167 | |
IllegalAccessException, |
168 | |
InvocationTargetException, |
169 | |
PrivilegedActionException |
170 | |
{ |
171 | 0 | List<String> classList |
172 | |
= ServiceProviderFinderFactory.getServiceProviderFinder(extContext). |
173 | |
getServiceProviderList(LIFECYCLE_PROVIDER); |
174 | 0 | Iterator<String> iter = classList.iterator(); |
175 | 0 | while (iter.hasNext()) |
176 | |
{ |
177 | 0 | String className = iter.next(); |
178 | 0 | Object obj = createClass(className,extContext); |
179 | 0 | if (DiscoverableLifecycleProvider.class.isAssignableFrom(obj.getClass())) |
180 | |
{ |
181 | 0 | DiscoverableLifecycleProvider discoverableLifecycleProvider = |
182 | |
(DiscoverableLifecycleProvider) obj; |
183 | 0 | if (discoverableLifecycleProvider.isAvailable()) |
184 | |
{ |
185 | 0 | extContext.getApplicationMap().put(LIFECYCLE_PROVIDER_INSTANCE_KEY, |
186 | |
discoverableLifecycleProvider); |
187 | 0 | return true; |
188 | |
} |
189 | |
} |
190 | 0 | } |
191 | 0 | return false; |
192 | |
} |
193 | |
}); |
194 | |
} |
195 | |
else |
196 | |
{ |
197 | 0 | List<String> classList = ServiceProviderFinderFactory.getServiceProviderFinder(extContext). |
198 | |
getServiceProviderList(LIFECYCLE_PROVIDER); |
199 | 0 | Iterator<String> iter = classList.iterator(); |
200 | 0 | while (iter.hasNext()) |
201 | |
{ |
202 | 0 | String className = iter.next(); |
203 | 0 | Object obj = createClass(className,extContext); |
204 | 0 | if (DiscoverableLifecycleProvider.class.isAssignableFrom(obj.getClass())) |
205 | |
{ |
206 | 0 | DiscoverableLifecycleProvider discoverableLifecycleProvider |
207 | |
= (DiscoverableLifecycleProvider) obj; |
208 | 0 | if (discoverableLifecycleProvider.isAvailable()) |
209 | |
{ |
210 | 0 | extContext.getApplicationMap().put(LIFECYCLE_PROVIDER_INSTANCE_KEY, |
211 | |
discoverableLifecycleProvider); |
212 | 0 | return (Boolean) true; |
213 | |
} |
214 | |
} |
215 | 0 | } |
216 | |
} |
217 | |
} |
218 | 0 | catch (ClassNotFoundException e) |
219 | |
{ |
220 | |
|
221 | |
} |
222 | 0 | catch (NoClassDefFoundError e) |
223 | |
{ |
224 | |
|
225 | |
} |
226 | 0 | catch (InstantiationException e) |
227 | |
{ |
228 | 0 | log.log(Level.SEVERE, "", e); |
229 | |
} |
230 | 0 | catch (IllegalAccessException e) |
231 | |
{ |
232 | 0 | log.log(Level.SEVERE, "", e); |
233 | |
} |
234 | 0 | catch (InvocationTargetException e) |
235 | |
{ |
236 | 0 | log.log(Level.SEVERE, "", e); |
237 | |
} |
238 | 0 | catch (PrivilegedActionException e) |
239 | |
{ |
240 | 0 | throw new FacesException(e); |
241 | 0 | } |
242 | 0 | return returnValue; |
243 | |
} |
244 | |
|
245 | |
private Object createClass(String className, ExternalContext externalContext) |
246 | |
throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException |
247 | |
{ |
248 | 0 | Class<?> clazz = ClassUtils.classForName(className); |
249 | |
|
250 | |
try |
251 | |
{ |
252 | 0 | return ClassUtils.newInstance(clazz, new Class<?>[]{ ExternalContext.class }, externalContext); |
253 | |
} |
254 | 0 | catch (NoSuchMethodException e) |
255 | |
{ |
256 | 0 | return ClassUtils.newInstance(clazz); |
257 | |
} |
258 | |
} |
259 | |
|
260 | |
|
261 | |
private LifecycleProvider resolveFallbackLifecycleProvider() |
262 | |
{ |
263 | |
try |
264 | |
{ |
265 | 0 | ClassUtils.classForName("javax.annotation.PreDestroy"); |
266 | |
} |
267 | 0 | catch (ClassNotFoundException e) |
268 | |
{ |
269 | |
|
270 | 0 | return new NoAnnotationLifecyleProvider(); |
271 | 0 | } |
272 | |
Context context; |
273 | |
try |
274 | |
{ |
275 | 0 | context = new InitialContext(); |
276 | |
try |
277 | |
{ |
278 | 0 | ClassUtils.classForName("javax.ejb.EJB"); |
279 | |
|
280 | 0 | return new AllAnnotationLifecycleProvider(context); |
281 | |
} |
282 | 0 | catch (ClassNotFoundException e) |
283 | |
{ |
284 | |
|
285 | 0 | return new ResourceAnnotationLifecycleProvider(context); |
286 | |
} |
287 | |
} |
288 | 0 | catch (NamingException e) |
289 | |
{ |
290 | |
|
291 | 0 | log.log(Level.SEVERE, "No InitialContext found. Using NoInjectionAnnotationProcessor.", e); |
292 | 0 | return new NoInjectionAnnotationLifecycleProvider(); |
293 | |
} |
294 | 0 | catch (NoClassDefFoundError e) |
295 | |
{ |
296 | |
|
297 | |
|
298 | |
|
299 | 0 | log.log(Level.SEVERE, "No InitialContext class definition found. Using NoInjectionAnnotationProcessor."); |
300 | 0 | return new NoInjectionAnnotationLifecycleProvider(); |
301 | |
} |
302 | |
} |
303 | |
} |