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.PreDestroy;
25
26 import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
27 import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
28 import org.eclipse.jetty.plus.annotation.PreDestroyCallback;
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 PreDestroyAnnotationHandler extends AbstractIntrospectableAnnotationHandler
34 {
35 WebAppContext _context;
36
37 public PreDestroyAnnotationHandler (WebAppContext wac)
38 {
39 super(true);
40 _context = wac;
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(PreDestroy.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("pre-destroy");
67 if (origin != null &&
68 (origin == Origin.WebXml ||
69 origin == Origin.WebDefaults ||
70 origin == Origin.WebOverride))
71 return;
72
73 PreDestroyCallback callback = new PreDestroyCallback();
74 callback.setTarget(clazz.getName(), m.getName());
75
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
83 lifecycles.add(callback);
84 }
85 }
86 }
87 }
88 }