001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.operations;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.lang3.ArrayUtils;
023import org.apache.commons.vfs2.FileObject;
024import org.apache.commons.vfs2.FileSystemException;
025import org.apache.commons.vfs2.FileSystemManager;
026
027/**
028 * The default implementations for {@link FileOperations}.
029 *
030 * @since 0.1
031 */
032public class DefaultFileOperations implements FileOperations {
033
034    /**
035     */
036    private final FileSystemManager fsmanager;
037
038    /**
039     */
040    private final FileObject fileObject;
041
042    /**
043     * Constructs a new instance.
044     *
045     * @param file The file.
046     */
047    public DefaultFileOperations(final FileObject file) {
048        fileObject = file;
049
050        fsmanager = file.getFileSystem().getFileSystemManager();
051    }
052
053    /**
054     * @param operationClass The Class that performs the operation.
055     * @return The FileOperation.
056     * @throws FileSystemException if an error occurs.
057     */
058    @Override
059    public FileOperation getOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
060
061        final String scheme = fileObject.getURL().getProtocol();
062        final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
063
064        FileSystemException.requireNonNull(providers, "vfs.operation/operation-not-supported.error", operationClass);
065
066        FileOperation resultOperation = null;
067
068        for (final FileOperationProvider provider : providers) {
069            resultOperation = provider.getOperation(fileObject, operationClass);
070
071            if (resultOperation != null) {
072                break;
073            }
074        }
075
076        return FileSystemException.requireNonNull(resultOperation, "vfs.operation/operation-not-supported.error", operationClass);
077    }
078
079    /**
080     * @return The operation classes.
081     * @throws FileSystemException If an error occurs.
082     */
083    @Override
084    public Class<? extends FileOperation>[] getOperations() throws FileSystemException {
085
086        final String scheme = fileObject.getURL().getProtocol();
087        final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
088
089        if (providers == null) {
090            return null;
091        }
092
093        final List<Class<? extends FileOperation>> operations = new ArrayList<>();
094
095        for (final FileOperationProvider provider : providers) {
096            provider.collectOperations(operations, fileObject);
097        }
098
099        @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}