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.hivetranse.transaction.jdbc; |
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.sql.Connection; |
22 | |
23 | import javax.sql.DataSource; |
24 | |
25 | import org.apache.commons.logging.Log; |
26 | import org.apache.hivemind.ApplicationRuntimeException; |
27 | import org.apache.hivemind.ServiceImplementationFactory; |
28 | import org.apache.hivemind.ServiceImplementationFactoryParameters; |
29 | import org.apache.hivemind.util.ToStringBuilder; |
30 | |
31 | import net.sourceforge.hivetranse.transaction.MandatoryTransactionException; |
32 | import 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 | */ |
41 | public 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 | } |