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

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

Introduction

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

Prototype

public void setTemporaryFileStore(final TemporaryFileStore tempFileStore) throws FileSystemException 

Source Link

Document

Sets the temporary file store to use.

Usage

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

/**
 * This method implements the jar deployer which configures the executor services. 
 * Frequently monitors the deploy directory and configures the executor services map 
 * once the jar is deployed in deploy directory and reconfigures if the jar is modified and 
 * placed in the deploy directory./*from   ww  w .j a  v  a2 s .  co m*/
 */
public void run() {

    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    try {
        fsManager.init();
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    File file = new File(scanDirectory.split(";")[0]);
    File[] files = file.listFiles();
    CopyOnWriteArrayList<String> classList = new CopyOnWriteArrayList<String>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory())
            continue;
        //Long lastModified=(Long) fileMap.get(files[i].getName());
        if (files[i].getName().endsWith(".jar")) {
            String filePath = files[i].getAbsolutePath();
            FileObject jarFile = null;
            try {
                jarFile = fsManager.resolveFile("jar:" + filePath);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //logger.info("filePath"+filePath);
            filePath = filePath.substring(0, filePath.toLowerCase().lastIndexOf(".jar"));
            WebClassLoader customClassLoader = null;
            try {
                URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                URL[] urls = loader.getURLs();
                try {
                    customClassLoader = new WebClassLoader(urls);
                    log.info(customClassLoader.geturlS());
                    customClassLoader.addURL(new URL("file:/" + files[i].getAbsolutePath()));
                    CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList();
                    getUsersJars(new File(libDir), jarList);
                    for (String jarFilePath : jarList)
                        customClassLoader.addURL(new URL("file:/" + jarFilePath.replace("\\", "/")));
                    log.info("deploy=" + customClassLoader.geturlS());
                    this.urlClassLoaderMap.put(scanDirectory + "/" + files[i].getName(), customClassLoader);
                    jarsDeployed.add(files[i].getName());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                log.info(urlClassLoaderMap);
                getChildren(jarFile, classList);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            for (int classCount = 0; classCount < classList.size(); classCount++) {
                String classwithpackage = classList.get(classCount).substring(0,
                        classList.get(classCount).indexOf(".class"));
                classwithpackage = classwithpackage.replace("/", ".");
                log.info("classList:" + classwithpackage.replace("/", "."));
                try {
                    if (!classwithpackage.contains("$")) {
                        Class executorServiceClass = customClassLoader.loadClass(classwithpackage);
                        //log.info("executor class in ExecutorServicesConstruct"+executorServiceClass);
                        //log.info();
                        if (!executorServiceClass.isInterface()) {
                            Annotation[] classServicesAnnot = executorServiceClass.getDeclaredAnnotations();
                            if (classServicesAnnot != null) {
                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                    if (classServicesAnnot[annotcount] instanceof RemoteCall) {
                                        RemoteCall remoteCall = (RemoteCall) classServicesAnnot[annotcount];
                                        //registry.unbind(remoteCall.servicename());
                                        log.info(remoteCall.servicename().trim());
                                        try {
                                            //for(int count=0;count<500;count++){
                                            RemoteInterface reminterface = (RemoteInterface) UnicastRemoteObject
                                                    .exportObject((Remote) executorServiceClass.newInstance(),
                                                            2004);
                                            registry.rebind(remoteCall.servicename().trim(), reminterface);
                                            //}
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }

                        Method[] methods = executorServiceClass.getDeclaredMethods();
                        for (Method method : methods) {
                            Annotation[] annotations = method.getDeclaredAnnotations();
                            for (Annotation annotation : annotations) {
                                if (annotation instanceof ExecutorServiceAnnot) {
                                    ExecutorServiceAnnot executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                                    executorServiceInfo.setMethod(method);
                                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                                    //log.info("method="+executorServiceAnnot.servicename());
                                    //log.info("method info="+executorServiceInfo);
                                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                                    executorServiceMap.put(executorServiceAnnot.servicename(),
                                            executorServiceInfo);
                                }
                            }
                        }

                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            ClassLoaderUtil.closeClassLoader(customClassLoader);
            try {
                jarFile.close();
            } catch (FileSystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fsManager.closeFileSystem(jarFile.getFileSystem());
        }
    }
    fsManager.close();
    fsManager = new StandardFileSystemManager();
    try {
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JarFileListener jarFileListener = new JarFileListener(executorServiceMap, libDir, urlClassLoaderMap,
            jarsDeployed);
    DefaultFileMonitor fm = new DefaultFileMonitor(jarFileListener);
    jarFileListener.setFm(fm);
    FileObject listendir = null;
    String[] dirsToScan = scanDirectory.split(";");
    try {
        FileSystemOptions opts = new FileSystemOptions();
        FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        fsManager.init();
        for (String dir : dirsToScan) {
            if (dir.startsWith("ftp://")) {
                listendir = fsManager.resolveFile(dir, opts);
            } else {
                listendir = fsManager.resolveFile(dir);
            }
            fm.addFile(listendir);
        }
    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    fm.setRecursive(true);
    fm.setDelay(3000);
    fm.start();
    //fsManager.close();
}

From source file:com.web.server.JarDeployer.java

/**
 * This method implements the jar deployer which configures the executor services. 
 * Frequently monitors the deploy directory and configures the executor services map 
 * once the jar is deployed in deploy directory and reconfigures if the jar is modified and 
 * placed in the deploy directory.//from   w  ww  .ja v a  2 s . c o m
 */
public void run() {

    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    try {
        fsManager.init();
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    File file = new File(scanDirectory.split(";")[0]);
    File[] files = file.listFiles();
    CopyOnWriteArrayList<String> classList = new CopyOnWriteArrayList<String>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory())
            continue;
        //Long lastModified=(Long) fileMap.get(files[i].getName());
        if (files[i].getName().endsWith(".jar")) {
            String filePath = files[i].getAbsolutePath();
            FileObject jarFile = null;
            try {
                jarFile = fsManager.resolveFile("jar:" + filePath);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //logger.info("filePath"+filePath);
            filePath = filePath.substring(0, filePath.toLowerCase().lastIndexOf(".jar"));
            WebClassLoader customClassLoader = null;
            try {
                URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                URL[] urls = loader.getURLs();
                try {
                    customClassLoader = new WebClassLoader(urls);
                    System.out.println(customClassLoader.geturlS());
                    new WebServer().addURL(new URL("file:/" + files[i].getAbsolutePath()), customClassLoader);
                    CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList();
                    getUsersJars(new File(libDir), jarList);
                    for (String jarFilePath : jarList)
                        new WebServer().addURL(new URL("file:/" + jarFilePath.replace("\\", "/")),
                                customClassLoader);
                    System.out.println("deploy=" + customClassLoader.geturlS());
                    this.urlClassLoaderMap.put(scanDirectory + "/" + files[i].getName(), customClassLoader);
                    jarsDeployed.add(files[i].getName());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(urlClassLoaderMap);
                getChildren(jarFile, classList);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            for (int classCount = 0; classCount < classList.size(); classCount++) {
                String classwithpackage = classList.get(classCount).substring(0,
                        classList.get(classCount).indexOf(".class"));
                classwithpackage = classwithpackage.replace("/", ".");
                System.out.println("classList:" + classwithpackage.replace("/", "."));
                try {
                    if (!classwithpackage.contains("$")) {
                        Class executorServiceClass = customClassLoader.loadClass(classwithpackage);
                        //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                        //System.out.println();
                        if (!executorServiceClass.isInterface()) {
                            Annotation[] classServicesAnnot = executorServiceClass.getDeclaredAnnotations();
                            if (classServicesAnnot != null) {
                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                    if (classServicesAnnot[annotcount] instanceof RemoteCall) {
                                        RemoteCall remoteCall = (RemoteCall) classServicesAnnot[annotcount];
                                        //registry.unbind(remoteCall.servicename());
                                        System.out.println(remoteCall.servicename().trim());
                                        try {
                                            //for(int count=0;count<500;count++){
                                            RemoteInterface reminterface = (RemoteInterface) UnicastRemoteObject
                                                    .exportObject((Remote) executorServiceClass.newInstance(),
                                                            2004);
                                            registry.rebind(remoteCall.servicename().trim(), reminterface);
                                            //}
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }

                        Method[] methods = executorServiceClass.getDeclaredMethods();
                        for (Method method : methods) {
                            Annotation[] annotations = method.getDeclaredAnnotations();
                            for (Annotation annotation : annotations) {
                                if (annotation instanceof ExecutorServiceAnnot) {
                                    ExecutorServiceAnnot executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                                    executorServiceInfo.setMethod(method);
                                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                                    //System.out.println("method="+executorServiceAnnot.servicename());
                                    //System.out.println("method info="+executorServiceInfo);
                                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                                    executorServiceMap.put(executorServiceAnnot.servicename(),
                                            executorServiceInfo);
                                }
                            }
                        }

                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            ClassLoaderUtil.closeClassLoader(customClassLoader);
            try {
                jarFile.close();
            } catch (FileSystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fsManager.closeFileSystem(jarFile.getFileSystem());
        }
    }
    fsManager.close();
    fsManager = new StandardFileSystemManager();
    try {
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JarFileListener jarFileListener = new JarFileListener(executorServiceMap, libDir, urlClassLoaderMap,
            jarsDeployed);
    DefaultFileMonitor fm = new DefaultFileMonitor(jarFileListener);
    jarFileListener.setFm(fm);
    FileObject listendir = null;
    String[] dirsToScan = scanDirectory.split(";");
    try {
        FileSystemOptions opts = new FileSystemOptions();
        FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        fsManager.init();
        for (String dir : dirsToScan) {
            if (dir.startsWith("ftp://")) {
                listendir = fsManager.resolveFile(dir, opts);
            } else {
                listendir = fsManager.resolveFile(dir);
            }
            fm.addFile(listendir);
        }
    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    fm.setRecursive(true);
    fm.setDelay(3000);
    fm.start();
    //fsManager.close();
}

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 {/*from  w w  w. ja va2s  .  com*/
        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();
    }

}