View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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  
19  package org.eclipse.jetty.util.component;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  
27  import org.eclipse.jetty.util.IO;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  import org.eclipse.jetty.util.resource.Resource;
31  
32  public 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          try(Resource r = Resource.newResource(file);)
54          {
55              _files.add(r.getFile());
56          }
57      }
58      
59      public void addFile(File file)
60      {
61          _files.add(file);
62      }
63      
64      public void addFiles(Collection<File> files)
65      {
66          _files.addAll(files);
67      }
68      
69      public void removeFile(String file) throws IOException
70      {
71          try(Resource r = Resource.newResource(file);)
72          {
73              _files.remove(r.getFile());
74          }
75      }
76      
77      public void removeFile(File file)
78      {
79          _files.remove(file);
80      }
81      
82      @Override
83      public void destroy()
84      {
85          for (File file : _files)
86          {
87              if (file.exists())
88              {
89                  if (LOG.isDebugEnabled())
90                      LOG.debug("Destroy {}",file);
91                  IO.delete(file);
92              }
93          }
94      }
95  
96  }