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.servicemix.vfs;
018
019 import java.io.IOException;
020
021 import org.apache.commons.vfs.FileObject;
022 import org.apache.commons.vfs.FileSystemException;
023 import org.apache.commons.vfs.FileSystemManager;
024 import org.apache.commons.vfs.VFS;
025
026 /**
027 * class for resolving a path to a FileObject
028 *
029 * @author lhein
030 */
031 public class FileObjectResolver {
032
033 /**
034 * returns the file object to use
035 *
036 * @param manager the file system manager
037 * @param path the path
038 * @return the file object
039 * @throws IllegalArgumentException on wrong parameters
040 * @throws IOException on resolving errors
041 */
042 public static FileObject resolveToFileObject(FileSystemManager manager, String path) throws IllegalArgumentException, IOException {
043 FileObject answer = null;
044
045 try {
046 if (manager == null) {
047 manager = VFS.getManager();
048 }
049 if (path == null) {
050 throw new IllegalArgumentException("You must specify a path property");
051 }
052
053 answer = manager.resolveFile(path);
054 if (answer == null) {
055 throw new IOException("Could not resolve file: " + path);
056 }
057
058 try {
059 answer.createFolder();
060 }
061 catch (FileSystemException e) {
062 throw createIOException("Failed to create folder.", e);
063 }
064 }
065 catch (FileSystemException e) {
066 throw createIOException("Failed to initialize file system manager.", e);
067 }
068
069 return answer;
070 }
071
072 /*
073 * Helper method to create an IOException with a cause
074 *
075 * (Java 5 doesn't have an IOException(String, Exception) constructor)
076 */
077 private static IOException createIOException(String message, Exception cause) {
078 final IOException exception = new IOException(message);
079 exception.initCause(cause);
080 return exception;
081 }
082 }