EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hivetranse.transaction.jdbc]

COVERAGE SUMMARY FOR SOURCE FILE [ConnectionProxyFactory.java]

nameclass, %method, %block, %line, %
ConnectionProxyFactory.java100% (2/2)100% (5/5)85%  (121/142)83%  (30/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ConnectionProxyFactory$ConnectionProxy100% (1/1)100% (2/2)72%  (53/74)70%  (14/20)
invoke (Object, Method, Object []): Object 100% (1/1)64%  (38/59)60%  (9/15)
ConnectionProxyFactory$ConnectionProxy (ConnectionProxyFactory, Log, String, ... 100% (1/1)100% (15/15)100% (5/5)
     
class ConnectionProxyFactory100% (1/1)100% (3/3)100% (68/68)100% (16/16)
ConnectionProxyFactory (TransactionService): void 100% (1/1)100% (6/6)100% (3/3)
access$000 (ConnectionProxyFactory): TransactionService 100% (1/1)100% (3/3)100% (1/1)
createCoreServiceImplementation (ServiceImplementationFactoryParameters): Object 100% (1/1)100% (59/59)100% (12/12)

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.hivetranse.transaction.jdbc;
16 
17import java.lang.reflect.InvocationHandler;
18import java.lang.reflect.InvocationTargetException;
19import java.lang.reflect.Method;
20import java.lang.reflect.Proxy;
21import java.sql.Connection;
22 
23import javax.sql.DataSource;
24 
25import org.apache.commons.logging.Log;
26import org.apache.hivemind.ApplicationRuntimeException;
27import org.apache.hivemind.ServiceImplementationFactory;
28import org.apache.hivemind.ServiceImplementationFactoryParameters;
29import org.apache.hivemind.util.ToStringBuilder;
30 
31import net.sourceforge.hivetranse.transaction.MandatoryTransactionException;
32import net.sourceforge.hivetranse.transaction.TransactionService;
33 
34/**
35 * This service creates Proxys to JDBC Connections.
36 * <p>
37 * <b>ServiceModel must be singleton</b>
38 *
39 * @author        Jean-Francois Poilpret
40 */
41public class ConnectionProxyFactory implements ServiceImplementationFactory
42{
43        public ConnectionProxyFactory(TransactionService txService)
44        {
45                _txService = txService;
46        }
47        
48        public Object        createCoreServiceImplementation(
49                                                ServiceImplementationFactoryParameters factoryParams)
50        {
51                Log logger = factoryParams.getLog();
52                String serviceId = factoryParams.getServiceId();
53                Class serviceInterface = factoryParams.getServiceInterface();
54 
55                // Make sure that serviceInterface == Connection
56                // (this factory is dedicated exclusively to JDBC Connections)
57                if (!Connection.class.equals(serviceInterface))
58                {
59                        // throw error
60                        logger.error("ConnectionProxyFactory can only build Connection services.");
61                        throw new ApplicationRuntimeException("Bad interface");
62                }
63                
64                // Read parameter
65                Object service = factoryParams.getFirstParameter();
66                if (service == null || !(service instanceof DataSource))
67                {
68                        // throw error
69                        logger.error("ConnectionProxyFactory needs one DataSource service as parameter.");
70                        throw new ApplicationRuntimeException("Bad DataSource interface");
71                }
72                DataSource ds = (DataSource) service;
73                
74        return Proxy.newProxyInstance(
75                        factoryParams.getInvokingModule().getClassResolver().getClassLoader(),
76                        new Class[] {serviceInterface},
77                        new ConnectionProxy(logger, serviceId, ds));
78        }
79        
80        private class ConnectionProxy implements InvocationHandler
81        {
82                public ConnectionProxy(Log logger, String serviceId, DataSource ds)
83                {
84                        _logger = logger;
85                        _serviceId = serviceId;
86                        _ds = ds;
87                }
88                
89                //CSOFF: IllegalThrowsCheck
90                public Object        invoke(Object proxy, Method method, Object[] args)
91                        throws Throwable
92                {
93                        if ("toString".equals(method.getName()))
94                        {
95                        ToStringBuilder builder = new ToStringBuilder(this);
96                        builder.append("serviceId", _serviceId);
97                        return builder.toString();
98                        }
99 
100                        ConnectionsRepository repository =
101                                        (ConnectionsRepository) _txService.getCurrentTransaction();
102                        if (repository == null)
103                        {
104                                _logger.warn("No active transaction");
105                                throw new MandatoryTransactionException("No active transaction");
106                        }
107                        Connection cnx = repository.getConnection(_serviceId, _ds);
108                        try
109                        {
110                                return method.invoke(cnx, args);
111                        }
112                        catch (IllegalAccessException e)
113                        {
114                                // Can never happen (normally!)
115                                _logger.fatal("Unexpected", e);
116                                throw e;
117                        }
118                        catch (InvocationTargetException e)
119                        {
120                                throw e.getTargetException();
121                        }
122                }
123                //CSON: IllegalThrowsCheck
124                
125                private final Log                        _logger;
126                private final String                _serviceId;
127                private final DataSource        _ds;
128        }
129 
130        private final TransactionService        _txService;
131}

[all classes][net.sourceforge.hivetranse.transaction.jdbc]
EMMA 2.0.5312 (C) Vladimir Roubtsov