1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.annotations;
20
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23
24 import javax.annotation.PostConstruct;
25
26 import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
27 import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
28 import org.eclipse.jetty.plus.annotation.PostConstructCallback;
29 import org.eclipse.jetty.webapp.MetaData;
30 import org.eclipse.jetty.webapp.Origin;
31 import org.eclipse.jetty.webapp.WebAppContext;
32
33 public class PostConstructAnnotationHandler extends AbstractIntrospectableAnnotationHandler
34 {
35 protected WebAppContext _context;
36
37 public PostConstructAnnotationHandler (WebAppContext wac)
38 {
39 super(true);
40 _context = wac;
41 }
42
43
44 public void doHandle(Class clazz)
45 {
46
47 if (Util.supportsPostConstructPreDestroy(clazz))
48 {
49 Method[] methods = clazz.getDeclaredMethods();
50 for (int i=0; i<methods.length; i++)
51 {
52 Method m = (Method)methods[i];
53 if (m.isAnnotationPresent(PostConstruct.class))
54 {
55 if (m.getParameterTypes().length != 0)
56 throw new IllegalStateException(m+" has parameters");
57 if (m.getReturnType() != Void.TYPE)
58 throw new IllegalStateException(m+" is not void");
59 if (m.getExceptionTypes().length != 0)
60 throw new IllegalStateException(m+" throws checked exceptions");
61 if (Modifier.isStatic(m.getModifiers()))
62 throw new IllegalStateException(m+" is static");
63
64
65
66 MetaData metaData = _context.getMetaData();
67 Origin origin = metaData.getOrigin("post-construct");
68 if (origin != null &&
69 (origin == Origin.WebXml ||
70 origin == Origin.WebDefaults ||
71 origin == Origin.WebOverride))
72 return;
73
74 PostConstructCallback callback = new PostConstructCallback();
75 callback.setTarget(clazz.getName(), m.getName());
76 LifeCycleCallbackCollection lifecycles = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
77 if (lifecycles == null)
78 {
79 lifecycles = new LifeCycleCallbackCollection();
80 _context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION,lifecycles);
81 }
82 lifecycles.add(callback);
83 }
84 }
85 }
86 }
87 }