<commit_before>package org.lightmare.ejb.handlers;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

import org.lightmare.cache.ConnectionData;
import org.lightmare.cache.InjectionData;
import org.lightmare.cache.InterceptorData;
import org.lightmare.cache.MetaContainer;
import org.lightmare.cache.MetaData;
import org.lightmare.ejb.EjbConnector;
import org.lightmare.ejb.interceptors.InvocationContextImpl;
import org.lightmare.jpa.jta.BeanTransactions;
import org.lightmare.utils.CollectionUtils;
import org.lightmare.utils.ObjectUtils;
import org.lightmare.utils.reflect.MetaUtils;

/**
 * Handler class to intercept bean method calls to provide database transactions
 * 
 * @author Levan
 * 
 */
public class BeanHandler implements InvocationHandler, Cloneable {

    // EJB bean instance
    private Object bean;

    // EJB bean class
    private final Class<?> beanClass;

    // Bean's field with Resource annotation
    private final Field transactionField;

    // Connections for given EJB bean instance
    private final Collection<ConnectionData> connectionDatas;

    // Injections from given EJB bean instance
    private final Collection<InjectionData> injectionDatas;

    // Interceptors for given bean instance
    private final Collection<InterceptorData> interceptorDatas;

    // EJB meta data for given bean instance
    private final MetaData metaData;

    protected BeanHandler(final MetaData metaData) {

	this.beanClass = metaData.getBeanClass();
	this.transactionField = metaData.getTransactionField();
	this.connectionDatas = metaData.getConnections();
	this.injectionDatas = metaData.getInjects();
	this.interceptorDatas = metaData.getInterceptors();
	this.metaData = metaData;
    }

    public MetaData getMetaData() {
	return metaData;
    }

    public Object getBean() {
	return bean;
    }

    /**
     * Sets bean instance to handler, should be called only after clonning
     * 
     * @param bean
     */
    protected void setBean(final Object bean) {
	this.bean = bean;
    }

    /**
     * Sets passed value to beans {@link Field}
     * 
     * @param field
     * @param value
     * @throws IOException
     */
    private void setFieldValue(Field field, Object value) throws IOException {

	MetaUtils.setFieldValue(field, bean, value);
    }

    /**
     * Invokes passed bean {@link Method}
     * 
     * @param method
     * @param arguments
     * @return {@link Object}
     * @throws IOException<commit_msg>improved code / comments at utility classes<commit_after>package org.lightmare.ejb.handlers;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

import org.lightmare.cache.ConnectionData;
import org.lightmare.cache.InjectionData;
import org.lightmare.cache.InterceptorData;
import org.lightmare.cache.MetaContainer;
import org.lightmare.cache.MetaData;
import org.lightmare.ejb.EjbConnector;
import org.lightmare.ejb.interceptors.InvocationContextImpl;
import org.lightmare.jpa.jta.BeanTransactions;
import org.lightmare.utils.CollectionUtils;
import org.lightmare.utils.ObjectUtils;
import org.lightmare.utils.reflect.MetaUtils;

/**
 * Handler class to intercept bean method calls to provide database transactions
 * 
 * @author Levan
 * 
 */
public class BeanHandler implements InvocationHandler, Cloneable {

    // EJB bean instance
    private Object bean;

    // EJB bean class
    private final Class<?> beanClass;

    // Bean's field with Resource annotation
    private final Field transactionField;

    // Connections for given EJB bean instance
    private final Collection<ConnectionData> connectionDatas;

    // Injections from given EJB bean instance
    private final Collection<InjectionData> injectionDatas;

    // Interceptors for given bean instance
    private final Collection<InterceptorData> interceptorDatas;

    // EJB meta data for given bean instance
    private final MetaData metaData;

    protected BeanHandler(final MetaData metaData) {

	this.beanClass = metaData.getBeanClass();
	this.transactionField = metaData.getTransactionField();
	this.connectionDatas = metaData.getConnections();
	this.injectionDatas = metaData.getInjects();
	this.interceptorDatas = metaData.getInterceptors();
	this.metaData = metaData;
    }

    public MetaData getMetaData() {
	return metaData;
    }

    public Object getBean() {
	return bean;
    }

    /**
     * Sets bean instance to handler, should be called only after cloning
     * 
     * @param bean
     */
    protected void setBean(final Object bean) {
	this.bean = bean;
    }

    /**
     * Sets passed value to beans {@link Field}
     * 
     * @param field
     * @param value
     * @throws IOException
     */
    private void setFieldValue(Field field, Object value) throws IOException {

	MetaUtils.setFieldValue(field, bean, value);
    }

    /**
     * Invokes passed bean {@link Method}
     * 
     * @param method
     * @param arguments
     * @return {@link Object}
     * @throws IOException
{'fraction_non_alphanumeric': 0.0862910008410429, 'fraction_numerical': 0.0, 'mean_word_length': 3.905940594059406, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1084676', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1624, 'n_words': 442}