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