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 javax.naming.NamingException; |
22 | |
import javax.naming.Context; |
23 | |
import javax.annotation.Resource; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.lang.reflect.InvocationTargetException; |
26 | |
import java.lang.reflect.Field; |
27 | |
|
28 | |
|
29 | |
public class ResourceAnnotationLifecycleProvider extends NoInjectionAnnotationLifecycleProvider |
30 | |
{ |
31 | |
|
32 | |
protected Context context; |
33 | |
private static final String JAVA_COMP_ENV = "java:comp/env/"; |
34 | |
|
35 | |
public ResourceAnnotationLifecycleProvider(Context context) |
36 | 0 | { |
37 | 0 | this.context = context; |
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
protected void processAnnotations(Object instance) |
45 | |
throws IllegalAccessException, InvocationTargetException, NamingException |
46 | |
{ |
47 | |
|
48 | 0 | if (context == null) |
49 | |
{ |
50 | |
|
51 | 0 | return; |
52 | |
} |
53 | |
|
54 | 0 | checkAnnotation(instance.getClass(), instance); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | } |
68 | |
|
69 | |
private void checkAnnotation(Class clazz, Object instance) |
70 | |
throws NamingException, IllegalAccessException, InvocationTargetException |
71 | |
{ |
72 | |
|
73 | 0 | Field[] fields = clazz.getDeclaredFields(); |
74 | 0 | for (Field field : fields) |
75 | |
{ |
76 | 0 | checkFieldAnnotation(field, instance); |
77 | |
} |
78 | |
|
79 | |
|
80 | 0 | Method[] methods = clazz.getDeclaredMethods(); |
81 | 0 | for (Method method : methods) |
82 | |
{ |
83 | 0 | checkMethodAnnotation(method, instance); |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
protected void checkMethodAnnotation(Method method, Object instance) |
88 | |
throws NamingException, IllegalAccessException, InvocationTargetException |
89 | |
{ |
90 | 0 | if (method.isAnnotationPresent(Resource.class)) |
91 | |
{ |
92 | 0 | Resource annotation = method.getAnnotation(Resource.class); |
93 | 0 | lookupMethodResource(context, instance, method, annotation.name()); |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
protected void checkFieldAnnotation(Field field, Object instance) |
98 | |
throws NamingException, IllegalAccessException |
99 | |
{ |
100 | 0 | if (field.isAnnotationPresent(Resource.class)) |
101 | |
{ |
102 | 0 | Resource annotation = field.getAnnotation(Resource.class); |
103 | 0 | lookupFieldResource(context, instance, field, annotation.name()); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected static void lookupFieldResource(javax.naming.Context context, |
111 | |
Object instance, Field field, String name) |
112 | |
throws NamingException, IllegalAccessException |
113 | |
{ |
114 | |
|
115 | |
Object lookedupResource; |
116 | |
|
117 | 0 | if ((name != null) && (name.length() > 0)) |
118 | |
{ |
119 | |
|
120 | 0 | lookedupResource = context.lookup(JAVA_COMP_ENV + name); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | |
|
125 | 0 | lookedupResource = context.lookup(JAVA_COMP_ENV + instance.getClass().getName() + "/" + field.getName()); |
126 | |
} |
127 | |
|
128 | 0 | boolean accessibility = field.isAccessible(); |
129 | 0 | field.setAccessible(true); |
130 | 0 | field.set(instance, lookedupResource); |
131 | 0 | field.setAccessible(accessibility); |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
protected static void lookupMethodResource(javax.naming.Context context, |
139 | |
Object instance, Method method, String name) |
140 | |
throws NamingException, IllegalAccessException, InvocationTargetException |
141 | |
{ |
142 | |
|
143 | 0 | if (!method.getName().startsWith("set") |
144 | |
|| method.getParameterTypes().length != 1 |
145 | |
|| !method.getReturnType().getName().equals("void")) |
146 | |
{ |
147 | 0 | throw new IllegalArgumentException("Invalid method resource injection annotation"); |
148 | |
} |
149 | |
|
150 | |
Object lookedupResource; |
151 | |
|
152 | 0 | if ((name != null) && (name.length() > 0)) |
153 | |
{ |
154 | |
|
155 | 0 | lookedupResource = context.lookup(JAVA_COMP_ENV + name); |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | |
|
160 | 0 | lookedupResource = |
161 | |
context.lookup(JAVA_COMP_ENV + instance.getClass().getName() + "/" + method.getName().substring(3)); |
162 | |
} |
163 | |
|
164 | 0 | boolean accessibility = method.isAccessible(); |
165 | 0 | method.setAccessible(true); |
166 | 0 | method.invoke(instance, lookedupResource); |
167 | 0 | method.setAccessible(accessibility); |
168 | 0 | } |
169 | |
} |