Subversion Repositories bacoAlunos

Rev

Rev 1312 | Rev 1782 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
BERSERK - a BusinEss Runtime and SEcurity Resources Kit
Copyright (C) 2003 Goncalo Luiz

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

To contact Goncalo Luiz use the following e-mail address (use an @ instead of the _):
gedl_mega.ist.utl.pt
*/

package pt.utl.ist.berserk.logic.serviceManager;
/**
 * @author Jorge Martins
 * @version revision by Goncalo Luiz (gedl \AT/ rnl \DOT/  ist \DOT/ utl \DOT/ pt) at June the 15th, 2004
 **/

import pt.utl.ist.berserk.BerserkConfiguration;
import pt.utl.ist.berserk.ServiceRequest;
import pt.utl.ist.berserk.ServiceResponse;
import pt.utl.ist.berserk.logic.filterManager.FilteringResult;
import pt.utl.ist.berserk.logic.filterManager.IFilterBroker;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ExecutedFilterException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ServiceManagerException;
import pt.utl.ist.berserk.storage.ITransactionBroker;
import pt.utl.ist.berserk.storage.exceptions.StorageException;

import java.util.ArrayList;

public class TransactionalServiceInvoker extends ServiceInvoker
{

    static boolean debug = false;

    //TODO HIPOTESE DE TER AQUI UMA VERIFICACAO DE TRANSACAO CONTIDA
    //PROBLEMA E QUE TEMOS DE USAR A THREAD SE POR ALGUMA RAZAO MUDAMOS DE TRANSACTION BROKER
    //TUDO ESTRAGADO
    //NAO DA ISTO É UM SINGLETON boolean alreadyActive = false;
    //TIVEMOS DE TER COMO VAR LOCAL UMA VEZ QUE O InVOCADOR E NUCLEAR

        public final static TransactionalServiceInvoker invocador = new TransactionalServiceInvoker();

    public final 5+0%2Fdocs%2Fapi+Object">Object invoke(
                5+0%2Fdocs%2Fapi+Object">Object requester,
                IService service,
                1.5.0/docs/api/java/lang/String.html">String methodName,
                5+0%2Fdocs%2Fapi+Object">Object arguments[],
                1.5.0/docs/api/java/lang/String.html">String names[],
                IFilterBroker broker)
                throws ServiceManagerException, 1.5.0/docs/api/java/lang/Throwable.html">Throwable
        {
                boolean passedPre = false;
                boolean passedPost = false;
                ServiceRequest request = new ServiceRequest(requester, arguments,names,service,methodName);
                ServiceResponse response = new ServiceResponse();
                FilteringResult filteringResult = null;
                ITransactionBroker tb = BerserkConfiguration.getApplicationTransactionBroker();
        boolean alreadyActive = tb.isTransactionActive();
                try
                {

            if(debug)
                1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId()  + "Starting transaction for service: " + service.getClass().getName() + " method:" + methodName);
            if(!alreadyActive)
            {
                            tb.beginTransaction();
            }
            else
            {
                1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId()  + "AVISO : ESTA A CHAMAR UM SERVICO TRANSACIONAL DENTRO DE UMA TRANSACAO, VAMOS MANTER A MESMA TRASACAO");
                1.5.0/docs/api/java/lang/System.html">System.out.println(">>>>>>TRANSACTION WARNING BUT NO PROBLEM: AVISO: Já existia uma transação activa e foi iniciada outra neste momento sem fechar a anterior ");
                for (1.5.0/docs/api/java/lang/StackTraceElement.html">StackTraceElement ste : 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getStackTrace()) {
                    1.5.0/docs/api/java/lang/System.html">System.out.println("\t" + ste);
                }

            }
                        try
                        {
                            filteringResult = broker.executePreFiltering(request, response);
                            passedPre = filteringResult.isSuccess();
                        }
                        catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
                        {
                                throw new ExecutedFilterException(
                                        "a not 'FilterException' exception occured while executing pre filter chain",
                                        e);
                        }
                        if (passedPre)
                        {
                                response.setReturnObject(doInvocation(request.getService(), methodName, request.getArguments()));
                        }
                        else
                        {
                                throw new FilterChainFailedException(
                                                "failed to execute postFiltering on service" + service.getClass(),filteringResult);
                        }
                        try
                        {
                            filteringResult = broker.executePostFiltering(request, response);
                            passedPost = filteringResult.isSuccess();
                        }
                        catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
                        {
                                throw new ExecutedFilterException(
                                        "a not 'FilterException' exception occured while executing pre filter chain",
                                        e);
                        }
                        if (passedPost)
                        {
                                //the sucessfull case
                if(!alreadyActive)
                                    tb.commitTransaction();
                                return response.getReturnObject();
                        }
                        else
                        {
                                throw new FilterChainFailedException(
                                "failed to execute postFiltering on service" + service.getClass(),filteringResult);
                        }
                }
                catch (FilterChainFailedException ex)
                {
                        //ok let this exception go up
                        try
                        {
                                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                                    tb.abortTransaction();
                        }
                        catch (StorageException e)
                        {
                                throw new ServiceManagerException("Storage Exception while aborting transaction",e);
                        }
                        throw ex;
                }
                catch (ExecutedFilterException ex)
                {
                        //ok let this exception go up
                        try
                        {
                                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                                    tb.abortTransaction();
                        }
                        catch (StorageException e)
                        {
                            throw new ServiceManagerException("Storage Exception while aborting transaction",e);
                        }
                        throw ex;
                }
                catch (StorageException ex)
                {
                        try
                        {
                                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                                    tb.abortTransaction();
                        }
                        catch (StorageException e)
                        {
                            throw new ServiceManagerException("Storage Exception while aborting transaction",e);
                        }
                        throw new ServiceManagerException("Storage exception while executing transactional service",ex);
                }
                catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable ex)
                {
                        //ok let this exception go up
                        try
                        {
                                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                    tb.abortTransaction();
                        }
                        catch (StorageException e)
                        {
                            throw new ServiceManagerException("Storage Exception while aborting transaction",e);
                        }
                        throw ex;
                }
               
        }





    public final 5+0%2Fdocs%2Fapi+Object">Object checkFilters(
            5+0%2Fdocs%2Fapi+Object">Object requester,
            IService service,
            1.5.0/docs/api/java/lang/String.html">String methodName,
            5+0%2Fdocs%2Fapi+Object">Object arguments[],
            1.5.0/docs/api/java/lang/String.html">String names[],
            IFilterBroker broker)
            throws ServiceManagerException, 1.5.0/docs/api/java/lang/Throwable.html">Throwable
    {
        boolean passedPre = false;

        ServiceRequest request = new ServiceRequest(requester, arguments,names,service,methodName);
        request.setExcludeChains(new ArrayList<String>());
        request.getExcludeChains().add("Logger");
        ServiceResponse response = new ServiceResponse();
        FilteringResult filteringResult = null;
        ITransactionBroker tb = BerserkConfiguration.getApplicationTransactionBroker();
        boolean alreadyActive = tb.isTransactionActive();
        try
        {
            if(debug)
                1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId()  + ": Starting transaction for service: " + service.getClass().getName() + " method:" + methodName);
            if(!alreadyActive)
            {
                tb.beginTransaction();
            }
            else
            {
                1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId()  +" AVISO : ESTA A CHAMAR UM SERVICO TRANSACIONAL DENTRO DE UMA TRANSACAO, VAMOS MANTER A MESMA TRASACAO");
                1.5.0/docs/api/java/lang/System.html">System.out.println(">>>>>>TRANSACTION WARNING BUT NO PROBLEM: AVISO: Já existia uma transação activa e foi iniciada outra neste momento sem fechar a anterior ");
                for (1.5.0/docs/api/java/lang/StackTraceElement.html">StackTraceElement ste : 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getStackTrace()) {
                    1.5.0/docs/api/java/lang/System.html">System.out.println("\t" + ste);
                }
            }

            try
            {
                filteringResult = broker.executePreFiltering(request, response);
                passedPre = filteringResult.isSuccess();
            }
            catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
            {
                throw new ExecutedFilterException(
                        "a not 'FilterException' exception occured while executing pre filter chain",
                        e);
            }
            if (passedPre)
            {
                if(!alreadyActive)
                    tb.commitTransaction();
                return true;
            }
            else
            {
                throw new FilterChainFailedException(
                        "failed to execute postFiltering on service" + service.getClass(),filteringResult);
            }
        }
        catch (FilterChainFailedException ex)
        {
            //ok let this exception go up
            try
            {
                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                    tb.abortTransaction();
            }
            catch (StorageException e)
            {
                throw new ServiceManagerException("Storage Exception while aborting transaction",e);
            }
            throw ex;
        }
        catch (ExecutedFilterException ex)
        {
            //ok let this exception go up
            try
            {
                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                    tb.abortTransaction();
            }
            catch (StorageException e)
            {
                throw new ServiceManagerException("Storage Exception while aborting transaction",e);
            }
            throw ex;
        }
        catch (StorageException ex)
        {
            try
            {
                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                    tb.abortTransaction();
            }
            catch (StorageException e)
            {
                throw new ServiceManagerException("Storage Exception while aborting transaction",e);
            }
            throw new ServiceManagerException("Storage exception while executing transactional service",ex);
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable ex)
        {
            //ok let this exception go up
            try
            {
                //if we get here, something went wrong with the executing service. we should roll back all changes
                if(!alreadyActive)
                    tb.abortTransaction();
            }
            catch (StorageException e)
            {
                throw new ServiceManagerException("Storage Exception while aborting transaction",e);
            }
            throw ex;
        }

    }
}