ips1ap101.ejb.core.sqlagent.SqlAgentBrokerBean.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.ejb.core.sqlagent.SqlAgentBrokerBean.java

Source

/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package ips1ap101.ejb.core.sqlagent;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jms.ObjectMessage;
import ips1ap101.ejb.core.jms.UtilityProcessMessengerLocal;
import ips1ap101.lib.base.app.constant.EAB;
import ips1ap101.lib.base.constant.CBM;
import ips1ap101.lib.base.entity.enumeration.CondicionEjeFunEnumeration;
import ips1ap101.lib.base.util.ThrowableUtils;
import ips1ap101.lib.core.app.Bitacora;
import ips1ap101.lib.core.app.ExcepcionAplicacion;
import ips1ap101.lib.core.app.TLC;
import ips1ap101.lib.core.control.Auditor;
import ips1ap101.lib.core.db.util.SqlAgent;
import ips1ap101.lib.core.jms.message.SqlAgentMessage;
import ips1ap101.lib.core.util.STP;
import ips1ap101.lib.core.util.Utils;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
@Stateless
public class SqlAgentBrokerBean implements SqlAgentBrokerLocal {

    @Resource(name = EAB.DATABASE_RESOURCE_NAME, mappedName = EAB.DATABASE_RESOURCE_MAPPED_NAME)
    private javax.sql.DataSource ds;

    @EJB
    private UtilityProcessMessengerLocal messenger;

    @Override
    public boolean connected() throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().connected();
    }

    @Override
    public boolean isStoredProcedure(String sql) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().isStoredProcedure(sql);
    }

    @Override
    public Object executeProcedure(String sql) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeProcedure(sql);
    }

    @Override
    public Object executeProcedure(String sql, Object[] args) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeProcedure(sql, args);
    }

    @Override
    public ResultSet executeQuery(String sql) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeQuery(sql);
    }

    @Override
    public ResultSet executeQuery(String sql, int limite) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeQuery(sql, limite);
    }

    @Override
    public ResultSet executeQuery(String sql, Object[] args) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeQuery(sql, args);
    }

    @Override
    public ResultSet executeQuery(String sql, int limite, Object[] args) throws SQLException {
        TLC.getConnection(ds);
        return TLC.getAgenteSql().executeQuery(sql, limite, args);
    }

    @Override
    public ObjectMessage executeProcedure(String procedimiento, long funcion) {
        return executeProcedure(procedimiento, funcion, null);
    }

    @Override
    public ObjectMessage executeProcedure(String procedimiento, long funcion, Object[] args) {
        Bitacora.trace(this.getClass(), "executeProcedure", procedimiento, String.valueOf(funcion));
        Utils.traceObjectArray(args);
        Long rastro = null;
        ObjectMessage reply = null;
        try {
            procedimiento = StringUtils.trimToEmpty(procedimiento);
            if (STP.esIdentificadorArchivoValido(procedimiento)) {
                if (TLC.getControlador().esFuncionAutorizada(funcion)) {
                    TLC.getConnection(ds);
                    rastro = TLC.getControlador().ponerProcesoPendiente(funcion);
                    SqlAgentMessage message = new SqlAgentMessage(procedimiento, funcion);
                    message.setRastro(rastro);
                    message.setMensaje(Bitacora.getTextoMensaje(CBM.PROCESS_EXECUTION_REQUEST, procedimiento));
                    message.setArgumentos(args);
                    reply = messenger.send(message);
                    TLC.getBitacora().info(message.getMensaje());
                } else {
                    throw new ExcepcionAplicacion(
                            Bitacora.getTextoMensaje(CBM.FUNCION_NO_AUTORIZADA, procedimiento));
                }
            } else {
                throw new ExcepcionAplicacion(
                        Bitacora.getTextoMensaje(CBM.IDENTIFICADOR_ARCHIVO_INVALIDO, procedimiento));
            }
        } catch (Exception ex) {
            CondicionEjeFunEnumeration condicion = CondicionEjeFunEnumeration.EJECUCION_CANCELADA;
            String mensaje = ThrowableUtils.getString(ex);
            Auditor.grabarRastroProceso(rastro, condicion, null, mensaje);
            TLC.getBitacora().error(mensaje);
        }
        return reply;
    }

    @Override
    public SqlAgentMessage processSqlAgentMessage(SqlAgentMessage message) {
        return SqlAgent.executeProcedure(message);
    }

}