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