Example usage for org.apache.commons.vfs FileSystemException getCause

List of usage examples for org.apache.commons.vfs FileSystemException getCause

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileSystemException getCause.

Prototype

public final Throwable getCause() 

Source Link

Document

Retrieve root cause of the exception.

Usage

From source file:com.adito.networkplaces.store.cifs.CIFSMount.java

public FileObject createVFSFileObject(String path,
        PasswordCredentials credentials/*, DAVTransaction transaction*/)
        throws IOException, DAVAuthenticationRequiredException {

    super.getStore().getName();

    URI uri = getRootVFSURI(SystemProperties.get("jcifs.encoding", "cp860"));

    try {//  w w  w  .  j  av  a  2s . c  om
        uri.setScheme("smb");
        if (credentials != null) {
            uri.setUserinfo(DAVUtilities.encodeURIUserInfo(credentials.getUsername()
                    + (credentials.getPassword() != null ? ":" + new String(credentials.getPassword()) : "")));
        }
        uri.setPath(uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/")
                + DAVUtilities.encodePath(path, SystemProperties.get("jcifs.encoding", "cp860")));
        FileObject root = getStore().getRepository().getFileSystemManager().resolveFile(uri.toString());
        if (root.getType().equals(FileType.FOLDER)) {
            // Extra check so that the correct exception is thrown.
            root.getChildren();
        }
        return root;
    } catch (FileSystemException fse) {
        if (fse.getCause().getClass().getName().equals("jcifs.smb.SmbAuthException")) {
            throw new DAVAuthenticationRequiredException(getMountString());
        }
        if (fse.getCause() != null && fse.getCause() instanceof SmbException
                && ((SmbException) fse.getCause()).getRootCause() != null
                && "Connection timeout".equals(((SmbException) fse.getCause()).getRootCause().getMessage())) {
            throw new UnknownHostException(uri.getHost());
        }
        if (log.isDebugEnabled())
            log.debug("File system exception! ", fse);
        throw fse;
    }
}

From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;

    // Clear list files to remove after copy process
    // This list is also added to result files name
    list_files_remove.clear();/*from  www.j ava 2  s.c o  m*/
    list_add_result.clear();

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, this);

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT COPY FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CanNotCopyFolderToFile",
                            realSourceFilefoldername, realDestinationFilefoldername));

                    NbrFail++;

                } else {

                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // Copy the file to the destination folder

                        destinationfilefolder.copyFrom(sourcefilefolder.getParent(),
                                new TextOneFileSelector(sourcefilefolder.getParent().toString(),
                                        sourcefilefolder.getName().getBaseName(),
                                        destinationfilefolder.toString()));
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobCopyFiles.Log.FileCopied",
                                    sourcefilefolder.getName().toString(),
                                    destinationfilefolder.getName().toString()));
                        }

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        destinationfilefolder.copyFrom(sourcefilefolder,
                                new TextOneToOneFileSelector(destinationfilefolder));
                    } else {
                        // Both source and destination are folders
                        if (isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobCopyFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));

                        }

                        TextFileSelector textFileSelector = new TextFileSelector(sourcefilefolder,
                                destinationfilefolder, realWildcard, parentJob);
                        try {
                            destinationfilefolder.copyFrom(sourcefilefolder, textFileSelector);
                        } finally {
                            textFileSelector.shutdown();
                        }
                    }

                    // Remove Files if needed
                    if (remove_source_files && !list_files_remove.isEmpty()) {
                        String sourceFilefoldername = sourcefilefolder.toString();
                        int trimPathLength = sourceFilefoldername.length() + 1;
                        FileObject removeFile;

                        for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext()
                                && !parentJob.isStopped();) {
                            String fileremoventry = iter.next();
                            removeFile = null; // re=null each iteration
                            // Try to get the file relative to the existing connection
                            if (fileremoventry.startsWith(sourceFilefoldername)) {
                                if (trimPathLength < fileremoventry.length()) {
                                    removeFile = sourcefilefolder
                                            .getChild(fileremoventry.substring(trimPathLength));
                                }
                            }

                            // Unable to retrieve file through existing connection; Get the file through a new VFS connection
                            if (removeFile == null) {
                                removeFile = KettleVFS.getFileObject(fileremoventry, this);
                            }

                            // Remove ONLY Files
                            if (removeFile.getType() == FileType.FILE) {
                                boolean deletefile = removeFile.delete();
                                logBasic(" ------ ");
                                if (!deletefile) {
                                    logError("      " + BaseMessages.getString(PKG,
                                            "JobCopyFiles.Error.Exception.CanRemoveFileFolder",
                                            fileremoventry));
                                } else {
                                    if (isDetailed()) {
                                        logDetailed("      " + BaseMessages.getString(PKG,
                                                "JobCopyFiles.Log.FileFolderRemoved", fileremoventry));
                                    }
                                }
                            }
                        }
                    }

                    // Add files to result files name
                    if (add_result_filesname && !list_add_result.isEmpty()) {
                        String destinationFilefoldername = destinationfilefolder.toString();
                        int trimPathLength = destinationFilefoldername.length() + 1;
                        FileObject addFile;

                        for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext();) {
                            String fileaddentry = iter.next();
                            addFile = null; // re=null each iteration

                            // Try to get the file relative to the existing connection
                            if (fileaddentry.startsWith(destinationFilefoldername)) {
                                if (trimPathLength < fileaddentry.length()) {
                                    addFile = destinationfilefolder
                                            .getChild(fileaddentry.substring(trimPathLength));
                                }
                            }

                            // Unable to retrieve file through existing connection; Get the file through a new VFS connection
                            if (addFile == null) {
                                addFile = KettleVFS.getFileObject(fileaddentry, this);
                            }

                            // Add ONLY Files
                            if (addFile.getType() == FileType.FILE) {
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, addFile,
                                        parentJob.getJobname(), toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (isDetailed()) {
                                    logDetailed(" ------ ");
                                    logDetailed("      " + BaseMessages.getString(PKG,
                                            "JobCopyFiles.Log.FileAddedToResultFilesName", fileaddentry));
                                }
                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));

        }
    } catch (FileSystemException fse) {
        logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcessFileSystemException",
                fse.getMessage()));
        Throwable throwable = fse.getCause();
        while (throwable != null) {
            logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CausedBy", throwable.getMessage()));
            throwable = throwable.getCause();
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcess",
                realSourceFilefoldername, realDestinationFilefoldername, e.getMessage()), e);
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
                sourcefilefolder = null;
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
                destinationfilefolder = null;
            } catch (IOException ex) { /* Ignore */
            }
        }
    }

    return entrystatus;
}