1// 2// ======================================================================== 3// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4// ------------------------------------------------------------------------ 5// All rights reserved. This program and the accompanying materials 6// are made available under the terms of the Eclipse Public License v1.0 7// and Apache License v2.0 which accompanies this distribution. 8// 9// The Eclipse Public License is available at 10// http://www.eclipse.org/legal/epl-v10.html 11// 12// The Apache License v2.0 is available at 13// http://www.opensource.org/licenses/apache2.0.php 14// 15// You may elect to redistribute this code under either of these licenses. 16// ======================================================================== 17// 18 19package org.eclipse.jetty.util.component; 20 21import java.io.File; 22import java.io.IOException; 23import java.util.ArrayList; 24import java.util.Collection; 25import java.util.List; 26 27import org.eclipse.jetty.util.IO; 28import org.eclipse.jetty.util.log.Log; 29import org.eclipse.jetty.util.log.Logger; 30import org.eclipse.jetty.util.resource.Resource; 31 32public class FileDestroyable implements Destroyable 33{ 34 private static final Logger LOG = Log.getLogger(FileDestroyable.class); 35 final List<File> _files = new ArrayList<File>(); 36 37 public FileDestroyable() 38 { 39 } 40 41 public FileDestroyable(String file) throws IOException 42 { 43 _files.add(Resource.newResource(file).getFile()); 44 } 45 46 public FileDestroyable(File file) 47 { 48 _files.add(file); 49 } 50 51 public void addFile(String file) throws IOException 52 { 53 _files.add(Resource.newResource(file).getFile()); 54 } 55 56 public void addFile(File file) 57 { 58 _files.add(file); 59 } 60 61 public void addFiles(Collection<File> files) 62 { 63 _files.addAll(files); 64 } 65 66 public void removeFile(String file) throws IOException 67 { 68 _files.remove(Resource.newResource(file).getFile()); 69 } 70 71 public void removeFile(File file) 72 { 73 _files.remove(file); 74 } 75 76 public void destroy() 77 { 78 for (File file : _files) 79 { 80 if (file.exists()) 81 { 82 LOG.debug("Destroy {}",file); 83 IO.delete(file); 84 } 85 } 86 } 87 88} 89