{dbcp, dbcp.getClass()}, {dbcp2, dbcp2.getClass()}, {new P6DataSource(druidDataSource), druidDataSource.getClass()} });
@Override public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?,?> env) throws Exception { final Reference ref = (Reference) refObj; final String className = ref.getClassName(); if (className != null && className.equals(DATASOURCE_CLASS_NAME)) { P6DataSource dataSource; try { dataSource = (P6DataSource) Class.forName(className).newInstance(); } catch (Exception ex) { throw new RuntimeException("Unable to create DataSource of " + "class '" + className + "': " + ex.toString()); } // name of the real datasource dataSource.setRealDataSource((String) ref.get("dataSourceName").getContent()); return dataSource; } else { // Who's class is this anyway, I ask ya! return null; } } }
@SuppressWarnings("unchecked") <T> T castRealDS(Class<T> iface) throws SQLException { if (realDataSource == null) { bindDataSource(); } if (iface.isInstance(realDataSource)) { return ((T) realDataSource); } else if (isWrapperFor(iface)) { return unwrap(iface); } else { throw new IllegalStateException("realdatasource type not supported: " + realDataSource); } }
env.putAll(parseDelimitedString(custom)); Map<String, String> props = parseDelimitedString(options.getRealDataSourceProperties()); if (props != null) { setDataSourceProperties(props);
@Before public void setup() throws Exception { // make sure to reinit properly new P6TestFramework("ds") { }; user = "sa"; password ="sa"; // please note non-typical DB name // however it seems that the typical one "jdbc:h2:mem:p6spy" // caused authorization exception, see: https://github.com/p6spy/p6spy/issues/76 // in some test execution sequences (on some setups only) // I suspect non-proper cleanup in some of the previous tests causing this error // url = "jdbc:h2:mem:p6spy"; url = "jdbc:h2:mem:p6spyDSTest"; driverClass = "org.h2.Driver"; P6DataSource spyDs = new P6DataSource(); spyDs.setRealDataSource("jdbc/realDs"); spyDsResource = new Resource("jdbc/spyDs", spyDs); ((P6TestLogger) P6LogQuery.getLogger()).clearLogs(); }
final CallableStatement mockedCallableStatement = mock(CallableStatement.class); wrappedDataSource = new P6DataSource(mockedDataSource); ((P6DataSource) wrappedDataSource).setJdbcEventListenerFactory(new JdbcEventListenerFactory() { @Override public JdbcEventListener createJdbcEventListener() {
@Override public void setLoginTimeout(int inVar) throws SQLException { if (realDataSource == null) { bindDataSource(); } realDataSource.setLoginTimeout(inVar); }
@Override public PooledConnection getPooledConnection(String user, String password) throws SQLException { if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } return new P6XAConnection(castRealDS(ConnectionPoolDataSource.class).getPooledConnection(user, password), this.jdbcEventListenerFactory); }
@Override public Reference getReference() throws NamingException { final Reference reference = new Reference(getClass().getName(), P6DataSourceFactory.class.getName(), null); reference.add(new StringRefAddr("dataSourceName", getRealDataSource())); return reference; }
@Before public void setUp() throws Exception { // make sure to reinit properly new P6TestFramework("ds") { }; jndiResources = new ArrayList<Resource>(); // create the real data sources and bind to jndi JdbcDataSource realDs1 = new JdbcDataSource(); realDs1.setUser("sa"); realDs1.setURL("jdbc:h2:mem:multids1"); jndiResources.add(new Resource("jdbc/realDs1", realDs1)); JdbcDataSource realDs2 = new JdbcDataSource(); realDs2.setUser("sa"); realDs2.setURL("jdbc:h2:mem:multids2"); jndiResources.add(new Resource("jdbc/realDs2", realDs2)); JDBCDataSource realDs3 = new JDBCDataSource(); realDs3.setUser("sa"); realDs3.setPassword(""); realDs3.setUrl("jdbc:hsqldb:mem:multids3"); jndiResources.add(new Resource("jdbc/realDs3", realDs3)); // create the spy wrapper data sources and bind to jndi P6DataSource spyDs1 = new P6DataSource(); spyDs1.setRealDataSource("jdbc/realDs1"); jndiResources.add(new Resource("jdbc/spyDs1", spyDs1)); P6DataSource spyDs3 = new P6DataSource(); spyDs3.setRealDataSource("jdbc/realDs3"); jndiResources.add(new Resource("jdbc/spyDs3", spyDs3)); // reset captured log messages ((P6TestLogger) P6LogQuery.getLogger()).clearLogs(); }
@Override public DataSource decorate(String beanName, DataSource dataSource) { P6DataSource p6DataSource = new P6DataSource(dataSource); p6DataSource.setJdbcEventListenerFactory(jdbcEventListenerFactory); return p6DataSource; }
@Override public int getLoginTimeout() throws SQLException { if (realDataSource == null) { bindDataSource(); } return realDataSource.getLoginTimeout(); }
@Override public XAConnection getXAConnection() throws SQLException { if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } return new P6XAConnection(castRealDS(XADataSource.class).getXAConnection(), this.jdbcEventListenerFactory); }
env.putAll(parseDelimitedString(custom)); Map<String, String> props = parseDelimitedString(options.getRealDataSourceProperties()); if (props != null) { setDataSourceProperties(props);
@Override public Reference getReference() throws NamingException { final Reference reference = new Reference(getClass().getName(), P6DataSourceFactory.class.getName(), null); reference.add(new StringRefAddr("dataSourceName", getRealDataSource())); return reference; }
@Override public DataSource dataSource(DataSource dataSource) { return new P6DataSource(dataSource); } };
@SuppressWarnings("unchecked") <T> T castRealDS(Class<T> iface) throws SQLException { if (realDataSource == null) { bindDataSource(); } if (iface.isInstance(realDataSource)) { return ((T) realDataSource); } else if (isWrapperFor(iface)) { return unwrap(iface); } else { throw new IllegalStateException("realdatasource type not supported: " + realDataSource); } }
@Override public PrintWriter getLogWriter() throws SQLException { if (realDataSource == null) { bindDataSource(); } return realDataSource.getLogWriter(); }
@Override public PooledConnection getPooledConnection() throws SQLException { if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } return new P6XAConnection(castRealDS(ConnectionPoolDataSource.class).getPooledConnection(), this.jdbcEventListenerFactory); }
@Override public Object getObjectInstance(Object refObj, Name nm, Context ctx, Hashtable<?,?> env) throws Exception { final Reference ref = (Reference) refObj; final String className = ref.getClassName(); if (className != null && className.equals(DATASOURCE_CLASS_NAME)) { P6DataSource dataSource; try { dataSource = (P6DataSource) Class.forName(className).newInstance(); } catch (Exception ex) { throw new RuntimeException("Unable to create DataSource of " + "class '" + className + "': " + ex.toString()); } // name of the real datasource dataSource.setRealDataSource((String) ref.get("dataSourceName").getContent()); return dataSource; } else { // Who's class is this anyway, I ask ya! return null; } } }