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: */
017: package org.apache.commons.vfs.operations;
018:
019: import org.apache.commons.vfs.FileSystemException;
020: import org.apache.commons.vfs.FileObject;
021:
022: import java.util.*;
023:
024: /**
025: * todo: add class description here
026: *
027: * @author Siarhei Baidun
028: * @since 0.1
029: */
030: public abstract class AbstractFileOperationProvider implements
031: FileOperationProvider {
032:
033: /**
034: * Available operations. Operations could be registered for different schemes.
035: * Some operations can work only for "file" scheme, other - for "svnhttp(s)",
036: * "svn", "svnssh", but not for "file", etc. The Map has scheme as a key and
037: * Colleaction of operations that are available for that scheme.
038: */
039: private Collection operations = new ArrayList();
040:
041: /**
042: * Gather available operations for the specified FileObject and put them into
043: * specified operationsList.
044: *
045: * @param operationsList
046: * the list of available operations for the specivied FileObject.
047: * The operationList contains classes of available operations, e.g.
048: * Class objects.
049: * @param file
050: * the FileObject for which we want to get the list of available
051: * operations.
052: * @throws org.apache.commons.vfs.FileSystemException
053: * if list of operations cannto be retrieved.
054: */
055: public final void collectOperations(
056: final Collection operationsList, final FileObject file)
057: throws FileSystemException {
058:
059: doCollectOperations(operations, operationsList, file);
060: }
061:
062: /**
063: *
064: * @throws FileSystemException
065: */
066: protected abstract void doCollectOperations(
067: final Collection availableOperations,
068: final Collection resultList, final FileObject file)
069: throws FileSystemException;
070:
071: /**
072: * @param file
073: * the FileObject for which we need a operation.
074: * @param operationClass
075: * the Class which instance we are needed.
076: * @return the requried operation instance.
077: * @throws org.apache.commons.vfs.FileSystemException
078: * if operation cannot be retrieved.
079: */
080: public final FileOperation getOperation(FileObject file,
081: Class operationClass) throws FileSystemException {
082: Class implementation = lookupOperation(operationClass);
083:
084: FileOperation operationInstance = instantiateOperation(file,
085: implementation);
086:
087: return operationInstance;
088: }
089:
090: /**
091: *
092: * @param operationClass
093: * @return
094: * @throws FileSystemException
095: */
096: protected abstract FileOperation instantiateOperation(
097: final FileObject file, final Class operationClass)
098: throws FileSystemException;
099:
100: /**
101: *
102: * @param operationClass
103: * @return never returns null
104: */
105: protected final Class lookupOperation(final Class operationClass)
106: throws FileSystemException {
107: // check validity of passed class
108: if (!FileOperation.class.isAssignableFrom(operationClass)) {
109: throw new FileSystemException(
110: "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: Class operation = (Class) iterator.next();
118: if (operationClass.isAssignableFrom(operation)) {
119: foundClass = operation;
120: break;
121: }
122: }
123:
124: if (foundClass == null) {
125: throw new FileSystemException(
126: "vfs.operation/not-found.error", operationClass);
127: }
128:
129: return foundClass;
130: }
131:
132: /**
133: *
134: * @param operationClass
135: * @throws FileSystemException
136: */
137: protected final void addOperation(final Class operationClass)
138: throws FileSystemException {
139: // check validity of passed class
140: if (!FileOperation.class.isAssignableFrom(operationClass)) {
141: throw new FileSystemException(
142: "vfs.operation/cant-register.error", operationClass);
143: }
144:
145: // ok, lets add it to the list
146: operations.add(operationClass);
147: }
148: }
|