@Test public void testSPIRegistration() throws Exception { Connection connection = new TestConnection(); Connection wrapper = new WrapperConnection(connection); GenericUnWrapper.CONNECTION_METHODS.put( WrapperConnection.class, WrapperConnection.class.getMethod("getUnderlyingConnection", null)); UnWrapper uw = DataSourceFinder.getUnWrapper(wrapper); assertNotNull("registed and canUnwrap", uw); if (uw instanceof GenericUnWrapper) { assertSame("Generic unwrapper is working", connection, uw.unwrap(wrapper)); } } }
@Override public Object unwrap(Class c) throws SQLException { if (isWrapperFor(c)) { return getWrapped(); } return null; } }
public DataSource createDataSource(Map params) throws IOException { return createNewDataSource(params); }
@Test public void testUnwrapConnection() throws SQLException, NoSuchMethodException, SecurityException { Connection connection = new TestConnection(); Connection wrapper = new WrapperConnection(connection); assertTrue(wrapper.isWrapperFor(Connection.class)); Connection unwrap = wrapper.unwrap(Connection.class); assertSame(connection, unwrap); UnWrapper unwrapper = new GenericUnWrapper(); assertFalse(unwrapper.canUnwrap(wrapper)); try { assertNull(unwrapper.unwrap(wrapper)); fail("Cannot unwrap yet"); } catch (Exception expected) { } GenericUnWrapper.CONNECTION_METHODS.put( WrapperConnection.class, WrapperConnection.class.getMethod("getUnderlyingConnection", null)); assertTrue(unwrapper.canUnwrap(wrapper)); assertSame(connection, unwrapper.unwrap(wrapper)); }
/** * Checks each available datasource implementation in turn and returns the first one which * claims to support the resource identified by the params object. * * @param params A Map object which contains a defenition of the resource to connect to. for * file based resources the property 'url' should be set within this Map. * @return The first datasource which claims to process the required resource, returns null if * none can be found. * @throws IOException If a suitable loader can be found, but it can not be attached to the * specified resource without errors. */ public static synchronized DataSource getDataSource(Map params) throws IOException { Iterator ps = getAvailableDataSources(); DataSourceFactorySpi fac; while (ps.hasNext()) { fac = (DataSourceFactorySpi) ps.next(); try { if (fac.canProcess(params)) { return fac.createDataSource(params); } } catch (Throwable t) { /** The logger for the filter module. */ LOGGER.log(Level.WARNING, "Could not acquire " + fac.getDescription() + ":" + t, t); // Protect against DataStores that don't carefully // code canProcess } } return null; }
public void tearDown() throws Exception { final String leakMessage = "Expected no active connection, either there is a connection leak " + "or you forgot to close some object holding onto connections in the tests (e.g., a reader, an iterator)"; if (dataSource instanceof BasicDataSource) { BasicDataSource bds = (BasicDataSource) dataSource; assertEquals(leakMessage, 0, bds.getNumActive()); } else if (dataSource instanceof DBCPDataSource) { BasicDataSource bds = (BasicDataSource) ((DBCPDataSource) dataSource).getWrapped(); assertEquals(leakMessage, 0, bds.getNumActive()); } if (dataSource instanceof BasicDataSource) { BasicDataSource bds = (BasicDataSource) dataSource; bds.close(); } else if (dataSource instanceof ManageableDataSource) { ((ManageableDataSource) dataSource).close(); } }
Iterator ps = getUnWrappers(); UnWrapper uw; while (ps.hasNext()) { if (uw.canUnwrap(conn)) { return uw;
/** * Builds up a default DBCP DataSource that easy to use connection factories can use to setup a * connection pool. * * @param url the jdbc url * @param driverName the jdbc driver full qualified class name * @param username * @param password * @param validationQuery the validation query to be used for connection liveliness on borrow, * or null, if no check is to be performed * @return * @throws DataSourceException */ public static ManageableDataSource buildDefaultDataSource( String url, String driverName, String username, String password, String validationQuery) throws DataSourceException { return buildDefaultDataSource( url, driverName, username, password, 10, 1, validationQuery, false, 0); }
public void testDbcpFactory() throws IOException { assertTrue(new DBCPDataSourceFactory().isAvailable()); DataSourceFinder.scanForPlugins(); Map map = new HashMap(); map.put(DBCPDataSourceFactory.DSTYPE.key, "DBCP"); map.put(DBCPDataSourceFactory.DRIVERCLASS.key, "org.h2.Driver"); map.put(DBCPDataSourceFactory.JDBC_URL.key, "jdbc:h2:mem:test_mem"); map.put(DBCPDataSourceFactory.USERNAME.key, "admin"); map.put(DBCPDataSourceFactory.PASSWORD.key, ""); map.put(DBCPDataSourceFactory.MAXACTIVE.key, Integer.valueOf(10)); map.put(DBCPDataSourceFactory.MAXIDLE.key, Integer.valueOf(0)); DataSource source = DataSourceFinder.getDataSource(map); assertNotNull(source); assertTrue(source instanceof BasicDataSource); }
/** * Finds all implementations of DataStoreFactory which have registered using the services * mechanism, and that have the appropriate libraries on the classpath. * * @return An iterator over all discovered DataStores which have registered factories, and whose * available method returns true. */ public static synchronized Iterator<DataSourceFactorySpi> getAvailableDataSources() { Stream<DataSourceFactorySpi> factories = getServiceRegistry().getFactories(DataSourceFactorySpi.class, null, null); // results are collected into HashSet (even though iterator is returned) // to find broken implementations early rather than later caller code Set<DataSourceFactorySpi> availableDS = factories .filter(dsFactory -> dsFactory.isAvailable()) .collect(Collectors.toCollection(HashSet::new)); return availableDS.iterator(); }
/** * Constructor * * @param config the Config object * @throws IOException */ JDBCAccessBase(Config config) throws IOException { super(); this.config = config; this.dataSource = DataSourceFinder.getDataSource(config.getDataSourceParams()); }
/** * Scans for factory plug-ins on the application class path. This method is needed because the * application class path can theoretically change, or additional plug-ins may become available. * Rather than re-scanning the classpath on every invocation of the API, the class path is * scanned automatically only on the first invocation. Clients can call this method to prompt a * re-scan. Thus this method need only be invoked by sophisticated applications which * dynamically make new plug-ins available at runtime. */ public static synchronized void scanForPlugins() { getServiceRegistry().scanForPlugins(); } }
public boolean canProcess(Map params) { return super.canProcess(params) && "JNDI".equals(params.get("dstype")); }
public Connection unwrap(Connection conn) { if (!canUnwrap(conn)) throw new IllegalArgumentException( "This unwrapper can only handle instances of " + DelegatingConnection.class); Connection unwrapped = ((DelegatingConnection) conn).getInnermostDelegate(); if (unwrapped == null) throw new RuntimeException( "Could not unwrap connection. Is the DBCP pool configured " + "to allow access to underlying connections?"); return unwrapped; }
public DataSource createDataSource(Map params) throws IOException { return createNewDataSource(params); }
public JDBCAccessCustom(Config config) throws IOException { super(); this.config = config; this.dataSource = DataSourceFinder.getDataSource(config.getDataSourceParams()); }
/** * Finds all implementations of UnWrapper which have registered using the services mechanism, * and that have the appropriate libraries on the classpath. * * @return An iterator over all discovered UnWrapper which have registered factories */ public static synchronized Iterator<UnWrapper> getUnWrappers() { return getServiceRegistry().getFactories(UnWrapper.class, null, null).iterator(); }
public boolean canProcess(Map params) { return super.canProcess(params) && "DBCP".equals(params.get("dstype")); }
public Statement unwrap(Statement statement) { if (!canUnwrap(statement)) throw new IllegalArgumentException( "This unwrapper can only handle instances of " + DelegatingStatement.class); Statement unwrapped = ((DelegatingStatement) statement).getInnermostDelegate(); if (unwrapped == null) throw new RuntimeException( "Could not unwrap connection. Is the DBCP pool configured " + "to allow access to underlying connections?"); return unwrapped; } }
/** * @return datasource for this dialect object * @throws Exception */ private DataSource getDataSource() throws Exception { if (dataSource != null) { return dataSource; } Config config = getConfig(); dataSource = DataSourceFinder.getDataSource(config.getDataSourceParams()); return dataSource; }