Example usage for org.apache.commons.vfs2 VFS getManager

List of usage examples for org.apache.commons.vfs2 VFS getManager

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 VFS getManager.

Prototype

public static synchronized FileSystemManager getManager() throws FileSystemException 

Source Link

Document

Returns the default FileSystemManager instance.

Usage

From source file:io.dockstore.common.FileProvisioning.java

public void provisionOutputFile(FileInfo file, String cwlOutputPath) {
    File sourceFile = new File(cwlOutputPath);
    long inputSize = sourceFile.length();
    if (file.getUrl().startsWith("s3://")) {
        AmazonS3 s3Client = FileProvisioning.getAmazonS3Client(config);
        String trimmedPath = file.getUrl().replace("s3://", "");
        List<String> splitPathList = Lists.newArrayList(trimmedPath.split("/"));
        String bucketName = splitPathList.remove(0);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, Joiner.on("/").join(splitPathList),
                sourceFile);/*  w ww .  j  a  va  2 s  . c  o m*/
        putObjectRequest.setGeneralProgressListener(new ProgressListener() {
            ProgressPrinter printer = new ProgressPrinter();
            long runningTotal = 0;

            @Override
            public void progressChanged(ProgressEvent progressEvent) {
                if (progressEvent.getEventType() == ProgressEventType.REQUEST_BYTE_TRANSFER_EVENT) {
                    runningTotal += progressEvent.getBytesTransferred();
                }
                printer.handleProgress(runningTotal, inputSize);
            }
        });
        try {
            s3Client.putObject(putObjectRequest);
        } finally {
            System.out.println();
        }
    } else {
        try {
            FileSystemManager fsManager;
            // trigger a copy from the URL to a local file path that's a UUID to avoid collision
            fsManager = VFS.getManager();
            // check for a local file path
            FileObject dest = fsManager.resolveFile(file.getUrl());
            FileObject src = fsManager.resolveFile(sourceFile.getAbsolutePath());
            copyFromInputStreamToOutputStream(src.getContent().getInputStream(), inputSize,
                    dest.getContent().getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("Could not provision output files", e);
        }
    }
}

From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

@Override
String archive(String fromPath) throws IOException {
    if (archiveURI == null) {
        throw new IOException("No archive directory defined - cannot archive");
    }/* w  ww . j  a v a2 s  .  c om*/

    String toPath = archiveURI.toString() + (fromPath.startsWith("/") ? fromPath.substring(1) : fromPath);

    FileObject toFile = VFS.getManager().resolveFile(toPath, archiveOptions);
    toFile.refresh();
    // Create the toPath's parent dir(s) if they don't exist
    toFile.getParent().createFolder();
    getChild(fromPath).moveTo(toFile);
    toFile.close();
    return toPath;
}

From source file:hadoopInstaller.installation.Installer.java

private void getBundleInstallDirectory(String resource, FileObject file) throws InstallationFatalError {
    getLog().trace("HadoopInstaller.InstallationDirectory.Start", resource); //$NON-NLS-1$
    try {// w  w  w  .ja va2 s  .  com
        String uri = URI.create(file.getName().getURI().replaceFirst("file:", "tgz:")) //$NON-NLS-1$ //$NON-NLS-2$
                .toString();
        FileObject tgzFile;
        tgzFile = VFS.getManager().resolveFile(uri);
        String name = tgzFile.getChildren()[0].getName().getBaseName();
        getDirectories().put(resource, name);
        getLog().debug("HadoopInstaller.InstallationDirectory.Success", //$NON-NLS-1$
                resource, name);
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.InstallationDirectory.Error", //$NON-NLS-1$
                file.getName().getBaseName());
    }
}

From source file:hadoopInstaller.installation.Installer.java

private FileObject getConfigurationSchema() throws InstallationFatalError {
    /*//from  w w w. ja  va 2s .c  om
     * As i can not find a way to load a resource directly using VFS, the
     * resource is written to a temporary VFS file and we return that file.
     */
    try {
        FileObject configurationSchema;
        configurationSchema = VFS.getManager().resolveFile("ram:///" + InstallerConstants.CONFIGURATION_SCHEMA);//$NON-NLS-1$
        try (OutputStream out = configurationSchema.getContent().getOutputStream();
                InputStream in = this.getClass()
                        .getResourceAsStream(InstallerConstants.CONFIGURATION_SCHEMA);) {
            while (in.available() > 0) {
                out.write(in.read());
            }
        }
        return configurationSchema;
    } catch (IOException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.Configure.CouldNotReadFile", //$NON-NLS-1$
                InstallerConstants.CONFIGURATION_SCHEMA);
    }
}

From source file:net.simon04.guavavfs.VirtualFiles.java

/**
 * Returns true if the files contains the same bytes.
 *
 * @throws IOException if an I/O error occurs
 *///  ww w . j ava  2 s.c o m
public static boolean equal(String file1, String file2) throws IOException {
    checkNotNull(file1);
    checkNotNull(file2);
    final FileSystemManager vfs = VFS.getManager();
    if (Objects.equals(file1, file2) || vfs.resolveFile(file1).equals(vfs.resolveFile(file2))) {
        return true;
    }

    /*
     * Some operating systems may return zero as the length for files
     * denoting system-dependent entities such as devices or pipes, in
     * which case we must fall back on comparing the bytes directly.
     */
    long len1 = length(file1);
    long len2 = length(file2);
    if (len1 != 0 && len2 != 0 && len1 != len2) {
        return false;
    }
    return asByteSource(file1).contentEquals(asByteSource(file2));
}

From source file:hadoopInstaller.installation.Installer.java

private void loadConfiguration() throws InstallationFatalError {
    getLog().trace("HadoopInstaller.Configure.Start", //$NON-NLS-1$
            InstallerConstants.CONFIGURATION_FILE);
    String localDirectoryName = System.getProperty("user.dir"); //$NON-NLS-1$
    try {//from w w  w .  j a  v a2s . com
        setLocalDirectory(VFS.getManager().resolveFile(localDirectoryName));
        FileObject configurationFile = getLocalDirectory().resolveFile(InstallerConstants.CONFIGURATION_FILE);

        FileObject configurationSchema = getConfigurationSchema();
        setConfig(InstallerConfigurationParser
                .generateConfigurationFrom(XMLDocumentReader.parse(configurationFile, configurationSchema)));
        try {
            configurationFile.close();
        } catch (FileSystemException ex) {
            getLog().warn(ex, "HadoopInstaller.File.CouldNotClose", //$NON-NLS-1$
                    InstallerConstants.CONFIGURATION_FILE);
        }
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.Configure.CouldNotFindFile", //$NON-NLS-1$
                InstallerConstants.CONFIGURATION_FILE, localDirectoryName);
    } catch (InstallerConfigurationParseError e) {
        throw new InstallationFatalError(e.getCause(), "HadoopInstaller.Configure.CouldNotReadFile", //$NON-NLS-1$
                InstallerConstants.CONFIGURATION_FILE);
    }
    getLog().info("HadoopInstaller.Configure.Success"); //$NON-NLS-1$
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignSource.java

public WGADesign getDesign(final String name) throws WGADesignRetrievalException {
    try {/*w  w w . jav a2s  .  c o  m*/

        // Design archive (obsolete)
        if (name.startsWith(DESIGNNAMEPREFIX_ARCHIVE)) {
            String designName = name.substring(DESIGNNAMEPREFIX_ARCHIVE.length());
            FileObject designFile = _dir.resolveFile(designName + ARCHIVED_DESIGN_EXTENSION);
            if (!designFile.exists()) {
                return null;
            }

            WGADesign design = new WGADesign();
            design.setSource(this);
            design.setName(name);
            design.setTitle(designFile.getName().getBaseName());
            design.setDescription("Design at location " + designFile.getURL().toString());
            return design;
        }

        // Additional directory
        else if (name.startsWith(DESIGNNAMEPREFIX_ADDITIONALDIR)) {
            String designName = name.substring(DESIGNNAMEPREFIX_ADDITIONALDIR.length());
            String dirPath = _additionalDirs.get(designName);
            if (dirPath == null) {
                return null;
            }

            FileObject dir = VFS.getManager().resolveFile((String) dirPath);
            if (!dir.exists() || !dir.getType().equals(FileType.FOLDER)) {
                return null;
            }

            FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(dir);

            // Non-empty directories without syncinfo may not be used as design directories
            if (syncInfo == null && dir.getChildren().length != 0) {
                return null;
            }

            WGADesign design = new WGADesign();
            design.setSource(this);
            design.setName(DESIGNNAMEPREFIX_ADDITIONALDIR + name);
            design.setTitle(dir.getName().getBaseName());
            design.setDescription("Design at location " + dir.getURL().toString());
            design.setConfig(loadConfig(syncInfo));
            design.setOverlayData(loadOverlayData(syncInfo));

            return design;

        }

        // Regular design in configured directory
        else {
            _dir.refresh();
            FileObject child = _dir.resolveFile(name);
            if (!child.exists()) {
                return null;
            }

            FileObject resolvedChild = WGUtils.resolveDirLink(child);
            if (!resolvedChild.getType().equals(FileType.FOLDER)) {
                return null;
            }

            FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(resolvedChild);

            // Non-empty directories without syncinfo may not be used as design directories
            if (syncInfo == null && child.getChildren().length != 0) {
                return null;
            }

            WGADesign design = new WGADesign();
            design.setSource(this);
            design.setName(child.getName().getBaseName());
            design.setTitle(child.getName().getBaseName());
            design.setDescription("Design at location " + child.getURL().toString());
            design.setConfig(loadConfig(syncInfo));
            design.setOverlayData(loadOverlayData(syncInfo));
            return design;
        }
    } catch (FileSystemException e) {
        throw new WGADesignRetrievalException("Exception retrieving file system designs", e);
    }
}

From source file:fi.mystes.synapse.mediator.vfs.VfsFileTransferUtility.java

/**
 * Helper method to resolve file by given path.
 * /*from w  w w  .  j av  a 2  s. c o  m*/
 * @param path
 *            To resolve file with
 * @return Resolved file
 * @throws FileSystemException
 *             If resolving file fails
 */
private FileObject resolveFile(String path) throws FileSystemException {
    return VFS.getManager().resolveFile(path, fsOptions);
}

From source file:cz.lbenda.dataman.db.DbStructureFactory.java

public static void loadDatabaseStructureFromXML(DatabaseStructureType databaseStructure, DbConfig dbConfig) {
    if (databaseStructure == null) {
        return;//from  w  w w  .jav a  2 s.  c  o m
    }
    if (!StringUtils.isBlank(databaseStructure.getSrc())) {
        new Thread(() -> {
            try {
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile((FileObject) null, databaseStructure.getSrc());
                InputStream is = fileObject.getContent().getInputStream();
                loadDatabaseStructureFromXML(is, dbConfig);
            } catch (FileSystemException e) {
                LOG.error("Problem with getting FS manager or read data from file", e);
                ExceptionMessageFrmController
                        .showException("Problem with getting FS manager or read data from file", e);
            }
        }).start();
    } else {
        new Thread(() -> {
            dbConfig.getCatalogs().clear();
            Map<Object, TableDesc> tableDescFromTableType = new HashMap<>();
            Map<Object, ColumnDesc> columnDescFromColumnType = new HashMap<>();

            databaseStructure.getCatalog().forEach(catalogType -> {
                CatalogDesc catalogDesc = new CatalogDesc(catalogType.getName());
                dbConfig.getCatalogs().add(catalogDesc);
                catalogType.getSchema().forEach(schemaType -> {
                    SchemaDesc schemaDesc = new SchemaDesc(catalogDesc, schemaType.getName());
                    catalogDesc.getSchemas().add(schemaDesc);
                    schemaType.getTable().forEach(tableType -> {
                        TableDesc tableDesc = new TableDesc(schemaDesc, tableType.getTableType(),
                                tableType.getName());
                        tableDesc.setDbConfig(dbConfig);
                        tableDescFromTableType.put(tableType, tableDesc);
                        schemaDesc.getTables().add(tableDesc);
                        tableType.getColumn().forEach(columnType -> {
                            ColumnDesc columnDesc = new ColumnDesc(tableDesc, columnType.getName(),
                                    columnType.getLabel(), dataTypeTypeToColumnType(columnType.getDataType()),
                                    columnType.getSize(), columnType.getScale(), columnType.isNullable(),
                                    columnType.isAutoincrement(), columnType.isGenerated(),
                                    columnType.getDefaultValue());
                            columnDescFromColumnType.put(columnType, columnDesc);
                            columnDesc.setPosition(tableDesc.getColumns().size() + 1);
                            columnDesc.setPK(columnType.isIsPK());
                            tableDesc.getColumns().add(columnDesc);
                        });
                    });
                });
            });

            databaseStructure.getCatalog()
                    .forEach(catalogType -> catalogType.getSchema().forEach(schemaType -> schemaType.getTable()
                            .forEach(tableType -> tableType.getForeignKey().forEach(foreignKeyType -> {
                                ForeignKey foreignKey = new ForeignKey(foreignKeyType.getName(),
                                        tableDescFromTableType.get(foreignKeyType.getMasterTable()),
                                        columnDescFromColumnType
                                                .get(foreignKeyType.getMasterColumn().get(0).getColumn()),
                                        tableDescFromTableType.get(tableType),
                                        columnDescFromColumnType
                                                .get(foreignKeyType.getSlaveColumn().get(0).getColumn()),
                                        foreignKeyType.getUpdateRule(), foreignKeyType.getDeleteRule());
                                foreignKey.getMasterTable().getForeignKeys().add(foreignKey);
                                foreignKey.getSlaveTable().getForeignKeys().add(foreignKey);
                            }))));
        }).run();
    }
}

From source file:fr.cls.atoll.motu.library.misc.ftp.TestFtp.java

public static void testFtp() {

    // System.setProperty("http.proxyHost", "proxy.cls.fr"); // adresse IP
    // System.setProperty("http.proxyPort", "8080");
    // System.setProperty("socksProxyHost", "proxy.cls.fr");
    // System.setProperty("socksProxyPort", "1080");

    try {/*from   w ww  . j  a  v  a  2  s  .c  o m*/

        // String user = "anonymous";
        // String pass = "email";
        // String server = "ftp.cls.fr";
        //
        // FTPClient client = new FTPClient();
        // client.connect(server);
        // System.out.print(client.getReplyString());
        // int reply = client.getReplyCode();
        // if (!FTPReply.isPositiveCompletion(reply))
        // {
        // throw new IllegalArgumentException("cant connect: " + reply);
        // }
        // if (!client.login(user, pass))
        // {
        // throw new IllegalArgumentException("login failed");
        // }
        // client.enterLocalPassiveMode();
        // client.disconnect();

        FileSystemOptions opts = new FileSystemOptions();
        // FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

        String server = "proxy.cls.fr";
        String user = "anonymous@ftp.unidata.ucar.edu";
        String pass = "";

        server = "ftp.cls.fr";
        user = "anonymous";
        pass = "email";

        server = "proxy-bureautique.cls.fr";
        user = "misgw@ftp.cmcc.it";
        pass = "myo2010";

        StaticUserAuthenticator auth = new StaticUserAuthenticator(null, user, pass);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        FileObject fo = VFS.getManager().resolveFile(
                "ftp://proxy-bureautique.cls.fr/GLOBAL_REANALYSIS_PHYS_001_004_a/global-reanalysis-phys-001-004-a-ran-it-cglors-icemod//global-reanalysis-phys-001-004-a-ran-it-cglors-icemod_20090101_20091231_20110331.nc",
                opts);

        FTPClient ftpClient = FtpClientFactory.createConnection(server, 21, user.toCharArray(),
                pass.toCharArray(), ".", opts);
    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        // } catch (SocketException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}