<commit_before>package org.lightmare.cache;

import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;

import org.apache.log4j.Logger;
import org.lightmare.jndi.JndiManager;
import org.lightmare.jpa.JPAManager;
import org.lightmare.utils.NamingUtils;
import org.lightmare.utils.ObjectUtils;

/**
 * Container class to cache connections
 * 
 * @author levan
 * 
 */
public class ConnectionContainer {

    // Keeps unique EntityManagerFactories builded by unit names
    private static final ConcurrentMap<String, ConnectionSemaphore> CONNECTIONS = new ConcurrentHashMap<String, ConnectionSemaphore>();

    private static final Logger LOG = Logger
	    .getLogger(ConnectionContainer.class);

    public static boolean checkForEmf(String unitName) {

	boolean check = ObjectUtils.available(unitName);

	if (check) {
	    check = CONNECTIONS.containsKey(unitName);
	}

	return check;
    }

    /**
     * Gets {@link ConnectionSemaphore} from cache without waiting for lock
     * 
     * @param unitName
     * @return {@link ConnectionSemaphore}
     */
    public static ConnectionSemaphore getSemaphore(String unitName) {

	return CONNECTIONS.get(unitName);
    }

    /**
     * Creates and locks {@link ConnectionSemaphore} instance
     * 
     * @param unitName
     * @return {@link ConnectionSemaphore}
     */
    private static ConnectionSemaphore createSemaphore(String unitName) {

	ConnectionSemaphore semaphore = CONNECTIONS.get(unitName);
	ConnectionSemaphore current = null;
	if (semaphore == null) {
	    semaphore = new ConnectionSemaphore();
	    semaphore.setUnitName(unitName);
	    semaphore.setInProgress(Boolean.TRUE);
	    semaphore.setCached(Boolean.TRUE);
	    current = CONNECTIONS.putIfAbsent(unitName, semaphore);
	}
	if (current == null) {
	    current = semaphore;
	}
	current.incrementUser();

	return current;
    }

    /**
     * Caches {@link ConnectionSemaphore} with lock
     * 
     * @param unitName
     * @param jndiName
     * @return {@link ConnectionSemaphore}
     */
    public static ConnectionSemaphore setSemaphore(String unitName,
	    String jndiName) {

	ConnectionSemaphore semaphore = null;

	if (ObjectUtils.available(unitName)) {

	    semaphore = createSemaphore(unitName);
	    if (ObjectUtils.available(jndiName)) {
		ConnectionSemaphore existent = CONNECTIONS.putIfAbsent(
			jndiName, semaphore);
		if (existent == null) {
		    semaphore.setJndiName(jndiName);
		}
	    }
	}

	return semaphore;
    }

    /**
     * Waits until {@link ConnectionSemaphore} is in progress (locked)
     * 
     * @param semaphore
     */
    private static void awaitConnection(ConnectionSemaphore semaphore) {

	synchronized (semaphore) {
	    boolean inProgress = semaphore.isInProgress()
		    && !semaphore.isBound();
	    while (inProgress) {
		try {
		    semaphore.wait();
		    inProgress = semaphore.isInProgress()
			    && !semaphore.isBound();
		} catch (InterruptedException ex) {
		    inProgress = Boolean.FALSE;
		    LOG.error(ex.getMessage(), ex);
		}
	    }
	}
    }

    /**<commit_msg>improved code in ConnectionContainer class<commit_after>package org.lightmare.cache;

import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;

import org.apache.log4j.Logger;
import org.lightmare.jndi.JndiManager;
import org.lightmare.jpa.JPAManager;
import org.lightmare.utils.NamingUtils;
import org.lightmare.utils.ObjectUtils;

/**
 * Container class to cache connections
 * 
 * @author levan
 * 
 */
public class ConnectionContainer {

    // Keeps unique EntityManagerFactories builded by unit names
    private static final ConcurrentMap<String, ConnectionSemaphore> CONNECTIONS = new ConcurrentHashMap<String, ConnectionSemaphore>();

    private static final Logger LOG = Logger
	    .getLogger(ConnectionContainer.class);

    public static boolean checkForEmf(String unitName) {

	boolean check = ObjectUtils.available(unitName);

	if (check) {
	    check = CONNECTIONS.containsKey(unitName);
	}

	return check;
    }

    /**
     * Gets {@link ConnectionSemaphore} from cache without waiting for lock
     * 
     * @param unitName
     * @return {@link ConnectionSemaphore}
     */
    public static ConnectionSemaphore getSemaphore(String unitName) {

	return CONNECTIONS.get(unitName);
    }

    /**
     * Creates and locks {@link ConnectionSemaphore} instance
     * 
     * @param unitName
     * @return {@link ConnectionSemaphore}
     */
    private static ConnectionSemaphore createSemaphore(String unitName) {

	ConnectionSemaphore semaphore = CONNECTIONS.get(unitName);
	ConnectionSemaphore current = null;
	if (semaphore == null) {
	    semaphore = new ConnectionSemaphore();
	    semaphore.setUnitName(unitName);
	    semaphore.setInProgress(Boolean.TRUE);
	    semaphore.setCached(Boolean.TRUE);
	    current = CONNECTIONS.putIfAbsent(unitName, semaphore);
	}
	if (current == null) {
	    current = semaphore;
	}
	current.incrementUser();

	return current;
    }

    /**
     * Caches {@link ConnectionSemaphore} with lock
     * 
     * @param unitName
     * @param jndiName
     * @return {@link ConnectionSemaphore}
     */
    public static ConnectionSemaphore setSemaphore(String unitName,
	    String jndiName) {

	ConnectionSemaphore semaphore = null;

	if (ObjectUtils.available(unitName)) {

	    semaphore = createSemaphore(unitName);
	    if (ObjectUtils.available(jndiName)) {
		ConnectionSemaphore existent = CONNECTIONS.putIfAbsent(
			jndiName, semaphore);
		if (existent == null) {
		    semaphore.setJndiName(jndiName);
		}
	    }
	}

	return semaphore;
    }

    /**
     * Waits until {@link ConnectionSemaphore} is in progress (locked)
     * 
     * @param semaphore
     */
    private static void awaitConnection(ConnectionSemaphore semaphore) {

	synchronized (semaphore) {
	    boolean inProgress = semaphore.isInProgress()
		    && ObjectUtils.notTrue(semaphore.isBound());
	    while (inProgress) {
		try {
		    semaphore.wait();
		    inProgress = semaphore.isInProgress()
			    && ObjectUtils.notTrue(semaphore.isBound());
		} catch (InterruptedException ex) {
		    inProgress = Boolean.FALSE;
		    LOG.error(ex.getMessage(), ex);
		}
	    }
	}
    }

    /**
{'fraction_non_alphanumeric': 0.09955987251479739, 'fraction_numerical': 0.00030353619669145547, 'mean_word_length': 3.5479641131815045, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '12438', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2010, 'n_words': 424}