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

COVERAGE SUMMARY FOR SOURCE FILE [DefaultExceptionMapper.java]

nameclass, %method, %block, %line, %
DefaultExceptionMapper.java100% (1/1)100% (6/6)70%  (166/237)81%  (40.5/50)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultExceptionMapper100% (1/1)100% (6/6)70%  (166/237)81%  (40.5/50)
wrapThrowable (Class, Throwable): Throwable 100% (1/1)44%  (48/109)47%  (8/17)
getConstructors (Class): Constructor [] 100% (1/1)85%  (57/67)97%  (17.5/18)
<static initializer> 100% (1/1)100% (30/30)100% (4/4)
DefaultExceptionMapper (Log, ClassMatcher): void 100% (1/1)100% (9/9)100% (4/4)
getConstructor (Class, Class []): Constructor 100% (1/1)100% (7/7)100% (3/3)
translate (Throwable): Throwable 100% (1/1)100% (15/15)100% (4/4)

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.Constructor;
18import java.lang.reflect.InvocationTargetException;
19import java.util.HashMap;
20import java.util.Map;
21 
22import org.apache.commons.logging.Log;
23 
24import net.sourceforge.hiveutils.service.ExceptionMapper;
25import net.sourceforge.hiveutils.util.ClassMatcher;
26 
27/**
28 * Default implementation of <code>ExceptionMapper</code>, based on a
29 * <code>ClassMatcher</code> to do the exception translation.
30 * <p>
31 * It can be instantiated directly by the <code>AdapterBuilderFactory</code>
32 * depending on the parameters specified in the module descriptor.
33 *
34 * @author Jean-Francois Poilpret
35 */
36public class DefaultExceptionMapper implements ExceptionMapper
37{
38        /**
39         * Constructs the <code>ExceptionMapper</code> from the passed
40         * <code>ClassMatcher</code>.
41         * @param logger logger to be used for warnings
42         * @param matcher the matcher used to find how to translate thrown
43         * exceptions
44         */
45        public DefaultExceptionMapper(Log logger, ClassMatcher matcher)
46        {
47                _logger = logger;
48                _matcher = matcher;
49        }
50        
51        public Throwable        translate(Throwable t)
52        {
53                Class clazz = (Class) _matcher.get(t);
54                if (clazz != null)
55                {
56                        return wrapThrowable(clazz, t);
57                }
58                else
59                {
60                        return t;
61                }
62        }
63        
64        protected Throwable                wrapThrowable(Class clazz, Throwable source)
65        {
66                Constructor[] ctors = getConstructors(clazz);
67                try
68                {
69                        if (ctors[CTOR_ALL] != null)
70                        {
71                                return (Throwable) ctors[CTOR_ALL].newInstance(
72                                                                        new Object[] {source.getMessage(), source});
73                        }
74                        if (ctors[CTOR_THROWABLE] != null)
75                        {
76                                return (Throwable) ctors[CTOR_THROWABLE].newInstance(
77                                                                        new Object[] {source});
78                        }
79                        if (ctors[CTOR_STRING] != null)
80                        {
81                                return (Throwable) ctors[CTOR_STRING].newInstance(
82                                                                        new Object[] {source.getMessage()});
83                        }
84                        return (Throwable) clazz.newInstance();
85                }
86                catch (InstantiationException e)
87                {
88                        _logger.warn(        "Exception Mapper could not instantiate " +
89                                                        clazz.getName(), e);
90                        return source;
91                }
92                catch (IllegalAccessException e)
93                {
94                        _logger.warn(        "Exception Mapper could not instantiate " +
95                                                        clazz.getName(), e);
96                        return source;
97                }
98                catch (InvocationTargetException e)
99                {
100                        _logger.warn(        "Exception Mapper could not instantiate " +
101                                                        clazz.getName(), e);
102                        return source;
103                }
104        }
105 
106        static protected Constructor[]        getConstructors(Class clazz)
107        {
108                Constructor[] ctors;
109                synchronized (_constructors)
110                {
111                        ctors = _constructors.get(clazz);
112                }
113                if (ctors == null)
114                {
115                        ctors = new Constructor[NUM_CTORS];
116 
117                        Constructor ctor;
118                        int index;
119 
120                        index = CTOR_ALL;
121                        ctor = getConstructor(clazz, ARGS_ALL);
122                        if (ctor == null)
123                        {
124                                index = CTOR_THROWABLE;
125                                ctor = getConstructor(clazz, ARGS_THROWABLE);
126                        }
127                        if (ctor == null)
128                        {
129                                index = CTOR_STRING;
130                                ctor = getConstructor(clazz, ARGS_STRING);
131                        }
132 
133                        // Only keep the first matching constructor
134                        ctors[index] = ctor;
135                        synchronized (_constructors)
136                        {
137                                _constructors.put(clazz, ctors);
138                        }
139                }
140                return ctors;
141        }
142        
143        static protected Constructor        getConstructor(Class clazz, Class[] args)
144        {
145                try
146                {
147                        return clazz.getConstructor(args);
148                }
149                catch (NoSuchMethodException e)
150                {
151                        return null;
152                }
153        }
154        
155        private final Log                        _logger;
156        private final ClassMatcher        _matcher;
157        // map Class --> Constructor[4]
158        // gives the existing constructors for this class in the following order:
159        // 0        constructor(String, Throwable)
160        // 1        constructor(Throwable)
161        // 2        constructor(String)
162        // 3        default constructor
163        // if one constructor does not exist, its value is null
164        static private final Map<Class, Constructor[]>        _constructors = 
165                                                                                        new HashMap<Class, Constructor[]>();
166 
167        static final protected int                CTOR_ALL                = 0;
168        static final protected int                CTOR_THROWABLE        = 1;
169        static final protected int                CTOR_STRING                = 2;
170        static final protected int                NUM_CTORS                = 3;
171        
172        static final private Class[]        ARGS_ALL                = new Class[] {        String.class, 
173                                                                                                                                        Throwable.class};
174        static final private Class[]        ARGS_THROWABLE        = new Class[] {Throwable.class};
175        static final private Class[]        ARGS_STRING                = new Class[] {String.class};
176}

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