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

net.sf.hajdbc.DatabaseClusterRegistry Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
package net.sf.hajdbc;

import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.sql.DatabaseClusterFactoryImpl;
import net.sf.hajdbc.sql.DriverDatabase;
import net.sf.hajdbc.sql.DriverDatabaseClusterConfiguration;
import net.sf.hajdbc.util.TimePeriod;
import net.sf.hajdbc.util.concurrent.LifecycleRegistry;
import net.sf.hajdbc.util.concurrent.MapRegistryStoreFactory;
import net.sf.hajdbc.util.concurrent.Registry;
import net.sf.hajdbc.xml.XMLDatabaseClusterConfigurationFactory;

/**
 * 注册管理器
 * @author dib
 *
 */
public enum DatabaseClusterRegistry {
	registry;
	private static final String CONFIG = "config";
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(DatabaseClusterRegistry.class);

	volatile TimePeriod timeout = new TimePeriod(10, TimeUnit.SECONDS);
	
	volatile DatabaseClusterFactory factory = new DatabaseClusterFactoryImpl();
	
	final Map> configurationFactories = new ConcurrentHashMap>();
	private final Registry.Factory, Properties, SQLException> registryFactory = new Registry.Factory, Properties, SQLException>()
	{
		@Override
		public DatabaseCluster create(String id, Properties properties) throws SQLException
		{
			DatabaseClusterConfigurationFactory configurationFactory = configurationFactories.get(id);
			
			if (configurationFactory == null)
			{
				String config = (properties != null) ? properties.getProperty(CONFIG) : null;
				configurationFactory = new XMLDatabaseClusterConfigurationFactory(DriverDatabaseClusterConfiguration.class, id, config);
			}
			
			return factory.createDatabaseCluster(id, configurationFactory);
		}

		@Override
		public TimePeriod getTimeout()
		{
			return timeout;
		}
	};
	private final Registry, Properties, SQLException> reg = new LifecycleRegistry, Properties, SQLException>(registryFactory, new MapRegistryStoreFactory(), ExceptionType.SQL.getExceptionFactory());

	
	public DatabaseCluster get(String id,Properties context) throws SQLException
	{
		return reg.get(id,context);
	}
	
	public DatabaseCluster get(String id) throws SQLException
	{
		return reg.get(id);
	}

	public void stop(String id) throws SQLException
	{
		reg.remove(id);
	}

	public void setFactory(DatabaseClusterFactory databaseClusterFactory)
	{
		factory = databaseClusterFactory;
	}

	public void setConfigurationFactory(String id, DatabaseClusterConfigurationFactory configurationFactory)
	{
		configurationFactories.put(id,  configurationFactory);
	}
	
	public void setTimeout(long value, TimeUnit unit)
	{
		timeout = new TimePeriod(value, unit);
	}

	public DatabaseClusterFactory getFactory() {
		return factory;
	}
	
    

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy