1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.spi.impl; |
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.spi.InjectionProvider; |
30 | |
import org.apache.myfaces.spi.InjectionProviderException; |
31 | |
|
32 | |
public class TomcatAnnotationInjectionProvider extends InjectionProvider |
33 | |
{ |
34 | 0 | private static Logger log = Logger.getLogger(TomcatAnnotationInjectionProvider.class.getName()); |
35 | |
|
36 | |
private ExternalContext externalContext; |
37 | |
private org.apache.AnnotationProcessor annotationProcessor; |
38 | |
|
39 | |
public TomcatAnnotationInjectionProvider(ExternalContext externalContext) |
40 | 0 | { |
41 | 0 | this.externalContext = externalContext; |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
public Object inject(Object instance) throws InjectionProviderException |
46 | |
{ |
47 | |
try |
48 | |
{ |
49 | 0 | annotationProcessor.processAnnotations(instance); |
50 | |
} |
51 | 0 | catch (IllegalAccessException ex) |
52 | |
{ |
53 | 0 | throw new InjectionProviderException(ex); |
54 | |
} |
55 | 0 | catch (InvocationTargetException ex) |
56 | |
{ |
57 | 0 | throw new InjectionProviderException(ex); |
58 | |
} |
59 | 0 | catch (NamingException ex) |
60 | |
{ |
61 | 0 | throw new InjectionProviderException(ex); |
62 | 0 | } |
63 | 0 | return null; |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public void preDestroy(Object instance, Object creationMetaData) throws InjectionProviderException |
68 | |
{ |
69 | 0 | if (log.isLoggable(Level.FINEST)) |
70 | |
{ |
71 | 0 | log.info("Destroy instance of " + instance.getClass().getName()); |
72 | |
} |
73 | |
try |
74 | |
{ |
75 | 0 | annotationProcessor.preDestroy(instance); |
76 | |
} |
77 | 0 | catch (IllegalAccessException ex) |
78 | |
{ |
79 | 0 | throw new InjectionProviderException(ex); |
80 | |
} |
81 | 0 | catch (InvocationTargetException ex) |
82 | |
{ |
83 | 0 | throw new InjectionProviderException(ex); |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | |
@Override |
88 | |
public boolean isAvailable() |
89 | |
{ |
90 | |
try |
91 | |
{ |
92 | 0 | annotationProcessor = (org.apache.AnnotationProcessor) ((ServletContext) |
93 | |
externalContext.getContext()).getAttribute(org.apache.AnnotationProcessor.class.getName()); |
94 | 0 | return annotationProcessor != null; |
95 | |
} |
96 | 0 | catch (Throwable e) |
97 | |
{ |
98 | |
|
99 | |
} |
100 | 0 | return false; |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public void postConstruct(Object instance, Object creationMetaData) throws InjectionProviderException |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 0 | annotationProcessor.postConstruct(instance); |
109 | |
} |
110 | 0 | catch (IllegalAccessException ex) |
111 | |
{ |
112 | 0 | throw new InjectionProviderException(ex); |
113 | |
} |
114 | 0 | catch (InvocationTargetException ex) |
115 | |
{ |
116 | 0 | throw new InjectionProviderException(ex); |
117 | 0 | } |
118 | 0 | } |
119 | |
} |