Search in sources :

Example 76 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class FileInputList method createFileList.

public static FileInputList createFileList(VariableSpace space, String[] fileName, String[] fileMask, String[] excludeFileMask, String[] fileRequired, boolean[] includeSubdirs, FileTypeFilter[] fileTypeFilters) {
    FileInputList fileInputList = new FileInputList();
    // Replace possible environment variables...
    final String[] realfile = space.environmentSubstitute(fileName);
    final String[] realmask = space.environmentSubstitute(fileMask);
    final String[] realExcludeMask = space.environmentSubstitute(excludeFileMask);
    for (int i = 0; i < realfile.length; i++) {
        final String onefile = realfile[i];
        final String onemask = realmask[i];
        final String excludeonemask = realExcludeMask[i];
        final boolean onerequired = YES.equalsIgnoreCase(fileRequired[i]);
        final boolean subdirs = includeSubdirs[i];
        final FileTypeFilter filter = ((fileTypeFilters == null || fileTypeFilters[i] == null) ? FileTypeFilter.ONLY_FILES : fileTypeFilters[i]);
        if (Utils.isEmpty(onefile)) {
            continue;
        }
        try {
            FileObject directoryFileObject = KettleVFS.getFileObject(onefile, space);
            boolean processFolder = true;
            if (onerequired) {
                if (!directoryFileObject.exists()) {
                    // if we don't find folder..no need to continue
                    fileInputList.addNonExistantFile(directoryFileObject);
                    processFolder = false;
                } else {
                    if (!directoryFileObject.isReadable()) {
                        fileInputList.addNonAccessibleFile(directoryFileObject);
                        processFolder = false;
                    }
                }
            }
            // 
            if (processFolder) {
                if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) {
                    // it's a directory
                    FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() {

                        @Override
                        public boolean traverseDescendents(FileSelectInfo info) {
                            return info.getDepth() == 0 || subdirs;
                        }

                        @Override
                        public boolean includeFile(FileSelectInfo info) {
                            // Never return the parent directory of a file list.
                            if (info.getDepth() == 0) {
                                return false;
                            }
                            FileObject fileObject = info.getFile();
                            try {
                                if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) {
                                    String name = info.getFile().getName().getBaseName();
                                    boolean matches = true;
                                    if (!Utils.isEmpty(onemask)) {
                                        matches = Pattern.matches(onemask, name);
                                    }
                                    boolean excludematches = false;
                                    if (!Utils.isEmpty(excludeonemask)) {
                                        excludematches = Pattern.matches(excludeonemask, name);
                                    }
                                    return (matches && !excludematches);
                                }
                                return false;
                            } catch (IOException ex) {
                                // Upon error don't process the file.
                                return false;
                            }
                        }
                    });
                    if (fileObjects != null) {
                        for (int j = 0; j < fileObjects.length; j++) {
                            FileObject fileObject = fileObjects[j];
                            if (fileObject.exists()) {
                                fileInputList.addFile(fileObject);
                            }
                        }
                    }
                    if (Utils.isEmpty(fileObjects)) {
                        if (onerequired) {
                            fileInputList.addNonAccessibleFile(directoryFileObject);
                        }
                    }
                    // Sort the list: quicksort, only for regular files
                    fileInputList.sortFiles();
                } else if (directoryFileObject instanceof CompressedFileFileObject) {
                    FileObject[] children = directoryFileObject.getChildren();
                    for (int j = 0; j < children.length; j++) {
                        // See if the wildcard (regexp) matches...
                        String name = children[j].getName().getBaseName();
                        boolean matches = true;
                        if (!Utils.isEmpty(onemask)) {
                            matches = Pattern.matches(onemask, name);
                        }
                        boolean excludematches = false;
                        if (!Utils.isEmpty(excludeonemask)) {
                            excludematches = Pattern.matches(excludeonemask, name);
                        }
                        if (matches && !excludematches) {
                            fileInputList.addFile(children[j]);
                        }
                    }
                // We don't sort here, keep the order of the files in the archive.
                } else {
                    FileObject fileObject = KettleVFS.getFileObject(onefile, space);
                    if (fileObject.exists()) {
                        if (fileObject.isReadable()) {
                            fileInputList.addFile(fileObject);
                        } else {
                            if (onerequired) {
                                fileInputList.addNonAccessibleFile(fileObject);
                            }
                        }
                    } else {
                        if (onerequired) {
                            fileInputList.addNonExistantFile(fileObject);
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (onerequired) {
                fileInputList.addNonAccessibleFile(new NonAccessibleFileObject(onefile));
            }
            log.logError(Const.getStackTracker(e));
        }
    }
    return fileInputList;
}
Also used : CompressedFileFileObject(org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject) IOException(java.io.IOException) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) CompressedFileFileObject(org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject) FileObject(org.apache.commons.vfs2.FileObject)

Example 77 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class TwoWayPasswordEncoderPluginType method registerXmlPlugins.

@Override
protected void registerXmlPlugins() throws KettlePluginException {
    for (PluginFolderInterface folder : pluginFolders) {
        if (folder.isPluginXmlFolder()) {
            List<FileObject> pluginXmlFiles = findPluginXmlFiles(folder.getFolder());
            for (FileObject file : pluginXmlFiles) {
                try {
                    Document document = XMLHandler.loadXMLFile(file);
                    Node pluginNode = XMLHandler.getSubNode(document, "plugin");
                    if (pluginNode != null) {
                        registerPluginFromXmlResource(pluginNode, KettleVFS.getFilename(file.getParent()), this.getClass(), false, file.getParent().getURL());
                    }
                } catch (Exception e) {
                    // We want to report this plugin.xml error, perhaps an XML typo or something like that...
                    // 
                    log.logError("Error found while reading password encoder plugin.xml file: " + file.getName().toString(), e);
                }
            }
        }
    }
}
Also used : PluginFolderInterface(org.pentaho.di.core.plugins.PluginFolderInterface) Node(org.w3c.dom.Node) FileObject(org.apache.commons.vfs2.FileObject) Document(org.w3c.dom.Document) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException)

Example 78 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryTalendJobExec method prepareJarFiles.

private URL[] prepareJarFiles(FileObject zipFile) throws Exception {
    // zip:file:///tmp/foo.zip
    FileInputList fileList = FileInputList.createFileList(this, new String[] { "zip:" + zipFile.toString() }, // Include mask: only jar files
    new String[] { ".*\\.jar$" }, // Exclude mask: only jar files
    new String[] { ".*classpath\\.jar$" }, // File required
    new String[] { "Y" }, // Search sub-directories
    new boolean[] { true });
    List<URL> files = new ArrayList<URL>();
    // 
    for (FileObject file : fileList.getFiles()) {
        FileObject jarfilecopy = KettleVFS.createTempFile(file.getName().getBaseName(), ".jar", environmentSubstitute("${java.io.tmpdir}"));
        jarfilecopy.copyFrom(file, new AllFileSelector());
        files.add(jarfilecopy.getURL());
    }
    return files.toArray(new URL[files.size()]);
}
Also used : AllFileSelector(org.apache.commons.vfs2.AllFileSelector) ArrayList(java.util.ArrayList) FileObject(org.apache.commons.vfs2.FileObject) FileInputList(org.pentaho.di.core.fileinput.FileInputList) URL(java.net.URL)

Example 79 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryZipFile method createParentFolder.

private boolean createParentFolder(String filename) {
    // Check for parent folder
    FileObject parentfolder = null;
    boolean result = false;
    try {
        // Get parent folder
        parentfolder = KettleVFS.getFileObject(filename, this).getParent();
        if (!parentfolder.exists()) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.CanNotFindFolder", "" + parentfolder.getName()));
            }
            parentfolder.createFolder();
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.FolderCreated", "" + parentfolder.getName()));
            }
        } else {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryZipFile.FolderExists", "" + parentfolder.getName()));
            }
        }
        result = true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntryZipFile.CanNotCreateFolder", "" + parentfolder.getName()), e);
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
            } catch (Exception ex) {
            // Ignore
            }
        }
    }
    return result;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) IOException(java.io.IOException)

Example 80 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryZipFile method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    // reset values
    String realZipfilename;
    String realWildcard = null;
    String realWildcardExclude = null;
    String realTargetdirectory;
    String realMovetodirectory = environmentSubstitute(movetoDirectory);
    // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if (parentJobMeta.getNamedClusterEmbedManager() != null) {
        parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
    }
    // Sanity check
    boolean SanityControlOK = true;
    if (afterZip == 2) {
        if (Utils.isEmpty(realMovetodirectory)) {
            SanityControlOK = false;
            logError(BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
        } else {
            FileObject moveToDirectory = null;
            try {
                moveToDirectory = KettleVFS.getFileObject(realMovetodirectory, this);
                if (moveToDirectory.exists()) {
                    if (moveToDirectory.getType() == FileType.FOLDER) {
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderExist", realMovetodirectory));
                        }
                    } else {
                        SanityControlOK = false;
                        logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotFolder", realMovetodirectory));
                    }
                } else {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist", realMovetodirectory));
                    }
                    if (createMoveToDirectory) {
                        moveToDirectory.createFolder();
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderCreaterd", realMovetodirectory));
                        }
                    } else {
                        SanityControlOK = false;
                        logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist", realMovetodirectory));
                    }
                }
            } catch (Exception e) {
                SanityControlOK = false;
                logError(BaseMessages.getString(PKG, "JobZipFiles.ErrorGettingMoveToFolder.Label", realMovetodirectory), e);
            } finally {
                if (moveToDirectory != null) {
                    realMovetodirectory = KettleVFS.getFilename(moveToDirectory);
                    try {
                        moveToDirectory.close();
                    } catch (Exception e) {
                        logError("Error moving to directory", e);
                        SanityControlOK = false;
                    }
                }
            }
        }
    }
    if (!SanityControlOK) {
        return errorResult(result);
    }
    if (isFromPrevious) {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
        }
    }
    if (isFromPrevious && rows != null) {
        try {
            for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                // get arguments from previous job entry
                RowMetaAndData resultRow = rows.get(iteration);
                // get target directory
                realTargetdirectory = resultRow.getString(0, null);
                if (!Utils.isEmpty(realTargetdirectory)) {
                    // get wildcard to include
                    if (!Utils.isEmpty(resultRow.getString(1, null))) {
                        realWildcard = resultRow.getString(1, null);
                    }
                    // get wildcard to exclude
                    if (!Utils.isEmpty(resultRow.getString(2, null))) {
                        realWildcardExclude = resultRow.getString(2, null);
                    }
                    // get destination zip file
                    realZipfilename = resultRow.getString(3, null);
                    if (!Utils.isEmpty(realZipfilename)) {
                        if (!processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder)) {
                            return errorResult(result);
                        }
                    } else {
                        logError("destination zip filename is empty! Ignoring row...");
                    }
                } else {
                    logError("Target directory is empty! Ignoring row...");
                }
            }
        } catch (Exception e) {
            logError("Erreur during process!", e);
            result.setResult(false);
            result.setNrErrors(1);
        }
    } else if (!isFromPrevious) {
        if (!Utils.isEmpty(sourceDirectory)) {
            // get values from job entry
            realZipfilename = getFullFilename(environmentSubstitute(zipFilename), addDate, addTime, specifyFormat, dateTimeFormat);
            realWildcard = environmentSubstitute(wildCard);
            realWildcardExclude = environmentSubstitute(excludeWildCard);
            realTargetdirectory = environmentSubstitute(sourceDirectory);
            boolean success = processRowFile(parentJob, result, realZipfilename, realWildcard, realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder);
            if (success) {
                result.setResult(true);
            } else {
                errorResult(result);
            }
        } else {
            logError("Source folder/file is empty! Ignoring row...");
        }
    }
    // End
    return result;
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) FileObject(org.apache.commons.vfs2.FileObject) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)646 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)203 FileSystemException (org.apache.commons.vfs2.FileSystemException)173 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)104 KettleFileException (org.pentaho.di.core.exception.KettleFileException)97 Test (org.junit.Test)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)68 File (java.io.File)60 InputStream (java.io.InputStream)48 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)37 KettleStepException (org.pentaho.di.core.exception.KettleStepException)36 ArrayList (java.util.ArrayList)35 ResultFile (org.pentaho.di.core.ResultFile)33 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)32 Result (org.pentaho.di.core.Result)32 OutputStream (java.io.OutputStream)29 FileName (org.apache.commons.vfs2.FileName)29 KettleValueException (org.pentaho.di.core.exception.KettleValueException)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)28