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

(-)java/org/apache/catalina/startup/WebAnnotationSet.java (-68 / +178 lines)
Lines 19-24 Link Here
19
package org.apache.catalina.startup;
19
package org.apache.catalina.startup;
20
20
21
21
22
import java.lang.reflect.Field;
23
import java.lang.reflect.Method;
24
import java.security.AccessController;
25
import java.security.PrivilegedAction;
26
22
import javax.annotation.Resource;
27
import javax.annotation.Resource;
23
import javax.annotation.Resources;
28
import javax.annotation.Resources;
24
import javax.annotation.security.DeclareRoles;
29
import javax.annotation.security.DeclareRoles;
Lines 26-31 Link Here
26
31
27
import org.apache.catalina.Container;
32
import org.apache.catalina.Container;
28
import org.apache.catalina.Context;
33
import org.apache.catalina.Context;
34
import org.apache.catalina.Globals;
35
import org.apache.catalina.core.DefaultInstanceManager;
29
import org.apache.catalina.core.StandardWrapper;
36
import org.apache.catalina.core.StandardWrapper;
30
import org.apache.catalina.deploy.ContextEnvironment;
37
import org.apache.catalina.deploy.ContextEnvironment;
31
import org.apache.catalina.deploy.ContextResource;
38
import org.apache.catalina.deploy.ContextResource;
Lines 44-49 Link Here
44
51
45
public class WebAnnotationSet {
52
public class WebAnnotationSet {
46
    
53
    
54
    private static final String SEPARATOR = "/";
47
    
55
    
48
    // --------------------------------------------------------- Public Methods
56
    // --------------------------------------------------------- Public Methods
49
    
57
    
Lines 68-76 Link Here
68
     * Process the annotations for the listeners.
76
     * Process the annotations for the listeners.
69
     */
77
     */
70
    protected static void loadApplicationListenerAnnotations(Context context) {
78
    protected static void loadApplicationListenerAnnotations(Context context) {
79
        Class<?> classClass = null;
71
        String[] applicationListeners = context.findApplicationListeners();
80
        String[] applicationListeners = context.findApplicationListeners();
72
        for (int i = 0; i < applicationListeners.length; i++) {
81
        for (int i = 0; i < applicationListeners.length; i++) {
73
            loadClassAnnotation(context, applicationListeners[i]);
82
            classClass = loadClass(context, applicationListeners[i]);
83
            if (classClass == null) {
84
                continue;
85
            }
86
            
87
            loadClassAnnotation(context, classClass);
88
            loadFieldsAnnotation(context, classClass);
89
            loadMethodsAnnotation(context, classClass);
74
        }
90
        }
75
    }
91
    }
76
    
92
    
Lines 79-87 Link Here
79
     * Process the annotations for the filters.
95
     * Process the annotations for the filters.
80
     */
96
     */
81
    protected static void loadApplicationFilterAnnotations(Context context) {
97
    protected static void loadApplicationFilterAnnotations(Context context) {
98
        Class<?> classClass = null;
82
        FilterDef[] filterDefs = context.findFilterDefs();
99
        FilterDef[] filterDefs = context.findFilterDefs();
83
        for (int i = 0; i < filterDefs.length; i++) {
100
        for (int i = 0; i < filterDefs.length; i++) {
84
            loadClassAnnotation(context, (filterDefs[i]).getFilterClass());
101
            classClass = loadClass(context, (filterDefs[i]).getFilterClass());
102
            if (classClass == null) {
103
                continue;
104
            }
105
            
106
            loadClassAnnotation(context, classClass);
107
            loadFieldsAnnotation(context, classClass);
108
            loadMethodsAnnotation(context, classClass);
85
        }
109
        }
86
    }
110
    }
87
    
111
    
Lines 90-97 Link Here
90
     * Process the annotations for the servlets.
114
     * Process the annotations for the servlets.
91
     */
115
     */
92
    protected static void loadApplicationServletAnnotations(Context context) {
116
    protected static void loadApplicationServletAnnotations(Context context) {
93
        
94
        ClassLoader classLoader = context.getLoader().getClassLoader();
95
        StandardWrapper wrapper = null;
117
        StandardWrapper wrapper = null;
96
        Class<?> classClass = null;
118
        Class<?> classClass = null;
97
        
119
        
Lines 104-122 Link Here
104
                    continue;
126
                    continue;
105
                }
127
                }
106
                
128
                
107
                try {
129
                classClass = loadClass(context, wrapper.getServletClass());
108
                    classClass = classLoader.loadClass(wrapper.getServletClass());
109
                } catch (ClassNotFoundException e) {
110
                    // We do nothing
111
                } catch (NoClassDefFoundError e) {
112
                    // We do nothing
113
                }
114
                
115
                if (classClass == null) {
130
                if (classClass == null) {
116
                    continue;
131
                    continue;
117
                }
132
                }
118
                
133
                
119
                loadClassAnnotation(context, wrapper.getServletClass());
134
                loadClassAnnotation(context, classClass);
135
                loadFieldsAnnotation(context, classClass);
136
                loadMethodsAnnotation(context, classClass);
137
                
120
                /* Process RunAs annotation which can be only on servlets.
138
                /* Process RunAs annotation which can be only on servlets.
121
                 * Ref JSR 250, equivalent to the run-as element in
139
                 * Ref JSR 250, equivalent to the run-as element in
122
                 * the deployment descriptor
140
                 * the deployment descriptor
Lines 135-157 Link Here
135
    /**
153
    /**
136
     * Process the annotations on a context for a given className.
154
     * Process the annotations on a context for a given className.
137
     */
155
     */
138
    protected static void loadClassAnnotation(Context context, String fileString) {
156
    protected static void loadClassAnnotation(Context context, Class<?> classClass) {
139
        
140
        ClassLoader classLoader = context.getLoader().getClassLoader();
141
        Class<?> classClass = null;
142
        
143
        try {
144
            classClass = classLoader.loadClass(fileString);
145
        } catch (ClassNotFoundException e) {
146
            // We do nothing
147
        } catch (NoClassDefFoundError e) {
148
            // We do nothing
149
        }
150
        
151
        if (classClass == null) {
152
            return;
153
        }
154
        
155
        // Initialize the annotations
157
        // Initialize the annotations
156
        
158
        
157
        if (classClass.isAnnotationPresent(Resource.class)) {
159
        if (classClass.isAnnotationPresent(Resource.class)) {
Lines 249-255 Link Here
249
        
251
        
250
    }
252
    }
251
    
253
    
254
    protected static void loadFieldsAnnotation(Context context, Class<?> classClass) {
255
        // Initialize the annotations
256
        Field[] fields = getDeclaredFields(classClass);
257
        if (fields != null && fields.length > 0) {
258
            for (Field field : fields) {
259
                if (field.isAnnotationPresent(Resource.class)) {
260
                    Resource annotation = field.getAnnotation(Resource.class);
261
                    String defaultName = classClass.getName() + SEPARATOR + field.getName();
262
                    String defaultType = field.getType().getCanonicalName();
263
                    addResource(context, annotation, defaultName, defaultType);
264
                }
265
            }
266
        }
267
    }    
252
    
268
    
269
    protected static void loadMethodsAnnotation(Context context, Class<?> classClass) {
270
        // Initialize the annotations
271
        Method[] methods = getDeclaredMethods(classClass);
272
        if (methods != null && methods.length > 0) {
273
            for (Method method : methods) {
274
                if (method.isAnnotationPresent(Resource.class)) {
275
                    Resource annotation = method.getAnnotation(Resource.class);
276
                    
277
                    checkBeanNamingConventions(method);
278
                    
279
                    String defaultName = classClass.getName() + SEPARATOR + DefaultInstanceManager.getName(method);
280
                    String defaultType = (method.getParameterTypes()[0]).getCanonicalName();
281
                    addResource(context, annotation, defaultName, defaultType);
282
                }
283
            }
284
        }
285
    }
286
    
253
    /**
287
    /**
254
     * Process a Resource annotation to set up a Resource.
288
     * Process a Resource annotation to set up a Resource.
255
     * Ref JSR 250, equivalent to the resource-ref,
289
     * Ref JSR 250, equivalent to the resource-ref,
Lines 257-278 Link Here
257
     * or service-ref element in the deployment descriptor.
291
     * or service-ref element in the deployment descriptor.
258
     */
292
     */
259
    protected static void addResource(Context context, Resource annotation) {
293
    protected static void addResource(Context context, Resource annotation) {
294
        addResource(context, annotation, null, null);
295
    }
296
    
297
    protected static void addResource(Context context, Resource annotation, String defaultName, String defaultType) {
298
        String name = getName(annotation, defaultName);
260
        
299
        
261
        if (annotation.type().getCanonicalName().equals("java.lang.String") ||
300
        String type = getType(annotation, defaultType);
262
                annotation.type().getCanonicalName().equals("java.lang.Character") ||
301
        
263
                annotation.type().getCanonicalName().equals("java.lang.Integer") ||
302
        if (type.equals("java.lang.String") ||
264
                annotation.type().getCanonicalName().equals("java.lang.Boolean") ||
303
                type.equals("java.lang.Character") ||
265
                annotation.type().getCanonicalName().equals("java.lang.Double") ||
304
                type.equals("java.lang.Integer") ||
266
                annotation.type().getCanonicalName().equals("java.lang.Byte") ||
305
                type.equals("java.lang.Boolean") ||
267
                annotation.type().getCanonicalName().equals("java.lang.Short") ||
306
                type.equals("java.lang.Double") ||
268
                annotation.type().getCanonicalName().equals("java.lang.Long") ||
307
                type.equals("java.lang.Byte") ||
269
                annotation.type().getCanonicalName().equals("java.lang.Float")) {
308
                type.equals("java.lang.Short") ||
309
                type.equals("java.lang.Long") ||
310
                type.equals("java.lang.Float")) {
270
            
311
            
271
            // env-ref element
312
            // env-ref element
272
            ContextEnvironment resource = new ContextEnvironment();
313
            ContextEnvironment resource = new ContextEnvironment();
273
            
314
            
274
            resource.setName(annotation.name());
315
            resource.setName(name);
275
            resource.setType(annotation.type().getCanonicalName());
316
            resource.setType(type);
276
            
317
            
277
            resource.setDescription(annotation.description());
318
            resource.setDescription(annotation.description());
278
            
319
            
Lines 280-316 Link Here
280
            
321
            
281
            context.getNamingResources().addEnvironment(resource);
322
            context.getNamingResources().addEnvironment(resource);
282
            
323
            
283
        } else if (annotation.type().getCanonicalName().equals("javax.xml.rpc.Service")) {
324
        } else if (type.equals("javax.xml.rpc.Service")) {
284
            
325
            
285
            // service-ref element
326
            // service-ref element
286
            ContextService service = new ContextService();
327
            ContextService service = new ContextService();
287
            
328
            
288
            service.setName(annotation.name());
329
            service.setName(name);
289
            service.setWsdlfile(annotation.mappedName());
330
            service.setWsdlfile(annotation.mappedName());
290
            
331
            
291
            service.setType(annotation.type().getCanonicalName());
332
            service.setType(type);
292
            service.setDescription(annotation.description());
333
            service.setDescription(annotation.description());
293
            
334
            
294
            context.getNamingResources().addService(service);
335
            context.getNamingResources().addService(service);
295
            
336
            
296
        } else if (annotation.type().getCanonicalName().equals("javax.sql.DataSource") ||
337
        } else if (type.equals("javax.sql.DataSource") ||
297
                annotation.type().getCanonicalName().equals("javax.jms.ConnectionFactory") ||
338
                type.equals("javax.jms.ConnectionFactory") ||
298
                annotation.type().getCanonicalName()
339
                type.equals("javax.jms.QueueConnectionFactory") ||
299
                .equals("javax.jms.QueueConnectionFactory") ||
340
                type.equals("javax.jms.TopicConnectionFactory") ||
300
                annotation.type().getCanonicalName()
341
                type.equals("javax.mail.Session") ||
301
                .equals("javax.jms.TopicConnectionFactory") ||
342
                type.equals("java.net.URL") ||
302
                annotation.type().getCanonicalName().equals("javax.mail.Session") ||
343
                type.equals("javax.resource.cci.ConnectionFactory") ||
303
                annotation.type().getCanonicalName().equals("java.net.URL") ||
344
                type.equals("org.omg.CORBA_2_3.ORB") ||
304
                annotation.type().getCanonicalName()
345
                type.endsWith("ConnectionFactory")) {
305
                .equals("javax.resource.cci.ConnectionFactory") ||
306
                annotation.type().getCanonicalName().equals("org.omg.CORBA_2_3.ORB") ||
307
                annotation.type().getCanonicalName().endsWith("ConnectionFactory")) {
308
            
346
            
309
            // resource-ref element
347
            // resource-ref element
310
            ContextResource resource = new ContextResource();
348
            ContextResource resource = new ContextResource();
311
            
349
            
312
            resource.setName(annotation.name());
350
            resource.setName(name);
313
            resource.setType(annotation.type().getCanonicalName());
351
            resource.setType(type);
314
            
352
            
315
            if (annotation.authenticationType()
353
            if (annotation.authenticationType()
316
                    == Resource.AuthenticationType.CONTAINER) {
354
                    == Resource.AuthenticationType.CONTAINER) {
Lines 327-357 Link Here
327
            
365
            
328
            context.getNamingResources().addResource(resource);
366
            context.getNamingResources().addResource(resource);
329
            
367
            
330
        } else if (annotation.type().getCanonicalName().equals("javax.jms.Queue") ||
368
        } else if (type.equals("javax.jms.Queue") ||
331
                annotation.type().getCanonicalName().equals("javax.jms.Topic")) {
369
                type.equals("javax.jms.Topic")) {
332
            
370
            
333
            // message-destination-ref
371
            // message-destination-ref
334
            MessageDestinationRef resource = new MessageDestinationRef();
372
            MessageDestinationRef resource = new MessageDestinationRef();
335
            
373
            
336
            resource.setName(annotation.name());
374
            resource.setName(name);
337
            resource.setType(annotation.type().getCanonicalName());
375
            resource.setType(type);
338
            
376
            
339
            resource.setUsage(annotation.mappedName());
377
            resource.setUsage(annotation.mappedName());
340
            resource.setDescription(annotation.description());
378
            resource.setDescription(annotation.description());
341
            
379
            
342
            context.getNamingResources().addMessageDestinationRef(resource);
380
            context.getNamingResources().addMessageDestinationRef(resource);
343
            
381
            
344
        } else if (annotation.type().getCanonicalName()
382
        } else if (type.equals("javax.resource.cci.InteractionSpec") ||
345
                .equals("javax.resource.cci.InteractionSpec") ||
383
                type.equals("javax.transaction.UserTransaction") ||
346
                annotation.type().getCanonicalName()
347
                .equals("javax.transaction.UserTransaction") ||
348
                true) {
384
                true) {
349
            
385
            
350
            // resource-env-ref
386
            // resource-env-ref
351
            ContextResourceEnvRef resource = new ContextResourceEnvRef();
387
            ContextResourceEnvRef resource = new ContextResourceEnvRef();
352
            
388
            
353
            resource.setName(annotation.name());
389
            resource.setName(name);
354
            resource.setType(annotation.type().getCanonicalName());
390
            resource.setType(type);
355
            
391
            
356
            resource.setProperty("mappedName", annotation.mappedName());
392
            resource.setProperty("mappedName", annotation.mappedName());
357
            resource.setDescription(annotation.description());
393
            resource.setDescription(annotation.description());
Lines 363-367 Link Here
363
        
399
        
364
    }
400
    }
365
    
401
    
402
    private static void checkBeanNamingConventions(Method method) {
403
        if (!method.getName().startsWith("set")
404
                || method.getName().length() < 4
405
                || method.getParameterTypes().length != 1
406
                || !method.getReturnType().getName().equals("void")) {
407
            throw new IllegalArgumentException("Invalid method resource injection annotation.");
408
        }
409
    }
366
    
410
    
411
    private static String getType(Resource annotation, String defaultType) {
412
        String type = annotation.type().getCanonicalName();
413
        if (type == null || type.equals("java.lang.Object")) {
414
            if (defaultType != null) {
415
                type = defaultType;
416
            }
417
        }
418
        return type;
419
    }
420
    
421
    private static String getName(Resource annotation, String defaultName) {
422
        String name = annotation.name();
423
        if (name == null || name.equals("")) {
424
            if (defaultName != null) {
425
                name = defaultName;
426
            }
427
        }
428
        return name;
429
    }
430
    
431
    private static Field[] getDeclaredFields(Class<?> classClass) {
432
        Field[] fields = null;
433
        if (Globals.IS_SECURITY_ENABLED) {
434
            final Class<?> clazz = classClass;
435
            fields = AccessController.doPrivileged(
436
                    new PrivilegedAction<Field[]>(){
437
                @Override
438
                public Field[] run(){
439
                    return clazz.getDeclaredFields();
440
                }
441
            });
442
        } else {
443
            fields = classClass.getDeclaredFields();
444
        }
445
        return fields;
446
    }
447
    
448
    private static Method[] getDeclaredMethods(Class<?> classClass) {
449
        Method[] methods = null;
450
        if (Globals.IS_SECURITY_ENABLED) {
451
            final Class<?> clazz = classClass;
452
            methods = AccessController.doPrivileged(
453
                    new PrivilegedAction<Method[]>(){
454
                @Override
455
                public Method[] run(){
456
                    return clazz.getDeclaredMethods();
457
                }
458
            });
459
        } else {
460
            methods = classClass.getDeclaredMethods();
461
        }
462
        return methods;
463
    }
464
    
465
    private static Class<?> loadClass(Context context, String fileString) {
466
        ClassLoader classLoader = context.getLoader().getClassLoader();
467
        Class<?> classClass = null;
468
        try {
469
            classClass = classLoader.loadClass(fileString);
470
        } catch (ClassNotFoundException e) {
471
            // We do nothing
472
        } catch (NoClassDefFoundError e) {
473
            // We do nothing
474
        }
475
        return classClass;
476
    }
367
}
477
}

Return to bug 52974