Example usage for org.springframework.util ResourceUtils getFile

List of usage examples for org.springframework.util ResourceUtils getFile

Introduction

In this page you can find the example usage for org.springframework.util ResourceUtils getFile.

Prototype

public static File getFile(URI resourceUri) throws FileNotFoundException 

Source Link

Document

Resolve the given resource URI to a java.io.File , i.e.

Usage

From source file:org.alfresco.repo.content.transform.AbstractContentTransformerTest.java

/**
 * Helper method to load one of the "The quick brown fox" files from the
 * classpath./*  www. j  a v  a  2s . c  o  m*/
 * 
 * @param quickname file required, eg <b>quick.txt</b>
 * @return Returns a test resource loaded from the classpath or <tt>null</tt> if
 *      no resource could be found.
 * @throws IOException
 */
public static File loadNamedQuickTestFile(String quickname) throws IOException {
    String quickNameAndPath = "quick/" + quickname;
    URL url = AbstractContentTransformerTest.class.getClassLoader().getResource(quickNameAndPath);
    if (url == null) {
        return null;
    }
    if (ResourceUtils.isJarURL(url)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using a temp file for quick resource that's in a jar." + quickNameAndPath);
        }
        try {
            InputStream is = AbstractContentTransformerTest.class.getClassLoader()
                    .getResourceAsStream(quickNameAndPath);
            File tempFile = TempFileProvider.createTempFile(is, quickname, ".tmp");
            return tempFile;
        } catch (Exception error) {
            logger.error("Failed to load a quick file from a jar. " + error);
            return null;
        }
    }
    return ResourceUtils.getFile(url);
}

From source file:org.alfresco.repo.web.scripts.custommodel.CustomModelImportTest.java

private File getResourceFile(String xmlFileName) throws FileNotFoundException {
    URL url = CustomModelImportTest.class.getClassLoader().getResource(RESOURCE_PREFIX + xmlFileName);
    if (url == null) {
        fail("Cannot get the resource: " + xmlFileName);
    }/*from   w  w w  .  j  a v a2  s. co m*/
    return ResourceUtils.getFile(url);
}

From source file:org.dbist.DbistTest.java

@Test
public final void testVelocity() throws Exception {
    String query = FileUtils.readFileToString(ResourceUtils.getFile("classpath:org/dbist/query.vm"));

    StringWriter writer = new StringWriter();
    VelocityContext vc = new VelocityContext();
    vc.put("name", "junguitar");
    vc.put("name1", "junguitar");
    Velocity.evaluate(vc, writer, query, query);

    System.out.println(writer);/*ww  w  .j a  va  2 s. com*/
}

From source file:org.dbist.DbistTest.java

@Test
public final void testGroovy() throws Exception {
    String query = FileUtils.readFileToString(ResourceUtils.getFile("classpath:org/dbist/query.groovy"));

    StringWriter writer = new StringWriter();
    VelocityContext vc = new VelocityContext();
    vc.put("name", "junguitar");
    vc.put("name1", "junguitar");
    Velocity.evaluate(vc, writer, query, query);

    System.out.println(writer);//from ww w. j a va 2  s .com
}

From source file:org.jahia.utils.FileUtils.java

/**
 * Returns the last modified date of the specified resource.
 * /*from ww w  .  jav  a  2s .  co  m*/
 * @param resource
 *            resource to check the last modified date on
 * @return the last modified date of the specified resource
 * @throws IOException
 *             in case of an I/O error
 */
public static long getLastModified(Resource resource) throws IOException {
    URL resourceUrl = resource.getURL();
    return ResourceUtils.isJarURL(resourceUrl)
            ? ResourceUtils.getFile(ResourceUtils.extractJarFileURL(resourceUrl)).lastModified()
            : resource.lastModified();
}

From source file:org.kuali.rice.core.impl.config.module.Log4jLifeCycle.java

/**
* Checks if the passed in file exists./*w  ww  .  j  a va 2 s  .  co  m*/
*
* @param log4jSettingsPath the file
* @return true if exists
*/
private boolean checkPropertiesFileExists(String log4jSettingsPath) {
    if (StringUtils.isBlank(log4jSettingsPath)) {
        return false;
    }

    boolean exists;

    try {
        exists = ResourceUtils.getFile(log4jSettingsPath).exists();
    } catch (FileNotFoundException e) {
        exists = false;
    }

    if (!exists) {
        System.out.println(LOG4J_FILE_NOT_FOUND + log4jSettingsPath);
    }

    return exists;
}

From source file:org.springframework.boot.devtools.classpath.ClassPathFolders.java

private void addUrl(URL url) {
    if (url.getProtocol().equals("file") && url.getPath().endsWith("/")) {
        try {/*ww w  . jav a 2 s . co m*/
            this.folders.add(ResourceUtils.getFile(url));
        } catch (Exception ex) {
            logger.warn("Unable to get classpath URL " + url);
            logger.trace("Unable to get classpath URL " + url, ex);
        }
    }
}

From source file:org.springframework.boot.devtools.restart.server.RestartServer.java

private boolean updateFileSystem(URL url, String name, ClassLoaderFile classLoaderFile) {
    if (!isFolderUrl(url.toString())) {
        return false;
    }//w w w .  j a v a 2s. c o  m
    try {
        File folder = ResourceUtils.getFile(url);
        File file = new File(folder, name);
        if (file.exists() && file.canWrite()) {
            if (classLoaderFile.getKind() == Kind.DELETED) {
                return file.delete();
            }
            FileCopyUtils.copy(classLoaderFile.getContents(), file);
            return true;
        }
    } catch (IOException ex) {
        // Ignore
    }
    return false;
}

From source file:org.springframework.core.io.ClassPathResource.java

/**
 * This implementation determines the underlying File
 * (or jar file, in case of a resource in a jar/zip).
 *///from   ww w  .j a  v a2 s  .c o  m
protected File getFileForLastModifiedCheck() throws IOException {
    URL url = getURL();
    if (ResourceUtils.isJarURL(url)) {
        URL actualUrl = ResourceUtils.extractJarFileURL(url);
        return ResourceUtils.getFile(actualUrl);
    } else {
        return ResourceUtils.getFile(url, getDescription());
    }
}

From source file:org.springframework.data.solr.server.support.SolrClientFactoryBean.java

/**
 * Creates a Solr {@link CoreContainer} that can be used to run an embedded
 * Solr server.//ww  w  .ja  v a2 s  .  c o m
 *
 * @param path The path to a local Solr installation to use for running the
 *             embedded server.
 * @return A {@link CoreContainer}.
 * @throws FileNotFoundException if the specified installation path
 *                               does not exist.
 */
private CoreContainer createEmbeddedContainer(final String path) throws FileNotFoundException {
    final String solrDirectory = ResourceUtils.getFile(path).getPath();

    final File solrConfigurationFile = new File(solrDirectory + "/solr.xml");

    return CoreContainer.createAndLoad(ResourceUtils.getFile(path).toPath(), solrConfigurationFile.toPath());
}