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