<commit_before>/*
 * Copyright (c) 2000-2007 MetaMatrix, Inc.
 * All rights reserved.
 */
package org.teiid.test.framework.datasource;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.teiid.test.framework.ConfigPropertyLoader;
import org.teiid.test.framework.ConfigPropertyNames;
import org.teiid.test.framework.exception.QueryTestFailedException;
import org.teiid.test.framework.exception.TransactionRuntimeException;
import org.teiid.test.util.StringUtil;

import com.metamatrix.common.xml.XMLReaderWriter;
import com.metamatrix.common.xml.XMLReaderWriterImpl;

/**
 * The DataSourceMgr is responsible for loading and managing datasources defined by the datasource
 * mapping properties file {@see #DATASOURCE_MAPPING_FILE} and the mapped
 * datasource properties files. The {@link #getDatasourceProperties(String)}
 * returns the properties defined for that datasourceid, which is mapped in the
 * mnappings file. This mapping allows the test
 * 
 * @author vanhalbert
 * 
 */
public class DataSourceMgr {
	

	static final String RELATIVE_DIRECTORY = "datasources/";
	static final String DATASOURCE_MAPPING_FILE = "datasource_mapping.xml";

	private static DataSourceMgr _instance = null;
	
	private Map<String, Map<String, DataSource>>dstypeMap = new HashMap<String, Map<String, DataSource>>();  //key=datasourcetype

	private Map<String, DataSource> allDatasourcesMap = new HashMap<String, DataSource>();  // key=datasource name
	
	private Map<String, DataSource> modelToDatasourceMap = new HashMap<String, DataSource>();  // key=modelname + "_" + datasourcename
																								// key=modelname + "_" + groupname
	
	// this set is use to track datasources that have already been assigned
	private Set<String> assignedDataSources = new HashSet<String>();
	
	private int lastassigned = 0;
	private Set<String> excludedDBTypes = null;


	private DataSourceMgr() {
	}

	public static synchronized DataSourceMgr getInstance() {
		if (_instance == null) {
			_instance = new DataSourceMgr();
			try {
				_instance.loadDataSourceMappings();
			} catch (QueryTestFailedException e) {
				// TODO Auto-generated catch block
				throw new TransactionRuntimeException(e);
			} catch (TransactionRuntimeException e) {
				// TODO Auto-generated catch block
				throw e;
			}

		}
		return _instance;
	}
	
	public static synchronized void reset() {
		
		_instance.dstypeMap.clear();
		_instance.allDatasourcesMap.clear();
		_instance.modelToDatasourceMap.clear();
		_instance.assignedDataSources.clear();
		
		_instance = null;
		
	}
	
	public int numberOfAvailDataSources() {
		return allDatasourcesMap.size();
	}
	
	public boolean hasAvailableDataSource(int numRequiredDataSources) {

		excludedDBTypes = new HashSet<String>(3);
		String excludeprop = ConfigPropertyLoader.getProperty(ConfigPropertyNames.EXCLUDE_DATASBASE_TYPES_PROP);
		
		if (excludeprop == null || excludeprop.length() == 0) {
			return (numRequiredDataSources <= numberOfAvailDataSources());
		}
		
		List<String> eprops = StringUtil.split(excludeprop, ",");
		excludedDBTypes.addAll(eprops);
		        
    	int cntexcluded = 0;
		Iterator<DataSource> it= allDatasourcesMap.values().iterator();
		while(it.hasNext()) {
			DataSource ds = it.next();
			if (excludedDBTypes.contains(ds.getDBType())) {
				cntexcluded++;
			}
		}
		
		return(numRequiredDataSources <=  (numberOfAvailDataSources() - cntexcluded)); <commit_msg>Teiid 773 - setting up the xa transaction tests<commit_after>/*
 * Copyright (c) 2000-2007 MetaMatrix, Inc.
 * All rights reserved.
 */
package org.teiid.test.framework.datasource;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.teiid.test.framework.ConfigPropertyLoader;
import org.teiid.test.framework.ConfigPropertyNames;
import org.teiid.test.framework.exception.QueryTestFailedException;
import org.teiid.test.framework.exception.TransactionRuntimeException;
import org.teiid.test.util.StringUtil;

import com.metamatrix.common.xml.XMLReaderWriter;
import com.metamatrix.common.xml.XMLReaderWriterImpl;

/**
 * The DataSourceMgr is responsible for loading and managing datasources defined by the datasource
 * mapping properties file {@see #DATASOURCE_MAPPING_FILE} and the mapped
 * datasource properties files. The {@link #getDatasourceProperties(String)}
 * returns the properties defined for that datasourceid, which is mapped in the
 * mnappings file. This mapping allows the test
 * 
 * @author vanhalbert
 * 
 */
public class DataSourceMgr {
	

	static final String RELATIVE_DIRECTORY = "datasources/";
	static final String DATASOURCE_MAPPING_FILE = "datasource_mapping.xml";

	private static DataSourceMgr _instance = null;
	
	private Map<String, Map<String, DataSource>>dstypeMap = new HashMap<String, Map<String, DataSource>>();  //key=datasourcetype

	private Map<String, DataSource> allDatasourcesMap = new HashMap<String, DataSource>();  // key=datasource name
	
	private Map<String, DataSource> modelToDatasourceMap = new HashMap<String, DataSource>();  // key=modelname + "_" + datasourcename
																								// key=modelname + "_" + groupname
	
	// this set is use to track datasources that have already been assigned
	private Set<String> assignedDataSources = new HashSet<String>();
	
	private int lastassigned = 0;
	private Set<String> excludedDBTypes = null;


	private DataSourceMgr() {
	}

	public static synchronized DataSourceMgr getInstance() {
		if (_instance == null) {
			_instance = new DataSourceMgr();
			try {
				_instance.loadDataSourceMappings();
			} catch (QueryTestFailedException e) {
				// TODO Auto-generated catch block
				throw new TransactionRuntimeException(e);
			} catch (TransactionRuntimeException e) {
				// TODO Auto-generated catch block
				throw e;
			}

		}
		return _instance;
	}
	
	public static synchronized void reset() {
		
		_instance.dstypeMap.clear();
		_instance.allDatasourcesMap.clear();
		_instance.modelToDatasourceMap.clear();
		_instance.assignedDataSources.clear();
		
		_instance = null;
		
	}
	
	public int numberOfAvailDataSources() {
		return allDatasourcesMap.size();
	}
	
	public boolean hasAvailableDataSource(int numRequiredDataSources) {
		// reset the mapping at the start of each test
		_instance.modelToDatasourceMap.clear();
		excludedDBTypes = new HashSet<String>(3);
		
		
		String excludeprop = ConfigPropertyLoader.getProperty(ConfigPropertyNames.EXCLUDE_DATASBASE_TYPES_PROP);
		
		if (excludeprop == null || excludeprop.length() == 0) {
			return (numRequiredDataSources <= numberOfAvailDataSources());
		}
		
		List<String> eprops = StringUtil.split(excludeprop, ",");
		excludedDBTypes.addAll(eprops);
		        
    	int cntexcluded = 0;
		Iterator<DataSource> it= allDatasourcesMap.values().iterator();
		while(it.hasNext()) {
			DataSource ds = it.next();
			if (excludedDBTypes.contains(ds.getDBType())) {
				cntexcluded++;
			}
		}
		
		return(numRequiredDataSources <=  (numberOfAvailDataSources() - cntexcluded)); 
{'fraction_non_alphanumeric': 0.10235168359166222, 'fraction_numerical': 0.0036076964190272583, 'mean_word_length': 4.602544910179641, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6350498', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2198, 'n_words': 535}