Example usage for org.apache.commons.vfs FileObject getType

List of usage examples for org.apache.commons.vfs FileObject getType

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject getType.

Prototype

public FileType getType() throws FileSystemException;

Source Link

Document

Returns this file's type.

Usage

From source file:com.panet.imeta.trans.steps.mail.Mail.java

private void setAttachedFilesList(Object[] r, LogWriter log) throws Exception {
    String realSourceFileFoldername = null;
    String realSourceWildcard = null;
    FileObject sourcefile = null;
    FileObject file = null;/*from  w  w w .  java  2  s  . c  o m*/

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;

    if (meta.isZipFilenameDynamic())
        data.ZipFilename = data.previousRowMeta.getString(r, data.indexOfDynamicZipFilename);

    try {

        if (meta.isDynamicFilename()) {
            // dynamic attached filenames
            if (data.indexOfSourceFilename > -1)
                realSourceFileFoldername = data.previousRowMeta.getString(r, data.indexOfSourceFilename);

            if (data.indexOfSourceWildcard > -1)
                realSourceWildcard = data.previousRowMeta.getString(r, data.indexOfSourceWildcard);

        } else {
            // static attached filenames
            realSourceFileFoldername = data.realSourceFileFoldername;
            realSourceWildcard = data.realSourceWildcard;
        }

        if (!Const.isEmpty(realSourceFileFoldername)) {
            sourcefile = KettleVFS.getFileObject(realSourceFileFoldername);
            if (sourcefile.exists()) {
                long FileSize = 0;
                FileObject list[] = null;
                if (sourcefile.getType() == FileType.FILE) {
                    list = new FileObject[1];
                    list[0] = sourcefile;
                } else
                    list = sourcefile
                            .findFiles(new TextFileSelector(sourcefile.toString(), realSourceWildcard));
                if (list.length > 0) {

                    boolean zipFiles = meta.isZipFiles();
                    if (zipFiles && data.zipFileLimit == 0) {
                        masterZipfile = new File(
                                System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + data.ZipFilename);

                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                    }

                    for (int i = 0; i < list.length; i++) {

                        file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                        if (zipFiles) {

                            if (data.zipFileLimit == 0) {
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();
                            } else
                                FileSize += file.getContent().getSize();
                        } else {
                            addAttachedFilePart(file);
                        }
                    } // end for
                    if (zipFiles) {
                        if (log.isDebug())
                            log.logDebug(toString(), Messages.getString("Mail.Log.FileSize", "" + FileSize));
                        if (log.isDebug())
                            log.logDebug(toString(),
                                    Messages.getString("Mail.Log.LimitSize", "" + data.zipFileLimit));

                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit) {

                            masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                                    + data.ZipFilename);

                            zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                            for (int i = 0; i < list.length; i++) {

                                file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();

                            }

                        }
                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit || data.zipFileLimit == 0) {
                            file = KettleVFS.getFileObject(masterZipfile.getAbsolutePath());
                            addAttachedFilePart(file);
                        }
                    }
                }
            } else {
                log.logError(toString(),
                        Messages.getString("Mail.Error.SourceFileFolderNotExists", realSourceFileFoldername));
            }
        }
    } catch (Exception e) {
        log.logError(toString(), e.getMessage());
    } finally {
        if (sourcefile != null) {
            try {
                sourcefile.close();
            } catch (Exception e) {
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
            }
        }

        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
                zipOutputStream.close();
            } catch (IOException e) {
                log.logError(toString(), "Unable to close attachement zip file archive : " + e.toString());
            }
        }
    }

}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) throws KettleException {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;//from w  w  w.  jav  a2s  . com
    result.setEntryNr(1);
    result.setResult(false);

    NrErrors = 0;
    NrSuccess = 0;
    successConditionBroken = false;
    successConditionBrokenExit = false;
    limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);

    if (simulate) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.SimulationOn"));
    }
    if (include_subfolders) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.IncludeSubFoldersOn"));
    }

    String MoveToFolder = environmentSubstitute(destinationFolder);
    // Get source and destination files, also wildcard
    String vsourcefilefolder[] = source_filefolder;
    String vdestinationfilefolder[] = destination_filefolder;
    String vwildcard[] = wildcard;

    if (iffileexists.equals("move_file")) {
        if (Const.isEmpty(MoveToFolder)) {
            log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.MoveToFolderMissing"));
            return result;
        }
        FileObject folder = null;
        try {
            folder = KettleVFS.getFileObject(MoveToFolder);
            if (!folder.exists()) {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                if (create_move_to_folder) {
                    folder.createFolder();
                } else {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                    return result;
                }
            }
            if (!folder.getType().equals(FileType.FOLDER)) {
                log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.NotFolder", MoveToFolder));
                return result;
            }
        } catch (Exception e) {
            log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.GettingMoveToFolder",
                    MoveToFolder, e.getMessage()));
            return result;
        } finally {
            if (folder != null) {
                try {
                    folder.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    }

    if (arg_from_previous) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));
    }
    if (arg_from_previous && rows != null) {
        for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                result.setNrErrors(NrErrors);
                displayResults(log);
                return result;
            }

            resultRow = rows.get(iteration);

            // Get source and destination file names, also wildcard
            String vsourcefilefolder_previous = resultRow.getString(0, null);
            String vdestinationfilefolder_previous = resultRow.getString(1, null);
            String vwildcard_previous = resultRow.getString(2, null);

            if (!Const.isEmpty(vsourcefilefolder_previous) && !Const.isEmpty(vdestinationfilefolder_previous)) {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ProcessingRow",
                            vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous));

                if (!ProcessFileFolder(vsourcefilefolder_previous, vdestinationfilefolder_previous,
                        vwildcard_previous, parentJob, result, MoveToFolder, log)) {
                    // The move process fail
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobMoveFiles.Log.IgnoringRow", vsourcefilefolder[iteration],
                                    vdestinationfilefolder[iteration], vwildcard[iteration]));
            }
        }
    } else if (vsourcefilefolder != null && vdestinationfilefolder != null) {
        for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                result.setEntryNr(NrErrors);
                displayResults(log);
                return result;
            }

            if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) {
                // ok we can process this file/folder
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ProcessingRow",
                            vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));

                if (!ProcessFileFolder(vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i], parentJob,
                        result, MoveToFolder, log)) {
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.IgnoringRow",
                            vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
            }
        }
    }

    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus())
        result.setResult(true);

    displayResults(log);

    return result;
}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*www  .  j av  a 2  s.  c  om*/
    result.setEntryNr(1);

    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;

    String realFilenameSource = environmentSubstitute(zipFilename);
    String realWildcardSource = environmentSubstitute(wildcardSource);
    String realWildcard = environmentSubstitute(wildcard);
    String realWildcardExclude = environmentSubstitute(wildcardexclude);
    String realTargetdirectory = environmentSubstitute(targetdirectory);
    String realMovetodirectory = environmentSubstitute(movetodirectory);

    limitFiles = Const.toInt(environmentSubstitute(getLimit()), 10);
    NrErrors = 0;
    NrSuccess = 0;
    successConditionBroken = false;
    successConditionBrokenExit = false;

    if (isfromprevious) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));

        if (rows.size() == 0)
            return result;
    } else {
        if (Const.isEmpty(zipFilename)) {
            // Zip file/folder is missing
            log.logError(toString(), Messages.getString("JobUnZip.No_ZipFile_Defined.Label"));
            return result;
        }
    }

    FileObject fileObject = null;
    FileObject targetdir = null;
    FileObject movetodir = null;

    try {

        // Let's make some checks here, before running job entry ...

        boolean exitjobentry = false;
        // Target folder
        targetdir = KettleVFS.getFileObject(realTargetdirectory);
        if (!targetdir.exists()) {
            if (createfolder) {
                targetdir.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.Log.TargetFolderCreated", realTargetdirectory));

            } else {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                exitjobentry = true;
            }
        } else {
            if (!(targetdir.getType() == FileType.FOLDER)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFolder.Label", realTargetdirectory));
                exitjobentry = true;
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.TargetFolderExists.Label", realTargetdirectory));
            }
        }

        // If user want to move zip files after process
        // movetodirectory must be provided
        if (afterunzip == 2) {
            if (Const.isEmpty(movetodirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.MoveToDirectoryEmpty.Label"));
                exitjobentry = true;
            } else {
                movetodir = KettleVFS.getFileObject(realMovetodirectory);
                if (!(movetodir.exists()) || !(movetodir.getType() == FileType.FOLDER)) {
                    if (createMoveToDirectory) {
                        movetodir.createFolder();
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.MoveToFolderCreated",
                                    realMovetodirectory));
                    } else {
                        log.logError(Messages.getString("JobUnZip.Error.Label"),
                                Messages.getString("JobUnZip.MoveToDirectoryNotExists.Label"));
                        exitjobentry = true;
                    }
                }
            }
        }

        // We found errors...now exit
        if (exitjobentry)
            return result;

        if (isfromprevious) {
            if (rows != null) // Copy the input row to the (command line)
            // arguments
            {
                for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                    if (successConditionBroken) {
                        if (!successConditionBrokenExit) {
                            log.logError(toString(),
                                    Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                            successConditionBrokenExit = true;
                        }
                        result.setEntryNr(NrErrors);
                        return result;
                    }

                    resultRow = rows.get(iteration);

                    // Get sourcefile/folder and wildcard
                    realFilenameSource = resultRow.getString(0, null);
                    realWildcardSource = resultRow.getString(1, null);

                    fileObject = KettleVFS.getFileObject(realFilenameSource);
                    if (fileObject.exists()) {
                        processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                                realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
                    } else {
                        updateErrors();
                        log.logError(toString(),
                                Messages.getString("JobUnZip.Error.CanNotFindFile", realFilenameSource));
                    }
                }
            }
        } else {
            fileObject = KettleVFS.getFileObject(realFilenameSource);
            if (!fileObject.exists()) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.ZipFile.NotExists.Label", realFilenameSource));
                return result;
            }

            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobUnZip.Zip_FileExists.Label", realFilenameSource));
            if (Const.isEmpty(targetdirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                return result;
            }

            processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                    realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
        }
    } catch (Exception e) {
        log.logError(Messages.getString("JobUnZip.Error.Label"),
                Messages.getString("JobUnZip.ErrorUnzip.Label", realFilenameSource, e.getMessage()));
        updateErrors();
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (targetdir != null) {
            try {
                targetdir.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetodir != null) {
            try {
                movetodir.close();
            } catch (IOException ex) {
            }
            ;
        }
    }

    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus())
        result.setResult(true);
    displayResults(log);

    return result;
}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

private boolean unzipFile(LogWriter log, FileObject sourceFileObject, String realTargetdirectory,
        String realWildcard, String realWildcardExclude, Result result, Job parentJob, FileObject fileObject,
        FileObject movetodir, String realMovetodirectory) {
    boolean retval = false;

    try {//from w  w  w.  j av  a  2s  . c o  m

        if (log.isDetailed())
            log.logDetailed(toString(),
                    Messages.getString("JobUnZip.Log.ProcessingFile", sourceFileObject.toString()));

        // Do you create a root folder?
        //
        if (rootzip) {
            String shortSourceFilename = sourceFileObject.getName().getBaseName();
            int lenstring = shortSourceFilename.length();
            int lastindexOfDot = shortSourceFilename.lastIndexOf('.');
            if (lastindexOfDot == -1)
                lastindexOfDot = lenstring;

            String foldername = realTargetdirectory + "/" + shortSourceFilename.substring(0, lastindexOfDot);
            FileObject rootfolder = KettleVFS.getFileObject(foldername);
            if (!rootfolder.exists()) {
                try {
                    rootfolder.createFolder();
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                Messages.getString("JobUnZip.Log.RootFolderCreated", foldername));
                } catch (Exception e) {
                    throw new Exception(Messages.getString("JobUnZip.Error.CanNotCreateRootFolder", foldername),
                            e);
                }
            }
        }

        // Try to read the entries from the VFS object...
        //
        String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI();
        FileObject zipFile = KettleVFS.getFileObject(zipFilename);
        FileObject[] items = zipFile.findFiles(new AllFileSelector() {
            public boolean traverseDescendents(FileSelectInfo info) {
                return true;
            }

            public boolean includeFile(FileSelectInfo info) {
                // Never return the parent directory of a file list.
                if (info.getDepth() == 0) {
                    return false;
                }

                FileObject fileObject = info.getFile();
                return fileObject != null;
            }
        });

        Pattern pattern = null;
        if (!Const.isEmpty(realWildcard)) {
            pattern = Pattern.compile(realWildcard);

        }
        Pattern patternexclude = null;
        if (!Const.isEmpty(realWildcardExclude)) {
            patternexclude = Pattern.compile(realWildcardExclude);

        }

        for (FileObject item : items) {

            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    log.logError(toString(),
                            Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                return false;
            }

            try {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ProcessingZipEntry",
                            item.getName().getURI(), sourceFileObject.toString()));

                // get real destination filename
                //
                String newFileName = realTargetdirectory + Const.FILE_SEPARATOR
                        + getTargetFilename(item.getName().getPath());
                FileObject newFileObject = KettleVFS.getFileObject(newFileName);

                if (item.getType().equals(FileType.FOLDER)) {
                    // Directory
                    //
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                Messages.getString("JobUnZip.CreatingDirectory.Label", newFileName));

                    // Create Directory if necessary ...
                    //
                    if (!newFileObject.exists())
                        newFileObject.createFolder();
                } else {
                    // File
                    //
                    boolean getIt = true;
                    boolean getItexclude = false;

                    // First see if the file matches the regular expression!
                    //
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(item.getName().getURI());
                        getIt = matcher.matches();
                    }

                    if (patternexclude != null) {
                        Matcher matcherexclude = patternexclude.matcher(item.getName().getURI());
                        getItexclude = matcherexclude.matches();
                    }

                    boolean take = takeThisFile(log, item, newFileName);

                    if (getIt && !getItexclude && take) {
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobUnZip.ExtractingEntry.Label",
                                    item.getName().getURI(), newFileName));

                        if (iffileexist == IF_FILE_EXISTS_UNIQ) {
                            // Create file with unique name

                            int lenstring = newFileName.length();
                            int lastindexOfDot = newFileName.lastIndexOf('.');
                            if (lastindexOfDot == -1)
                                lastindexOfDot = lenstring;

                            newFileName = newFileName.substring(0, lastindexOfDot)
                                    + StringUtil.getFormattedDateTimeNow(true)
                                    + newFileName.substring(lastindexOfDot, lenstring);

                            if (log.isDebug())
                                log.logDebug(toString(),
                                        Messages.getString("JobUnZip.Log.CreatingUniqFile", newFileName));
                        }

                        // See if the folder to the target file exists...
                        //
                        if (!newFileObject.getParent().exists()) {
                            newFileObject.getParent().createFolder(); // creates
                            // the
                            // whole
                            // path.
                        }
                        InputStream is = null;
                        OutputStream os = null;

                        try {
                            is = KettleVFS.getInputStream(item);
                            os = KettleVFS.getOutputStream(newFileObject, false);

                            if (is != null) {
                                byte[] buff = new byte[2048];
                                int len;

                                while ((len = is.read(buff)) > 0) {
                                    os.write(buff, 0, len);
                                }

                                // Add filename to result filenames
                                addFilenameToResultFilenames(result, parentJob, newFileName);
                            }
                        } finally {
                            if (is != null)
                                is.close();
                            if (os != null)
                                os.close();
                        }
                    } // end if take
                }
            } catch (Exception e) {
                updateErrors();
                log.logError(toString(), Messages.getString("JobUnZip.Error.CanNotProcessZipEntry",
                        item.getName().getURI(), sourceFileObject.toString()), e);
            }
        } // End while

        // Here gc() is explicitly called if e.g. createfile is used in the
        // same
        // job for the same file. The problem is that after creating the
        // file the
        // file object is not properly garbaged collected and thus the file
        // cannot
        // be deleted anymore. This is a known problem in the JVM.

        // System.gc();

        // Unzip done...
        if (afterunzip == 1) {
            // delete zip file
            boolean deleted = fileObject.delete();
            if (!deleted) {
                updateErrors();
                log.logError(toString(),
                        Messages.getString("JobUnZip.Cant_Delete_File.Label", sourceFileObject.toString()));
            }
            // File deleted
            if (log.isDebug())
                log.logDebug(toString(),
                        Messages.getString("JobUnZip.File_Deleted.Label", sourceFileObject.toString()));
        } else if (afterunzip == 2) {
            FileObject destFile = null;
            // Move File
            try {
                String destinationFilename = movetodir + Const.FILE_SEPARATOR
                        + fileObject.getName().getBaseName();
                destFile = KettleVFS.getFileObject(destinationFilename);

                fileObject.moveTo(destFile);

                // File moved
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobUnZip.Log.FileMovedTo",
                            sourceFileObject.toString(), realMovetodirectory));
            } catch (Exception e) {
                updateErrors();
                log.logError(toString(), Messages.getString("JobUnZip.Cant_Move_File.Label",
                        sourceFileObject.toString(), realMovetodirectory, e.getMessage()));
            } finally {
                if (destFile != null) {
                    try {
                        destFile.close();
                    } catch (IOException ex) {
                    }
                    ;
                }
            }
        }

        retval = true;
    } catch (Exception e) {
        updateErrors();
        log.logError(Messages.getString("JobUnZip.Error.Label"),
                Messages.getString("JobUnZip.ErrorUnzip.Label", sourceFileObject.toString(), e.getMessage()),
                e);
    }

    return retval;
}

From source file:com.panet.imeta.job.entries.copyfiles.JobEntryCopyFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result) {

    LogWriter log = LogWriter.getInstance();
    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();/*  www.  j a v a  2s  . 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 {

        // Here gc() is explicitly called if e.g. createfile is used in the
        // same
        // job for the same file. The problem is that after creating the
        // file the
        // file object is not properly garbaged collected and thus the file
        // cannot
        // be deleted anymore. This is a known problem in the JVM.

        System.gc();

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);

        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)// destinationfilefolder.getType().equals(FileType.FILE))
                {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT COPY FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobCopyFiles.Log.Forbidden"),
                            Messages.getString("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 (log.isDetailed())
                            log.logDetailed(Messages.getString("JobCopyFiles.Log.FileCopiedInfos"),
                                    Messages.getString("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 (log.isDetailed()) {
                            log.logDetailed("", "  ");
                            log.logDetailed(toString(), Messages.getString("JobCopyFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));

                        }
                        destinationfilefolder.copyFrom(sourcefilefolder,
                                new TextFileSelector(sourcefilefolder.toString(),
                                        destinationfilefolder.toString(), realWildcard, parentJob));
                    }

                    // Remove Files if needed
                    if (remove_source_files && !list_files_remove.isEmpty()) {
                        for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext()
                                && !parentJob.isStopped();) {
                            String fileremoventry = (String) iter.next();
                            // Remove ONLY Files
                            if (KettleVFS.getFileObject(fileremoventry).getType() == FileType.FILE) {
                                boolean deletefile = KettleVFS.getFileObject(fileremoventry).delete();
                                log.logBasic("", " ------ ");
                                if (!deletefile) {
                                    log.logError("      " + Messages.getString("JobCopyFiles.Log.Error"),
                                            Messages.getString(
                                                    "JobCopyFiles.Error.Exception.CanRemoveFileFolder",
                                                    fileremoventry));
                                } else {
                                    if (log.isDetailed())
                                        log.logDetailed(
                                                "      " + Messages
                                                        .getString("JobCopyFiles.Log.FileFolderRemovedInfos"),
                                                Messages.getString("JobCopyFiles.Log.FileFolderRemoved",
                                                        fileremoventry));
                                }
                            }
                        }
                    }

                    // Add files to result files name
                    if (add_result_filesname && !list_add_result.isEmpty()) {
                        for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext();) {
                            String fileaddentry = (String) iter.next();
                            // Add ONLY Files
                            if (KettleVFS.getFileObject(fileaddentry).getType() == FileType.FILE) {
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                                        KettleVFS.getFileObject(fileaddentry), parentJob.getJobname(),
                                        toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (log.isDetailed()) {
                                    log.logDetailed("", " ------ ");
                                    log.logDetailed(
                                            "      " + Messages.getString("JobCopyFiles.Log.ResultFilesName"),
                                            Messages.getString("JobCopyFiles.Log.FileAddedToResultFilesName",
                                                    fileaddentry));
                                }
                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobCopyFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            log.logError(toString(),
                    Messages.getString("JobCopyFiles.Error.SourceFileNotExists", realSourceFilefoldername));

        }
    } catch (IOException e) {
        log.logError("Error", Messages.getString("JobCopyFiles.Error.Exception.CopyProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();

            } catch (IOException ex) {
            }
            ;
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();

            } catch (IOException ex) {
            }
            ;
        }
    }

    return entrystatus;
}

From source file:com.panet.imeta.job.entries.waitforfile.JobEntryWaitForFile.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*from  w  ww. j ava 2  s  . c  om*/

    // starttime (in seconds)
    long timeStart = System.currentTimeMillis() / 1000;

    if (filename != null) {
        FileObject fileObject = null;
        String realFilename = getRealFilename();
        try {
            fileObject = KettleVFS.getFileObject(realFilename);

            long iMaximumTimeout = Const.toInt(getRealMaximumTimeout(),
                    Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0));
            long iCycleTime = Const.toInt(getRealCheckCycleTime(), Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 0));

            //
            // Sanity check on some values, and complain on insanity
            //
            if (iMaximumTimeout < 0) {
                iMaximumTimeout = Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0);
                if (log.isBasic())
                    log.logBasic(toString(), "Maximum timeout invalid, reset to " + iMaximumTimeout);
            }

            if (iCycleTime < 1) {
                // If lower than 1 set to the default
                iCycleTime = Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 1);
                if (log.isBasic())
                    log.logBasic(toString(), "Check cycle time invalid, reset to " + iCycleTime);
            }

            if (iMaximumTimeout == 0) {
                if (log.isBasic())
                    log.logBasic(toString(), "Waiting indefinitely for file [" + realFilename + "]");
            } else {
                if (log.isBasic())
                    log.logBasic(toString(),
                            "Waiting " + iMaximumTimeout + " seconds for file [" + realFilename + "]");
            }

            boolean continueLoop = true;
            while (continueLoop && !parentJob.isStopped()) {
                fileObject = KettleVFS.getFileObject(realFilename);

                if (fileObject.exists()) {
                    // file exists, we're happy to exit
                    if (log.isBasic())
                        log.logBasic(toString(), "Detected file [" + realFilename + "] within timeout");
                    result.setResult(true);
                    continueLoop = false;

                    // add filename to result filenames
                    if (addFilenameToResult && fileObject.getType() == FileType.FILE) {
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                                parentJob.getJobname(), toString());
                        resultFile.setComment(Messages.getString("JobWaitForFile.FilenameAdded"));
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }
                } else {
                    long now = System.currentTimeMillis() / 1000;

                    if ((iMaximumTimeout > 0) && (now > (timeStart + iMaximumTimeout))) {
                        continueLoop = false;

                        // file doesn't exist after timeout, either true or
                        // false
                        if (isSuccessOnTimeout()) {
                            if (log.isBasic())
                                log.logBasic(toString(),
                                        "Didn't detect file [" + realFilename + "] before timeout, success");
                            result.setResult(true);
                        } else {
                            if (log.isBasic())
                                log.logBasic(toString(),
                                        "Didn't detect file [" + realFilename + "] before timeout, failure");
                            result.setResult(false);
                        }
                    }

                    // sleep algorithm
                    long sleepTime = 0;

                    if (iMaximumTimeout == 0) {
                        sleepTime = iCycleTime;
                    } else {
                        if ((now + iCycleTime) < (timeStart + iMaximumTimeout)) {
                            sleepTime = iCycleTime;
                        } else {
                            sleepTime = iCycleTime - ((now + iCycleTime) - (timeStart + iMaximumTimeout));
                        }
                    }

                    try {
                        if (sleepTime > 0) {
                            if (log.isDetailed()) {
                                log.logDetailed(toString(), "Sleeping " + sleepTime
                                        + " seconds before next check for file [" + realFilename + "]");
                            }
                            Thread.sleep(sleepTime * 1000);
                        }
                    } catch (InterruptedException e) {
                        // something strange happened
                        result.setResult(false);
                        continueLoop = false;
                    }
                }
            }

            if (!parentJob.isStopped() && fileObject.exists() && isFileSizeCheck()) {
                long oldSize = -1;
                long newSize = fileObject.getContent().getSize();

                if (log.isDetailed())
                    log.logDetailed(toString(), "File [" + realFilename + "] is " + newSize + " bytes long");
                if (log.isBasic())
                    log.logBasic(toString(), "Waiting until file [" + realFilename + "] stops growing for "
                            + iCycleTime + " seconds");
                while (oldSize != newSize && !parentJob.isStopped()) {
                    try {
                        if (log.isDetailed()) {
                            log.logDetailed(toString(), "Sleeping " + iCycleTime
                                    + " seconds, waiting for file [" + realFilename + "] to stop growing");
                        }
                        Thread.sleep(iCycleTime * 1000);
                    } catch (InterruptedException e) {
                        // something strange happened
                        result.setResult(false);
                        continueLoop = false;
                    }
                    oldSize = newSize;
                    newSize = fileObject.getContent().getSize();
                    if (log.isDetailed()) {
                        log.logDetailed(toString(),
                                "File [" + realFilename + "] is " + newSize + " bytes long");
                    }
                }
                if (log.isBasic())
                    log.logBasic(toString(), "Stopped waiting for file [" + realFilename + "] to stop growing");
            }

            if (parentJob.isStopped()) {
                result.setResult(false);
            }
        } catch (IOException e) {
            log.logBasic(toString(), "Exception while waiting for file [" + realFilename + "] to stop growing: "
                    + e.getMessage());
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (Exception e) {
                }
            }
        }
    } else {
        log.logError(toString(), "No filename is defined.");
    }

    return result;
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

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

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

    try {//from  w w  w  . j a  v a  2s  .  c  om

        // Here gc() is explicitly called if e.g. createfile is used in the
        // same
        // job for the same file. The problem is that after creating the
        // file the
        // file object is not properly garbaged collected and thus the file
        // cannot
        // be deleted anymore. This is a known problem in the JVM.

        System.gc();

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder))
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);

        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 MOVE FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobMoveFiles.Log.Forbidden"),
                            Messages.getString("JobMoveFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

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

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (log.isDetailed()) {
                            log.logDetailed(toString(), "  ");
                            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null)
                                        return false;
                                } catch (Exception ex) {
                                    // Upon error don't process the
                                    // file.
                                    return false;
                                }

                                finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                        } catch (IOException ex) {
                                        }
                                        ;
                                    }

                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        log.logError(toString(), Messages.getString(
                                                "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername,
                                        realWildcard, log, parentJob, result, movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }

                }
                entrystatus = true;
            } // end if
            else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobMoveFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } // end if
        else {
            log.logError(toString(),
                    Messages.getString("JobMoveFiles.Error.SourceFileNotExists", realSourceFilefoldername));
        }
    } // end try
    catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return entrystatus;
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

private boolean MoveOneFile(FileObject Currentfile, FileObject sourcefilefolder,
        String realDestinationFilefoldername, String realWildcard, LogWriter log, Job parentJob, Result result,
        FileObject movetofolderfolder) {
    boolean entrystatus = false;
    FileObject file_name = null;/*from   w  w w. j  a va  2  s.c  o m*/

    try {
        if (!Currentfile.toString().equals(sourcefilefolder.toString())) {
            // Pass over the Base folder itself

            // return destination short filename
            String sourceshortfilename = Currentfile.getName().getBaseName();
            String shortfilename = sourceshortfilename;
            try {
                shortfilename = getDestinationFilename(sourceshortfilename);
            } catch (Exception e) {
                log.logError(toString(),
                        Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                Currentfile.getName().getBaseName(), e.toString())));
                return entrystatus;
            }

            int lenCurrent = sourceshortfilename.length();
            String short_filename_from_basefolder = shortfilename;
            if (!isDoNotKeepFolderStructure())
                short_filename_from_basefolder = Currentfile.toString()
                        .substring(sourcefilefolder.toString().length(), Currentfile.toString().length());
            short_filename_from_basefolder = short_filename_from_basefolder.substring(0,
                    short_filename_from_basefolder.length() - lenCurrent) + shortfilename;

            // Built destination filename
            file_name = KettleVFS.getFileObject(
                    realDestinationFilefoldername + Const.FILE_SEPARATOR + short_filename_from_basefolder);

            if (!Currentfile.getParent().toString().equals(sourcefilefolder.toString())) {

                // Not in the Base Folder..Only if include sub folders
                if (include_subfolders) {
                    // Folders..only if include subfolders
                    if (Currentfile.getType() == FileType.FOLDER) {
                        if (include_subfolders && move_empty_folders && Const.isEmpty(wildcard)) {
                            entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                    log, parentJob, result);
                        }
                    } else {

                        if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                            entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                    log, parentJob, result);
                        }
                    }
                }
            } else {
                // In the Base Folder...
                // Folders..only if include subfolders
                if (Currentfile.getType() == FileType.FOLDER) {
                    if (include_subfolders && move_empty_folders && Const.isEmpty(wildcard)) {
                        entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder, log,
                                parentJob, result);
                    }
                } else {

                    // file...Check if exists
                    if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                        entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder, log,
                                parentJob, result);

                    }
                }

            }

        }
        entrystatus = true;

    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error", e.toString()));
    } finally {
        if (file_name != null) {
            try {
                file_name.close();

            } catch (IOException ex) {
            }
            ;
        }

    }
    return entrystatus;
}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Does a 'cp' command.//from  w  w  w . j  a  va  2 s. c  om
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Does an 'ls' command./* w  w  w  .java 2  s .  com*/
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}