001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.io;
020
021import org.apache.reef.annotations.Provided;
022import org.apache.reef.io.parameters.TempFileRootFolder;
023import org.apache.reef.tang.annotations.Parameter;
024
025import javax.inject.Inject;
026import java.io.File;
027import java.io.IOException;
028import java.nio.file.Files;
029import java.nio.file.Path;
030import java.nio.file.attribute.FileAttribute;
031import java.util.logging.Level;
032import java.util.logging.Logger;
033
034/**
035 * Creates temp files in a directory named "temp" within  the configured directory.
036 */
037@Provided
038public final class ConfigurableDirectoryTempFileCreator implements TempFileCreator {
039  private static final Logger LOG = Logger.getLogger(ConfigurableDirectoryTempFileCreator.class.getName());
040  private final File tempFolderAsFile;
041  private final Path tempFolderAsPath;
042
043  @Inject
044  ConfigurableDirectoryTempFileCreator(
045      @Parameter(TempFileRootFolder.class) final String rootFolder) throws IOException {
046    this.tempFolderAsFile = new File(rootFolder);
047    if (!this.tempFolderAsFile.exists() && !this.tempFolderAsFile.mkdirs()) {
048      LOG.log(Level.WARNING, "Failed to create [{0}]", this.tempFolderAsFile.getAbsolutePath());
049    }
050    this.tempFolderAsPath = this.tempFolderAsFile.toPath();
051    LOG.log(Level.FINE, "Temporary files and folders will be created in [{0}]",
052        this.tempFolderAsFile.getAbsolutePath());
053  }
054
055
056  @Override
057  public File createTempFile(final String prefix, final String suffix) throws IOException {
058    final File result = File.createTempFile(prefix, suffix, this.tempFolderAsFile);
059    if (LOG.isLoggable(Level.FINEST)) {
060      LOG.log(Level.FINEST, "Created temporary file: {0}", result.getAbsolutePath());
061    }
062    return result;
063  }
064
065  @Override
066  public File createTempDirectory(final String prefix, final FileAttribute<?> fileAttributes) throws IOException {
067    final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix, fileAttributes).toFile();
068    if (LOG.isLoggable(Level.FINEST)) {
069      LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath());
070    }
071    return result;
072  }
073
074  @Override
075  public File createTempDirectory(final String prefix) throws IOException {
076    final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix).toFile();
077    if (LOG.isLoggable(Level.FINEST)) {
078      LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath());
079    }
080    return result;
081  }
082}