Package org.openrdf.sail.helpers

Source Code of org.openrdf.sail.helpers.SailBase

/*
* Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2008.
*
* Licensed under the Aduna BSD-style license.
*/
package org.openrdf.sail.helpers;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.openrdf.sail.Sail;
import org.openrdf.sail.SailConnection;
import org.openrdf.sail.SailMetaData;
import org.openrdf.store.StoreException;

/**
* SailBase is an abstract Sail implementation that takes care of common sail
* tasks, including proper closing of active connections and a grace period for
* active connections during shutdown of the store.
*
* @author Herko ter Horst
* @author jeen
* @author Arjohn Kampman
* @author James Leigh
*/
public abstract class SailBase implements Sail {

  /*-----------*
   * Constants *
   *-----------*/

  protected final Logger logger = LoggerFactory.getLogger(this.getClass());

  /*-----------*
   * Variables *
   *-----------*/

  /**
   * Directory to store information related to this sail in.
   */
  private File dataDir;

  /**
   * Flag indicating whether the Sail is shutting down.
   */
  private boolean shutDownInProgress = false;

  private final SailConnectionTracker tracker;

  /*--------------*
   * Constructors *
   *--------------*/

  public SailBase() {
    tracker = createSailConnectionTracker();
  }

  /*---------*
   * Methods *
   *---------*/

  protected SailConnectionTracker createSailConnectionTracker() {
    return new SailConnectionTracker();
  }

  public SailMetaData getMetaData()
    throws StoreException
  {
    return new SailMetaDataImpl();
  }

  public void setDataDir(File dataDir) {
    this.dataDir = dataDir;
  }

  public File getDataDir() {
    return dataDir;
  }

  public SailConnection getConnection()
    throws StoreException
  {
    if (shutDownInProgress) {
      throw new IllegalStateException("shut down in progress");
    }

    SailConnection con = getConnectionInternal();
    con = tracker.track(con);
    con = wrapConnection(con);
    return con;
  }

  /**
   * returns a store-specific SailConnection object.
   *
   * @return a SailConnection
   * @throws StoreException
   */
  protected abstract SailConnection getConnectionInternal()
    throws StoreException;

  /**
   * Called by {@link #getConnection()} to allow sails to wrap returned
   * connections with a custom connection. By default, this method wraps
   * connections with a {@link PreconditionSailConnection}.
   */
  protected SailConnection wrapConnection(SailConnection con)
    throws StoreException
  {
    return new PreconditionSailConnection(con);
  }

  public void shutDown()
    throws StoreException
  {
    // indicate no more new connections should be given out.
    shutDownInProgress = true;

    try {
      tracker.closeAll();

      shutDownInternal();
    }
    finally {
      shutDownInProgress = false;
    }
  }

  /**
   * Do store-specific operations to ensure proper shutdown of the store.
   *
   * @throws StoreException
   */
  protected abstract void shutDownInternal()
    throws StoreException;

  public String toString() {
    if (getDataDir() == null) {
      return super.toString();
    }
    return getDataDir().toString();
  }
}
TOP

Related Classes of org.openrdf.sail.helpers.SailBase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.