Search in sources :

Example 11 with DataSourceProxyConfig

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig in project druid by alibaba.

the class LogFilterTest method test_logFilter_2.

public void test_logFilter_2() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    {
        log4jFilter.init(dataSource);
        setLogDisableAll(log4jFilter, true);
        config.getFilters().add(log4jFilter);
    }
    CommonsLogFilter logFilter = new CommonsLogFilter();
    {
        logFilter.init(dataSource);
        setLogDisableAll(logFilter, true);
        config.getFilters().add(logFilter);
    }
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.BLOB, Types.CLOB, Types.NCLOB, Types.BINARY, Types.OTHER };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }
    };
    ConnectionProxy conn = (ConnectionProxy) dataSource.connect(new Properties());
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
    }
    {
        final MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                throw new SQLException();
            }
        };
        StatementProxy stmt = new StatementProxyImpl(conn, new MockStatement(conn) {

            public ResultSet getResultSet() throws SQLException {
                return rs;
            }
        }, 0);
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.statement_getResultSet(stmt);
    }
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        {
            logFilter.setResultSetLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
        {
            logFilter.setResultSetNextAfterLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
    }
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement)

Example 12 with DataSourceProxyConfig

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig in project druid by alibaba.

the class DruidDataSource method initFromWrapDriverUrl.

private void initFromWrapDriverUrl() throws SQLException {
    if (!jdbcUrl.startsWith(DruidDriver.DEFAULT_PREFIX)) {
        return;
    }
    DataSourceProxyConfig config = DruidDriver.parseConfig(jdbcUrl, null);
    this.driverClass = config.getRawDriverClassName();
    LOG.error("error url : '" + jdbcUrl + "', it should be : '" + config.getRawUrl() + "'");
    this.jdbcUrl = config.getRawUrl();
    if (this.name == null) {
        this.name = config.getName();
    }
    for (Filter filter : config.getFilters()) {
        addFilter(filter);
    }
}
Also used : DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) WallFilter(com.alibaba.druid.wall.WallFilter) Filter(com.alibaba.druid.filter.Filter)

Aggregations

DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)12 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)10 MockDriver (com.alibaba.druid.mock.MockDriver)5 Properties (java.util.Properties)5 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)4 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)4 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 FilterEventAdapter (com.alibaba.druid.filter.FilterEventAdapter)3 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)3 StatFilter (com.alibaba.druid.filter.stat.StatFilter)3 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)3 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)3 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)3 SQLException (java.sql.SQLException)3 Filter (com.alibaba.druid.filter.Filter)2 FilterChain (com.alibaba.druid.filter.FilterChain)2 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)2 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2