1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.operations;
18
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileSystemManager;
21 import org.apache.commons.vfs.FileObject;
22
23 import java.util.List;
24 import java.util.ArrayList;
25
26 /***
27 * todo: add class description here
28 *
29 * @author Siarhei Baidun
30 * @since 0.1
31 */
32 public class DefaultFileOperations implements FileOperations
33 {
34 /***
35 *
36 */
37 private FileSystemManager fsmanager;
38
39 /***
40 *
41 */
42 private FileObject fileObject;
43
44 /***
45 *
46 * @param file
47 */
48 public DefaultFileOperations(final FileObject file)
49 {
50 fileObject = file;
51
52 fsmanager = file.getFileSystem().getFileSystemManager();
53 }
54
55 /***
56 * @return
57 * @throws org.apache.commons.vfs.FileSystemException
58 *
59 */
60 public Class[] getOperations() throws FileSystemException
61 {
62
63 final String scheme = fileObject.getURL().getProtocol();
64 final FileOperationProvider[] providers = fsmanager
65 .getOperationProviders(scheme);
66
67 if (providers == null)
68 {
69 return null;
70 }
71
72 final List operations = new ArrayList();
73
74 for (int i = 0; i < providers.length; i++)
75 {
76 FileOperationProvider provider = providers[i];
77
78 provider.collectOperations(operations, fileObject);
79 }
80
81 return (Class[]) operations.toArray(new Class[] {});
82 }
83
84 /***
85 * @param operationClass
86 * @return
87 * @throws org.apache.commons.vfs.FileSystemException
88 *
89 */
90 public FileOperation getOperation(Class operationClass)
91 throws FileSystemException
92 {
93
94 final String scheme = fileObject.getURL().getProtocol();
95 final FileOperationProvider[] providers = fsmanager
96 .getOperationProviders(scheme);
97
98 if (providers == null)
99 {
100 throw new FileSystemException(
101 "vfs.provider/operation-not-supported.error", operationClass);
102 }
103
104 FileOperation resultOperation = null;
105
106 for (int i = 0; i < providers.length; i++)
107 {
108 FileOperationProvider provider = providers[i];
109
110 resultOperation = provider.getOperation(fileObject, operationClass);
111
112 if (resultOperation != null)
113 {
114 break;
115 }
116 }
117
118 if (resultOperation == null)
119 {
120 throw new FileSystemException(
121 "vfs.provider/operation-not-supported.error", operationClass);
122 }
123
124 return resultOperation;
125 }
126
127 /***
128 * @param operationClass
129 * the operation's class.
130 *
131 * @return true if the operation of specified class is supported for current
132 * FileObject and false otherwise.
133 *
134 * @throws org.apache.commons.vfs.FileSystemException
135 *
136 */
137 public boolean hasOperation(Class operationClass) throws FileSystemException
138 {
139 Class[] operations = getOperations();
140 if (operations == null)
141 {
142 return false;
143 }
144
145 for (int i = 0; i < operations.length; i++)
146 {
147 Class operation = operations[i];
148 if (operationClass.isAssignableFrom(operation))
149 {
150 return true;
151 }
152 }
153 return false;
154 }
155 }