<commit_before>package ch.elexis.core.jpa.datasource.internal;

import java.util.Hashtable;
import java.util.Map;
import java.util.Optional;

import javax.sql.DataSource;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.common.DBConnection;
import ch.elexis.core.common.DBConnection.DBType;

import static ch.elexis.core.constants.ElexisEnvironmentPropertyConstants.*;
import ch.elexis.core.jpa.datasource.test.TestDatabaseConnection;
import ch.elexis.core.services.IElexisDataSource;
import ch.elexis.core.utils.CoreUtil;

@Component(immediate = true, property = "id=default")
public class ElexisDataSourceService implements IElexisDataSource {
	
	private static Logger log = LoggerFactory.getLogger(ElexisDataSourceService.class);
	
	private static ServiceRegistration<DataSource> servReg;
	private static ElexisPoolingDataSource currentDataSource;
	
	@Activate
	public void activate(){
		System.out.println("Activating ElexisDataSourceService ...");
		log.debug("Activating ...");
		if (CoreUtil.isTestMode()) {
			log.warn("- test-mode -");
			IStatus setDBConnection = setDBConnection(new TestDatabaseConnection());
			if (!setDBConnection.isOK()) {
				log.error("Error setting db connection", setDBConnection.getMessage());
				System.out.println("ERROR " + setDBConnection.getMessage());
			}
			return;
		}
		
		DBConnection connection = getEnvironmentProvidedDbConnection();
		if (connection != null) {
			log.info("Initializing Database connection via environment variables.");
			setDBConnection(connection);
		}
		
	}
	
	public IStatus setDBConnection(DBConnection dbConnection){
		try {
			if (servReg != null) {
				log.info("Unregistering service registration");
				currentDataSource.deactivate();
				servReg.unregister();
				servReg = null;
				currentDataSource = null;
			}
			
			currentDataSource = new ElexisPoolingDataSource(dbConnection);
			currentDataSource.activate();
			Hashtable<String, String> properties = new Hashtable<>();
			properties.put("id", "default");
			servReg = FrameworkUtil.getBundle(getClass()).getBundleContext()
				.registerService(DataSource.class, currentDataSource, properties);
			return Status.OK_STATUS;
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// Logging might not even been initialized yet
			// so leave the stack trace to sysout
			e.printStackTrace();<commit_msg>[15922] Show status on error<commit_after>package ch.elexis.core.jpa.datasource.internal;

import java.util.Hashtable;
import java.util.Map;
import java.util.Optional;

import javax.sql.DataSource;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.common.DBConnection;
import ch.elexis.core.common.DBConnection.DBType;

import static ch.elexis.core.constants.ElexisEnvironmentPropertyConstants.*;
import ch.elexis.core.jpa.datasource.test.TestDatabaseConnection;
import ch.elexis.core.services.IElexisDataSource;
import ch.elexis.core.utils.CoreUtil;

@Component(immediate = true, property = "id=default")
public class ElexisDataSourceService implements IElexisDataSource {
	
	private static Logger log = LoggerFactory.getLogger(ElexisDataSourceService.class);
	
	private static ServiceRegistration<DataSource> servReg;
	private static ElexisPoolingDataSource currentDataSource;
	
	@Activate
	public void activate(){
		System.out.println("Activating ElexisDataSourceService ...");
		log.debug("Activating ...");
		if (CoreUtil.isTestMode()) {
			log.warn("- test-mode -");
			IStatus setDBConnection = setDBConnection(new TestDatabaseConnection());
			if (!setDBConnection.isOK()) {
				log.error("Error setting db connection", setDBConnection.getMessage());
				System.out.println("ERROR " + setDBConnection.getMessage());
			}
			return;
		}
		
		DBConnection connection = getEnvironmentProvidedDbConnection();
		if (connection != null) {
			log.info("Initializing Database connection via environment variables.");
			IStatus setDBConnection = setDBConnection(connection);
			if (!setDBConnection.isOK()) {
				log.error("Error setting db connection", setDBConnection.getMessage());
				System.out.println("ERROR " + setDBConnection.getMessage());
			}
		}
		
	}
	
	public IStatus setDBConnection(DBConnection dbConnection){
		try {
			if (servReg != null) {
				log.info("Unregistering service registration");
				currentDataSource.deactivate();
				servReg.unregister();
				servReg = null;
				currentDataSource = null;
			}
			
			currentDataSource = new ElexisPoolingDataSource(dbConnection);
			currentDataSource.activate();
			Hashtable<String, String> properties = new Hashtable<>();
			properties.put("id", "default");
			servReg = FrameworkUtil.getBundle(getClass()).getBundleContext()
				.registerService(DataSource.class, currentDataSource, properties);
			return Status.OK_STATUS;
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// Logging might not even been initialized yet
			// so leave the stack trace to sysout
			e.printStackTrace();
{'fraction_non_alphanumeric': 0.1102774384924097, 'fraction_numerical': 0.0019193857965451055, 'mean_word_length': 5.250817884405671, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6749532', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1613, 'n_words': 328}