1 package org.apache.torque.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.commons.pool.BasePoolableObjectFactory;
25 import org.apache.torque.TorqueException;
26
27 /***
28 * @version $Id: MethodCacheKey.java 474969 2006-11-14 20:41:36Z tv $
29 */
30 public class MethodCacheKey implements Serializable
31 {
32 /***
33 * Serial version
34 */
35 private static final long serialVersionUID = -1831486431185021200L;
36
37 int n;
38 private Serializable instanceOrClass;
39 private String method;
40 private Serializable arg1;
41 private Serializable arg2;
42 private Serializable arg3;
43 private Serializable[] moreThanThree;
44 private String groupKey;
45
46 public MethodCacheKey()
47 {
48 }
49
50 public MethodCacheKey(Serializable instanceOrClass, String method)
51 {
52 init(instanceOrClass, method);
53 }
54
55 public MethodCacheKey(Serializable instanceOrClass, String method,
56 Serializable arg1)
57 {
58 init(instanceOrClass, method, arg1);
59 }
60
61 public MethodCacheKey(Serializable instanceOrClass, String method,
62 Serializable arg1, Serializable arg2)
63 {
64 init(instanceOrClass, method, arg1, arg2);
65 }
66
67 public MethodCacheKey(Serializable instanceOrClass, String method,
68 Serializable arg1, Serializable arg2,
69 Serializable arg3)
70 {
71 init(instanceOrClass, method, arg1, arg2, arg3);
72 }
73
74 public MethodCacheKey(Serializable[] moreThanThree)
75 {
76 init(moreThanThree);
77 }
78
79 /***
80 * Initialize key for method with no arguments.
81 *
82 * @param instanceOrClass the Object on which the method is invoked. if
83 * the method is static, a String representing the class name is used.
84 * @param method the method name
85 */
86 public void init(Serializable instanceOrClass, String method)
87 {
88 n = 0;
89 this.instanceOrClass = instanceOrClass;
90 this.method = method;
91 groupKey = instanceOrClass.toString() + method;
92 }
93
94 /***
95 * Initialize key for method with one argument.
96 *
97 * @param instanceOrClass the Object on which the method is invoked. if
98 * the method is static, a String representing the class name is used.
99 * @param method the method name
100 * @param arg1 first method arg, may be null
101 */
102 public void init(Serializable instanceOrClass, String method,
103 Serializable arg1)
104 {
105 init(instanceOrClass, method);
106 n = 1;
107 this.arg1 = arg1;
108 }
109
110 /***
111 * Initialize key for method with two arguments.
112 *
113 * @param instanceOrClass the Object on which the method is invoked. if
114 * the method is static, a String representing the class name is used.
115 * @param method the method name
116 * @param arg1 first method arg, may be null
117 * @param arg2 second method arg, may be null
118 */
119 public void init(Serializable instanceOrClass, String method,
120 Serializable arg1, Serializable arg2)
121 {
122 init(instanceOrClass, method);
123 n = 2;
124 this.arg1 = arg1;
125 this.arg2 = arg2;
126 }
127
128
129 /***
130 * Initialize key for method with two arguments.
131 *
132 * @param instanceOrClass the Object on which the method is invoked. if
133 * the method is static, a String representing the class name is used.
134 * @param method the method name
135 * @param arg1 first method arg, may be null
136 * @param arg2 second method arg, may be null
137 */
138 public void init(Serializable instanceOrClass, String method,
139 Serializable arg1, Serializable arg2,
140 Serializable arg3)
141 {
142 init(instanceOrClass, method);
143 n = 3;
144 this.arg1 = arg1;
145 this.arg2 = arg2;
146 this.arg3 = arg3;
147 }
148
149 /***
150 * Initialize key for method with more than three arguments.
151 *
152 * @param keys Serializable[] where
153 * [0]=>the Object on which the method is invoked
154 * if the method is static, a String representing the class name is used.
155 * [1]=>the method name
156 * [n] where n>1 are the method arguments
157 */
158 public void init(Serializable[] keys)
159 {
160 init(keys[0], (String) keys[1]);
161 n = keys.length - 2;
162 if (n > 0)
163 {
164 this.arg1 = keys[2];
165 if (n > 1)
166 {
167 this.arg2 = keys[3];
168 if (n > 2)
169 {
170 this.arg3 = keys[4];
171 if (n > 3)
172 {
173 this.moreThanThree = keys;
174 }
175 }
176 }
177 }
178 }
179
180 public String getGroupKey()
181 {
182 return groupKey;
183 }
184
185 public boolean equals(Object obj)
186 {
187 boolean equal = false;
188 if (obj instanceof MethodCacheKey)
189 {
190 MethodCacheKey sck = (MethodCacheKey) obj;
191 equal = (sck.n == n);
192 equal &= ObjectUtils.equals(sck.method, method);
193 equal &= ObjectUtils.equals(sck.instanceOrClass, instanceOrClass);
194 if (equal && n > 0)
195 {
196 equal &= ObjectUtils.equals(sck.arg1, arg1);
197 if (equal && n > 1)
198 {
199 equal &= ObjectUtils.equals(sck.arg2, arg2);
200 if (equal && n > 2)
201 {
202 equal &= ObjectUtils.equals(sck.arg3, arg3);
203 if (equal && n > 3)
204 {
205 for (int i = 5; i < n + 2; i++)
206 {
207 equal &= ObjectUtils.equals(
208 sck.moreThanThree[i], moreThanThree[i]);
209 }
210 }
211 }
212 }
213 }
214 }
215
216 return equal;
217 }
218
219 public int hashCode()
220 {
221 int h = instanceOrClass.hashCode();
222 h += method.hashCode();
223 if (n > 0)
224 {
225 h += (arg1 == null ? 0 : arg1.hashCode());
226 if (n > 1)
227 {
228 h += (arg2 == null ? 0 : arg2.hashCode());
229 if (n > 2)
230 {
231 h += (arg3 == null ? 0 : arg3.hashCode());
232 if (n > 3)
233 {
234 for (int i = 5; i < n + 2; i++)
235 {
236 h += (moreThanThree[i] == null ? 0
237 : moreThanThree[i].hashCode());
238 }
239 }
240 }
241 }
242 }
243 return h;
244 }
245
246 public String toString()
247 {
248 StringBuffer sb = new StringBuffer(50);
249 sb.append(instanceOrClass);
250 sb.append("::");
251 sb.append(method).append('(');
252 if (n > 0)
253 {
254 sb.append(arg1);
255 if (n > 1)
256 {
257 sb.append(", ").append(arg2);
258 if (n > 2)
259 {
260 sb.append(", ").append(arg3);
261 if (n > 3)
262 {
263 for (int i = 5; i < n + 2; i++)
264 {
265 sb.append(", ").append(moreThanThree[i]);
266 }
267 }
268 }
269 }
270 }
271 sb.append(')');
272 return sb.toString();
273 }
274
275
276
277 public static class Factory
278 extends BasePoolableObjectFactory
279 {
280 /***
281 * Creates an instance that can be returned by the pool.
282 * @return an instance that can be returned by the pool.
283 */
284 public Object makeObject()
285 throws Exception
286 {
287 return new MethodCacheKey();
288 }
289
290 /***
291 * Uninitialize an instance to be returned to the pool.
292 * @param obj the instance to be passivated
293 */
294 public void passivateObject(Object obj)
295 throws Exception
296 {
297 MethodCacheKey key = (MethodCacheKey) obj;
298 if (key.instanceOrClass == null && key.method == null)
299 {
300 throw new TorqueException(
301 "Attempted to return key to pool twice.");
302 }
303 key.instanceOrClass = null;
304 key.method = null;
305 key.arg1 = null;
306 key.arg2 = null;
307 key.arg3 = null;
308 key.moreThanThree = null;
309 key.groupKey = null;
310 }
311 }
312 }