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