All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.semantic.persistence.impl.ElmoTransactionalAspect Maven / Gradle / Ivy

There is a newer version: 1.2.29
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tidalwave.semantic.persistence.impl;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.openrdf.elmo.sesame.SesameManager;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.aspect.Aspect;
import it.tidalwave.openrdf.elmo.ElmoManagerThreadLocal;
import org.openrdf.elmo.sesame.SesameManagerFactory;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class ElmoTransactionalAspect extends Aspect
  {
    private final static String CLASS = ElmoTransactionalAspect.class.getName();
    private final static Logger logger = Logger.getLogger(CLASS);

    private boolean demarcate = false;

    @Nonnull
    private final SesameManagerFactory smf;

    public ElmoTransactionalAspect (final @Nonnull SesameManagerFactory smf)
      {
        this.smf = smf;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void runBefore()
      {
        logger.finest(">>>> checking whether I need to demarcate a transaction");

        try
          {
            ElmoManagerThreadLocal.get();
          }
        catch (IllegalStateException e)
          {
            demarcate = true;
            logger.fine("******** begin transaction");
            final SesameManager em = smf.createElmoManager();
            ElmoManagerThreadLocal.set(em);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void runAfter()
      {
        if (demarcate)
          {
            logger.fine("******** commit transaction");
            // FIXME: commit etc
            ElmoManagerThreadLocal.set(null);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void handleException (final @Nonnull Throwable t)
      {
        if (demarcate)
          {
            logger.fine("******** rollback transaction");
            // FIXME: rollback etc
            ElmoManagerThreadLocal.set(null);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void runFinally()
      {
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean equals (final @CheckForNull Object object)
      {
        if (object == null)
          {
            return false;
          }

        return getClass() == object.getClass();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public int hashCode()
      {
        return CLASS.hashCode();
      }
 
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String toString()
      {
        return String.format("SesameTransactionalAspect@%x", System.identityHashCode(this));
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy