@Test public void testGetMockConnectionMakesTcp() throws ClassNotFoundException, SQLException { TestDriver testDriver = TestDriver.getSpy(); final InetSocketAddress loopbackAddress = new InetSocketAddress(getLoopbackAddress(), 4242); try { doAnswer(invocation -> { Sniffy.logSocket(1, loopbackAddress, 100, 2, 3); return invocation.callRealMethod(); }).when(testDriver).connect(any(), any()); try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } } finally { Mockito.reset(testDriver); } try (Connection connection = DriverManager.getConnection("sniffy:jdbc:h2spy:mem:", "sa", "sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); } }
public Connection connect(String url, Properties info) throws SQLException { if (null == url || !acceptsURL(url)) return null; String originUrl = extractOriginUrl(url); String userName = info.getProperty("user"); checkConnectionAllowed(originUrl, userName, true); Driver originDriver; try { originDriver = DriverManager.getDriver(originUrl); } catch (SQLException e) { try { reloadServiceProviders(); originDriver = DriverManager.getDriver(originUrl); } catch (Exception e2) { throw e; } } if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info); long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Connection delegateConnection = originDriver.connect(originUrl, info); return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, originUrl, userName) )); } finally { Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL); } }
SniffyDriver implements Driver, Constants { public Connection connect(String url, Properties info) throws SQLException { if (null == url || !acceptsURL(url)) return null; String originUrl = extractOriginUrl(url); String userName = info.getProperty("user"); checkConnectionAllowed(originUrl, userName, true); Driver originDriver; try { originDriver = DriverManager.getDriver(originUrl); } catch (SQLException e) { try { reloadServiceProviders(); originDriver = DriverManager.getDriver(originUrl); } catch (Exception e2) { throw e; } } if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info); long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Connection delegateConnection = originDriver.connect(originUrl, info); return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, originUrl, userName) )); } finally { Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL); } } }
SniffyDriver implements Driver, Constants { public Connection connect(String url, Properties info) throws SQLException { if (null == url || !acceptsURL(url)) return null; String originUrl = extractOriginUrl(url); String userName = info.getProperty("user"); checkConnectionAllowed(originUrl, userName, true); Driver originDriver; try { originDriver = DriverManager.getDriver(originUrl); } catch (SQLException e) { try { reloadServiceProviders(); originDriver = DriverManager.getDriver(originUrl); } catch (Exception e2) { throw e; } } if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info); long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Connection delegateConnection = originDriver.connect(originUrl, info); return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, originUrl, userName) )); } finally { Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL); } }  }
SniffyDriver implements Driver, Constants { public Connection connect(String url, Properties info) throws SQLException { if (null == url || !acceptsURL(url)) return null; String originUrl = extractOriginUrl(url); String userName = info.getProperty("user"); checkConnectionAllowed(originUrl, userName, true); Driver originDriver; try { originDriver = DriverManager.getDriver(originUrl); } catch (SQLException e) { try { reloadServiceProviders(); originDriver = DriverManager.getDriver(originUrl); } catch (Exception e2) { throw e; } } if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info); long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Connection delegateConnection = originDriver.connect(originUrl, info); return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, originUrl, userName) )); } finally { Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL); } }  Connection connect(String url, Properties info); boolean acceptsURL(String url); DriverPropertyInfo[] getPropertyInfo(String url, Properties info); int getMajorVersion(); int getMinorVersion(); boolean jdbcCompliant(); Logger getParentLogger(); }
SniffyDriver implements Driver, Constants { public Connection connect(String url, Properties info) throws SQLException { if (null == url || !acceptsURL(url)) return null; String originUrl = extractOriginUrl(url); String userName = info.getProperty("user"); checkConnectionAllowed(originUrl, userName, true); Driver originDriver; try { originDriver = DriverManager.getDriver(originUrl); } catch (SQLException e) { try { reloadServiceProviders(); originDriver = DriverManager.getDriver(originUrl); } catch (Exception e2) { throw e; } } if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info); long start = System.currentTimeMillis(); try { Sniffy.enterJdbcMethod(); Connection delegateConnection = originDriver.connect(originUrl, info); return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, originUrl, userName) )); } finally { Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL); } }  Connection connect(String url, Properties info); boolean acceptsURL(String url); DriverPropertyInfo[] getPropertyInfo(String url, Properties info); int getMajorVersion(); int getMinorVersion(); boolean jdbcCompliant(); Logger getParentLogger();  }