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

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

Introduction

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

Prototype

public URL getURL() throws FileSystemException;

Source Link

Document

Returns a URL representing this file.

Usage

From source file:com.pongasoft.util.io.IOUtils.java

/**
 * Wraps the provided file object within a jar file object
 * (ex: <code>createJarFileObject(file:///tmp/foo.jar)</code> will return
 * <code>jar:file:///tmp.foo.jar!/</code>
 *
 * @param fileObject the orginal jar file
 * @return the wrapped file object (note that it the orignial object does not exists, it
 *         is simply returned (as wrapping it throws an exception...)
 * @throws IOException if there is something wrong
 *///from   w  w w . ja v a 2s .  c o m
public static FileObject createJarFileObject(FileObject fileObject) throws IOException {
    if (fileObject == null)
        return null;

    if (fileObject.exists()) {
        FileSystemManager fsm = fileObject.getFileSystem().getFileSystemManager();
        return fsm.resolveFile("jar:" + fileObject.getURL() + "!/");
    } else
        return fileObject;
}

From source file:functionalTests.multiprotocol.TestVFSProviderMultiProtocol.java

/**
 * Testing the File server deployment using multi-protocol
 * @throws Exception//  w ww . j  ava  2  s.  c o  m
 */
@Test
public void testVFSProviderMP() throws Exception {

    logger.info("**************** Testing deploying dataspace server with protocol list : " + protocolsToTest);

    CentralPAPropertyRepository.PA_COMMUNICATION_PROTOCOL.setValue(protocolsToTest.get(0));
    String add_str = protocolsToTest.get(1);
    for (int i = 2; i < protocolsToTest.size(); i++) {
        add_str += "," + protocolsToTest.get(i);
    }
    CentralPAPropertyRepository.PA_COMMUNICATION_ADDITIONAL_PROTOCOLS.setValue(add_str);

    FileSystemServerDeployer deployer = new FileSystemServerDeployer("space name",
            SERVER_PATH.getAbsolutePath(), true);

    try {

        String[] urls = deployer.getVFSRootURLs();
        logger.info("Received urls :" + Arrays.asList(urls));
        Assert.assertEquals(
                "Number of urls of the FileSystemServerDeployer should match the number of protocols + the file protocol",
                protocolsToTest.size() + 1, urls.length);

        // check the file server uris

        URI receiveduri = new URI(urls[0]);
        Assert.assertEquals("protocol of first uri " + receiveduri + " should be file", "file",
                receiveduri.getScheme());

        for (int i = 1; i < urls.length; i++) {
            receiveduri = new URI(urls[i]);
            Assert.assertEquals("protocol of uri " + urls[i] + " should match the expected protocol",
                    "pap" + protocolsToTest.get(i - 1), receiveduri.getScheme());
        }

        // use the file server
        for (int i = 0; i < urls.length; i++) {
            File f = new File(System.getProperty("java.io.tmpdir"), "testfile_" + i);
            f.createNewFile();
            logger.info("Trying to use : " + urls[i]);
            FileObject source = fileSystemManager.resolveFile(f.toURI().toURL().toExternalForm());
            FileObject dest = fileSystemManager.resolveFile(urls[i] + "/" + f.getName());
            dest.copyFrom(source, Selectors.SELECT_SELF);
            Assert.assertTrue("Copy successful of " + source.getURL() + " to " + dest.getURL(), dest.exists());

        }

    } finally {
        deployer.terminate();
    }

}

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();/*ww  w .  jav  a  2  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.fer.hr.service.datasource.ClassPathResourceDatasourceManager.java

private void setPath(String path) {

    FileSystemManager fileSystemManager;
    try {/*w w  w.  j  a v  a 2 s  .  c om*/
        fileSystemManager = VFS.getManager();

        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        repoURL = fileObject.getURL();
        if (repoURL == null) {
            throw new Exception("Cannot load connection repository from path: " + path);
        } else {
            load();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

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

private void build(FileObject parent, boolean isJar) throws KettleConfigException {
    try {/*from  w ww .  j  a  va 2 s. co m*/
        FileObject xml = null;

        if (isJar) {
            FileObject exploded = explodeJar(parent);

            // try reading annotations first ...
            ResolverUtil<Plugin> resPlugins = new ResolverUtil<Plugin>();

            // grab all jar files not part of the "lib"
            File fparent = new File(exploded.getURL().getFile());
            File[] files = fparent.listFiles(new JarNameFilter());

            URL[] classpath = new URL[files.length];
            for (int i = 0; i < files.length; i++)
                classpath[i] = files[i].toURI().toURL();

            ClassLoader cl = new PDIClassLoader(classpath, Thread.currentThread().getContextClassLoader());
            resPlugins.setClassLoader(cl);

            for (FileObject couldBeJar : exploded.getChildren()) {
                if (couldBeJar.getName().getExtension().equals(JAR))
                    resPlugins.loadImplementationsInJar(Const.EMPTY_STRING, couldBeJar.getURL(),
                            tests.values().toArray(new ResolverUtil.Test[2]));
            }

            for (Class<? extends Plugin> match : resPlugins.getClasses()) {
                for (Class<? extends Annotation> cannot : tests.keySet()) {
                    Annotation annot = match.getAnnotation(cannot);
                    if (annot != null)
                        fromAnnotation(annot, exploded, match);
                }

            }

            // and we also read from the xml if present
            xml = exploded.getChild(Plugin.PLUGIN_XML_FILE);

            if (xml == null || !xml.exists())
                return;

            parent = exploded;

        } else
            xml = parent.getChild(Plugin.PLUGIN_XML_FILE);

        // then read the xml if it is there
        if (xml != null && xml.isReadable())
            fromXML(xml, parent);

    } catch (Exception e) {
        throw new KettleConfigException(e);
    }

    // throw new KettleConfigException("Unable to read plugin.xml from " +
    // parent);
}

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

private void fromAnnotation(Annotation annot, FileObject directory, Class<?> match) throws IOException {
    Class type = annot.annotationType();

    if (type == Job.class) {
        Job jobAnnot = (Job) annot;//from  w  w w. j  a  v  a 2s . c o m
        String[] libs = getLibs(directory);
        Set<JobPlugin> jps = (Set<JobPlugin>) this.plugins.get(Job.class);
        JobPlugin pg = new JobPlugin(Plugin.TYPE_PLUGIN, jobAnnot.id(), jobAnnot.type().getDescription(),
                jobAnnot.tooltip(), directory.getURL().getFile(), libs, jobAnnot.image(), match.getName(),
                jobAnnot.categoryDescription());

        pg.setClassLoader(match.getClassLoader());

        jps.add(pg);

    } else if (type == Step.class) {
        Step jobAnnot = (Step) annot;
        String[] libs = getLibs(directory);
        Set<StepPlugin> jps = (Set<StepPlugin>) this.plugins.get(Step.class);
        StepPlugin pg = new StepPlugin(Plugin.TYPE_PLUGIN, jobAnnot.name(), jobAnnot.description(),
                jobAnnot.tooltip(), directory.getURL().getFile(), libs, jobAnnot.image(), match.getName(),
                jobAnnot.categoryDescription(), Const.EMPTY_STRING);

        pg.setClassLoader(match.getClassLoader());

        jps.add(pg);
    }
}

From source file:com.panet.imeta.trans.steps.mail.Mail.java

private void addAttachedFilePart(FileObject file) throws Exception {
    // create a data source

    MimeBodyPart files = new MimeBodyPart();
    // create a data source
    URLDataSource fds = new URLDataSource(file.getURL());
    // get a data Handler to manipulate this file type;
    files.setDataHandler(new DataHandler(fds));
    // include the file in the data source
    files.setFileName(file.getName().getBaseName());
    // add the part with the file in the BodyPart();
    data.parts.addBodyPart(files);//w ww.  j  av  a  2  s. c o m
    if (log.isDetailed())
        log.logDetailed(toString(), Messages.getString("Mail.Log.AttachedFile", fds.getName()));

}

From source file:net.sf.vfsjfilechooser.plaf.metal.MetalVFSFileChooserUI.java

private void doFileSelectionModeChanged(PropertyChangeEvent e) {
    if (fileNameLabel != null) {
        populateFileNameLabel();/*from ww w  .  ja va2 s . c  o  m*/
    }

    clearIconCache();

    VFSJFileChooser fc = getFileChooser();
    FileObject currentDirectory = fc.getCurrentDirectory();

    if ((currentDirectory != null) && fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()
            && fc.getFileSystemView().isFileSystem(currentDirectory)) {
        String url = null;

        try {
            url = currentDirectory.getURL().toExternalForm();
        } catch (FileSystemException e1) {
            e1.printStackTrace();
        }

        setFileName(url);
    } else {
        setFileName(null);
    }
}

From source file:net.sf.vfsjfilechooser.plaf.metal.MetalVFSFileChooserUI.java

private String fileNameString(FileObject fileObject) {
    if (fileObject == null) {
        return null;
    } else {/*from ww w.j av a 2 s .c  o m*/
        VFSJFileChooser fc = getFileChooser();

        if ((fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled())
                || (fc.isDirectorySelectionEnabled() && fc.isFileSelectionEnabled()
                        && fc.getFileSystemView().isFileSystemRoot(fileObject))) {
            String url = null;

            try {
                url = fileObject.getURL().toExternalForm();
            } catch (FileSystemException ex) {
                ex.printStackTrace();
            }

            return url;
        } else {
            return fileObject.getName().getBaseName();
        }
    }
}

From source file:net.sf.vfsjfilechooser.plaf.metal.MetalVFSFileChooserUI.java

private void doDirectoryChanged(PropertyChangeEvent e) {
    VFSJFileChooser fc = getFileChooser();
    AbstractVFSFileSystemView fsv = fc.getFileSystemView();

    clearIconCache();/*from   w  ww . j a va 2  s .  c  o m*/

    FileObject currentDirectory = fc.getCurrentDirectory();

    if (currentDirectory != null) {
        directoryComboBoxModel.addItem(currentDirectory);
        directoryComboBox.setSelectedItem(currentDirectory);
        fc.setCurrentDirectory(currentDirectory);

        if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
            if (fsv.isFileSystem(currentDirectory)) {
                String url = null;

                try {
                    url = currentDirectory.getURL().toExternalForm();
                } catch (FileSystemException e1) {
                    e1.printStackTrace();
                }

                setFileName(url);
            } else {
                setFileName(null);
            }
        }
    }
}