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

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

Introduction

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

Prototype

public void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException;

Source Link

Document

Copies another file, and all its descendents, to this file.

Usage

From source file:com.panet.imeta.core.plugins.PluginLoader.java

/**
 * "Deploys" the plugin jar file./*  w  w  w  . j  a  va2s  . c  o  m*/
 * 
 * @param parent
 * @return
 * @throws FileSystemException
 */
private FileObject explodeJar(FileObject parent) throws FileSystemException {
    // By Alex, 7/13/07
    // Since the JVM does not support nested jars and
    // URLClassLoaders, we have to hack it
    // see
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4735639
    // 
    // We do so by exploding the jar, sort of like deploying it
    FileObject dest = VFS.getManager().resolveFile(Const.getKettleDirectory() + File.separator + WORK_DIR);
    dest.createFolder();

    FileObject destFile = dest.resolveFile(parent.getName().getBaseName());

    if (!destFile.exists())
        destFile.createFolder();
    else
        // delete children
        for (FileObject child : destFile.getChildren())
            child.delete(new AllFileSelector());

    // force VFS to treat it as a jar file explicitly with children,
    // etc. and copy
    destFile.copyFrom(!(parent instanceof JarFileObject)
            ? VFS.getManager().resolveFile(JAR + ":" + parent.getName().getURI())
            : parent, new AllFileSelector());

    return destFile;
}

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();//w w  w .j a v a 2 s . c o m
    list_add_result.clear();

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

    try {

        // 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:org.apache.commons.vfs.example.Shell.java

/**
 * Does a 'cp' command.// www  . j a  v a2  s  .  c  o m
 */
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.bibalex.gallery.storage.BAGStorage.java

public static void copyFile(String sourceUrlStr, String targetUrlsStr) throws BAGException {
    try {/*from ww w . j a  v  a 2 s.co m*/
        FileObject targetFO = VFS.getManager().resolveFile(targetUrlsStr);
        FileObject sourceFO = VFS.getManager().resolveFile(sourceUrlStr);

        targetFO.copyFrom(sourceFO, new AllFileSelector());
    } catch (FileSystemException e) {
        throw new BAGException(e);
    }
}

From source file:org.codehaus.mojo.unix.core.FsFileCollector.java

private Callable packageFile(final FileObject from, final UnixFsObject.RegularFile to) {
    return new Callable() {
        public Object call() throws Exception {
            FileObject toFile = root.resolveFile(to.path.string);

            toFile.getParent().createFolder();
            toFile.copyFrom(from, Selectors.SELECT_SELF);
            toFile.getContent().setLastModifiedTime(to.lastModified.toDateTime().toDate().getTime());
            return Unit.unit();
        }/*from w w  w . j a v a 2s.  c  o  m*/
    };
}

From source file:org.codehaus.mojo.unix.maven.sysvpkg.PkgUnixPackage.java

public FileObject fromFile(final FileObject fromFile, UnixFsObject.RegularFile file)
        throws FileSystemException {
    // If it is a file on the local file system, just point the entry in the prototype file to it
    if (fromFile.getFileSystem() instanceof LocalFileSystem) {
        return fromFile;
    }/*from w  w  w. j  a  v a  2s. c o  m*/

    // Creates a file under the working directory that should match the destination path
    final FileObject tmpFile = workingDirectory.resolveFile(file.path.string);

    operations = operations.cons(new Callable() {
        public Object call() throws Exception {
            OutputStream outputStream = null;
            try {
                tmpFile.getParent().createFolder();
                tmpFile.copyFrom(fromFile, Selectors.SELECT_ALL);
                tmpFile.getContent().setLastModifiedTime(fromFile.getContent().getLastModifiedTime());
            } finally {
                IOUtil.close(outputStream);
            }

            return Unit.unit();
        }
    });

    return tmpFile;
}

From source file:org.efaps.webdav4vfs.handler.CopyHandler.java

@Override()
protected void copyOrMove(final FileObject object, final FileObject target, final int depth)
        throws FileSystemException {
    target.copyFrom(object, new FileSelector() {
        public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
            return fileSelectInfo.getDepth() <= depth;
        }//from   w w  w .  ja va  2s.  c  om

        public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
            return fileSelectInfo.getDepth() < depth;
        }
    });
}

From source file:org.geoserver.importer.VFSWorker.java

/**
 * Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously
 * exist./*from  w ww.j  av a 2 s  .  c  o m*/
 */
public void extractTo(File archiveFile, File targetFolder) throws IOException {

    FileSystemManager manager = VFS.getManager();
    String sourceURI = resolveArchiveURI(archiveFile);
    // String targetURI = resolveArchiveURI(targetFolder);
    FileObject source = manager.resolveFile(sourceURI);
    if (manager.canCreateFileSystem(source)) {
        source = manager.createFileSystem(source);
    }
    FileObject target = manager.createVirtualFileSystem(manager.resolveFile(targetFolder.getAbsolutePath()));

    FileSelector selector = new AllFileSelector() {
        @Override
        public boolean includeFile(FileSelectInfo fileInfo) {
            LOGGER.fine("Uncompressing " + fileInfo.getFile().getName().getFriendlyURI());
            return true;
        }
    };
    target.copyFrom(source, selector);
}

From source file:org.jbpm.form.builder.services.impl.fs.FSMenuService.java

protected void writeToURL(URL url, String json) throws FileNotFoundException, IOException {
    if (url.toExternalForm().startsWith("vfs")) {
        FileObject to = VFS.getManager().resolveFile(url.toExternalForm());
        File tmpFile = File.createTempFile("xxFilexx", ".json");
        FileUtils.writeStringToFile(tmpFile, json);
        FileObject from = VFS.getManager().toFileObject(tmpFile);
        to.copyFrom(from, new AllFileSelector());
        FileUtils.deleteQuietly(tmpFile);
    } else {/*from w  ww. j a v a2 s . c om*/
        FileUtils.writeStringToFile(FileUtils.toFile(url), json);
    }
}

From source file:org.jclouds.vfs.tools.blobstore.BlobStoreShell.java

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

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

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