001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.util;
019
020import java.io.File;
021import java.io.IOException;
022import java.security.PrivilegedAction;
023import java.security.PrivilegedExceptionAction;
024
025public class Files
026{
027        public static String TEMP_FILE_PREFIX = "ha-jdbc_";
028        
029        public static File createTempFile(final String suffix) throws IOException
030        {
031                PrivilegedExceptionAction<File> action = new PrivilegedExceptionAction<File>()
032                {
033                        @Override
034                        public File run() throws IOException
035                        {
036                                return File.createTempFile(TEMP_FILE_PREFIX, suffix);
037                        }
038                };
039                
040                return Security.run(action, IOException.class);
041        }
042        
043        public static void delete(final File file)
044        {
045                PrivilegedAction<Void> action = new PrivilegedAction<Void>()
046                {
047                        @Override
048                        public Void run()
049                        {
050                                if (!file.delete())
051                                {
052                                        file.deleteOnExit();
053                                }
054                                return null;
055                        }
056                };
057
058                Security.run(action);
059        }
060}