1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider;
18
19 import java.util.Map;
20 import java.util.TreeMap;
21 import java.util.stream.Stream;
22
23 import org.apache.commons.vfs2.FileName;
24 import org.apache.commons.vfs2.FileObject;
25 import org.apache.commons.vfs2.FileSystem;
26 import org.apache.commons.vfs2.FileSystemConfigBuilder;
27 import org.apache.commons.vfs2.FileSystemException;
28 import org.apache.commons.vfs2.FileSystemOptions;
29 import org.apache.commons.vfs2.provider.local.GenericFileNameParser;
30
31
32
33
34 public abstract class AbstractFileProvider extends AbstractVfsContainer implements FileProvider {
35
36 private static final AbstractFileSystemtractFileSystem">AbstractFileSystem[] EMPTY_ABSTRACT_FILE_SYSTEMS = new AbstractFileSystem[0];
37
38
39
40
41
42
43
44 private final Map<FileSystemKey, FileSystem> fileSystemMap = new TreeMap<>();
45
46 private FileNameParser parser;
47
48 public AbstractFileProvider() {
49 parser = GenericFileNameParser.getInstance();
50 }
51
52 protected FileNameParser getFileNameParser() {
53 return parser;
54 }
55
56 protected void setFileNameParser(final FileNameParser parser) {
57 this.parser = parser;
58 }
59
60
61
62
63 @Override
64 public void close() {
65 synchronized (fileSystemMap) {
66 fileSystemMap.clear();
67 }
68 super.close();
69 }
70
71
72
73
74
75
76
77
78
79
80 @Override
81 public FileObject createFileSystem(FileObject="jxr_keyword">final String scheme, final FileObject file,
82 final FileSystemOptions fileSystemOptions) throws FileSystemException {
83
84 throw new FileSystemException("vfs.provider/not-layered-fs.error", scheme);
85 }
86
87
88
89
90
91
92
93
94
95
96
97 protected void addFileSystem(final Comparable<?> key, final FileSystem fs) throws FileSystemException {
98
99 addComponent(fs);
100
101 final FileSystemKeyFileSystemKey.html#FileSystemKey">FileSystemKey treeKey = new FileSystemKey(key, fs.getFileSystemOptions());
102 ((AbstractFileSystem) fs).setCacheKey(treeKey);
103
104 synchronized (fileSystemMap) {
105 fileSystemMap.put(treeKey, fs);
106 }
107 }
108
109
110
111
112
113
114
115
116 protected FileSystem findFileSystem(final Comparable<?> key, final FileSystemOptions fileSystemOptions) {
117 synchronized (fileSystemMap) {
118 return fileSystemMap.get(new FileSystemKey(key, fileSystemOptions));
119 }
120 }
121
122
123
124
125
126
127 @Override
128 public FileSystemConfigBuilder getConfigBuilder() {
129 return null;
130 }
131
132
133
134
135 public void freeUnusedResources() {
136 final AbstractFileSystem[] abstractFileSystems;
137 synchronized (fileSystemMap) {
138
139 abstractFileSystems = fileSystemMap.values().toArray(EMPTY_ABSTRACT_FILE_SYSTEMS);
140 }
141
142
143 Stream.of(abstractFileSystems).filter(AbstractFileSystem::isReleaseable)
144 .forEach(AbstractFileSystem::closeCommunicationLink);
145 }
146
147
148
149
150
151
152 public void closeFileSystem(final FileSystem fileSystem) {
153 final AbstractFileSystem/../../org/apache/commons/vfs2/provider/AbstractFileSystem.html#AbstractFileSystem">AbstractFileSystem fs = (AbstractFileSystem) fileSystem;
154
155 final FileSystemKey key = fs.getCacheKey();
156 if (key != null) {
157 synchronized (fileSystemMap) {
158 fileSystemMap.remove(key);
159 }
160 }
161
162 removeComponent(fs);
163 fs.close();
164 }
165
166
167
168
169
170
171
172
173
174 @Override
175 public FileNameame.html#FileName">FileName parseUri(final FileName base, final String uri) throws FileSystemException {
176 if (getFileNameParser() != null) {
177 return getFileNameParser().parseUri(getContext(), base, uri);
178 }
179
180 throw new FileSystemException("vfs.provider/filename-parser-missing.error");
181 }
182 }