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.FileObject;
    21
    22  import java.util.*;
    23
    24  /***
    25   * todo: add class description here
    26   *
    27   * @author Siarhei Baidun
    28   * @since 0.1
    29   */
    30  public abstract class AbstractFileOperationProvider implements
    31  		FileOperationProvider
    32  {
    33
    34  	/***
    35  	 * Available operations. Operations could be registered for different schemes.
    36  	 * Some operations can work only for "file" scheme, other - for "svnhttp(s)",
    37  	 * "svn", "svnssh", but not for "file", etc. The Map has scheme as a key and
    38  	 * Colleaction of operations that are available for that scheme.
    39  	 */
    40  	private Collection operations = new ArrayList();
    41
    42  	/***
    43  	 * Gather available operations for the specified FileObject and put them into
    44  	 * specified operationsList.
    45  	 *
    46  	 * @param operationsList
    47  	 *            the list of available operations for the specivied FileObject.
    48  	 *            The operationList contains classes of available operations, e.g.
    49  	 *            Class objects.
    50  	 * @param file
    51  	 *            the FileObject for which we want to get the list of available
    52  	 *            operations.
    53  	 * @throws org.apache.commons.vfs.FileSystemException
    54  	 *             if list of operations cannto be retrieved.
    55  	 */
    56  	public final void collectOperations(final Collection operationsList,
    57  			final FileObject file) throws FileSystemException
    58  	{
    59
    60  		doCollectOperations(operations, operationsList, file);
    61  	}
    62
    63  	/***
    64  	 *
    65  	 * @throws FileSystemException
    66  	 */
    67  	protected abstract void doCollectOperations(
    68  			final Collection availableOperations, final Collection resultList,
    69  			final FileObject file) throws FileSystemException;
    70
    71  	/***
    72  	 * @param file
    73  	 *            the FileObject for which we need a operation.
    74  	 * @param operationClass
    75  	 *            the Class which instance we are needed.
    76  	 * @return the requried operation instance.
    77  	 * @throws org.apache.commons.vfs.FileSystemException
    78  	 *             if operation cannot be retrieved.
    79  	 */
    80  	public final FileOperation getOperation(FileObject file, Class operationClass)
    81  			throws FileSystemException
    82  	{
    83  		Class implementation = lookupOperation(operationClass);
    84
    85  		FileOperation operationInstance = instantiateOperation(file, implementation);
    86
    87  		return operationInstance;
    88  	}
    89
    90  	/***
    91  	 *
    92  	 * @param operationClass
    93  	 * @return
    94  	 * @throws FileSystemException
    95  	 */
    96  	protected abstract FileOperation instantiateOperation(final FileObject file,
    97  			final Class operationClass) throws FileSystemException;
    98
    99  	/***
    100 	 *
    101 	 * @param operationClass
    102 	 * @return never returns null
    103 	 */
    104 	protected final Class lookupOperation(final Class operationClass)
    105 			throws FileSystemException
    106 	{
    107 		// check validity of passed class
    108 		if (!FileOperation.class.isAssignableFrom(operationClass))
    109 		{
    110 			throw new FileSystemException("vfs.operation/wrong-type.error", operationClass);
    111 		}
    112
    113 		// find appropriate class
    114 		Class foundClass = null;
    115 		Iterator iterator = operations.iterator();
    116 		while (iterator.hasNext())
    117 		{
    118 			Class operation = (Class) iterator.next();
    119 			if (operationClass.isAssignableFrom(operation))
    120 			{
    121 				foundClass = operation;
    122 				break;
    123 			}
    124 		}
    125
    126 		if (foundClass == null)
    127 		{
    128 			throw new FileSystemException("vfs.operation/not-found.error", operationClass);
    129 		}
    130
    131 		return foundClass;
    132 	}
    133
    134 	/***
    135 	 *
    136 	 * @param operationClass
    137 	 * @throws FileSystemException
    138 	 */
    139 	protected final void addOperation(final Class operationClass)
    140 			throws FileSystemException
    141 	{
    142 		// check validity of passed class
    143 		if (!FileOperation.class.isAssignableFrom(operationClass))
    144 		{
    145 			throw new FileSystemException("vfs.operation/cant-register.error", operationClass);
    146 		}
    147
    148 		// ok, lets add it to the list
    149 		operations.add(operationClass);
    150 	}
    151 }