1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.cache;
18
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22
23 import org.apache.commons.vfs2.FileName;
24 import org.apache.commons.vfs2.FileObject;
25 import org.apache.commons.vfs2.FileSystem;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class DefaultFilesCache extends AbstractFilesCache {
46
47
48 private final ConcurrentMap<FileSystem, ConcurrentMap<FileName, FileObject>> filesystemCache = new ConcurrentHashMap<>(
49 10);
50
51 @Override
52 public void putFile(final FileObject file) {
53 final Map<FileName, FileObject> files = getOrCreateFilesystemCache(file.getFileSystem());
54 files.put(file.getName(), file);
55 }
56
57 @Override
58 public boolean putFileIfAbsent(final FileObject file) {
59 final ConcurrentMap<FileName, FileObject> files = getOrCreateFilesystemCache(file.getFileSystem());
60 return files.putIfAbsent(file.getName(), file) == null;
61 }
62
63 @Override
64 public FileObject getFile(final FileSystem filesystem, final FileName name) {
65
66 final Map<FileName, FileObject> files = filesystemCache.get(filesystem);
67 if (files == null) {
68
69 return null;
70 }
71
72 return files.get(name);
73 }
74
75 @Override
76 public void clear(final FileSystem filesystem) {
77
78 final Map<FileName, FileObject> files = filesystemCache.remove(filesystem);
79 if (files != null) {
80 files.clear();
81 }
82 }
83
84 protected ConcurrentMap<FileName, FileObject> getOrCreateFilesystemCache(final FileSystem filesystem) {
85 ConcurrentMap<FileName, FileObject> files = filesystemCache.get(filesystem);
86
87 while (files == null) {
88 filesystemCache.putIfAbsent(filesystem, new ConcurrentHashMap<>(200, 0.75f, 8));
89 files = filesystemCache.get(filesystem);
90 }
91
92 return files;
93 }
94
95 @Override
96 public void close() {
97 super.close();
98 filesystemCache.clear();
99 }
100
101 @Override
102 public void removeFile(final FileSystem filesystem, final FileName name) {
103
104 final Map<FileName, FileObject> files = filesystemCache.get(filesystem);
105 if (files != null) {
106 files.remove(name);
107
108
109 }
110 }
111 }