View Javadoc

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