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