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