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 if (!this.tempFolderAsFile.exists() && !this.tempFolderAsFile.mkdirs()) { 045 LOG.log(Level.WARNING, "Failed to create [{0}]", this.tempFolderAsFile.getAbsolutePath()); 046 } 047 this.tempFolderAsPath = this.tempFolderAsFile.toPath(); 048 LOG.log(Level.FINE, "Temporary files and folders will be created in [{0}]", 049 this.tempFolderAsFile.getAbsolutePath()); 050 } 051 052 053 @Override 054 public File createTempFile(final String prefix, final String suffix) throws IOException { 055 final File result = File.createTempFile(prefix, suffix, this.tempFolderAsFile); 056 if (LOG.isLoggable(Level.FINEST)) { 057 LOG.log(Level.FINEST, "Created temporary file: {0}", result.getAbsolutePath()); 058 } 059 return result; 060 } 061 062 @Override 063 public File createTempDirectory(final String prefix, final FileAttribute<?> fileAttributes) throws IOException { 064 final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix, fileAttributes).toFile(); 065 if (LOG.isLoggable(Level.FINEST)) { 066 LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath()); 067 } 068 return result; 069 } 070 071 @Override 072 public File createTempDirectory(final String prefix) throws IOException { 073 final File result = Files.createTempDirectory(this.tempFolderAsPath, prefix).toFile(); 074 if (LOG.isLoggable(Level.FINEST)) { 075 LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath()); 076 } 077 return result; 078 } 079}