KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > proxy > InvokerFacade


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.proxy;
7
8 import org.logicalcobwebs.proxool.ProxoolException;
9
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.lang.reflect.Modifier JavaDoc;
14
15 /**
16  * Invokes a method using a cached method.
17  * @version $Revision: 1.3 $, $Date: 2004/07/13 21:13:14 $
18  * @author billhorsman
19  * @author $Author: billhorsman $ (current maintainer)
20  * @since Proxool 0.9
21  */

22 public class InvokerFacade {
23     
24     private static Map JavaDoc methodMappers = new HashMap JavaDoc();
25     
26     /**
27      * Returns the method in the concrete class with an indentical signature to that passed
28      * @param concreteClass the class that we want to invoke methods on. It should either implement all methods on
29      * the injectable interface, or provide methods with an identical signature.
30      * @param injectableMethod provides signature that we are trying to match
31      * @return the method in the concrete class that we can invoke as if it were in the interface
32      * @throws org.logicalcobwebs.proxool.ProxoolException if the method is not found.
33      */

34     public static Method JavaDoc getConcreteMethod(Class JavaDoc concreteClass, Method JavaDoc injectableMethod) throws ProxoolException {
35         // Unless the concrete class is public we can't do anything
36
if (Modifier.isPublic(concreteClass.getModifiers())) {
37             Object JavaDoc key = concreteClass.getName() + ":" + injectableMethod.getName();
38             MethodMapper methodMapper = (MethodMapper) methodMappers.get(key);
39             if (methodMapper == null) {
40                 methodMapper = new MethodMapper(concreteClass);
41                 methodMappers.put(key, methodMapper);
42             }
43             return methodMapper.getConcreteMethod(injectableMethod);
44         } else {
45             return injectableMethod;
46         }
47     }
48
49     /**
50      * Override the method provided by the {@link #getConcreteMethod(java.lang.Class, java.lang.reflect.Method)}. Use this
51      * if you decide that the concrete method provided wasn't any good. For instance, if you get an IllegalAccessException
52      * whilst invoking the concrete method then you should perhaps try using the proxy supplied method instead.
53      * @param concreteClass the class we are invoking upon
54      * @param injectableMethod the method supplied by the proxy
55      * @param overridenMethod the one we are going to use (probably the same as injectrableMethod actually)
56      */

57     public static void overrideConcreteMethod(Class JavaDoc concreteClass, Method JavaDoc injectableMethod, Method JavaDoc overridenMethod) {
58         Object JavaDoc key = concreteClass.getName() + ":" + injectableMethod.getName();
59         MethodMapper methodMapper = (MethodMapper) methodMappers.get(key);
60         if (methodMapper == null) {
61             methodMapper = new MethodMapper(concreteClass);
62             methodMappers.put(key, methodMapper);
63         }
64         methodMapper.overrideConcreteMethod(injectableMethod, overridenMethod);
65     }
66
67 }
68 /*
69  Revision history:
70  $Log: InvokerFacade.java,v $
71  Revision 1.3 2004/07/13 21:13:14 billhorsman
72  Optimise using injectable interfaces on methods that are declared in non-public classes by not bothering to use concrete methods at all (it's not possible).
73
74  Revision 1.2 2004/07/13 21:06:16 billhorsman
75  Fix problem using injectable interfaces on methods that are declared in non-public classes.
76
77  Revision 1.1 2004/06/02 20:43:53 billhorsman
78  New classes to support injectable interfaces
79
80 */
Popular Tags