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.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import javax.faces.context.ExternalContext;
26 import javax.naming.NamingException;
27 import javax.servlet.ServletContext;
28
29 import org.apache.myfaces.shared.util.ClassUtils;
30
31 public class TomcatAnnotationLifecycleProvider implements
32 DiscoverableLifecycleProvider, LifecycleProvider2
33 {
34
35 private static Logger log = Logger.getLogger(TomcatAnnotationLifecycleProvider.class.getName());
36
37 private ExternalContext externalContext;
38 private org.apache.AnnotationProcessor annotationProcessor;
39
40 public TomcatAnnotationLifecycleProvider(ExternalContext externalContext)
41 {
42 this.externalContext = externalContext;
43 }
44
45
46 public Object newInstance(String className)
47 throws InstantiationException, IllegalAccessException, InvocationTargetException,
48 NamingException, ClassNotFoundException
49 {
50 Class<?> clazz = ClassUtils.classForName(className);
51 if (log.isLoggable(Level.FINEST))
52 {
53 log.finest("Creating instance of " + className);
54 }
55 Object object = clazz.newInstance();
56 annotationProcessor.processAnnotations(object);
57
58 return object;
59 }
60
61
62 public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException
63 {
64 if (log.isLoggable(Level.FINEST))
65 {
66 log.info("Destroy instance of " + o.getClass().getName());
67 }
68 annotationProcessor.preDestroy(o);
69 }
70
71 public boolean isAvailable()
72 {
73 try
74 {
75 annotationProcessor = (org.apache.AnnotationProcessor) ((ServletContext)
76 externalContext.getContext()).getAttribute(org.apache.AnnotationProcessor.class.getName());
77 return annotationProcessor != null;
78 }
79 catch (Throwable e)
80 {
81
82 }
83 return false;
84 }
85
86
87 public void postConstruct(Object o) throws IllegalAccessException,
88 InvocationTargetException
89 {
90 annotationProcessor.postConstruct(o);
91 }
92
93 }