1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.operations;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.vfs2.FileObject;
23 import org.apache.commons.vfs2.FileSystemException;
24 import org.apache.commons.vfs2.FileSystemManager;
25
26
27
28
29
30 public class DefaultFileOperations implements FileOperations {
31
32
33 private final FileSystemManager fsmanager;
34
35
36
37 private final FileObject fileObject;
38
39
40
41
42
43 public DefaultFileOperations(final FileObject file) {
44 fileObject = file;
45
46 fsmanager = file.getFileSystem().getFileSystemManager();
47 }
48
49
50
51
52
53 @Override
54 public Class<? extends FileOperation>[] getOperations() throws FileSystemException {
55
56 final String scheme = fileObject.getURL().getProtocol();
57 final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
58
59 if (providers == null) {
60 return null;
61 }
62
63 final List<Class<? extends FileOperation>> operations = new ArrayList<>();
64
65 for (final FileOperationProvider provider : providers) {
66 provider.collectOperations(operations, fileObject);
67 }
68
69 @SuppressWarnings("unchecked")
70 final Class<? extends FileOperation>[] array = (Class<? extends FileOperation>[]) operations
71 .toArray(new Class<?>[] {});
72 return array;
73 }
74
75
76
77
78
79
80 @Override
81 public FileOperation getOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
82
83 final String scheme = fileObject.getURL().getProtocol();
84 final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
85
86 FileSystemException.requireNonNull(providers, "vfs.operation/operation-not-supported.error", operationClass);
87
88 FileOperation resultOperation = null;
89
90 for (final FileOperationProvider provider : providers) {
91 resultOperation = provider.getOperation(fileObject, operationClass);
92
93 if (resultOperation != null) {
94 break;
95 }
96 }
97
98 return FileSystemException.requireNonNull(resultOperation, "vfs.operation/operation-not-supported.error", operationClass);
99 }
100
101
102
103
104
105
106
107 @Override
108 public boolean hasOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
109 final Class<? extends FileOperation>[] operations = getOperations();
110 if (operations == null) {
111 return false;
112 }
113
114 for (final Class<? extends FileOperation> operation : operations) {
115 if (operationClass.isAssignableFrom(operation)) {
116 return true;
117 }
118 }
119 return false;
120 }
121 }