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