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  import java.util.List;
19  
20  import org.eclipse.jetty.annotations.AnnotationParser.AnnotationHandler;
21  import org.eclipse.jetty.annotations.AnnotationParser.Value;
22  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
23  import org.eclipse.jetty.plus.annotation.PreDestroyCallback;
24  import org.eclipse.jetty.util.Loader;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.webapp.WebAppContext;
27  
28  public class PreDestroyAnnotationHandler implements AnnotationHandler
29  {
30      WebAppContext _wac;
31      
32      public PreDestroyAnnotationHandler (WebAppContext wac)
33      {
34          _wac = wac;
35      }
36  
37      public void handleClass(String className, int version, int access, String signature, String superName, String[] interfaces, String annotation,
38                              List<Value> values)
39      {
40          Log.warn("@PreDestroy annotation not applicable for classes: "+className);      
41      }
42  
43      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
44                              List<Value> values)
45      {
46          Log.warn("@PreDestroy annotation not applicable for fields: "+className+"."+fieldName);     
47      }
48  
49      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
50                               List<Value> values)
51      {
52          LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_wac.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
53  
54          Class clazz = null;
55          try
56          {
57              clazz = Loader.loadClass(null, className);
58  
59              Method m = clazz.getDeclaredMethod(methodName, Util.convertTypes(params));
60              if (!Util.isServletType(m.getDeclaringClass()))
61              {
62                  Log.debug("Ignored "+m.getName()+" as non-servlet type");
63                  return;
64              }
65              if (m.getParameterTypes().length != 0)
66                  throw new IllegalStateException(m+" has parameters");
67              if (m.getReturnType() != Void.TYPE)
68                  throw new IllegalStateException(m+" is not void");
69              if (m.getExceptionTypes().length != 0)
70                  throw new IllegalStateException(m+" throws checked exceptions");
71              if (Modifier.isStatic(m.getModifiers()))
72                  throw new IllegalStateException(m+" is static");
73  
74              PreDestroyCallback callback = new PreDestroyCallback(); 
75              callback.setTargetClass(m.getDeclaringClass());
76              callback.setTarget(m);
77              callbacks.add(callback);
78          }
79          catch (Exception e)
80          {
81              Log.warn(e);
82          }
83      }
84  }