View | Details | Raw Unified | Return to bug 45285
Collapse All | Expand All

(-)java/org/apache/catalina/util/DefaultAnnotationProcessor.java (-75 / +99 lines)
Lines 5-13 Link Here
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
7
 * the License.  You may obtain a copy of the License at
8
 * 
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 * 
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Lines 42-50 Link Here
42
 * @version $Revision$, $Date$
42
 * @version $Revision$, $Date$
43
 */
43
 */
44
public class DefaultAnnotationProcessor implements AnnotationProcessor {
44
public class DefaultAnnotationProcessor implements AnnotationProcessor {
45
    
45
46
    protected javax.naming.Context context = null;
46
    protected javax.naming.Context context = null;
47
    
47
48
    public DefaultAnnotationProcessor(javax.naming.Context context) {
48
    public DefaultAnnotationProcessor(javax.naming.Context context) {
49
        this.context = context;
49
        this.context = context;
50
    }
50
    }
Lines 55-88 Link Here
55
     */
55
     */
56
    public void postConstruct(Object instance)
56
    public void postConstruct(Object instance)
57
        throws IllegalAccessException, InvocationTargetException {
57
        throws IllegalAccessException, InvocationTargetException {
58
        
58
59
        Method[] methods = instance.getClass().getDeclaredMethods();
59
        Class<?> clazz = instance.getClass();
60
        Method postConstruct = null;
60
61
        for (int i = 0; i < methods.length; i++) {
61
        // Analyze each super class until there is none
62
            if (methods[i].isAnnotationPresent(PostConstruct.class)) {
62
        while (clazz != null) {
63
                if ((postConstruct != null) 
63
            Method[] methods = clazz.getDeclaredMethods();
64
                        || (methods[i].getParameterTypes().length != 0)
64
            Method postConstruct = null;
65
                        || (Modifier.isStatic(methods[i].getModifiers())) 
65
            for (int i = 0; i < methods.length; i++) {
66
                        || (methods[i].getExceptionTypes().length > 0)
66
                if (methods[i].isAnnotationPresent(PostConstruct.class)) {
67
                        || (!methods[i].getReturnType().getName().equals("void"))) {
67
                    if ((postConstruct != null)
68
                    throw new IllegalArgumentException("Invalid PostConstruct annotation");
68
                            || (methods[i].getParameterTypes().length != 0)
69
                            || (Modifier.isStatic(methods[i].getModifiers()))
70
                            || (methods[i].getExceptionTypes().length > 0)
71
                            || (!methods[i].getReturnType().getName().equals(
72
                                    "void"))) {
73
                        throw new IllegalArgumentException(
74
                                "Invalid PostConstruct annotation");
75
                    }
76
                    postConstruct = methods[i];
69
                }
77
                }
70
                postConstruct = methods[i];
78
            }
79
80
            // At the end the postconstruct annotated
81
            // method is invoked
82
            if (postConstruct != null) {
83
                boolean accessibility = postConstruct.isAccessible();
84
                postConstruct.setAccessible(true);
85
                postConstruct.invoke(instance);
86
                postConstruct.setAccessible(accessibility);
71
            }
87
            }
72
        }
88
89
            // will analyze the super class
90
            clazz = clazz.getSuperclass();
73
91
74
        // At the end the postconstruct annotated 
75
        // method is invoked
76
        if (postConstruct != null) {
77
            boolean accessibility = postConstruct.isAccessible();
78
            postConstruct.setAccessible(true);
79
            postConstruct.invoke(instance);
80
            postConstruct.setAccessible(accessibility);
81
        }
92
        }
82
        
93
94
83
    }
95
    }
84
    
96
85
    
97
86
    /**
98
    /**
87
     * Call preDestroy method on the specified instance.
99
     * Call preDestroy method on the specified instance.
88
     */
100
     */
Lines 88-121 Link Here
88
     */
100
     */
89
    public void preDestroy(Object instance)
101
    public void preDestroy(Object instance)
90
        throws IllegalAccessException, InvocationTargetException {
102
        throws IllegalAccessException, InvocationTargetException {
91
        
103
92
        Method[] methods = instance.getClass().getDeclaredMethods();
104
        Class<?> clazz = instance.getClass();
93
        Method preDestroy = null;
105
94
        for (int i = 0; i < methods.length; i++) {
106
        // Analyze each super class until there is none
95
            if (methods[i].isAnnotationPresent(PreDestroy.class)) {
107
        while (clazz != null) {
96
                if ((preDestroy != null) 
108
97
                        || (methods[i].getParameterTypes().length != 0)
109
            Method[] methods = instance.getClass().getDeclaredMethods();
98
                        || (Modifier.isStatic(methods[i].getModifiers())) 
110
            Method preDestroy = null;
99
                        || (methods[i].getExceptionTypes().length > 0)
111
            for (int i = 0; i < methods.length; i++) {
100
                        || (!methods[i].getReturnType().getName().equals("void"))) {
112
                if (methods[i].isAnnotationPresent(PreDestroy.class)) {
101
                    throw new IllegalArgumentException("Invalid PreDestroy annotation");
113
                    if ((preDestroy != null)
114
                            || (methods[i].getParameterTypes().length != 0)
115
                            || (Modifier.isStatic(methods[i].getModifiers()))
116
                            || (methods[i].getExceptionTypes().length > 0)
117
                            || (!methods[i].getReturnType().getName().equals(
118
                                    "void"))) {
119
                        throw new IllegalArgumentException(
120
                                "Invalid PreDestroy annotation");
121
                    }
122
                    preDestroy = methods[i];
102
                }
123
                }
103
                preDestroy = methods[i];
104
            }
124
            }
105
        }
125
126
            // At the end the postconstruct annotated
127
            // method is invoked
128
            if (preDestroy != null) {
129
                boolean accessibility = preDestroy.isAccessible();
130
                preDestroy.setAccessible(true);
131
                preDestroy.invoke(instance);
132
                preDestroy.setAccessible(accessibility);
133
            }
134
135
            // will analyze the super class
136
            clazz = clazz.getSuperclass();
106
137
107
        // At the end the postconstruct annotated 
108
        // method is invoked
109
        if (preDestroy != null) {
110
            boolean accessibility = preDestroy.isAccessible();
111
            preDestroy.setAccessible(true);
112
            preDestroy.invoke(instance);
113
            preDestroy.setAccessible(accessibility);
114
        }
138
        }
115
        
139
116
    }
140
    }
117
    
141
118
    
142
119
    /**
143
    /**
120
     * Inject resources in specified instance.
144
     * Inject resources in specified instance.
121
     */
145
     */
Lines 121-127 Link Here
121
     */
145
     */
122
    public void processAnnotations(Object instance)
146
    public void processAnnotations(Object instance)
123
        throws IllegalAccessException, InvocationTargetException, NamingException {
147
        throws IllegalAccessException, InvocationTargetException, NamingException {
124
        
148
125
        if (context == null) {
149
        if (context == null) {
126
            // No resource injection
150
            // No resource injection
127
            return;
151
            return;
Lines 126-134 Link Here
126
            // No resource injection
150
            // No resource injection
127
            return;
151
            return;
128
        }
152
        }
129
        
153
130
        Class<?> clazz = instance.getClass();
154
        Class<?> clazz = instance.getClass();
131
        
155
132
        while (clazz != null) {
156
        while (clazz != null) {
133
            // Initialize fields annotations
157
            // Initialize fields annotations
134
            Field[] fields = instance.getClass().getDeclaredFields();
158
            Field[] fields = instance.getClass().getDeclaredFields();
Lines 142-148 Link Here
142
                    lookupFieldResource(context, instance, fields[i], annotation.name());
166
                    lookupFieldResource(context, instance, fields[i], annotation.name());
143
                }
167
                }
144
                if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
168
                if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
145
                    WebServiceRef annotation = 
169
                    WebServiceRef annotation =
146
                        (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
170
                        (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
147
                    lookupFieldResource(context, instance, fields[i], annotation.name());
171
                    lookupFieldResource(context, instance, fields[i], annotation.name());
148
                }
172
                }
Lines 147-153 Link Here
147
                    lookupFieldResource(context, instance, fields[i], annotation.name());
171
                    lookupFieldResource(context, instance, fields[i], annotation.name());
148
                }
172
                }
149
                if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
173
                if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
150
                    PersistenceContext annotation = 
174
                    PersistenceContext annotation =
151
                        (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class);
175
                        (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class);
152
                    lookupFieldResource(context, instance, fields[i], annotation.name());
176
                    lookupFieldResource(context, instance, fields[i], annotation.name());
153
                }
177
                }
Lines 152-158 Link Here
152
                    lookupFieldResource(context, instance, fields[i], annotation.name());
176
                    lookupFieldResource(context, instance, fields[i], annotation.name());
153
                }
177
                }
154
                if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
178
                if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
155
                    PersistenceUnit annotation = 
179
                    PersistenceUnit annotation =
156
                        (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class);
180
                        (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class);
157
                    lookupFieldResource(context, instance, fields[i], annotation.name());
181
                    lookupFieldResource(context, instance, fields[i], annotation.name());
158
                }
182
                }
Lines 157-163 Link Here
157
                    lookupFieldResource(context, instance, fields[i], annotation.name());
181
                    lookupFieldResource(context, instance, fields[i], annotation.name());
158
                }
182
                }
159
            }
183
            }
160
            
184
161
            // Initialize methods annotations
185
            // Initialize methods annotations
162
            Method[] methods = instance.getClass().getDeclaredMethods();
186
            Method[] methods = instance.getClass().getDeclaredMethods();
163
            for (int i = 0; i < methods.length; i++) {
187
            for (int i = 0; i < methods.length; i++) {
Lines 170-176 Link Here
170
                    lookupMethodResource(context, instance, methods[i], annotation.name());
194
                    lookupMethodResource(context, instance, methods[i], annotation.name());
171
                }
195
                }
172
                if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
196
                if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
173
                    WebServiceRef annotation = 
197
                    WebServiceRef annotation =
174
                        (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
198
                        (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
175
                    lookupMethodResource(context, instance, methods[i], annotation.name());
199
                    lookupMethodResource(context, instance, methods[i], annotation.name());
176
                }
200
                }
Lines 175-181 Link Here
175
                    lookupMethodResource(context, instance, methods[i], annotation.name());
199
                    lookupMethodResource(context, instance, methods[i], annotation.name());
176
                }
200
                }
177
                if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
201
                if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
178
                    PersistenceContext annotation = 
202
                    PersistenceContext annotation =
179
                        (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class);
203
                        (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class);
180
                    lookupMethodResource(context, instance, methods[i], annotation.name());
204
                    lookupMethodResource(context, instance, methods[i], annotation.name());
181
                }
205
                }
Lines 180-186 Link Here
180
                    lookupMethodResource(context, instance, methods[i], annotation.name());
204
                    lookupMethodResource(context, instance, methods[i], annotation.name());
181
                }
205
                }
182
                if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
206
                if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
183
                    PersistenceUnit annotation = 
207
                    PersistenceUnit annotation =
184
                        (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class);
208
                        (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class);
185
                    lookupMethodResource(context, instance, methods[i], annotation.name());
209
                    lookupMethodResource(context, instance, methods[i], annotation.name());
186
                }
210
                }
Lines 185-206 Link Here
185
                    lookupMethodResource(context, instance, methods[i], annotation.name());
209
                    lookupMethodResource(context, instance, methods[i], annotation.name());
186
                }
210
                }
187
            }
211
            }
188
            
212
189
            clazz = clazz.getSuperclass();
213
            clazz = clazz.getSuperclass();
190
        }
214
        }
191
    }
215
    }
192
    
216
193
    
217
194
    /**
218
    /**
195
     * Inject resources in specified field.
219
     * Inject resources in specified field.
196
     */
220
     */
197
    protected static void lookupFieldResource(javax.naming.Context context, 
221
    protected static void lookupFieldResource(javax.naming.Context context,
198
            Object instance, Field field, String name)
222
            Object instance, Field field, String name)
199
        throws NamingException, IllegalAccessException {
223
        throws NamingException, IllegalAccessException {
200
    
224
201
        Object lookedupResource = null;
225
        Object lookedupResource = null;
202
        boolean accessibility = false;
226
        boolean accessibility = false;
203
        
227
204
        if ((name != null) &&
228
        if ((name != null) &&
205
                (name.length() > 0)) {
229
                (name.length() > 0)) {
206
            lookedupResource = context.lookup(name);
230
            lookedupResource = context.lookup(name);
Lines 207-213 Link Here
207
        } else {
231
        } else {
208
            lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());
232
            lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());
209
        }
233
        }
210
        
234
211
        accessibility = field.isAccessible();
235
        accessibility = field.isAccessible();
212
        field.setAccessible(true);
236
        field.setAccessible(true);
213
        field.set(instance, lookedupResource);
237
        field.set(instance, lookedupResource);
Lines 218-228 Link Here
218
    /**
242
    /**
219
     * Inject resources in specified method.
243
     * Inject resources in specified method.
220
     */
244
     */
221
    protected static void lookupMethodResource(javax.naming.Context context, 
245
    protected static void lookupMethodResource(javax.naming.Context context,
222
            Object instance, Method method, String name)
246
            Object instance, Method method, String name)
223
        throws NamingException, IllegalAccessException, InvocationTargetException {
247
        throws NamingException, IllegalAccessException, InvocationTargetException {
224
        
248
225
        if (!method.getName().startsWith("set") 
249
        if (!method.getName().startsWith("set")
226
                || method.getParameterTypes().length != 1
250
                || method.getParameterTypes().length != 1
227
                || !method.getReturnType().getName().equals("void")) {
251
                || !method.getReturnType().getName().equals("void")) {
228
            throw new IllegalArgumentException("Invalid method resource injection annotation");
252
            throw new IllegalArgumentException("Invalid method resource injection annotation");
Lines 227-236 Link Here
227
                || !method.getReturnType().getName().equals("void")) {
251
                || !method.getReturnType().getName().equals("void")) {
228
            throw new IllegalArgumentException("Invalid method resource injection annotation");
252
            throw new IllegalArgumentException("Invalid method resource injection annotation");
229
        }
253
        }
230
        
254
231
        Object lookedupResource = null;
255
        Object lookedupResource = null;
232
        boolean accessibility = false;
256
        boolean accessibility = false;
233
        
257
234
        if ((name != null) &&
258
        if ((name != null) &&
235
                (name.length() > 0)) {
259
                (name.length() > 0)) {
236
            lookedupResource = context.lookup(name);
260
            lookedupResource = context.lookup(name);
Lines 235-244 Link Here
235
                (name.length() > 0)) {
259
                (name.length() > 0)) {
236
            lookedupResource = context.lookup(name);
260
            lookedupResource = context.lookup(name);
237
        } else {
261
        } else {
238
            lookedupResource = 
262
            lookedupResource =
239
                context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3));
263
                context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3));
240
        }
264
        }
241
        
265
242
        accessibility = method.isAccessible();
266
        accessibility = method.isAccessible();
243
        method.setAccessible(true);
267
        method.setAccessible(true);
244
        method.invoke(instance, lookedupResource);
268
        method.invoke(instance, lookedupResource);
Lines 244-249 Link Here
244
        method.invoke(instance, lookedupResource);
268
        method.invoke(instance, lookedupResource);
245
        method.setAccessible(accessibility);
269
        method.setAccessible(accessibility);
246
    }
270
    }
247
    
271
248
272
249
}
273
}

Return to bug 45285