1 | /******************************************************************************* |
2 | * Copyright (c) 2009 IBM Corporation and others. |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | * which accompanies this distribution, and is available at |
6 | * http://www.eclipse.org/legal/epl-v10.html |
7 | * |
8 | * Contributors: |
9 | * IBM Corporation - initial API and implementation |
10 | *******************************************************************************/ |
11 | package org.eclipse.pde.api.tools.internal.util; |
12 | |
13 | import java.io.File; |
14 | import java.util.HashSet; |
15 | import java.util.Iterator; |
16 | |
17 | /** |
18 | * Manager to handle temp files that have been created. Used as a fall-back to |
19 | * ensure we clean up after ourselves |
20 | * |
21 | * @since 1.0.1 |
22 | */ |
23 | public final class FileManager { |
24 | |
25 | private static FileManager fInstance = null; |
26 | |
27 | /** |
28 | * The set of recorded file paths |
29 | */ |
30 | private static HashSet fFilePaths = null; |
31 | |
32 | /** |
33 | * Constructor |
34 | * private - no instantiation |
35 | */ |
36 | private FileManager() {} |
37 | |
38 | /** |
39 | * Returns the singleton instance of the manager |
40 | * @return the manager instance |
41 | */ |
42 | public synchronized static FileManager getManager() { |
43 | if(fInstance == null) { |
44 | fInstance = new FileManager(); |
45 | } |
46 | return fInstance; |
47 | } |
48 | |
49 | /** |
50 | * Records a file root path to be deleted on the next call to |
51 | * {@link #deleteFiles()}. |
52 | * @param absolutepath the absolute path in the local file system of the file to delete |
53 | */ |
54 | public void recordTempFileRoot(String absolutepath) { |
55 | if(absolutepath != null) { |
56 | if(fFilePaths == null) { |
57 | fFilePaths = new HashSet(10); |
58 | } |
59 | fFilePaths.add(absolutepath); |
60 | } |
61 | } |
62 | |
63 | /** |
64 | * Deletes all of the recorded file roots from the local filesystem (if still existing) |
65 | * and returns the success of the entire delete operation. |
66 | * @return true if all recorded files were deleted, false otherwise |
67 | */ |
68 | public boolean deleteFiles() { |
69 | boolean success = true; |
70 | if(fFilePaths != null) { |
71 | File file = null; |
72 | for(Iterator iter = fFilePaths.iterator(); iter.hasNext();) { |
73 | file = new File((String) iter.next()); |
74 | success &= Util.delete(file); |
75 | } |
76 | fFilePaths.clear(); |
77 | } |
78 | return success; |
79 | } |
80 | } |