1 package org.apache.torque.manager;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.Serializable;
58 import org.apache.commons.lang.ObjectUtils;
59 import org.apache.commons.pool.BasePoolableObjectFactory;
60 import org.apache.torque.TorqueException;
61
62 public class MethodCacheKey implements Serializable
63 {
64 //private static final Category log =
65 // Category.getInstance("org.apache.torque");
66
67 int n;
68 private Serializable instanceOrClass;
69 private String method;
70 private Serializable arg1;
71 private Serializable arg2;
72 private Serializable arg3;
73 private Serializable[] moreThanThree;
74 private String groupKey;
75
76 public MethodCacheKey()
77 {
78 }
79
80 public MethodCacheKey(Serializable instanceOrClass, String method)
81 {
82 init(instanceOrClass, method);
83 }
84
85 public MethodCacheKey(Serializable instanceOrClass, String method,
86 Serializable arg1)
87 {
88 init(instanceOrClass, method, arg1);
89 }
90
91 public MethodCacheKey(Serializable instanceOrClass, String method,
92 Serializable arg1, Serializable arg2)
93 {
94 init(instanceOrClass, method, arg1, arg2);
95 }
96
97 public MethodCacheKey(Serializable instanceOrClass, String method,
98 Serializable arg1, Serializable arg2,
99 Serializable arg3)
100 {
101 init(instanceOrClass, method, arg1, arg2, arg3);
102 }
103
104 public MethodCacheKey(Serializable[] moreThanThree)
105 {
106 init(moreThanThree);
107 }
108
109 /***
110 * Initialize key for method with no arguments.
111 *
112 * @param instanceOrClass the Object on which the method is invoked. if
113 * the method is static, a String representing the class name is used.
114 * @param method the method name
115 */
116 public void init(Serializable instanceOrClass, String method)
117 {
118 n = 0;
119 this.instanceOrClass = instanceOrClass;
120 this.method = method;
121 groupKey = instanceOrClass.toString() + method;
122 }
123
124 /***
125 * Initialize key for method with one argument.
126 *
127 * @param instanceOrClass the Object on which the method is invoked. if
128 * the method is static, a String representing the class name is used.
129 * @param method the method name
130 * @param arg1 first method arg, may be null
131 */
132 public void init(Serializable instanceOrClass, String method,
133 Serializable arg1)
134 {
135 init(instanceOrClass, method);
136 n = 1;
137 this.arg1 = arg1;
138 }
139
140 /***
141 * Initialize key for method with two arguments.
142 *
143 * @param instanceOrClass the Object on which the method is invoked. if
144 * the method is static, a String representing the class name is used.
145 * @param method the method name
146 * @param arg1 first method arg, may be null
147 * @param arg2 second method arg, may be null
148 */
149 public void init(Serializable instanceOrClass, String method,
150 Serializable arg1, Serializable arg2)
151 {
152 init(instanceOrClass, method);
153 n = 2;
154 this.arg1 = arg1;
155 this.arg2 = arg2;
156 }
157
158
159 /***
160 * Initialize key for method with two arguments.
161 *
162 * @param instanceOrClass the Object on which the method is invoked. if
163 * the method is static, a String representing the class name is used.
164 * @param method the method name
165 * @param arg1 first method arg, may be null
166 * @param arg2 second method arg, may be null
167 */
168 public void init(Serializable instanceOrClass, String method,
169 Serializable arg1, Serializable arg2,
170 Serializable arg3)
171 {
172 init(instanceOrClass, method);
173 n = 3;
174 this.arg1 = arg1;
175 this.arg2 = arg2;
176 this.arg3 = arg3;
177 }
178
179 /***
180 * Initialize key for method with more than two arguments.
181 *
182 * @param keys Serializable[] where
183 * [0]=>the Object on which the method is invoked
184 * if the method is static, a String representing the class name is used.
185 * [1]=>the method name
186 * [n] where n>1 are the method arguments
187 */
188 public void init(Serializable[] keys)
189 {
190 init(keys[0], (String) keys[1]);
191 n = keys.length - 2;
192 if (n > 0)
193 {
194 this.arg1 = keys[2];
195 if (n > 1)
196 {
197 this.arg2 = keys[3];
198 if (n > 2)
199 {
200 this.arg2 = keys[4];
201 if (n > 3)
202 {
203 this.moreThanThree = keys;
204 }
205 }
206 }
207 }
208 }
209
210 public String getGroupKey()
211 {
212 return groupKey;
213 }
214
215 public boolean equals(Object obj)
216 {
217 boolean equal = false;
218 if (obj instanceof MethodCacheKey)
219 {
220 MethodCacheKey sck = (MethodCacheKey) obj;
221 equal = (sck.n == n);
222 equal &= ObjectUtils.equals(sck.method, method);
223 equal &= ObjectUtils.equals(sck.instanceOrClass, instanceOrClass);
224 if (equal && n > 0)
225 {
226 equal &= ObjectUtils.equals(sck.arg1, arg1);
227 if (equal && n > 1)
228 {
229 equal &= ObjectUtils.equals(sck.arg2, arg2);
230 if (equal && n > 2)
231 {
232 equal &= ObjectUtils.equals(sck.arg3, arg3);
233 if (equal && n > 3)
234 {
235 for (int i = 5; i < n + 2; i++)
236 {
237 equal &= ObjectUtils.equals(
238 sck.moreThanThree[i], moreThanThree[i]);
239 }
240 }
241 }
242 }
243 }
244 }
245
246 return equal;
247 }
248
249 public int hashCode()
250 {
251 int h = instanceOrClass.hashCode();
252 h += method.hashCode();
253 if (n > 0)
254 {
255 h += (arg1 == null ? 0 : arg1.hashCode());
256 if (n > 1)
257 {
258 h += (arg2 == null ? 0 : arg2.hashCode());
259 if (n > 2)
260 {
261 h += (arg3 == null ? 0 : arg3.hashCode());
262 if (n > 3)
263 {
264 for (int i = 5; i < n + 2; i++)
265 {
266 h += (moreThanThree[i] == null ? 0
267 : moreThanThree[i].hashCode());
268 }
269 }
270 }
271 }
272 }
273 return h;
274 }
275
276 public String toString()
277 {
278 StringBuffer sb = new StringBuffer(50);
279 sb.append(instanceOrClass);
280 sb.append("::");
281 sb.append(method).append('(');
282 if (n > 0)
283 {
284 sb.append(arg1);
285 if (n > 1)
286 {
287 sb.append(", ").append(arg2);
288 if (n > 2)
289 {
290 sb.append(", ").append(arg3);
291 if (n > 3)
292 {
293 for (int i = 5; i < n + 2; i++)
294 {
295 sb.append(", ").append(moreThanThree[i]);
296 }
297 }
298 }
299 }
300 }
301 sb.append(')');
302 return sb.toString();
303 }
304
305 // ************* PoolableObjectFactory implementation *******************
306
307 public static class Factory
308 extends BasePoolableObjectFactory
309 {
310 /***
311 * Creates an instance that can be returned by the pool.
312 * @return an instance that can be returned by the pool.
313 */
314 public Object makeObject()
315 throws Exception
316 {
317 return new MethodCacheKey();
318 }
319
320 /***
321 * Uninitialize an instance to be returned to the pool.
322 * @param obj the instance to be passivated
323 */
324 public void passivateObject(Object obj)
325 throws Exception
326 {
327 MethodCacheKey key = (MethodCacheKey) obj;
328 if (key.instanceOrClass == null && key.method == null)
329 {
330 throw new TorqueException(
331 "Attempted to return key to pool twice.");
332 }
333 key.instanceOrClass = null;
334 key.method = null;
335 key.arg1 = null;
336 key.arg2 = null;
337 key.arg3 = null;
338 key.moreThanThree = null;
339 key.groupKey = null;
340 }
341 }
342 }
This page was automatically generated by Maven