This project has retired. For details please refer to its Attic page.
Source code
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;
022
023import javax.inject.Inject;
024import java.io.File;
025import java.io.IOException;
026import java.nio.file.Files;
027import java.nio.file.Path;
028import java.nio.file.attribute.FileAttribute;
029import java.util.logging.Level;
030import java.util.logging.Logger;
031
032/**
033 * Creates temp files in a directory named "temp" within  the current working directory.
034 */
035@Provided
036public final class WorkingDirectoryTempFileCreator implements TempFileCreator {
037  private static final Logger LOG = Logger.getLogger(WorkingDirectoryTempFileCreator.class.getName());
038  private final File tempFolderAsFile;
039  private final Path tempFolderAsPath;
040
041  @Inject
042  WorkingDirectoryTempFileCreator() throws IOException {
043    this.tempFolderAsFile = new File("./reef/temp");
044    this.tempFolderAsFile.mkdirs();
045    this.tempFolderAsPath = this.tempFolderAsFile.toPath();
046    LOG.log(Level.FINE, "Temporary files and folders will be created in [{0}]", this.tempFolderAsFile.getAbsolutePath());
047  }
048
049
050  @Override
051  public File createTempFile(final String prefix, final String suffix) throws IOException {
052    final File result = File.createTempFile(prefix, suffix, this.tempFolderAsFile);
053    if (LOG.isLoggable(Level.FINEST)) {
054      LOG.log(Level.FINEST, "Created temporary file: {0}", result.getAbsolutePath());
055    }
056    return result;
057  }
058
059  @Override
060  public File createTempDirectory(final String prefix, final FileAttribute<?> fileAttributes) throws IOException {
061    final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix, fileAttributes).toFile();
062    if (LOG.isLoggable(Level.FINEST)) {
063      LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath());
064    }
065    return result;
066  }
067
068  @Override
069  public File createTempDirectory(String prefix) throws IOException {
070    final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix).toFile();
071    if (LOG.isLoggable(Level.FINEST)) {
072      LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath());
073    }
074    return result;
075  }
076}