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.vfs2.FileObject;
023import org.apache.commons.vfs2.FileSystemException;
024import org.apache.commons.vfs2.FileSystemManager;
025
026/**
027 *
028 * @since 0.1
029 */
030public class DefaultFileOperations implements FileOperations {
031    /**
032     */
033    private final FileSystemManager fsmanager;
034
035    /**
036     */
037    private final FileObject fileObject;
038
039    /**
040     *
041     * @param file The file.
042     */
043    public DefaultFileOperations(final FileObject file) {
044        fileObject = file;
045
046        fsmanager = file.getFileSystem().getFileSystemManager();
047    }
048
049    /**
050     * @return The operation classes.
051     * @throws FileSystemException If an error occurs.
052     */
053    @Override
054    public Class<? extends FileOperation>[] getOperations() throws FileSystemException {
055
056        final String scheme = fileObject.getURL().getProtocol();
057        final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
058
059        if (providers == null) {
060            return null;
061        }
062
063        final List<Class<? extends FileOperation>> operations = new ArrayList<>();
064
065        for (final FileOperationProvider provider : providers) {
066            provider.collectOperations(operations, fileObject);
067        }
068
069        @SuppressWarnings("unchecked")
070        final Class<? extends FileOperation>[] array = (Class<? extends FileOperation>[]) operations
071                .toArray(new Class<?>[] {});
072        return array;
073    }
074
075    /**
076     * @param operationClass The Class that performs the operation.
077     * @return The FileOperation.
078     * @throws FileSystemException if an error occurs.
079     */
080    @Override
081    public FileOperation getOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
082
083        final String scheme = fileObject.getURL().getProtocol();
084        final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);
085
086        if (providers == null) {
087            throw new FileSystemException("vfs.operation/operation-not-supported.error", operationClass);
088        }
089
090        FileOperation resultOperation = null;
091
092        for (final FileOperationProvider provider : providers) {
093            resultOperation = provider.getOperation(fileObject, operationClass);
094
095            if (resultOperation != null) {
096                break;
097            }
098        }
099
100        if (resultOperation == null) {
101            throw new FileSystemException("vfs.operation/operation-not-supported.error", operationClass);
102        }
103
104        return resultOperation;
105    }
106
107    /**
108     * @param operationClass the operation's class.
109     * @return true if the operation of specified class is supported for current FileObject and false otherwise.
110     *
111     * @throws FileSystemException if an error occurs.
112     */
113    @Override
114    public boolean hasOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {
115        final Class<? extends FileOperation>[] operations = getOperations();
116        if (operations == null) {
117            return false;
118        }
119
120        for (final Class<? extends FileOperation> operation : operations) {
121            if (operationClass.isAssignableFrom(operation)) {
122                return true;
123            }
124        }
125        return false;
126    }
127}