1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
126
127
128
129
130
131
132
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 }