KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > FileCache


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.core.internal.filesystem;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import org.eclipse.core.filesystem.*;
16 import org.eclipse.core.filesystem.provider.FileStore;
17 import org.eclipse.core.internal.filesystem.local.LocalFile;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.osgi.util.NLS;
20
21 /**
22  * An instance of this class represents a directory on disk where cached
23  * files can be stored. Files in the cache expire on VM exit.
24  */

25 public class FileCache {
26     private static final String JavaDoc CACHE_DIR_NAME = "filecache";//$NON-NLS-1$
27

28     /**
29      * Thread safety for lazy instantiation of the cache
30      */

31     private static final Object JavaDoc creationLock = new Object JavaDoc();
32
33     /**
34      * The singleton file cache instance.
35      */

36     private static FileCache instance = null;
37
38     private File JavaDoc cacheDir;
39
40     /**
41      * Public accessor to obtain the singleton file cache instance,
42      * creating the cache lazily if necessary.
43      * @return The file cache instance
44      * @throws CoreException
45      */

46     public static FileCache getCache() throws CoreException {
47         synchronized (creationLock) {
48             if (instance == null)
49                 instance = new FileCache();
50             return instance;
51         }
52     }
53
54     /**
55      * Creates a new file cache.
56      *
57      * @throws CoreException If the file cache could not be created
58      */

59     private FileCache() throws CoreException {
60         IPath location = Activator.getCacheLocation();
61         File JavaDoc cacheParent = new File JavaDoc(location.toFile(), CACHE_DIR_NAME);
62         cleanOldCache(cacheParent);
63         cacheParent.mkdirs();
64         //make sure we have a unique non-existing cache directory
65
cacheDir = getUniqueDirectory(cacheParent, true);
66     }
67
68     /**
69      * Implements {@link FileStore#toLocalFile(int, IProgressMonitor)}
70      * @param source
71      * @param monitor
72      * @return The cached file
73      * @throws CoreException
74      */

75     public java.io.File JavaDoc cache(IFileStore source, IProgressMonitor monitor) throws CoreException {
76         try {
77             monitor.beginTask(NLS.bind(Messages.copying, toString()), 100);
78             IFileInfo myInfo = source.fetchInfo(EFS.NONE, Policy.subMonitorFor(monitor, 25));
79             if (!myInfo.exists())
80                 return new File JavaDoc(cacheDir, "Non-Existent-" + System.currentTimeMillis()); //$NON-NLS-1$
81
File JavaDoc result;
82             if (myInfo.isDirectory()) {
83                 result = getUniqueDirectory(cacheDir, false);
84             } else {
85                 result = File.createTempFile(source.getFileSystem().getScheme(), "efs", cacheDir); //$NON-NLS-1$
86
}
87             monitor.worked(25);
88             IFileStore resultStore = new LocalFile(result);
89             source.copy(resultStore, EFS.OVERWRITE, Policy.subMonitorFor(monitor, 25));
90             result.deleteOnExit();
91             return result;
92         } catch (IOException JavaDoc e) {
93             Policy.error(EFS.ERROR_WRITE, NLS.bind(Messages.couldNotWrite, toString()));
94             return null;//can't get here
95
} finally {
96             monitor.done();
97         }
98     }
99
100     /**
101      * Performs initial cleanup of any old cached state left over from previous
102      * sessions.
103      */

104     private void cleanOldCache(File JavaDoc cacheParent) throws CoreException {
105         //clear any old cache - this could be moved to a background thread
106
new LocalFile(cacheParent).delete(EFS.NONE, null);
107     }
108
109     /**
110      * Returns a new unique directory in the given parent directory.
111      *
112      * @param parent
113      * @param create <code>true</code> if the directory should
114      * be created, and false otherwise.
115      * @return The unique directory
116      */

117     private File JavaDoc getUniqueDirectory(File JavaDoc parent, boolean create) {
118         File JavaDoc dir;
119         long i = 0;
120         //find an unused directory name
121
do {
122             dir = new File JavaDoc(parent, Long.toString(System.currentTimeMillis() + i++));
123         } while (dir.exists());
124         if (create)
125             dir.mkdir();
126         return dir;
127     }
128 }
129
Popular Tags