View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @since 0.1
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       * @param file The file.
42       */
43      public DefaultFileOperations(final FileObject file) {
44          fileObject = file;
45  
46          fsmanager = file.getFileSystem().getFileSystemManager();
47      }
48  
49      /**
50       * @return The operation classes.
51       * @throws FileSystemException If an error occurs.
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       * @param operationClass The Class that performs the operation.
77       * @return The FileOperation.
78       * @throws FileSystemException if an error occurs.
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      * @param operationClass the operation's class.
103      * @return true if the operation of specified class is supported for current FileObject and false otherwise.
104      *
105      * @throws FileSystemException if an error occurs.
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 }