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