EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.service.impl]

COVERAGE SUMMARY FOR SOURCE FILE [AdapterProxyFactoryHelper.java]

nameclass, %method, %block, %line, %
AdapterProxyFactoryHelper.java100% (2/2)75%  (3/4)84%  (47/56)71%  (10/14)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AdapterProxyFactoryHelper$Bridge100% (1/1)100% (2/2)84%  (31/37)82%  (9/11)
invoke (Object, Method, Object []): Object 100% (1/1)76%  (19/25)67%  (4/6)
AdapterProxyFactoryHelper$Bridge (Object, ExceptionMapper, Map): void 100% (1/1)100% (12/12)100% (5/5)
     
class AdapterProxyFactoryHelper100% (1/1)50%  (1/2)84%  (16/19)33%  (1/3)
AdapterProxyFactoryHelper (): void 0%   (0/1)0%   (0/3)0%   (0/2)
createProxy (Class, Object, Map, ExceptionMapper): Object 100% (1/1)100% (16/16)100% (1/1)

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 
15package net.sourceforge.hiveutils.service.impl;
16 
17import java.lang.reflect.InvocationHandler;
18import java.lang.reflect.InvocationTargetException;
19import java.lang.reflect.Method;
20import java.lang.reflect.Proxy;
21import java.util.Map;
22 
23import 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 */
36final 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}

[all classes][net.sourceforge.hiveutils.service.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov