/** * 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; }
/** * 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(); }
if (fac.canProcess(params)) { return fac.createDataSource(params); LOGGER.log(Level.WARNING, "Could not acquire " + fac.getDescription() + ":" + t, t);
/** * 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 getAvailableDataSources() { Set availableDS = new HashSet(); Iterator it = getServiceRegistry().getServiceProviders(DataSourceFactorySpi.class, null, null); DataSourceFactorySpi dsFactory; while (it.hasNext()) { dsFactory = (DataSourceFactorySpi) it.next(); if (dsFactory.isAvailable()) { availableDS.add(dsFactory); } } return availableDS.iterator(); }
if (fac.canProcess(params)) { return fac.createDataSource(params); LOGGER.log(Level.WARNING, "Could not acquire " + fac.getDescription() + ":" + t, t);
/** * 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 getAvailableDataSources() { Set availableDS = new HashSet(); Iterator it = getServiceRegistry().getServiceProviders(DataSourceFactorySpi.class); DataSourceFactorySpi dsFactory; while (it.hasNext()) { dsFactory = (DataSourceFactorySpi) it.next(); if (dsFactory.isAvailable()) { availableDS.add(dsFactory); } } return availableDS.iterator(); }