Example usage for org.apache.commons.vfs2 FileObject getName

List of usage examples for org.apache.commons.vfs2 FileObject getName

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getName.

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.pentaho.big.data.impl.vfs.hdfs.HDFSFileObjectTest.java

@Test
public void testDoRename() throws Exception {
    FileObject fileObject = mock(FileObject.class);
    FileName fileName = mock(FileName.class);
    when(fileObject.getName()).thenReturn(fileName);
    String path2 = "fake-path-2";
    when(fileName.getPath()).thenReturn(path2);
    HadoopFileSystemPath newPath = mock(HadoopFileSystemPath.class);
    when(hadoopFileSystem.getPath(path2)).thenReturn(newPath);
    hdfsFileObject.doRename(fileObject);
    verify(hadoopFileSystem).rename(hadoopFileSystemPath, newPath);
}

From source file:org.pentaho.big.data.kettle.plugins.formats.impl.parquet.BaseParquetStepDialog.java

protected void browseForFileInputPath() {
    try {/*from   www.  j ava2s. com*/
        String path = transMeta.environmentSubstitute(wPath.getText());
        VfsFileChooserDialog fileChooserDialog;
        String fileName;
        if (Utils.isEmpty(path)) {
            fileChooserDialog = getVfsFileChooserDialog(null, null);
            fileName = selectedVFSScheme.getScheme() + "://";
        } else {
            FileObject initialFile = getInitialFile(wPath.getText());
            FileObject rootFile = initialFile.getFileSystem().getRoot();
            fileChooserDialog = getVfsFileChooserDialog(rootFile, initialFile);
            fileName = null;
        }

        FileObject selectedFile = fileChooserDialog.open(shell, null, selectedVFSScheme.getScheme(), true,
                fileName, FILES_FILTERS, fileFilterNames, true,
                VfsFileChooserDialog.VFS_DIALOG_OPEN_FILE_OR_DIRECTORY, true, true);
        if (selectedFile != null) {
            String filePath = selectedFile.getName().getFriendlyURI();
            if (!DEFAULT_LOCAL_PATH.equals(filePath)) {
                wPath.setText(filePath);
                updateLocation();
            }
        }
    } catch (KettleFileException ex) {
        log.logError(getBaseMsg("ParquetDialog.FileBrowser.KettleFileException"));
    } catch (FileSystemException ex) {
        log.logError(getBaseMsg("ParquetDialog.FileBrowser.FileSystemException"));
    }
}

From source file:org.pentaho.big.data.kettle.plugins.hdfs.trans.HadoopFileInputDialog.java

private List<String> getFirst(int nrlines, boolean skipHeaders) throws KettleException {
    HadoopFileInputMeta meta = new HadoopFileInputMeta();
    getInfo(meta);//www.  ja v a  2s  .c o m
    FileInputList textFileList = meta.getTextFileList(transMeta);

    InputStream fi = null;
    InputStream f = null;
    StringBuilder lineStringBuilder = new StringBuilder(256);
    int fileFormatType = meta.getFileFormatTypeNr();

    List<String> retval = new ArrayList<String>();

    if (textFileList.nrOfFiles() > 0) {
        FileObject file = textFileList.getFile(0);
        try {
            fi = KettleVFS.getInputStream(file);

            CompressionProvider provider = CompressionProviderFactory.getInstance()
                    .createCompressionProviderInstance(meta.content.fileCompression);
            f = provider.createInputStream(fi);

            InputStreamReader reader;
            if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
                reader = new InputStreamReader(f, meta.getEncoding());
            } else {
                reader = new InputStreamReader(f);
            }

            int linenr = 0;
            int maxnr = nrlines + (meta.content.header ? meta.content.nrHeaderLines : 0);

            if (skipHeaders) {
                // Skip the header lines first if more then one, it helps us position
                if (meta.content.layoutPaged && meta.content.nrLinesDocHeader > 0) {
                    int skipped = 0;
                    String line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
                    while (line != null && skipped < meta.content.nrLinesDocHeader - 1) {
                        skipped++;
                        line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
                    }
                }

                // Skip the header lines first if more then one, it helps us position
                if (meta.content.header && meta.content.nrHeaderLines > 0) {
                    int skipped = 0;
                    String line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
                    while (line != null && skipped < meta.content.nrHeaderLines - 1) {
                        skipped++;
                        line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
                    }
                }
            }

            String line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
            while (line != null && (linenr < maxnr || nrlines == 0)) {
                retval.add(line);
                linenr++;
                line = TextFileInputUtils.getLine(log, reader, fileFormatType, lineStringBuilder);
            }
        } catch (Exception e) {
            throw new KettleException(
                    BaseMessages.getString(BASE_PKG, "TextFileInputDialog.Exception.ErrorGettingFirstLines",
                            "" + nrlines, file.getName().getURI()),
                    e);
        } finally {
            try {
                f.close();
            } catch (Exception e) {
                // Ignore errors
            }
        }
    }

    return retval;
}

From source file:org.pentaho.big.data.kettle.plugins.job.AbstractJobEntryController.java

protected FileObject browseVfs(FileObject root, FileObject initial, int dialogMode, String[] schemeRestrictions,
        boolean showFileScheme, String defaultScheme, NamedCluster namedCluster, boolean showLocation,
        boolean showCustomUI) throws KettleFileException {
    if (initial == null) {
        initial = KettleVFS.getFileObject(Spoon.getInstance().getLastFileOpened());
    }/*www  .java2s. co  m*/
    if (root == null) {
        try {
            root = initial.getFileSystem().getRoot();
        } catch (FileSystemException e) {
            throw new KettleFileException(e);
        }
    }

    VfsFileChooserHelper fileChooserHelper = new VfsFileChooserHelper(getShell(),
            Spoon.getInstance().getVfsFileChooserDialog(root, initial), jobEntry);
    fileChooserHelper.setDefaultScheme(defaultScheme);
    fileChooserHelper.setSchemeRestrictions(schemeRestrictions);
    fileChooserHelper.setShowFileScheme(showFileScheme);
    if (namedCluster != null) {
        fileChooserHelper.setNamedCluster(namedCluster);
    }
    try {
        return fileChooserHelper.browse(getFileFilters(), getFileFilterNames(), initial.getName().getURI(),
                dialogMode, showLocation, showCustomUI);
    } catch (KettleException e) {
        throw new KettleFileException(e);
    } catch (FileSystemException e) {
        throw new KettleFileException(e);
    }
}

From source file:org.pentaho.big.data.kettle.plugins.kafka.KafkaConsumerInputDialog.java

private void selectFileTrans() {
    String curFile = transMeta.environmentSubstitute(wTransPath.getText());

    FileObject root = null;//from  ww  w.java 2s. c  om

    String parentFolder = null;
    try {
        parentFolder = KettleVFS.getFileObject(transMeta.environmentSubstitute(transMeta.getFilename()))
                .getParent().toString();
    } catch (Exception e) {
        // Take no action
    }

    try {
        root = KettleVFS.getFileObject(curFile != null ? curFile : Const.getUserHomeDirectory());

        VfsFileChooserDialog vfsFileChooser = Spoon.getInstance().getVfsFileChooserDialog(root.getParent(),
                root);
        FileObject file = vfsFileChooser.open(shell, null, Const.STRING_TRANS_FILTER_EXT,
                Const.getTransformationFilterNames(), VfsFileChooserDialog.VFS_DIALOG_OPEN_FILE);
        if (file == null) {
            return;
        }
        String fileName = file.getName().toString();
        if (fileName != null) {
            loadFileTrans(fileName);
            if (parentFolder != null && fileName.startsWith(parentFolder)) {
                fileName = fileName.replace(parentFolder,
                        "${" + Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY + "}");
            }
            wTransPath.setText(fileName);
            specificationMethod = ObjectLocationSpecificationMethod.FILENAME;
        }
    } catch (IOException | KettleException e) {
        new ErrorDialog(shell,
                BaseMessages.getString(PKG, "TransExecutorDialog.ErrorLoadingTransformation.DialogTitle"),
                BaseMessages.getString(PKG, "TransExecutorDialog.ErrorLoadingTransformation.DialogMessage"), e);
    }
}

From source file:org.pentaho.big.data.kettle.plugins.oozie.OozieJobExecutorJobEntryController.java

/**
 * Open the VFS file browser to allow for selection of the workflow job properties configuration file.
 *//*from w  w  w . ja  va2 s  . co m*/
@Bindable
public void browseWorkflowConfig() {
    FileObject path = null;
    try {
        path = KettleVFS.getFileObject(
                jobEntry.getVariableSpace().environmentSubstitute(getConfig().getOozieWorkflowConfig()));
    } catch (Exception e) {
        // Ignore, use null (default VFS browse path)
    }
    try {
        FileObject exportDir = browseVfs(null, path, VfsFileChooserDialog.VFS_DIALOG_OPEN_DIRECTORY, null, true,
                "file");
        if (exportDir != null) {
            getConfig().setOozieWorkflowConfig(exportDir.getName().getURI());
        }
    } catch (KettleFileException e) {
        getJobEntry().logError(BaseMessages.getString(OozieJobExecutorJobEntry.class, ERROR_BROWSING_DIRECTORY),
                e);
    }
}

From source file:org.pentaho.big.data.kettle.plugins.sqoop.ui.AbstractSqoopJobEntryController.java

protected FileObject getInitialFile(String path) throws FileSystemException {
    if (Const.isEmpty(path)) {
        path = "/";
    }/*from  w  ww.j a v a 2 s.  co m*/

    NamedCluster namedCluster = jobEntry.getNamedClusterService()
            .getNamedClusterByName(selectedNamedCluster.getName(), getMetaStore());
    if (namedCluster == null) {
        return null;
    }
    path = namedCluster.processURLsubstitution(path, getMetaStore(), jobEntry);

    FileObject initialFile = null;

    if (path != null) {
        String fileName = jobEntry.environmentSubstitute(path);
        if (fileName != null && !fileName.equals("")) {
            try {
                initialFile = KettleVFS.getFileObject(fileName);
                if (namedCluster.isMapr()) {
                    if (!initialFile.getName().getScheme().startsWith(Schemes.MAPRFS_SCHEME)) {
                        return null;
                    }
                } else if (!initialFile.getName().getScheme().startsWith(Schemes.HDFS_SCHEME)) {
                    return null;
                }
            } catch (Exception ex) {
                return null;
            }
        }
    }

    return initialFile;
}

From source file:org.pentaho.big.data.kettle.plugins.sqoop.ui.SqoopExportJobEntryController.java

public void browseForExportDirectory() {
    try {/*from  w w  w .  j  a va2 s . co  m*/
        String[] schemeRestrictions = new String[1];
        if (selectedNamedCluster != null && !"false".equals(selectedNamedCluster.getVariable("valid"))) {
            schemeRestrictions[0] = selectedNamedCluster.isMapr() ? Schemes.MAPRFS_SCHEME : Schemes.HDFS_SCHEME;
        } else {
            // must select cluster
            return;
        }

        String path = getConfig().getExportDir();
        FileObject initialFile = getInitialFile(path);

        if (initialFile == null) {
            showErrorDialog(BaseMessages.getString(PKG, "Sqoop.JobEntry.Connection.Error.title"),
                    BaseMessages.getString(PKG, "Sqoop.JobEntry.Connection.error"));
            return;
        }

        FileObject exportDir = browseVfs(null, initialFile, VfsFileChooserDialog.VFS_DIALOG_OPEN_DIRECTORY,
                schemeRestrictions, false, schemeRestrictions[0], selectedNamedCluster, false, false);
        VfsFileChooserDialog dialog = Spoon.getInstance().getVfsFileChooserDialog(null, null);
        boolean okPressed = dialog.okPressed;
        if (okPressed) {
            getConfig().setExportDir(exportDir != null ? exportDir.getName().getPath() : null);
            extractNamedClusterFromVfsFileChooser();
        }
    } catch (KettleFileException | FileSystemException e) {
        getJobEntry().logError(BaseMessages.getString(AbstractSqoopJobEntry.class, "ErrorBrowsingDirectory"),
                e);
    }
}

From source file:org.pentaho.big.data.kettle.plugins.sqoop.ui.SqoopImportJobEntryController.java

public void browseForTargetDirectory() {
    try {/*from   ww w . j  a v a 2  s .c om*/
        String[] schemeRestrictions = new String[1];
        if (selectedNamedCluster != null && !"false".equals(selectedNamedCluster.getVariable("valid"))) {
            schemeRestrictions[0] = selectedNamedCluster.isMapr() ? Schemes.MAPRFS_SCHEME : Schemes.HDFS_SCHEME;
        } else {
            // must select cluster
            return;
        }

        String path = getConfig().getTargetDir();
        FileObject initialFile = getInitialFile(path);

        if (initialFile == null) {
            showErrorDialog(BaseMessages.getString(PKG, "Sqoop.JobEntry.Connection.Error.title"),
                    BaseMessages.getString(PKG, "Sqoop.JobEntry.Connection.error"));
            return;
        }

        FileObject targetDir = browseVfs(null, initialFile, VfsFileChooserDialog.VFS_DIALOG_OPEN_DIRECTORY,
                schemeRestrictions, false, schemeRestrictions[0], selectedNamedCluster, false, false);
        VfsFileChooserDialog dialog = Spoon.getInstance().getVfsFileChooserDialog(null, null);
        boolean okPressed = dialog.okPressed;
        if (okPressed) {
            getConfig().setTargetDir(targetDir != null ? targetDir.getName().getPath() : null);
            extractNamedClusterFromVfsFileChooser();
        }
    } catch (KettleFileException | FileSystemException e) {
        getJobEntry().logError(BaseMessages.getString(AbstractSqoopJobEntry.class, "ErrorBrowsingDirectory"),
                e);
    }
}

From source file:org.pentaho.big.data.plugins.common.ui.VfsFileChooserHelper.java

public FileObject browse(String[] fileFilters, String[] fileFilterNames, String fileUri, FileSystemOptions opts,
        int fileDialogMode, boolean showLocation, boolean showCustomUI)
        throws KettleException, FileSystemException {
    // Get current file
    FileObject rootFile = null;//from w w  w . j  a v  a  2s .c o  m
    FileObject initialFile = null;

    if (fileUri != null) {
        initialFile = KettleVFS.getFileObject(fileUri, variableSpace, opts);
    } else {
        initialFile = KettleVFS.getFileObject(Spoon.getInstance().getLastFileOpened());
    }
    rootFile = initialFile.getFileSystem().getRoot();
    fileChooserDialog.setRootFile(rootFile);
    fileChooserDialog.setInitialFile(initialFile);
    fileChooserDialog.defaultInitialFile = rootFile;

    FileObject selectedFile = null;
    selectedFile = fileChooserDialog.open(shell, this.schemeRestrictions, getDefaultScheme(), showFileScheme(),
            initialFile.getName().getPath(), fileFilters, fileFilterNames,
            returnsUserAuthenticatedFileObjects(), fileDialogMode, showLocation, showCustomUI);

    return selectedFile;
}