01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.proxy;
23:
24: import java.io.Externalizable;
25: import java.lang.reflect.Method;
26:
27: import org.jboss.invocation.Invocation;
28: import org.jboss.invocation.Invoker;
29: import org.jboss.proxy.Interceptor;
30:
31: /**
32: * Handle toString, equals, hashCode locally on the client.
33: *
34: * @author Scott.Stark@jboss.org
35: * @author adrian@jboss.com
36: * @version $Revision: 57209 $
37: */
38: public class ClientMethodInterceptor extends Interceptor implements
39: Externalizable {
40: /** The serialVersionUID. @since 1.1.2.1 */
41: private static final long serialVersionUID = 6010013004557885014L;
42:
43: /** Handle methods locally on the client
44: *
45: * @param mi the invocation
46: * @return the result of the invocation
47: * @throws Throwable for any error
48: */
49: public Object invoke(Invocation mi) throws Throwable {
50: Method m = mi.getMethod();
51: String methodName = m.getName();
52: // Implement local methods
53: if (methodName.equals("toString")) {
54: Object obj = getObject(mi);
55: return obj.toString();
56: }
57: if (methodName.equals("equals")) {
58: Object obj = getObject(mi);
59: Object[] args = mi.getArguments();
60: String this String = obj.toString();
61: String argsString = args[0] == null ? "" : args[0]
62: .toString();
63: return new Boolean(this String.equals(argsString));
64: }
65: if (methodName.equals("hashCode")) {
66: Object obj = getObject(mi);
67: return new Integer(obj.hashCode());
68: }
69:
70: return getNext().invoke(mi);
71: }
72:
73: /**
74: * Get the object used in Object methods
75: *
76: * @param mi the invocation
77: * @return the object
78: */
79: protected Object getObject(Invocation mi) {
80: Object cacheId = mi.getInvocationContext().getCacheId();
81: if (cacheId != null)
82: return cacheId;
83: else
84: return mi.getInvocationContext().getInvoker();
85: }
86:
87: }
|