Example usage for org.apache.commons.io FileUtils toFile

List of usage examples for org.apache.commons.io FileUtils toFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils toFile.

Prototype

public static File toFile(URL url) 

Source Link

Document

Convert from a URL to a File.

Usage

From source file:org.apache.struts2.JSPLoader.java

protected String getJarUrl(Class clazz) {
    ProtectionDomain protectionDomain = clazz.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URL loc = codeSource.getLocation();
    File file = FileUtils.toFile(loc);
    return file.getAbsolutePath();
}

From source file:org.apache.tez.client.TestTezClientUtils.java

/**
 *
 *//*from  w ww. j a v a2s. c  om*/
@Test(timeout = 10000)
public void validateSetTezJarLocalResourcesDefinedExistingDirectory() throws Exception {
    URL[] cp = ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs();
    StringBuffer buffer = new StringBuffer();
    for (URL url : cp) {
        buffer.append(url.toExternalForm());
        buffer.append(",");
    }
    TezConfiguration conf = new TezConfiguration();
    conf.set(TezConfiguration.TEZ_LIB_URIS, buffer.toString());
    Credentials credentials = new Credentials();
    Map<String, LocalResource> localizedMap = new HashMap<String, LocalResource>();
    boolean usingArchive = TezClientUtils.setupTezJarsLocalResources(conf, credentials, localizedMap);
    Assert.assertFalse(usingArchive);
    Set<String> resourceNames = localizedMap.keySet();
    for (URL url : cp) {
        File file = FileUtils.toFile(url);
        if (file.isDirectory()) {
            String[] firList = file.list();
            for (String fileNme : firList) {
                File innerFile = new File(file, fileNme);
                if (!innerFile.isDirectory()) {
                    assertTrue(resourceNames.contains(innerFile.getName()));
                }
                // not supporting deep hierarchies 
            }
        } else {
            assertTrue(resourceNames.contains(file.getName()));
        }
    }
}

From source file:org.apache.usergrid.query.validator.AbstractQueryIT.java

private static List<Entity> loadEntitiesFromResource(String collection) {
    String json = null;//w w w .  jav a2 s .  co  m
    try {
        URL url = Thread.currentThread().getContextClassLoader().getResource(collection + ".json");
        json = FileUtils.readFileToString(FileUtils.toFile(url), Charset.forName("UTF-8"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    List<Entity> entities = new ArrayList<Entity>();
    List<Map<String, Object>> datas = (List<Map<String, Object>>) JsonUtils.parse(json);
    int index = 0;
    for (Map<String, Object> data : datas) {
        long created = System.currentTimeMillis() + (index * 1000);

        QueryEntity entity = new QueryEntity();
        entity.setType(collection);
        for (Map.Entry<String, Object> property : data.entrySet()) {
            if (StringUtils.equals("name", property.getKey()))
                entity.setName((String) property.getValue());
            else
                entity.setProperty(property.getKey(), property.getValue());
        }
        entity.setProperties(data);
        entity.setCreated(created);
        entity.setModified(created);
        entities.add(entity);
        index++;
    }
    return entities;
}

From source file:org.apache.usergrid.query.validator.QueryValidatorRunner.java

private List<Entity> loadEntities(String collection) {
    String json = null;//from  www. j  a  v  a2 s. c om
    try {
        URL url = Thread.currentThread().getContextClassLoader().getResource(collection + ".json");
        json = FileUtils.readFileToString(FileUtils.toFile(url), Charset.forName("UTF-8"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    List<Entity> entities = new ArrayList<Entity>();
    List<Map<String, Object>> datas = (List<Map<String, Object>>) JsonUtils.parse(json);
    for (Map<String, Object> data : datas) {
        Entity entity = new QueryEntity();
        entity.setType(collection);
        entity.setProperties(data);
        entity.setCreated(System.currentTimeMillis());
        entity.setModified(entity.getCreated());
        entities.add(entity);
    }
    return entities;
}

From source file:org.b3log.latke.Latkes.java

/**
 * Gets a file in web application with the specified path.
 *
 * @param path the specified path/*ww w.  ja  v a 2  s  . c om*/
 * @return file,
 * @see ServletContext#getResource(String)
 * @see ServletContext#getResourceAsStream(String)
 */
public static File getWebFile(final String path) {
    final ServletContext servletContext = AbstractServletListener.getServletContext();

    File ret;

    try {
        final URL resource = servletContext.getResource(path);

        if (null == resource) {
            return null;
        }

        ret = FileUtils.toFile(resource);

        if (null == ret) {
            final File tempdir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");

            ret = new File(tempdir.getPath() + path);

            FileUtils.copyURLToFile(resource, ret);

            ret.deleteOnExit();
        }

        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Reads file [path=" + path + "] failed", e);

        return null;
    }
}

From source file:org.chtijbug.drools.platform.runtime.builder.BusinessPackageAuthoringManagerTest.java

@Test
@Ignore//from w w  w.  jav  a2  s  .  c  o  m
public void should_get_everything_generated_on_guvnor() throws Exception {
    GuvnorRepositoryImpl guvnorRepository = new GuvnorRepositoryImpl("com.pymma.drools", "LATEST", "admin",
            "admin", "598");
    Xsd2JarTransformer xsd2JarTransformer = new Xsd2JarTransformer();
    BusinessPackageAuthoringManager toTest = new BusinessPackageAuthoringManager(guvnorRepository,
            xsd2JarTransformer, new MavenProjectFactory(), "/home/nheron/workspace-chtiJBUG/apache-maven-3.1.1",
            "/home/nheron/workspace-chtiJBUG/baseTestDirectory");

    URL wsdlFile = this.getClass().getResource("/newWSDL1.wsdl");
    URL modelFile = this.getClass().getResource("/model.xsd");
    InputStream wsdlContent = FileUtils.openInputStream(FileUtils.toFile(wsdlFile));
    InputStream modelContent = FileUtils.openInputStream(FileUtils.toFile(modelFile));

    toTest.createBusinessPackage(wsdlContent, modelContent);
}

From source file:org.chtijbug.drools.platform.runtime.builder.BusinessPackageAuthoringManagerTest.java

@Test
@Ignore//from w ww . jav  a 2  s.co  m
public void should_get_webservice_project_folder_generated() throws Exception {
    GuvnorRepositoryImpl guvnorRepository = new GuvnorRepositoryImpl("com.pymma.drools", "LATEST", "admin",
            "admin", "598");
    guvnorRepository.setBaseUrl("http://localhost:10080");
    Xsd2JarTransformer xsd2JarTransformer = new Xsd2JarTransformer();
    BusinessPackageAuthoringManager toTest = new BusinessPackageAuthoringManager(guvnorRepository,
            xsd2JarTransformer, new MavenProjectFactory(), "/home/nheron/workspace-chtiJBUG/apache-maven-3.1.1",
            "/home/nheron/workspace-chtiJBUG/baseTestDirectory");

    URL wsdlFile = this.getClass().getResource("/newWSDL1.wsdl");
    URL modelFile = this.getClass().getResource("/model.xsd");
    InputStream wsdlContent = FileUtils.openInputStream(FileUtils.toFile(wsdlFile));
    InputStream modelContent = FileUtils.openInputStream(FileUtils.toFile(modelFile));

    toTest.generateExecutionService(wsdlContent, modelContent);
}

From source file:org.chtijbug.drools.platform.runtime.utils.XpathQueryRunnerTest.java

@Test
public void should_get_package_name_equals_to_name1() throws Exception {
    URL wsdlFile = this.getClass().getResource("/newWSDL1.wsdl");
    FileInputStream wsdlContent = FileUtils.openInputStream(FileUtils.toFile(wsdlFile));
    XpathQueryRunner queryRunner = new XpathQueryRunner(wsdlContent);
    String packageName = queryRunner.executeXpath(XPATH_PACKAGE_NAME);
    assertThat(packageName).isEqualTo("newWSDL1");
}

From source file:org.chtijbug.drools.platform.runtime.utils.XpathQueryRunnerTest.java

@Test
public void should_get_all_processes_from_wsdl() throws Exception {
    URL wsdlFile = this.getClass().getResource("/newWSDL1.wsdl");
    FileInputStream wsdlContent = FileUtils.openInputStream(FileUtils.toFile(wsdlFile));
    XpathQueryRunner queryRunner = new XpathQueryRunner(wsdlContent);
    List<String> allProcessesName = queryRunner.executeXpathAsList(XPATH_PROCESSES_NAMES);
    assertThat(allProcessesName).hasSize(1);
    assertThat(allProcessesName.get(0)).isEqualTo("newWSDL1Operation");

}

From source file:org.chtijbug.drools.platform.runtime.utils.XpathQueryRunnerTest.java

@Test
public void should_get_xsd_filename_equals_to_model_xsd() throws Exception {
    URL wsdlFile = this.getClass().getResource("/newWSDL1.wsdl");
    FileInputStream wsdlContent = FileUtils.openInputStream(FileUtils.toFile(wsdlFile));
    XpathQueryRunner queryRunner = new XpathQueryRunner(wsdlContent);
    String xsdFilename = queryRunner.executeXpath(XPATH_XSD_NAME);
    assertThat(xsdFilename).isEqualTo("model.xsd");
}