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:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously exist.
 *
 * @param archiveFile//  w w w.j  av  a  2  s .c  om
 * @param targetFolder
 * @throws IOException
 */
public static void extractTo(Resource archiveFile, Resource targetFolder) throws IOException {
    FileSystemManager manager = VFS.getManager();
    String sourceURI = resolveArchiveURI(archiveFile);

    FileObject source = manager.resolveFile(sourceURI);
    if (manager.canCreateFileSystem(source)) {
        source = manager.createFileSystem(source);
    }
    FileObject target = manager
            .createVirtualFileSystem(manager.resolveFile(targetFolder.dir().getAbsolutePath()));

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

From source file:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Compress {@code sourceFolder} to the archive file {@code archiveFile}; both shall previously exist.
 * //www.  ja  v  a  2s  .c  om
 * @param sourceFolder
 * @param archiveFile
 * @throws IOException
 */
public static void compressTo(Resource sourceFolder, Resource archiveFile) throws IOException {
    // See https://commons.apache.org/proper/commons-vfs/filesystems.html
    // for the supported filesystems

    FileSystemManager manager = VFS.getManager();

    FileObject sourceDir = manager
            .createVirtualFileSystem(manager.resolveFile(sourceFolder.dir().getAbsolutePath()));

    try {
        if ("zip".equalsIgnoreCase(FileUtils.getExtension(archiveFile.path()))) {
            // apache VFS does not support ZIP as writable FileSystem

            OutputStream fos = archiveFile.out();

            // Create access to zip.
            ZipOutputStream zos = new ZipOutputStream(fos);

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            fos.close();
        } else {
            // Create access to archive.
            FileObject zipFile = manager.resolveFile(resolveArchiveURI(archiveFile));
            zipFile.createFile();
            ZipOutputStream zos = new ZipOutputStream(zipFile.getContent().getOutputStream());

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            zipFile.close();
            manager.closeFileSystem(zipFile.getFileSystem());
        }
    } finally {
        manager.closeFileSystem(sourceDir.getFileSystem());
    }
}

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

public ImportData resolve(Importer importer) throws IOException {
    // prepare the target
    Directory target = Directory.createNew(importer.getUploadRoot());

    FileSystemManager manager = null;//  www .j  a v  a2s.  co m
    FileObject fo = null;
    try {
        manager = VFS.getManager();

        if (username != null) {
            StaticUserAuthenticator auth = new StaticUserAuthenticator(domain, username, password);
            FileSystemOptions opts = new FileSystemOptions();
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            fo = manager.resolveFile(location, opts);
        } else {
            fo = manager.resolveFile(location);
        }

        target.accept(fo);

    } finally {
        if (fo != null) {
            FileSystem fs = fo.getFileSystem();
            fo.close();
            manager.closeFileSystem(fs);
        }
    }

    return target;
}

From source file:org.horiam.ResourceManager.model.TestObjectToXmlToObject.java

@BeforeClass
public static void start() throws FileSystemException {

    fsManager = VFS.getManager();

    xmls = new HashMap<String, FileObject>();

    populate();/*from   w ww.j  av a2 s.  c om*/
}

From source file:org.hotswap.agent.watch.vfs.WatcherVFS.java

public WatcherVFS() throws FileSystemException {
    fm = new DefaultFileMonitor(new FileListener() {
        @Override/* ww  w .j  ava2s .c  om*/
        public void fileCreated(FileChangeEvent fce) throws Exception {
            callListeners(fce, FileEvent.CREATE);
        }

        @Override
        public void fileDeleted(FileChangeEvent fce) throws Exception {
            callListeners(fce, FileEvent.DELETE);
        }

        @Override
        public void fileChanged(FileChangeEvent fce) throws Exception {
            callListeners(fce, FileEvent.MODIFY);
        }
    });

    fm.setRecursive(true);
    manager = VFS.getManager();
}

From source file:org.jahia.bin.listeners.JahiaContextLoaderListener.java

@Override
public void contextInitialized(ServletContextEvent event) {

    startupTime = System.currentTimeMillis();
    startupWithTrust(Jahia.isEnterpriseEdition() ? (Jahia.getBuildNumber() + "." + Jahia.getEEBuildNumber())
            : String.valueOf(Jahia.getBuildNumber()));

    logger.info("Starting up Digital Experience Manager, please wait...");

    servletContext = event.getServletContext();

    Jahia.setContextPath(servletContext.getContextPath());

    initWebAppRoot();//from  www  .  j  a  v  a  2 s. com

    if (System.getProperty("jahia.config") == null) {
        setSystemProperty("jahia.config", "");
    }
    if (System.getProperty("jahia.license") == null) {
        setSystemProperty("jahia.license", "");
    }

    try {
        // verify supported Java version
        Jahia.verifyJavaVersion(servletContext.getInitParameter("supported_jdk_versions"));
    } catch (JahiaInitializationException e) {
        throw new JahiaRuntimeException(e);
    }

    detectPID(servletContext);

    GroovyPatcher.executeScripts(servletContext, "beforeContextInitializing");

    // initialize VFS file system (solves classloader issue: https://issues.apache.org/jira/browse/VFS-228 )
    try {
        VFS.getManager();
    } catch (FileSystemException e) {
        throw new JahiaRuntimeException(e);
    }

    try {
        long timer = System.currentTimeMillis();
        logger.info("Start initializing Spring root application context");

        running = true;

        super.contextInitialized(event);

        logger.info("Spring Root application context initialized in {} ms",
                (System.currentTimeMillis() - timer));

        // initialize services registry
        ServicesRegistry.getInstance().init();

        // fire Spring event that the root context is initialized
        WebApplicationContext rootCtx = ContextLoader.getCurrentWebApplicationContext();
        rootCtx.publishEvent(new RootContextInitializedEvent(rootCtx));

        if (Jahia.isEnterpriseEdition()) {
            requireLicense();
        }

        boolean isProcessingServer = SettingsBean.getInstance().isProcessingServer();

        // execute patches after root context initialization
        if (isProcessingServer) {
            GroovyPatcher.executeScripts(servletContext, "rootContextInitialized");
        }

        // start OSGi container
        FrameworkService.getInstance().start();

    } catch (JahiaException e) {
        running = false;
        logger.error(e.getMessage(), e);
        throw new JahiaRuntimeException(e);
    } catch (RuntimeException e) {
        running = false;
        throw e;
    } finally {
        JCRSessionFactory.getInstance().closeAllSessions();
    }
}

From source file:org.jahia.modules.external.vfs.factory.VFSMountPointFactoryHandler.java

/**
 * Return the validation state of a vfs mount point
 * @param vfsMountPointFactory/*from   w w w. ja v a 2s  . com*/
 * @return
 */
private boolean validateVFS(VFSMountPointFactory vfsMountPointFactory) {
    try {
        VFS.getManager().resolveFile(vfsMountPointFactory.getRoot());
    } catch (FileSystemException e) {
        logger.warn("VFS mount point " + vfsMountPointFactory.getName() + " has validation problem "
                + e.getMessage());
        return false;
    }
    return true;
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

/**
 * Defines the root point of the DataSource
 *
 * @param rootUri//  w  w  w . ja  v a2  s  . c  o  m
 */
public void setRoot(String rootUri) {
    try {
        manager = VFS.getManager();
        root = manager.resolveFile(rootUri);
        rootPath = root.getName().getPath();
    } catch (Exception e) {
        throw new RuntimeException("Cannot set root to " + rootUri, e);
    }
}

From source file:org.jfunktor.common.vfs.VirtualFileSystem.java

/**
 * Checks whether the given file url is valid or not
 *
 * @param location//from   w  w  w . ja  v  a  2 s .  c om
 * @return
 */
public static boolean exists(URL location) throws FileNotFoundException, VFSException {
    FileObject obj = null;
    try {
        obj = VFS.getManager().resolveFile(location.toString());
        return obj.exists();
    } catch (FileSystemException e) {
        throw new VFSException(e);
    }
}

From source file:org.jfunktor.common.vfs.VirtualFileSystem.java

private static FileObject resolveFile(URL location) {
    FileObject obj = null;//from   ww  w. j  ava2 s.c om
    try {
        obj = VFS.getManager().resolveFile(location.toString());
    } catch (FileSystemException e) {
        e.printStackTrace();
    }
    return obj;
}