@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)); } } }
/** * 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; }
/** * Constructor * * @param config the Config object * @throws IOException */ JDBCAccessBase(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(); }
Iterator ps = getUnWrappers(); UnWrapper uw; while (ps.hasNext()) {
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); }
/** * 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(); } }
Iterator ps = getUnWrappers(); UnWrapper uw; while (ps.hasNext()) {
UnWrapper unwrapper = uwMap.get(testCon.getClass()); if (unwrapper == null) { unwrapper = DataSourceFinder.getUnWrapper(testCon); if (unwrapper == null) { unwrapper = UNWRAPPER_NOT_FOUND;
/** * 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(); }
Iterator ps = getUnWrappers(); UnWrapper uw; while (ps.hasNext()) {
public JDBCAccessCustom(Config config) throws IOException { super(); this.config = config; this.dataSource = DataSourceFinder.getDataSource(config.getDataSourceParams()); }
Iterator ps = getAvailableDataSources(); DataSourceFactorySpi fac; while (ps.hasNext()) {
UnWrapper uw = DataSourceFinder.getUnWrapper(conn); assertNotNull(uw); assertTrue(uw.canUnwrap(conn)); uw = DataSourceFinder.getUnWrapper(st); assertNotNull(uw); assertTrue(uw.canUnwrap(st)); uw = DataSourceFinder.getUnWrapper(ps); assertNotNull(uw); assertTrue(uw.canUnwrap(ps));
/** * Finds all implemtaions 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 getUnWrappers() { Set availableDS = new HashSet(); return getServiceRegistry().getServiceProviders(UnWrapper.class); }
Iterator ps = getUnWrappers(); UnWrapper uw; while (ps.hasNext()) {
/** * @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; }
Iterator ps = getAvailableDataSources(); DataSourceFactorySpi fac; while (ps.hasNext()) {
public OracleFeatureWriter(FeatureReader <SimpleFeatureType, SimpleFeature> fReader, QueryData queryData ) throws IOException { super(fReader, queryData); Connection conn = queryData.getConnection(); if(!(conn instanceof OracleConnection)) { UnWrapper uw = DataSourceFinder.getUnWrapper(conn); if(uw != null) conn = uw.unwrap(conn); } OracleConnection oracleConnection = (OracleConnection) conn; this.converter = new GeometryConverter(oracleConnection); }
/** * 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(); } }