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.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 }