KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ClientMethodInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
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: 37459 $
37  */

38 public class ClientMethodInterceptor extends Interceptor
39    implements Externalizable JavaDoc
40 {
41    /** The serialVersionUID. @since 1.1.2.1 */
42    private static final long serialVersionUID = 6010013004557885014L;
43
44    /** Handle methods locally on the client
45     *
46     * @param mi the invocation
47     * @return the result of the invocation
48     * @throws Throwable for any error
49     */

50    public Object JavaDoc invoke(Invocation mi) throws Throwable JavaDoc
51    {
52       Method JavaDoc m = mi.getMethod();
53       String JavaDoc methodName = m.getName();
54       // Implement local methods
55
if (methodName.equals("toString"))
56       {
57          Object JavaDoc obj = getObject(mi);
58          return obj.toString();
59       }
60       if (methodName.equals("equals"))
61       {
62          Object JavaDoc obj = getObject(mi);
63          Object JavaDoc[] args = mi.getArguments();
64          String JavaDoc thisString = obj.toString();
65          String JavaDoc argsString = args[0] == null ? "" : args[0].toString();
66          return new Boolean JavaDoc(thisString.equals(argsString));
67       }
68       if( methodName.equals("hashCode") )
69       {
70          Object JavaDoc obj = getObject(mi);
71          return new Integer JavaDoc(obj.hashCode());
72       }
73
74       return getNext().invoke(mi);
75    }
76
77    /**
78     * Get the object used in Object methods
79     *
80     * @param mi the invocation
81     * @return the object
82     */

83    protected Object JavaDoc getObject(Invocation mi)
84    {
85       Object JavaDoc cacheId = mi.getInvocationContext().getCacheId();
86       if (cacheId != null)
87          return cacheId;
88       else
89          return mi.getInvocationContext().getInvoker();
90    }
91    
92 }
93
Popular Tags