Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.fs;
 
 
 import java.io.File;
 import java.util.Date;
 import java.util.List;

Shutdown hook to delete temporary FileNodes (in particular: directories, because deleteAtExist is restricted to files) and execute arbirary other tasks. The implementation is intentionally tied to FileNode, it doesn't support Nodes in general because: 1) I create temp file on disk only - I can't see a use case for other node implementations. 2) node.delete() is might fail because server connections might already be closed 3) only java.world.File can create a temp file atomically
 
 public class OnShutdown extends Thread {
     private static OnShutdown singleton;

    
a static singleton, because I don't want a shutdown hook for every world instance
 
     public static synchronized OnShutdown get() {
         if ( == null) {
              = new OnShutdown();
             Runtime.getRuntime().addShutdownHook();
         }
         return ;
     }

    
null if the exit task has already been started
 
     private List<FileNodedelete;
 
     private final String prefix;
 
     private final String suffix;
     
     // to generate directory names
     private int dirNo;
 
     private final List<RunnableonShutdown;
     
     public OnShutdown() {
         this. = new ArrayList<FileNode>();
         this. = "sushi" + new SimpleDateFormat("MMdd-HHmm").format(new Date()) + "-"
             + (System.currentTimeMillis() % 100)  + "-";
         this. = ".tmp";
         this. = 0;
         this. = new ArrayList<Runnable>();
     }
 
     public synchronized void onShutdown(Runnable runnable) {
         .add(runnable);
     }
     
     //--
 
     public FileNode createFile(FileNode dirthrows IOException {
         FileNode file;
         
         file = new FileNode(dir.getRoot(), File.createTempFile(dir.getFile()));
         deleteAtExit(file);
         return file;
     }
 
     public FileNode createDirectory(FileNode dirthrows IOException {
         FileNode file;
     
         dir.checkDirectory();
         for (; true++) {
             file = dir.join( +  + );
             if (!file.exists()) {
                 file.mkdir(); // do not catch IOExceptios here -- it might be "disk full" ...
                 deleteAtExit(file);
                 return file;
             }
         }
     }
 
     //--
    
    @Override
    public synchronized void run() {
        List<FileNodetmp;
        
        tmp = ;
         = null;
        for (FileNode node : tmp) {
            tryDelete(node);
        }
        for (Runnable runnable : ) {
            runnable.run();
        }
    }

    
Deletes all temp files created until now, without waiting for shutdown - usefull for long-running processes. Caution: the caller has to be sure that none of the files is still in use. It's usually better to explicitly delete temporary files in your application if you're know they are no longer used.
    public synchronized void deleteNow() {
        for (FileNode node : ) {
            tryDelete(node);
        }
        .clear();
    }
    //--
    
    

Parameters:
node file or directory
    private synchronized void deleteAtExit(FileNode node) {
        if ( == null) {
            // already exiting
            tryDelete(node);
        } else {
            .add(node);
        }
    }
    private boolean tryDelete(FileNode node) {
        try {
            if (node.exists()) {
                node.delete();
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
New to GrepCode? Check out our FAQ X