View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-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.plus.annotation;
15  
16  import java.lang.reflect.Method;
17  import java.lang.reflect.Modifier;
18  
19  import org.eclipse.jetty.util.IntrospectionUtil;
20  
21  
22  
23  /**
24   * LifeCycleCallback
25   *
26   *
27   */
28  public abstract class LifeCycleCallback
29  {
30      public static final Object[] __EMPTY_ARGS = new Object[] {};
31      private Method _target;
32      private Class _targetClass;
33      
34      
35      public LifeCycleCallback()
36      {
37      }
38  
39  
40      /**
41       * @return the _targetClass
42       */
43      public Class getTargetClass()
44      {
45          return _targetClass;
46      }
47  
48  
49      /**
50       * @param name the class to set
51       */
52      public void setTargetClass(Class clazz)
53      {
54          _targetClass = clazz;
55      }
56      
57      
58      /**
59       * @return the target
60       */
61      public Method getTarget()
62      {
63          return _target;
64      }
65  
66      /**
67       * @param target the target to set
68       */
69      public void setTarget(Method target)
70      {
71          this._target = target;
72      }
73      
74      
75      
76  
77      public void setTarget (Class clazz, String methodName)
78      {
79          try
80          {
81              Method method = IntrospectionUtil.findMethod(clazz, methodName, null, true, true);
82              validate(clazz, method);
83              _target = method;
84              _targetClass = clazz;
85          }
86          catch (NoSuchMethodException e)
87          {
88              throw new IllegalArgumentException ("Method "+methodName+" not found on class "+clazz.getName());
89          }
90      }
91  
92  
93      
94      
95      public void callback (Object instance)
96      throws Exception
97      {
98          if (getTarget() != null)
99          {
100             boolean accessibility = getTarget().isAccessible();
101             getTarget().setAccessible(true);
102             getTarget().invoke(instance, __EMPTY_ARGS);
103             getTarget().setAccessible(accessibility);
104         }
105     }
106 
107     
108 
109     /**
110      * Find a method of the given name either directly in the given
111      * class, or inherited.
112      * 
113      * @param pack the package of the class under inspection
114      * @param clazz the class under inspection
115      * @param methodName the method to find 
116      * @param checkInheritance false on first entry, true if a superclass is being introspected
117      * @return
118      */
119     public Method findMethod (Package pack, Class clazz, String methodName, boolean checkInheritance)
120     {
121         if (clazz == null)
122             return null;
123 
124         try
125         {
126             Method method = clazz.getDeclaredMethod(methodName, null);
127             if (checkInheritance)
128             {
129                 int modifiers = method.getModifiers();
130                 if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers) || (!Modifier.isPrivate(modifiers)&&(pack.equals(clazz.getPackage()))))
131                     return method;
132                 else
133                     return findMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, true);
134             }
135             return method;
136         }
137         catch (NoSuchMethodException e)
138         {
139             return findMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, true);
140         }
141     }
142 
143     public boolean equals (Object o)
144     {
145         if (o==null)
146             return false;
147         if (!(o instanceof LifeCycleCallback))
148             return false;
149         LifeCycleCallback callback = (LifeCycleCallback)o;
150         
151         if (callback.getTargetClass()==null)
152         {
153             if (getTargetClass() != null)
154                 return false;
155         }
156         else if(!callback.getTargetClass().equals(getTargetClass()))
157            return false;
158         if (callback.getTarget()==null)
159         {
160             if (getTarget() != null)
161                 return false;
162         }
163         else if (!callback.getTarget().equals(getTarget()))
164             return false;
165         
166         return true;
167     }
168     
169     public abstract void validate (Class clazz, Method m);
170 }