Example usage for org.apache.commons.vfs2.impl StandardFileSystemManager toFileObject

List of usage examples for org.apache.commons.vfs2.impl StandardFileSystemManager toFileObject

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.impl StandardFileSystemManager toFileObject.

Prototype

@Override
public FileObject toFileObject(final File file) throws FileSystemException 

Source Link

Document

Converts a local file into a FileObject .

Usage

From source file:com.app.server.SARDeployer.java

public CopyOnWriteArrayList<String> unpack(final FileObject unpackFileObject, final File outputDir,
        StandardFileSystemManager fileSystemManager) throws IOException {
    outputDir.mkdirs();/*from   w  w  w.j av a 2 s .  co  m*/
    URLClassLoader webClassLoader;
    CopyOnWriteArrayList<String> classPath = new CopyOnWriteArrayList<String>();
    final FileObject packFileObject = fileSystemManager
            .resolveFile("jar:" + unpackFileObject.toString() + "!/");
    try {
        FileObject outputDirFileObject = fileSystemManager.toFileObject(outputDir);
        outputDirFileObject.copyFrom(packFileObject, new AllFileSelector());
        FileObject[] libs = outputDirFileObject.findFiles(new FileSelector() {

            public boolean includeFile(FileSelectInfo arg0) throws Exception {
                return arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jar");
            }

            public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
                // TODO Auto-generated method stub
                return true;
            }

        });
        /*String replaceString="file:///"+outputDir.getAbsolutePath().replace("\\","/");
        replaceString=replaceString.endsWith("/")?replaceString:replaceString+"/";*/
        // System.out.println(replaceString);
        for (FileObject lib : libs) {
            // System.out.println(outputDir.getAbsolutePath());
            // System.out.println(jsp.getName().getFriendlyURI());
            classPath.add(lib.getName().getFriendlyURI());
            // System.out.println(relJspName);
        }
    } finally {
        packFileObject.close();
    }
    return classPath;
}

From source file:com.app.server.WarDeployer.java

public Vector<URL> unpack(final FileObject unpackFileObject, final File outputDir,
        StandardFileSystemManager fileSystemManager, ConcurrentHashMap<String, String> jsps)
        throws IOException {
    outputDir.mkdirs();// ww  w  . j ava 2  s  .  co  m
    URLClassLoader webClassLoader;
    Vector<URL> libraries = new Vector<URL>();
    final FileObject packFileObject = fileSystemManager.resolveFile(unpackFileObject.toString());
    try {
        FileObject outputDirFileObject = fileSystemManager.toFileObject(outputDir);
        outputDirFileObject.copyFrom(packFileObject, new AllFileSelector());
        FileObject[] jspFiles = outputDirFileObject.findFiles(new FileSelector() {

            public boolean includeFile(FileSelectInfo arg0) throws Exception {
                return arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jsp")
                        || arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jar");
            }

            public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
                // TODO Auto-generated method stub
                return true;
            }

        });
        String replaceString = "file:///" + outputDir.getAbsolutePath().replace("\\", "/");
        replaceString = replaceString.endsWith("/") ? replaceString : replaceString + "/";
        // System.out.println(replaceString);
        for (FileObject jsplibs : jspFiles) {
            // System.out.println(outputDir.getAbsolutePath());
            // System.out.println(jsp.getName().getFriendlyURI());
            if (jsplibs.getName().getBaseName().endsWith(".jar")) {
                libraries.add(new URL(jsplibs.getName().getFriendlyURI()));
            } else {
                String relJspName = jsplibs.getName().getFriendlyURI().replace(replaceString, "");
                jsps.put(relJspName, relJspName);
            }
            // System.out.println(relJspName);
        }
    } finally {
        packFileObject.close();
    }
    return libraries;
}

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

public static void testVFS(String user, String pwd, String scheme, String host, String file) {

    StandardFileSystemManager fsManager = null;

    try {// ww  w.  j  av a 2s .c  o m
        fsManager = new StandardFileSystemManager();
        fsManager.setLogger(_LOG);

        StaticUserAuthenticator auth = new StaticUserAuthenticator(null, user, pwd);

        fsManager.setConfiguration(ConfigLoader.getInstance().get(Organizer.getVFSProviderConfig()));
        fsManager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
        // fsManager.addProvider("moi", new DefaultLocalFileProvider());
        fsManager.init();

        FileSystemOptions opts = new FileSystemOptions();
        FileSystemConfigBuilder fscb = fsManager.getFileSystemConfigBuilder(scheme);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        System.out.println(fsManager.getProviderCapabilities(scheme));

        if (fscb instanceof FtpFileSystemConfigBuilder) {
            FtpFileSystemConfigBuilder ftpFscb = (FtpFileSystemConfigBuilder) fscb;
            ftpFscb.setUserDirIsRoot(opts, true);
            ftpFscb.setPassiveMode(opts, true);

        }
        if (fscb instanceof HttpFileSystemConfigBuilder) {
            HttpFileSystemConfigBuilder httpFscb = (HttpFileSystemConfigBuilder) fscb;
            httpFscb.setProxyHost(opts, "proxy.cls.fr");
            httpFscb.setProxyPort(opts, 8080);

        }
        if (fscb instanceof SftpFileSystemConfigBuilder) {
            SftpFileSystemConfigBuilder sftpFscb = (SftpFileSystemConfigBuilder) fscb;
            sftpFscb.setUserDirIsRoot(opts, false);

            // TrustEveryoneUserInfo trustEveryoneUserInfo = new TrustEveryoneUserInfo();
            // trustEveryoneUserInfo.promptYesNo("eddfsdfs");
            // sftpFscb.setUserInfo(opts, new TrustEveryoneUserInfo());
            sftpFscb.setTimeout(opts, 5000);
            // SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            // SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

        }
        // FileObject fo =
        // fsManager.resolveFile("ftp://ftp.cls.fr/pub/oceano/AVISO/NRT-SLA/maps/rt/j2/h/msla_rt_j2_err_21564.nc.gz",
        // opts);

        // String uri = String.format("%s://%s/%s", scheme, host, file);
        // String uri = String.format("%s://%s/", scheme, host);
        // FileObject originBase = fsManager.resolveFile(uri, opts);
        // fsManager.setBaseFile(originBase);

        File tempDir = new File("c:/tempVFS");
        // File tempFile = File.createTempFile("AsciiEnvisat", ".txt", tempDir);
        File hostFile = new File(file);
        String fileName = hostFile.getName();
        File newFile = new File(tempDir, fileName);
        newFile.createNewFile();

        DefaultFileReplicator dfr = new DefaultFileReplicator(tempDir);
        fsManager.setTemporaryFileStore(dfr);
        // System.out.println(fsManager.getBaseFile());
        // System.out.println(dfr);
        // System.out.println(fsManager.getTemporaryFileStore());

        // FileObject ff = fsManager.resolveFile("sftp://t:t@CLS-EARITH.pc.cls.fr/AsciiEnvisat.txt",
        // opts);
        String uri = String.format("%s://%s/%s", scheme, host, file);
        FileObject ff = fsManager.resolveFile(uri, opts);
        FileObject dest = fsManager.toFileObject(newFile);

        //ff.getContent().getInputStream();
        dest.copyFrom(ff, Selectors.SELECT_ALL);
        //dest.copyFrom(ff, Selectors.SELECT_ALL);

        //            
        // URL url = ff.getURL();
        //            
        // url.openConnection();
        // URLConnection conn = url.openConnection();
        // InputStream in = conn.getInputStream();
        // in.close();

        // InputStream in = ff.getContent().getInputStream();

    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MotuException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // fsManager.close();
        // fsManager.freeUnusedResources();
    }

}