1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.service.impl; |
16 | |
17 | import java.lang.reflect.InvocationHandler; |
18 | import java.lang.reflect.InvocationTargetException; |
19 | import java.lang.reflect.Method; |
20 | import java.lang.reflect.Proxy; |
21 | import java.util.Map; |
22 | |
23 | import net.sourceforge.hiveutils.service.ExceptionMapper; |
24 | |
25 | /** |
26 | * Utility class to create a proxy object around a service implementation, in |
27 | * order to catch any exception thrown by the implementation and throw |
28 | * another exception, according to application developer-supplied rules. |
29 | * <p> |
30 | * As a matter of fact, such proxy creation also enables to make a POJO service |
31 | * implementation with no service interface usable by Hivemind, by just creating |
32 | * an interface with all public methods of the POJO. |
33 | * |
34 | * @author Jean-Francois Poilpret |
35 | */ |
36 | final public class AdapterProxyFactoryHelper |
37 | { |
38 | private AdapterProxyFactoryHelper() |
39 | { |
40 | } |
41 | |
42 | /** |
43 | * Creates a proxy of type <code>serviceInterface</code>, wrapping |
44 | * <code>service</code> implementation class, and translating any |
45 | * exception thrown by <code>service</code> into another exception through |
46 | * <code>mapper</code>. |
47 | * |
48 | * @param serviceInterface the interface that the proxy must implement; |
49 | * <code>service</code> does not have to implement that interface or any |
50 | * interface at all. |
51 | * @param service the actual service implementation class to be proxied |
52 | * @param methodsMapping a map from <code>serviceInterface</code> methods to |
53 | * <code>service</code> methods to be used to perform the method mapping |
54 | * from one class to the other |
55 | * @param exceptionMapper the instance to perform the mappping of any thrown |
56 | * <code>Throwable</code> into another one. |
57 | * @return the proxied service that implements <code>serviceInterface</code> |
58 | * and delegates calls to <code>service</code> |
59 | */ |
60 | static public Object createProxy(Class serviceInterface, |
61 | Object service, |
62 | Map methodsMapping, |
63 | ExceptionMapper exceptionMapper) |
64 | { |
65 | // Create Dynamic proxy |
66 | return Proxy.newProxyInstance( serviceInterface.getClassLoader(), |
67 | new Class[] {serviceInterface}, |
68 | new Bridge(service, exceptionMapper, methodsMapping)); |
69 | } |
70 | |
71 | static private class Bridge implements InvocationHandler |
72 | { |
73 | public Bridge(Object service, ExceptionMapper mapper, Map methods) |
74 | { |
75 | _service = service; |
76 | _mapper = mapper; |
77 | _methods = methods; |
78 | } |
79 | |
80 | //CSOFF: IllegalThrowsCheck |
81 | //CSOFF: IllegalCatchCheck |
82 | public Object invoke(Object proxy, Method method, Object[] args) |
83 | throws Throwable |
84 | { |
85 | // Find method to invoke on service |
86 | Method realMethod = (Method) _methods.get(method); |
87 | try |
88 | { |
89 | return realMethod.invoke(_service, args); |
90 | } |
91 | catch (InvocationTargetException e) |
92 | { |
93 | throw _mapper.translate(e.getTargetException()); |
94 | } |
95 | catch (Throwable t) |
96 | { |
97 | throw _mapper.translate(t); |
98 | } |
99 | } |
100 | //CSON: IllegalCatchCheck |
101 | //CSON: IllegalThrowsCheck |
102 | |
103 | private final Object _service; |
104 | private final ExceptionMapper _mapper; |
105 | private final Map _methods; |
106 | } |
107 | } |