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

net.sf.hajdbc.sql.DatabaseClusterFactoryImpl Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
/*
 * HA-JDBC: High-Availability JDBC
 * Copyright (C) 2012  Paul Ferraro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package net.sf.hajdbc.sql;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import net.sf.hajdbc.*;
import net.sf.hajdbc.state.health.NodeDatabaseRestoreListener;
import net.sf.hajdbc.state.health.NodeStateListener;

/**
 * @author Paul Ferraro
 */
public class DatabaseClusterFactoryImpl> implements DatabaseClusterFactory
{
	Map> databaseClusterListeners = new HashMap>();

	Map> synchronizationListeners = new HashMap>();

	Map> nodeStateListeners = new HashMap>();

	Map> nodeDatabaseRestoreListeners = new HashMap>();

	/**
	 * {@inheritDoc}
	 * @see net.sf.hajdbc.DatabaseClusterFactory#createDatabaseCluster(java.lang.String, net.sf.hajdbc.DatabaseClusterConfigurationFactory)
	 */
	@Override
	public DatabaseCluster createDatabaseCluster(String id, DatabaseClusterConfigurationFactory factory) throws SQLException
	{
		DatabaseCluster databaseCluster = new DatabaseClusterImpl(id, factory.createConfiguration(), factory);
		addDatabaseClusterListeners(id, databaseCluster);
		addSynchronizationListeners(id, databaseCluster);
		addNodeStateListeners(id, databaseCluster);
		addNodeDatabaseRestoreListeners(id,databaseCluster);
		return databaseCluster;
	}

	private void addDatabaseClusterListeners(String id,
			DatabaseCluster databaseCluster) {
		Set listeners = databaseClusterListeners.get(id);
		if(listeners!=null){
			for(DatabaseClusterListener listener:listeners){
				databaseCluster.addListener(listener);
			}
		}
	}
	
	private void addSynchronizationListeners(String id,
			DatabaseCluster databaseCluster) {
		Set listeners = synchronizationListeners.get(id);
		if(listeners!=null){
			for(SynchronizationListener listener:listeners){
				databaseCluster.addSynchronizationListener(listener);
			}
		}
	}

	private void addNodeStateListeners(String id,
																			DatabaseCluster databaseCluster) {
	Set listeners = nodeStateListeners.get(id);
	if(listeners!=null){
		for(NodeStateListener listener:listeners){
			databaseCluster.addListener(listener);
		}
	}
}

	private void addNodeDatabaseRestoreListeners(String id,
																		 DatabaseCluster databaseCluster) {
		Set listeners = nodeDatabaseRestoreListeners.get(id);
		if(listeners!=null){
			for(NodeDatabaseRestoreListener listener:listeners){
				databaseCluster.addListener(listener);
			}
		}
	}

	@Override
	public void addListener(String id, DatabaseClusterListener listener) {
		addListener(databaseClusterListeners,id,listener);
	}

	@Override
	public void removeListener(String id, DatabaseClusterListener listener) {
		removeListener(databaseClusterListeners,id,listener);
	}

	@Override
	public void addSynchronizationListener(String id,
			SynchronizationListener listener) {

		addListener(synchronizationListeners,id,listener);
	}

	@Override
	public void removeSynchronizationListener(String id,
			SynchronizationListener listener) {
		removeListener(synchronizationListeners,id,listener);

	}

	@Override
	public void addListener(String id, NodeStateListener listener) {
		addListener(nodeStateListeners,id,listener);
	}

	public  void addListener(Map> map, String id, T listener) {
		Set listeners = map.get(id);
		if(listeners==null){
			listeners = new LinkedHashSet();
		}
		if(listener!=null){
			listeners.add(listener);
			map.put(id, listeners);
		}
	}

	@Override
	public void removeListener(String id, NodeStateListener listener) {
		removeListener(nodeStateListeners,id,listener);
	}

	private   void removeListener(Map> map,String id, T listener) {
		Set listeners = map.get(id);
		if(listeners!=null){
			if(listener!=null){
				listeners.remove(listener);
			}
			if(listeners.isEmpty()){
				map.remove(id);
			}
		}
	}

	@Override
	public void addListener(String id, NodeDatabaseRestoreListener listener){
		addListener(nodeDatabaseRestoreListeners,id,listener);
	}

	@Override
	public void removeListener(String id,NodeDatabaseRestoreListener listener){
		removeListener(nodeDatabaseRestoreListeners,id,listener);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy