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 LifeCycleCallbackCollection _callbacks;
31
32 public PreDestroyAnnotationHandler (WebAppContext wac)
33 {
34 super(true);
35 _context = wac;
36 _callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
37 }
38
39 public void doHandle(Class clazz)
40 {
41
42 if (Util.isServletType(clazz))
43 {
44 Method[] methods = clazz.getDeclaredMethods();
45 for (int i=0; i<methods.length; i++)
46 {
47 Method m = (Method)methods[i];
48 if (m.isAnnotationPresent(PreDestroy.class))
49 {
50 if (m.getParameterTypes().length != 0)
51 throw new IllegalStateException(m+" has parameters");
52 if (m.getReturnType() != Void.TYPE)
53 throw new IllegalStateException(m+" is not void");
54 if (m.getExceptionTypes().length != 0)
55 throw new IllegalStateException(m+" throws checked exceptions");
56 if (Modifier.isStatic(m.getModifiers()))
57 throw new IllegalStateException(m+" is static");
58
59
60
61 MetaData metaData = ((MetaData)_context.getAttribute(MetaData.METADATA));
62 MetaData.Origin origin = metaData.getOrigin("pre-destroy");
63 if (origin != null &&
64 (origin == MetaData.Origin.WebXml ||
65 origin == MetaData.Origin.WebDefaults ||
66 origin == MetaData.Origin.WebOverride))
67 return;
68
69 PreDestroyCallback callback = new PreDestroyCallback();
70 callback.setTarget(clazz.getName(), m.getName());
71 _callbacks.add(callback);
72 }
73 }
74 }
75 }
76 }