<commit_before>/*
 *****************************************************************************
 * Copyright (c) 2004, 2007 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation - initial API and implementation
 *
 ******************************************************************************
 */ 

package org.eclipse.birt.data.engine.odaconsumer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.datatools.connectivity.oda.IConnection;
import org.eclipse.datatools.connectivity.oda.IDriver;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.consumer.services.IPropertyProvider;
import org.eclipse.datatools.connectivity.oda.profile.Constants;

/**
 * ConnectionManager manages a set of data source connections.  Calling 
 * <code>getInstance</code> will return an instance of <code>ConnectionManager</code>.
 * When the method <code>openConnection</code> is called, the 
 * <code>ConnectionManager</code> will attempt to open and return a 
 * <code>Connection</code> instance of the data source extension 
 * supported by that driver.
 */
public class ConnectionManager
{
	private static ConnectionManager sm_instance = null;
    
    private static final String DTP_CONN_PROFILE_APPL_ID = Constants.CONN_PROFILE_APPL_ID;
	
    // trace logging variables
	private static final String sm_className = ConnectionManager.class.getName();
	static final String sm_packageName = "org.eclipse.birt.data.engine.odaconsumer";  //$NON-NLS-1$
	private static LogHelper sm_logger = null;
	
	protected ConnectionManager( )
	{
	}
	
	/**
	 * Returns a <code>ConnectionManager</code> instance for getting opened 
	 * <code>Connections</code>.
	 * @return	a <code>ConnectionManager</code> instance.
	 */
	public static ConnectionManager getInstance( )
	{
		final String methodName = "getInstance";		 //$NON-NLS-1$
		getLogger().entering( sm_className, methodName );

		if( sm_instance == null )
		{
			synchronized( ConnectionManager.class )
			{
				if( sm_instance == null )
					sm_instance = new ConnectionManager( );
			}
		}

		getLogger().exiting( sm_className, methodName, sm_instance );		
		return sm_instance;
	}
    
    /**
     * Singleton instance release method.
     */
    public static void releaseInstance()
    {
    	DriverManager.releaseInstance();
        sm_instance = null;
        sm_logger = null;
    }

    private static LogHelper getLogger()
    {
        if( sm_logger == null )
        {
        	synchronized ( ConnectionManager.class )
        	{
        		if( sm_logger == null )
        			sm_logger = LogHelper.getInstance( sm_packageName );
        	}
        }
        
        return sm_logger;
    }

	/**
	 * Returns an opened <code>Connection</code> that is supported by the specified 
	 * data source extension using the specified connection properties.
	 * @param dataSourceElementId	id of the data source element defined
	 * 								in the data source extension.
	 * @param connectionProperties	connection properties to open the underlying connection.
	 * @return	an opened <code>Connection</code> instance. 
	 * @throws DataException	if data source error occurs.
     * @deprecated  since 2.2
	 */
	public Connection openConnection( String dataSourceElementId, 
			  						  Properties connectionProperties )
		throws DataException
    {
	    return openConnection( dataSourceElementId, 
	            				connectionProperties, null );
    }
	
	/**
	 * Same functionality as the first openConnection method, but
	 * with an additional argument to pass in an application context 
	 * to the underlying ODA driver.
	 * @param appContext	Application context map to pass thru to 
	 * 						the underlying ODA driver.
	 * @return	an opened <code>Connection</code> instance. 
	 * @throws DataException	if data source error occurs
	 */
	public Connection openConnection( String dataSourceElementId, 
									  Properties connectionProperties,
									  Map appContext )
		throws DataException
	{
		final String methodName = "openConnection"; //$NON-NLS-1$
		
		if( getLogger().isLoggingEnterExitLevel() )
			getLogger().entering( sm_className, methodName, 
								new Object[] { dataSourceElementId, connectionProperties } );
		
		try
		{
            DriverManager driverMgr = DriverManager.getInstance();

            // gets the driver's connection to open
            IDriver driverHelper = 
                driverMgr.getDriverHelper( dataSourceElementId );
			String dataSourceId = 
                driverMgr.getExtensionDataSourceId( dataSourceElementId );          

            // specifies default connection profile property provider service
            appContext = addProfileProviderService( appContext );

			// before calling getConnection, passes application context
			// to the oda driver, which in turn takes care of 
			// passing thru to the driver's connection(s) and quer(ies)
			driverHelper.setAppContext( appContext );
			
			IConnection connection = driverHelper.getConnection( dataSourceId );
			connection.open( connectionProperties );
			
			Connection ret = new Connection( connection, dataSourceElementId );
			
			getLogger().exiting( sm_className, methodName, ret );	
			return ret;
		}
		catch( OdaException ex )
		{
			getLogger().logp( Level.SEVERE, sm_className, methodName, 
							"Cannot open connection.", ex ); //$NON-NLS-1$
			
			throw new DataException( ResourceConstants.CANNOT_OPEN_CONNECTION, ex, 
			                         new Object[] { dataSourceElementId } );
		}
		catch( UnsupportedOperationException ex )
		{
			getLogger().logp( Level.SEVERE, sm_className, methodName, 
							"Cannot open connection.", ex ); //$NON-NLS-1$
			
			throw new DataException( ResourceConstants.CANNOT_OPEN_CONNECTION, ex, 
			                         new Object[] { dataSourceElementId } );
		}
	}

    /**<commit_msg>Fix a ODA connection bug. The current locale should be set to ODA driver[7902].<commit_after>/*
 *****************************************************************************
 * Copyright (c) 2004, 2007 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation - initial API and implementation
 *
 ******************************************************************************
 */ 

package org.eclipse.birt.data.engine.odaconsumer;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.datatools.connectivity.oda.IConnection;
import org.eclipse.datatools.connectivity.oda.IDriver;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.consumer.services.IPropertyProvider;
import org.eclipse.datatools.connectivity.oda.profile.Constants;

import com.ibm.icu.util.ULocale;

/**
 * ConnectionManager manages a set of data source connections.  Calling 
 * <code>getInstance</code> will return an instance of <code>ConnectionManager</code>.
 * When the method <code>openConnection</code> is called, the 
 * <code>ConnectionManager</code> will attempt to open and return a 
 * <code>Connection</code> instance of the data source extension 
 * supported by that driver.
 */
public class ConnectionManager
{
	private static ConnectionManager sm_instance = null;
    
    private static final String DTP_CONN_PROFILE_APPL_ID = Constants.CONN_PROFILE_APPL_ID;
	
    // trace logging variables
	private static final String sm_className = ConnectionManager.class.getName();
	static final String sm_packageName = "org.eclipse.birt.data.engine.odaconsumer";  //$NON-NLS-1$
	private static LogHelper sm_logger = null;
	
	protected ConnectionManager( )
	{
	}
	
	/**
	 * Returns a <code>ConnectionManager</code> instance for getting opened 
	 * <code>Connections</code>.
	 * @return	a <code>ConnectionManager</code> instance.
	 */
	public static ConnectionManager getInstance( )
	{
		final String methodName = "getInstance";		 //$NON-NLS-1$
		getLogger().entering( sm_className, methodName );

		if( sm_instance == null )
		{
			synchronized( ConnectionManager.class )
			{
				if( sm_instance == null )
					sm_instance = new ConnectionManager( );
			}
		}

		getLogger().exiting( sm_className, methodName, sm_instance );		
		return sm_instance;
	}
    
    /**
     * Singleton instance release method.
     */
    public static void releaseInstance()
    {
    	DriverManager.releaseInstance();
        sm_instance = null;
        sm_logger = null;
    }

    private static LogHelper getLogger()
    {
        if( sm_logger == null )
        {
        	synchronized ( ConnectionManager.class )
        	{
        		if( sm_logger == null )
        			sm_logger = LogHelper.getInstance( sm_packageName );
        	}
        }
        
        return sm_logger;
    }

	/**
	 * Returns an opened <code>Connection</code> that is supported by the specified 
	 * data source extension using the specified connection properties.
	 * @param dataSourceElementId	id of the data source element defined
	 * 								in the data source extension.
	 * @param connectionProperties	connection properties to open the underlying connection.
	 * @return	an opened <code>Connection</code> instance. 
	 * @throws DataException	if data source error occurs.
     * @deprecated  since 2.2
	 */
	public Connection openConnection( String dataSourceElementId, 
			  						  Properties connectionProperties )
		throws DataException
    {
	    return openConnection( dataSourceElementId, 
	            				connectionProperties, null );
    }
	
	/**
	 * Same functionality as the first openConnection method, but
	 * with an additional argument to pass in an application context 
	 * to the underlying ODA driver.
	 * @param appContext	Application context map to pass thru to 
	 * 						the underlying ODA driver.
	 * @return	an opened <code>Connection</code> instance. 
	 * @throws DataException	if data source error occurs
	 */
	public Connection openConnection( String dataSourceElementId, 
									  Properties connectionProperties,
									  Map appContext )
		throws DataException
	{
		final String methodName = "openConnection"; //$NON-NLS-1$
		
		if( getLogger().isLoggingEnterExitLevel() )
			getLogger().entering( sm_className, methodName, 
								new Object[] { dataSourceElementId, connectionProperties } );
		
		try
		{
            DriverManager driverMgr = DriverManager.getInstance();

            // gets the driver's connection to open
            IDriver driverHelper = 
                driverMgr.getDriverHelper( dataSourceElementId );
			String dataSourceId = 
                driverMgr.getExtensionDataSourceId( dataSourceElementId );          

            // specifies default connection profile property provider service
            appContext = addProfileProviderService( appContext );

			// before calling getConnection, passes application context
			// to the oda driver, which in turn takes care of 
			// passing thru to the driver's connection(s) and quer(ies)
			driverHelper.setAppContext( appContext );
			
			IConnection connection = driverHelper.getConnection( dataSourceId );
			ULocale locale = toULocale( appContext.get( "AppRuntimeLocale" ) );
			if( locale != null )
			{
				try
				{
					connection.setLocale( locale );
				}
				catch( UnsupportedOperationException ex )
				{
					
				}
			}
			connection.open( connectionProperties );
			
			Connection ret = new Connection( connection, dataSourceElementId );
			
			getLogger().exiting( sm_className, methodName, ret );	
			return ret;
		}
		catch( OdaException ex )
		{
			getLogger().logp( Level.SEVERE, sm_className, methodName, 
							"Cannot open connection.", ex ); //$NON-NLS-1$
			
			throw new DataException( ResourceConstants.CANNOT_OPEN_CONNECTION, ex, 
			                         new Object[] { dataSourceElementId } );
		}
		catch( UnsupportedOperationException ex )
		{
			getLogger().logp( Level.SEVERE, sm_className, methodName, 
							"Cannot open connection.", ex ); //$NON-NLS-1$
			
			throw new DataException( ResourceConstants.CANNOT_OPEN_CONNECTION, ex, 
			                         new Object[] { dataSourceElementId } );
		}
	}
	
	/**
	 * 
	 * @param source
	 * @return
	 */
	static ULocale toULocale( Object source )
	{
		if( source == null )
			return null;
			if ( source instanceof ULocale )
			{
				return (ULocale) source;
			}
			else if ( source instanceof Locale )
			{
				return ULocale.forLocale( ( Locale ) source );
			}
			else if ( source instanceof String )
			{
				return new ULocale( (String) source );
			}
		
		return null;
	}

    /**
{'fraction_non_alphanumeric': 0.10933570054741826, 'fraction_numerical': 0.00340287024707797, 'mean_word_length': 3.2286518611197996, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '2149062', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3569, 'n_words': 1030}