Ejemplo n.º 1
0
 public CassandraBinaryStore(String address) {
   this.address = address;
   this.cache = TransientBinaryStore.get();
 }
Ejemplo n.º 2
0
/**
 * A {@link BinaryStore} implementation that does not persist the binary values beyond the lifetime
 * of the virtual machine. This implementation extends {@link FileSystemBinaryStore} and uses a
 * temporary directory. Thus, the binary values are not stored in-memory.
 */
@ThreadSafe
public final class TransientBinaryStore extends FileSystemBinaryStore {

  private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";

  private static final String JBOSS_SERVER_DATA_DIR = "jboss.server.data.dir";

  private static final TransientBinaryStore INSTANCE = new TransientBinaryStore();

  protected static final File TRANSIENT_STORE_DIRECTORY = INSTANCE.getDirectory();

  /**
   * Obtain a shared {@link TransientBinaryStore} instance.
   *
   * @return the instance; never null
   */
  public static TransientBinaryStore get() {
    return INSTANCE;
  }

  /**
   * Obtain a new temporary directory that can be used by a transient binary store. Note that none
   * of the directories are actually created at this time, but are instead created (if needed)
   * during {@link #initializeStorage(File)}.
   *
   * @return the new directory; never null
   */
  private static File newTempDirectory() {
    String tempDirName = System.getProperty(JAVA_IO_TMPDIR);
    if (tempDirName == null) {
      throw new SystemFailureException(
          JcrI18n.tempDirectorySystemPropertyMustBeSet.text(JAVA_IO_TMPDIR));
    }
    File tempDir = new File(tempDirName);
    if (System.getProperty(JBOSS_SERVER_DATA_DIR) == null) {
      // We're not running in JBoss AS (where we always specify the directory where the binaries are
      // stored),
      // so log where the temporary directory is ...
      Logger.getLogger(TransientBinaryStore.class)
          .info(JcrI18n.tempDirectoryLocation, tempDir.getAbsolutePath());
    }

    // Create a temporary directory in the "java.io.tmpdir" directory ...
    return new File(tempDir, "modeshape-binary-store");
  }

  /** Create a new transient binary store. */
  private TransientBinaryStore() {
    super(newTempDirectory());
  }

  /**
   * Ensures that the directory used by this binary store exists and can be both read and written
   * to.
   *
   * @throws BinaryStoreException if the directory cannot be written to, read, or (if needed)
   *     created
   */
  @Override
  protected void initializeStorage(File directory) throws BinaryStoreException {
    if (!directory.exists()) {
      Logger.getLogger(getClass())
          .debug(
              "Creating temporary directory for transient binary store: {0}",
              directory.getAbsolutePath());
      directory.mkdirs();
    }
    if (!directory.canRead()) {
      throw new BinaryStoreException(
          JcrI18n.unableToReadTemporaryDirectory.text(directory.getAbsolutePath(), JAVA_IO_TMPDIR));
    }
    if (!directory.canWrite()) {
      throw new BinaryStoreException(
          JcrI18n.unableToWriteTemporaryDirectory.text(
              directory.getAbsolutePath(), JAVA_IO_TMPDIR));
    }
  }
}