All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.sylinx.hbatis.ext.ctxds.SPIManager Maven / Gradle / Ivy

The newest version!
package cn.sylinx.hbatis.ext.ctxds;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.concurrent.locks.ReentrantLock;

import cn.sylinx.hbatis.kit.ClassUtil;

class SPIManager {

	private static DynamicDatasourceProvider dynamicDatasourceProvider;

	private static ReentrantLock lock = new ReentrantLock();

	static DynamicDatasourceProvider getDynamicDatasourceProvider() {

		if (dynamicDatasourceProvider != null) {
			return dynamicDatasourceProvider;
		}

		// 加載DynamicDatasourceProvider
		loadDynamicDatasourceProvider();

		return dynamicDatasourceProvider;

	}

	private static void loadDynamicDatasourceProvider() {

		try {

			lock.lock();

			if (dynamicDatasourceProvider == null) {

				ServiceLoader sl = ServiceLoader.load(DynamicDatasourceProvider.class, ClassUtil.getDefaultClassLoader());
				Iterator it = sl.iterator();
				dynamicDatasourceProvider = it.hasNext() ? it.next() : null;
			}

			if (dynamicDatasourceProvider == null) {
				dynamicDatasourceProvider = new DefaultDynamicDatasourceProvider();
			}

		} finally {
			lock.unlock();
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy