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

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

Introduction

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

Prototype

public FileObject resolveFile(String path) throws FileSystemException;

Source Link

Document

Finds a file, relative to this file.

Usage

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

/**
 * "Deploys" the plugin jar file.//from w w w  .ja  v  a  2 s .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:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

@Test
public void testCaching3() throws Exception {
    FileSystemManager manager = VFS.getManager();
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/test";
    FileObject scratchFolder = manager.resolveFile(testFolder);

    // releaseable
    FileObject dir1 = scratchFolder.resolveFile("file1.txt");

    // avoid cache removal
    FileObject dir2 = scratchFolder.resolveFile("file2.txt");
    dir2.getContent();//from   w  w w.  j a  v a  2s .  c o  m

    // check if the cache still holds the right instance
    FileObject dir2_2 = scratchFolder.resolveFile("file2.txt");
    assertTrue(dir2 == dir2_2);

    // check if the cache still holds the right instance
    /* FileObject dir1_2 = scratchFolder.resolveFile("file1.txt");
     assertFalse(dir1 == dir1_2);*/
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

/**
 * ? ? , ?  ??  ?? ?/*from  w  w w  .  j a v a2  s  .c  o  m*/
 * ?  ? ? ? .
 * @throws Exception
 */
@Test
public void testCaching() throws Exception {
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/testfolder";
    FileSystemManager manager = VFS.getManager();

    FileObject scratchFolder = manager.resolveFile(testFolder);

    // testfolder ?  ? 
    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    // ? Manager ?
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

    // zip, jar, tgz, tar, tbz2, file
    if (!fs.hasProvider("file")) {
        fs.addProvider("file", new DefaultLocalFileProvider());
    }

    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();

    // ? ? ?
    FileObject foBase2 = fs.resolveFile(testFolder);
    log.debug("## scratchFolder.getName().getPath() : " + scratchFolder.getName().getPath());

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    // ??  ?
    FileObject[] fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // ??
    scratchFolder.resolveFile("file1.txt").createFile();

    // ? 
    // BUT cachedFolder ? ??  ?
    fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // 
    cachedFolder.refresh();
    // ?? 
    fos = cachedFolder.getChildren();
    assertTrue(contains(fos, "file1.txt"));

}

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

private String[] getLibs(FileObject pluginLocation) throws IOException {
    File[] jars = new File(pluginLocation.getURL().getFile()).listFiles(new JarNameFilter());

    String[] libs = new String[jars.length];
    for (int i = 0; i < jars.length; i++)
        libs[i] = jars[i].getPath();//from ww  w.  ja  v a2  s.co  m

    Arrays.sort(libs);
    int idx = Arrays.binarySearch(libs, DEFAULT_LIB);

    String[] retVal = null;

    if (idx < 0) // does not contain
    {
        String[] completeLib = new String[libs.length + 1];
        System.arraycopy(libs, 0, completeLib, 0, libs.length);
        completeLib[libs.length] = pluginLocation.resolveFile(DEFAULT_LIB).getURL().getFile();
        retVal = completeLib;
    } else
        retVal = libs;

    return retVal;
}

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

private void fromXML(FileObject xml, FileObject parent)
        throws IOException, ClassNotFoundException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(KettleVFS.getInputStream(xml));
    Node plugin = XMLHandler.getSubNode(doc, Plugin.PLUGIN);
    String id = XMLHandler.getTagAttribute(plugin, Plugin.ID);
    String description = XMLHandler.getTagAttribute(plugin, Plugin.DESCRIPTION);
    String iconfile = XMLHandler.getTagAttribute(plugin, Plugin.ICONFILE);
    String tooltip = XMLHandler.getTagAttribute(plugin, Plugin.TOOLTIP);
    String classname = XMLHandler.getTagAttribute(plugin, Plugin.CLASSNAME);
    String category = XMLHandler.getTagAttribute(plugin, Plugin.CATEGORY);
    String errorHelpfile = XMLHandler.getTagAttribute(plugin, Plugin.ERRORHELPFILE);

    // Localized categories
    ///*w w  w .  j  av a 2s .c o m*/
    Node locCatsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_CATEGORY);
    int nrLocCats = XMLHandler.countNodes(locCatsNode, Plugin.CATEGORY);
    Map<String, String> localizedCategories = new Hashtable<String, String>();
    for (int j = 0; j < nrLocCats; j++) {
        Node locCatNode = XMLHandler.getSubNodeByNr(locCatsNode, Plugin.CATEGORY, j);
        String locale = XMLHandler.getTagAttribute(locCatNode, Plugin.LOCALE);
        String locCat = XMLHandler.getNodeValue(locCatNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locCat)) {
            localizedCategories.put(locale.toLowerCase(), locCat);
        }
    }

    // Localized descriptions
    //
    Node locDescsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_DESCRIPTION);
    int nrLocDescs = XMLHandler.countNodes(locDescsNode, Plugin.DESCRIPTION);
    Map<String, String> localizedDescriptions = new Hashtable<String, String>();
    for (int j = 0; j < nrLocDescs; j++) {
        Node locDescNode = XMLHandler.getSubNodeByNr(locDescsNode, Plugin.DESCRIPTION, j);
        String locale = XMLHandler.getTagAttribute(locDescNode, Plugin.LOCALE);
        String locDesc = XMLHandler.getNodeValue(locDescNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locDesc)) {
            localizedDescriptions.put(locale.toLowerCase(), locDesc);
        }
    }

    // Localized tooltips
    //
    Node locTipsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_TOOLTIP);
    int nrLocTips = XMLHandler.countNodes(locTipsNode, Plugin.TOOLTIP);
    Map<String, String> localizedTooltips = new Hashtable<String, String>();
    for (int j = 0; j < nrLocTips; j++) {
        Node locTipNode = XMLHandler.getSubNodeByNr(locTipsNode, Plugin.TOOLTIP, j);
        String locale = XMLHandler.getTagAttribute(locTipNode, Plugin.LOCALE);
        String locTip = XMLHandler.getNodeValue(locTipNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locTip)) {
            localizedTooltips.put(locale.toLowerCase(), locTip);
        }
    }

    Node libsnode = XMLHandler.getSubNode(plugin, Plugin.LIBRARIES);
    int nrlibs = XMLHandler.countNodes(libsnode, Plugin.LIBRARY);
    String jarfiles[] = new String[nrlibs];
    for (int j = 0; j < nrlibs; j++) {
        Node libnode = XMLHandler.getSubNodeByNr(libsnode, Plugin.LIBRARY, j);
        String jarfile = XMLHandler.getTagAttribute(libnode, Plugin.NAME);
        jarfiles[j] = parent.resolveFile(jarfile).getURL().getFile();
        // System.out.println("jar files=" + jarfiles[j]);
    }

    // convert to URL
    List<URL> classpath = new ArrayList<URL>();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new FileSystemResourceLoader());
    for (int i = 0; i < jarfiles.length; i++) {
        try {
            Resource[] paths = resolver.getResources(jarfiles[i]);
            for (Resource path : paths) {
                classpath.add(path.getURL());
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        }
    }

    URL urls[] = classpath.toArray(new URL[classpath.size()]);

    URLClassLoader cl = new PDIClassLoader(urls, Thread.currentThread().getContextClassLoader());

    String iconFilename = parent.resolveFile(iconfile).getURL().getFile();

    Class<?> pluginClass = cl.loadClass(classname);

    // here we'll have to use some reflection in order to decide
    // which object we should instantiate!
    if (JobEntryInterface.class.isAssignableFrom(pluginClass)) {
        Set<JobPlugin> jps = (Set<JobPlugin>) this.plugins.get(Job.class);

        JobPlugin plg = new JobPlugin(Plugin.TYPE_PLUGIN, id, description, tooltip, parent.getName().getURI(),
                jarfiles, iconFilename, classname, category);
        plg.setClassLoader(cl);

        // Add localized information too...
        plg.setLocalizedCategories(localizedCategories);
        plg.setLocalizedDescriptions(localizedDescriptions);
        plg.setLocalizedTooltips(localizedTooltips);

        jps.add(plg);
    } else {
        String errorHelpFileFull = errorHelpfile;
        String path = parent.getName().getURI();
        if (!Const.isEmpty(errorHelpfile))
            errorHelpFileFull = (path == null) ? errorHelpfile : path + Const.FILE_SEPARATOR + errorHelpfile;

        StepPlugin sp = new StepPlugin(Plugin.TYPE_PLUGIN, new String[] { id }, description, tooltip, path,
                jarfiles, iconFilename, classname, category, errorHelpFileFull);

        // Add localized information too...
        sp.setLocalizedCategories(localizedCategories);
        sp.setLocalizedDescriptions(localizedDescriptions);
        sp.setLocalizedTooltips(localizedTooltips);

        Set<StepPlugin> sps = (Set<StepPlugin>) this.plugins.get(Step.class);
        sps.add(sp);
    }

}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Does a 'cp' command./* w  ww . j a v a2s  .com*/
 */
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.codehaus.cargo.container.spi.configuration.StandaloneConfigurationTest.java

/**
 * Test the creation of a config directory when the target directory does not exist yet.
 * @throws Exception If anything goes wrong.
 *///from ww w  .j a  va  2 s .c o  m
public void testCreateConfigDirWhenDirectoryDoesNotExist() throws Exception {
    String configDir = "ram:///cargo/testCreateConfigDirWhenDirectoryDoesNotExist";

    FileObject configDirObject = VFS.getManager().resolveFile(configDir);
    FileObject timestampFileObject = configDirObject.resolveFile(".cargo");

    configDirObject.delete(new AllFileSelector());

    TestableAbstractStandaloneConfiguration configuration = new TestableAbstractStandaloneConfiguration(
            configDir);
    configuration.setFileHandler(new VFSFileHandler());
    configuration.setupConfigurationDir();

    assertTrue("Config dir should have been created", configDirObject.exists());
    assertTrue("Cargo timestamp should have existed", timestampFileObject.exists());
}

From source file:org.codehaus.cargo.container.spi.configuration.StandaloneConfigurationTest.java

/**
 * Test the creation of a config directory when the target directory exists and is empty.
 * @throws Exception If anything goes wrong.
 *//*  w w w .  j  a va  2s .  com*/
public void testCreateConfigDirWhenDirectoryExistButIsEmpty() throws Exception {
    String configDir = "ram:///cargo/testCreateConfigDirWhenDirectoryExistButIsEmpty";

    FileObject configDirObject = VFS.getManager().resolveFile(configDir);
    FileObject timestampFileObject = configDirObject.resolveFile(".cargo");

    configDirObject.createFolder();

    TestableAbstractStandaloneConfiguration configuration = new TestableAbstractStandaloneConfiguration(
            configDir);
    configuration.setFileHandler(new VFSFileHandler());
    configuration.setupConfigurationDir();

    assertTrue("Cargo timestamp should have existed", timestampFileObject.exists());
}

From source file:org.codehaus.cargo.container.spi.configuration.StandaloneConfigurationTest.java

/**
 * Test the creation of a config directory when the target directory exists and is not empty.
 * @throws Exception If anything goes wrong.
 */// w ww . j a v  a 2s .  co m
public void testCreateConfigDirWhenDirectoryNotEmpty() throws Exception {
    String configDir = "ram:///cargo/testCreateConfigDirWhenDirectoryNotEmpty";

    FileObject configDirObject = VFS.getManager().resolveFile(configDir);
    configDirObject.resolveFile("somefile").createFile();

    TestableAbstractStandaloneConfiguration configuration = new TestableAbstractStandaloneConfiguration(
            configDir);
    configuration.setFileHandler(new VFSFileHandler());

    try {
        configuration.setupConfigurationDir();
        fail("Should have thrown a ContainerException as the directory is not empty");
    } catch (ContainerException expected) {
        assertEquals("Invalid configuration dir "
                + "[ram:///cargo/testCreateConfigDirWhenDirectoryNotEmpty]. When using standalone "
                + "configurations, the configuration dir must point to an empty directory. Note "
                + "that everything in that dir will get deleted by Cargo.", expected.getMessage());
    }
}

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

private static UnixFsObject.Directory createDirectory(String path, FileObject files,
        FileAttributes directoryAttributes) {
    try {/*from  w w  w  .j a  v a  2 s.  com*/
        return AssemblyOperationUtil.dirFromFileObject(relativePath(path), files.resolveFile(path),
                directoryAttributes);
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}