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.lang3.ArrayUtils;
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.FileSystemManager;
26  
27  /**
28   * The default implementations for {@link FileOperations}.
29   *
30   * @since 0.1
31   */
32  public class DefaultFileOperations implements FileOperations {
33  
34      /**
35       */
36      private final FileSystemManager fsmanager;
37  
38      /**
39       */
40      private final FileObject fileObject;
41  
42      /**
43       * Constructs a new instance.
44       *
45       * @param file The file.
46       */
47      public DefaultFileOperations(final FileObject file) {
48          fileObject = file;
49  
50          fsmanager = file.getFileSystem().getFileSystemManager();
51      }
52  
53      /**
54       * @param operationClass The Class that performs the operation.
55       * @return The FileOperation.
56       * @throws FileSystemException if an error occurs.
57       */
58      @Override
59      public FileOperation getOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
60  
61          final String scheme = fileObject.getURL().getProtocol();
62          final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
63  
64          FileSystemException.requireNonNull(providers, "vfs.operation/operation-not-supported.error", operationClass);
65  
66          FileOperation resultOperation = null;
67  
68          for (final FileOperationProvider provider : providers) {
69              resultOperation = provider.getOperation(fileObject, operationClass);
70  
71              if (resultOperation != null) {
72                  break;
73              }
74          }
75  
76          return FileSystemException.requireNonNull(resultOperation, "vfs.operation/operation-not-supported.error", operationClass);
77      }
78  
79      /**
80       * @return The operation classes.
81       * @throws FileSystemException If an error occurs.
82       */
83      @Override
84      public Class<? extends FileOperation>[] getOperations() throws FileSystemException {
85  
86          final String scheme = fileObject.getURL().getProtocol();
87          final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
88  
89          if (providers == null) {
90              return null;
91          }
92  
93          final List<Class<? extends FileOperation>> operations = new ArrayList<>();
94  
95          for (final FileOperationProvider provider : providers) {
96              provider.collectOperations(operations, fileObject);
97          }
98  
99          @SuppressWarnings("unchecked")
100         final Class<? extends FileOperation>[] array = (Class<? extends FileOperation>[]) operations
101                 .toArray(ArrayUtils.EMPTY_CLASS_ARRAY);
102         return array;
103     }
104 
105     /**
106      * @param operationClass the operation's class.
107      * @return true if the operation of specified class is supported for current FileObject and false otherwise.
108      * @throws FileSystemException if an error occurs.
109      */
110     @Override
111     public boolean hasOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
112         final Class<? extends FileOperation>[] operations = getOperations();
113         if (operations == null) {
114             return false;
115         }
116 
117         for (final Class<? extends FileOperation> operation : operations) {
118             if (operationClass.isAssignableFrom(operation)) {
119                 return true;
120             }
121         }
122         return false;
123     }
124 }