1
2
3
4
5
6
7
8
9
10
11
12package jdbc_wrapper;
13
14import java.sql.Connection;
15import java.sql.DriverManager;
16import java.sql.ResultSet;
17import java.sql.SQLException;
18import java.sql.Statement;
19import java.util.Properties;
20
21import javax.sql.DataSource;
22
23import net.sf.tie.InterceptionInjector;
24import net.sf.tie.ProxyInjector;
25import net.sf.tie.ext.InterceptionBuilder;
26import net.sf.tie.ext.interceptor.WrapReturnValueInterceptor;
27import net.sf.tie.ext.type.ReturnTypeRule;
28
29public class ConnectionManager
30{
31 private static final InterceptionInjector INJECTOR = buildInjector();
32
33 private static InterceptionInjector buildInjector()
34 {
35 InterceptionBuilder builder = new InterceptionBuilder();
36 builder.always(new JDBCLoggingInterceptor());
37 WrapReturnValueInterceptor wrapReturnValueInterceptor = new WrapReturnValueInterceptor();
38 builder.whenRuleMatches(getReturnTypeRule(), wrapReturnValueInterceptor);
39
40 ProxyInjector injector = new ProxyInjector(builder.done());
41 wrapReturnValueInterceptor.setInjector(injector);
42 return injector;
43 }
44
45 private static ReturnTypeRule getReturnTypeRule()
46 {
47 ReturnTypeRule returnTypeRule = new ReturnTypeRule();
48 returnTypeRule.addInheritedType(Statement.class);
49 returnTypeRule.addDirectType(Connection.class);
50 returnTypeRule.addDirectType(DataSource.class);
51 returnTypeRule.addDirectType(ResultSet.class);
52 return returnTypeRule;
53 }
54
55 public static Connection getConnection(String url) throws SQLException
56 {
57 return intercept(DriverManager.getConnection(url));
58 }
59
60 public static Connection getConnection(String url, String user, String password) throws SQLException
61 {
62 return intercept(DriverManager.getConnection(url, user, password));
63 }
64
65 public static Connection getConnection(String url, Properties properties) throws SQLException
66 {
67 return intercept(DriverManager.getConnection(url, properties));
68 }
69
70 public static DataSource intercept(DataSource dataSource)
71 {
72 return INJECTOR.wrapObject(DataSource.class, dataSource);
73 }
74
75 private static Connection intercept(Connection connection)
76 {
77 return INJECTOR.wrapObject(Connection.class, connection);
78 }
79}
// Copyright 2007, Tim Vernum
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package jdbc_wrapper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import net.sf.tie.InterceptionInjector;
import net.sf.tie.ProxyInjector;
import net.sf.tie.ext.InterceptionBuilder;
import net.sf.tie.ext.interceptor.WrapReturnValueInterceptor;
import net.sf.tie.ext.type.ReturnTypeRule;
public class ConnectionManager
{
private static final InterceptionInjector INJECTOR = buildInjector();
private static InterceptionInjector buildInjector()
{
InterceptionBuilder builder = new InterceptionBuilder();
builder.always(new JDBCLoggingInterceptor());
WrapReturnValueInterceptor wrapReturnValueInterceptor = new WrapReturnValueInterceptor();
builder.whenRuleMatches(getReturnTypeRule(), wrapReturnValueInterceptor);
ProxyInjector injector = new ProxyInjector(builder.done());
wrapReturnValueInterceptor.setInjector(injector);
return injector;
}
private static ReturnTypeRule getReturnTypeRule()
{
ReturnTypeRule returnTypeRule = new ReturnTypeRule();
returnTypeRule.addInheritedType(Statement.class);
returnTypeRule.addDirectType(Connection.class);
returnTypeRule.addDirectType(DataSource.class);
returnTypeRule.addDirectType(ResultSet.class);
return returnTypeRule;
}
public static Connection getConnection(String url) throws SQLException
{
return intercept(DriverManager.getConnection(url));
}
public static Connection getConnection(String url, String user, String password) throws SQLException
{
return intercept(DriverManager.getConnection(url, user, password));
}
public static Connection getConnection(String url, Properties properties) throws SQLException
{
return intercept(DriverManager.getConnection(url, properties));
}
public static DataSource intercept(DataSource dataSource)
{
return INJECTOR.wrapObject(DataSource.class, dataSource);
}
private static Connection intercept(Connection connection)
{
return INJECTOR.wrapObject(Connection.class, connection);
}
}