View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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          //Check that the PreDestroy is on a class that we're interested in
46          if (Util.supportsPostConstructPreDestroy(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                      //ServletSpec 3.0 p80 If web.xml declares even one predestroy then all predestroys
64                      //in fragments must be ignored. Otherwise, they are additive.
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  }