001: package org.apache.torque.manager;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.Serializable;
023: import org.apache.commons.lang.ObjectUtils;
024: import org.apache.commons.pool.BasePoolableObjectFactory;
025: import org.apache.torque.TorqueException;
026:
027: /**
028: * @version $Id: MethodCacheKey.java 474969 2006-11-14 20:41:36Z tv $
029: */
030: public class MethodCacheKey implements Serializable {
031: /**
032: * Serial version
033: */
034: private static final long serialVersionUID = -1831486431185021200L;
035:
036: int n;
037: private Serializable instanceOrClass;
038: private String method;
039: private Serializable arg1;
040: private Serializable arg2;
041: private Serializable arg3;
042: private Serializable[] moreThanThree;
043: private String groupKey;
044:
045: public MethodCacheKey() {
046: }
047:
048: public MethodCacheKey(Serializable instanceOrClass, String method) {
049: init(instanceOrClass, method);
050: }
051:
052: public MethodCacheKey(Serializable instanceOrClass, String method,
053: Serializable arg1) {
054: init(instanceOrClass, method, arg1);
055: }
056:
057: public MethodCacheKey(Serializable instanceOrClass, String method,
058: Serializable arg1, Serializable arg2) {
059: init(instanceOrClass, method, arg1, arg2);
060: }
061:
062: public MethodCacheKey(Serializable instanceOrClass, String method,
063: Serializable arg1, Serializable arg2, Serializable arg3) {
064: init(instanceOrClass, method, arg1, arg2, arg3);
065: }
066:
067: public MethodCacheKey(Serializable[] moreThanThree) {
068: init(moreThanThree);
069: }
070:
071: /**
072: * Initialize key for method with no arguments.
073: *
074: * @param instanceOrClass the Object on which the method is invoked. if
075: * the method is static, a String representing the class name is used.
076: * @param method the method name
077: */
078: public void init(Serializable instanceOrClass, String method) {
079: n = 0;
080: this .instanceOrClass = instanceOrClass;
081: this .method = method;
082: groupKey = instanceOrClass.toString() + method;
083: }
084:
085: /**
086: * Initialize key for method with one argument.
087: *
088: * @param instanceOrClass the Object on which the method is invoked. if
089: * the method is static, a String representing the class name is used.
090: * @param method the method name
091: * @param arg1 first method arg, may be null
092: */
093: public void init(Serializable instanceOrClass, String method,
094: Serializable arg1) {
095: init(instanceOrClass, method);
096: n = 1;
097: this .arg1 = arg1;
098: }
099:
100: /**
101: * Initialize key for method with two arguments.
102: *
103: * @param instanceOrClass the Object on which the method is invoked. if
104: * the method is static, a String representing the class name is used.
105: * @param method the method name
106: * @param arg1 first method arg, may be null
107: * @param arg2 second method arg, may be null
108: */
109: public void init(Serializable instanceOrClass, String method,
110: Serializable arg1, Serializable arg2) {
111: init(instanceOrClass, method);
112: n = 2;
113: this .arg1 = arg1;
114: this .arg2 = arg2;
115: }
116:
117: /**
118: * Initialize key for method with two arguments.
119: *
120: * @param instanceOrClass the Object on which the method is invoked. if
121: * the method is static, a String representing the class name is used.
122: * @param method the method name
123: * @param arg1 first method arg, may be null
124: * @param arg2 second method arg, may be null
125: */
126: public void init(Serializable instanceOrClass, String method,
127: Serializable arg1, Serializable arg2, Serializable arg3) {
128: init(instanceOrClass, method);
129: n = 3;
130: this .arg1 = arg1;
131: this .arg2 = arg2;
132: this .arg3 = arg3;
133: }
134:
135: /**
136: * Initialize key for method with more than three arguments.
137: *
138: * @param keys Serializable[] where
139: * [0]=>the Object on which the method is invoked
140: * if the method is static, a String representing the class name is used.
141: * [1]=>the method name
142: * [n] where n>1 are the method arguments
143: */
144: public void init(Serializable[] keys) {
145: init(keys[0], (String) keys[1]);
146: n = keys.length - 2;
147: if (n > 0) {
148: this .arg1 = keys[2];
149: if (n > 1) {
150: this .arg2 = keys[3];
151: if (n > 2) {
152: this .arg3 = keys[4];
153: if (n > 3) {
154: this .moreThanThree = keys;
155: }
156: }
157: }
158: }
159: }
160:
161: public String getGroupKey() {
162: return groupKey;
163: }
164:
165: public boolean equals(Object obj) {
166: boolean equal = false;
167: if (obj instanceof MethodCacheKey) {
168: MethodCacheKey sck = (MethodCacheKey) obj;
169: equal = (sck.n == n);
170: equal &= ObjectUtils.equals(sck.method, method);
171: equal &= ObjectUtils.equals(sck.instanceOrClass,
172: instanceOrClass);
173: if (equal && n > 0) {
174: equal &= ObjectUtils.equals(sck.arg1, arg1);
175: if (equal && n > 1) {
176: equal &= ObjectUtils.equals(sck.arg2, arg2);
177: if (equal && n > 2) {
178: equal &= ObjectUtils.equals(sck.arg3, arg3);
179: if (equal && n > 3) {
180: for (int i = 5; i < n + 2; i++) {
181: equal &= ObjectUtils.equals(
182: sck.moreThanThree[i],
183: moreThanThree[i]);
184: }
185: }
186: }
187: }
188: }
189: }
190:
191: return equal;
192: }
193:
194: public int hashCode() {
195: int h = instanceOrClass.hashCode();
196: h += method.hashCode();
197: if (n > 0) {
198: h += (arg1 == null ? 0 : arg1.hashCode());
199: if (n > 1) {
200: h += (arg2 == null ? 0 : arg2.hashCode());
201: if (n > 2) {
202: h += (arg3 == null ? 0 : arg3.hashCode());
203: if (n > 3) {
204: for (int i = 5; i < n + 2; i++) {
205: h += (moreThanThree[i] == null ? 0
206: : moreThanThree[i].hashCode());
207: }
208: }
209: }
210: }
211: }
212: return h;
213: }
214:
215: public String toString() {
216: StringBuffer sb = new StringBuffer(50);
217: sb.append(instanceOrClass);
218: sb.append("::");
219: sb.append(method).append('(');
220: if (n > 0) {
221: sb.append(arg1);
222: if (n > 1) {
223: sb.append(", ").append(arg2);
224: if (n > 2) {
225: sb.append(", ").append(arg3);
226: if (n > 3) {
227: for (int i = 5; i < n + 2; i++) {
228: sb.append(", ").append(moreThanThree[i]);
229: }
230: }
231: }
232: }
233: }
234: sb.append(')');
235: return sb.toString();
236: }
237:
238: // ************* PoolableObjectFactory implementation *******************
239:
240: public static class Factory extends BasePoolableObjectFactory {
241: /**
242: * Creates an instance that can be returned by the pool.
243: * @return an instance that can be returned by the pool.
244: */
245: public Object makeObject() throws Exception {
246: return new MethodCacheKey();
247: }
248:
249: /**
250: * Uninitialize an instance to be returned to the pool.
251: * @param obj the instance to be passivated
252: */
253: public void passivateObject(Object obj) throws Exception {
254: MethodCacheKey key = (MethodCacheKey) obj;
255: if (key.instanceOrClass == null && key.method == null) {
256: throw new TorqueException(
257: "Attempted to return key to pool twice.");
258: }
259: key.instanceOrClass = null;
260: key.method = null;
261: key.arg1 = null;
262: key.arg2 = null;
263: key.arg3 = null;
264: key.moreThanThree = null;
265: key.groupKey = null;
266: }
267: }
268: }
|