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 {
37 this.context = context;
38 }
39
40
41
42
43
44 protected void processAnnotations(Object instance)
45 throws IllegalAccessException, InvocationTargetException, NamingException
46 {
47
48 if (context == null)
49 {
50
51 return;
52 }
53
54 checkAnnotation(instance.getClass(), instance);
55
56
57
58
59
60
61
62
63
64
65
66
67 }
68
69 private void checkAnnotation(Class clazz, Object instance)
70 throws NamingException, IllegalAccessException, InvocationTargetException
71 {
72
73 Field[] fields = clazz.getDeclaredFields();
74 for (Field field : fields)
75 {
76 checkFieldAnnotation(field, instance);
77 }
78
79
80 Method[] methods = clazz.getDeclaredMethods();
81 for (Method method : methods)
82 {
83 checkMethodAnnotation(method, instance);
84 }
85 }
86
87 protected void checkMethodAnnotation(Method method, Object instance)
88 throws NamingException, IllegalAccessException, InvocationTargetException
89 {
90 if (method.isAnnotationPresent(Resource.class))
91 {
92 Resource annotation = method.getAnnotation(Resource.class);
93 lookupMethodResource(context, instance, method, annotation.name());
94 }
95 }
96
97 protected void checkFieldAnnotation(Field field, Object instance)
98 throws NamingException, IllegalAccessException
99 {
100 if (field.isAnnotationPresent(Resource.class))
101 {
102 Resource annotation = field.getAnnotation(Resource.class);
103 lookupFieldResource(context, instance, field, annotation.name());
104 }
105 }
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 if ((name != null) && (name.length() > 0))
118 {
119
120 lookedupResource = context.lookup(JAVA_COMP_ENV + name);
121 }
122 else
123 {
124
125 lookedupResource = context.lookup(JAVA_COMP_ENV + instance.getClass().getName() + "/" + field.getName());
126 }
127
128 boolean accessibility = field.isAccessible();
129 field.setAccessible(true);
130 field.set(instance, lookedupResource);
131 field.setAccessible(accessibility);
132 }
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 if (!method.getName().startsWith("set")
144 || method.getParameterTypes().length != 1
145 || !method.getReturnType().getName().equals("void"))
146 {
147 throw new IllegalArgumentException("Invalid method resource injection annotation");
148 }
149
150 Object lookedupResource;
151
152 if ((name != null) && (name.length() > 0))
153 {
154
155 lookedupResource = context.lookup(JAVA_COMP_ENV + name);
156 }
157 else
158 {
159
160 lookedupResource =
161 context.lookup(JAVA_COMP_ENV + instance.getClass().getName() + "/" + getFieldName(method));
162 }
163
164 boolean accessibility = method.isAccessible();
165 method.setAccessible(true);
166 method.invoke(instance, lookedupResource);
167 method.setAccessible(accessibility);
168 }
169
170
171
172
173
174
175
176
177 protected static String getFieldName(Method setter)
178 {
179 StringBuilder name = new StringBuilder(setter.getName());
180
181
182 name.delete(0, 3);
183
184
185 name.setCharAt(0, Character.toLowerCase(name.charAt(0)));
186
187 return name.toString();
188 }
189
190 }