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.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          //Check that the PreDestroy is on a class that we're interested in
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                      //ServletSpec 3.0 p80 If web.xml declares even one predestroy then all predestroys
59                      //in fragments must be ignored. Otherwise, they are additive.                    
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  }