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.Origin;
26 import org.eclipse.jetty.webapp.WebAppContext;
27
28 public class PreDestroyAnnotationHandler extends AbstractIntrospectableAnnotationHandler
29 {
30 WebAppContext _context;
31
32 public PreDestroyAnnotationHandler (WebAppContext wac)
33 {
34 super(true);
35 _context = wac;
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(PreDestroy.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 Origin origin = metaData.getOrigin("pre-destroy");
62 if (origin != null &&
63 (origin == Origin.WebXml ||
64 origin == Origin.WebDefaults ||
65 origin == Origin.WebOverride))
66 return;
67
68 PreDestroyCallback callback = new PreDestroyCallback();
69 callback.setTarget(clazz.getName(), m.getName());
70
71 LifeCycleCallbackCollection lifecycles = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
72 if (lifecycles == null)
73 {
74 lifecycles = new LifeCycleCallbackCollection();
75 _context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, lifecycles);
76 }
77
78 lifecycles.add(callback);
79 }
80 }
81 }
82 }
83 }