Example usage for org.springframework.core.io Resource getFile

List of usage examples for org.springframework.core.io Resource getFile

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getFile.

Prototype

File getFile() throws IOException;

Source Link

Document

Return a File handle for this resource.

Usage

From source file:org.openpplsoft.runtime.TraceFileVerifier.java

/**
 * Some SQL statements in the PS tracefile are not issued by OPS,
 * either b/c they're unnecessary/redundant or relate to functionality
 * not yet supported. Passing a file containing a list of SQL statements
 * to this method will cause the verifier to ignore them if they are found
 * in the tracefile.//from   ww w .  j ava  2  s .  c o m
 * @param filename name of file in classpath containing SQL stmts to ignore.
 */
public static void ignoreStmtsInFile(final String filename) {
    try {
        final Resource ignoredSqlRsrc = new ClassPathResource(filename);
        final BufferedReader ignoreFileReader = new BufferedReader(new FileReader(ignoredSqlRsrc.getFile()));

        String line;
        while ((line = ignoreFileReader.readLine()) != null) {
            ignoredStmts.put(line, true);
        }
        ignoreFileReader.close();
    } catch (final java.io.FileNotFoundException fnfe) {
        throw new OPSVMachRuntimeException(fnfe.getMessage(), fnfe);
    } catch (final java.io.IOException ioe) {
        throw new OPSVMachRuntimeException(ioe.getMessage(), ioe);
    }
}

From source file:com.google.api.ads.adwords.awalerting.AwAlerting.java

/**
 * Load JSON configuration file specified in the properties file. First try to load the JSON
 * configuration file from the same folder as the properties file; if it does not exist, try to
 * load it from the default location.//from   w  ww.j  a  v a 2 s .c  o  m
 *
 * @param propertiesPath the path to the properties file
 * @return JSON configuration loaded from the json file
 * @throws AlertConfigLoadException error reading properties / json file
 */
private static JsonObject getAlertsConfig(String propertiesPath) throws AlertConfigLoadException {
    LOGGER.info("Using properties file: {}", propertiesPath);

    Resource propertiesResource = new ClassPathResource(propertiesPath);
    if (!propertiesResource.exists()) {
        propertiesResource = new FileSystemResource(propertiesPath);
    }

    JsonObject alertsConfig = null;
    try {
        Properties properties = initApplicationContextAndProperties(propertiesResource);

        // Load alerts config from the same folder as the properties file
        String alertsConfigFilename = properties.getProperty("aw.alerting.alerts");
        String propertiesFolder = propertiesResource.getFile().getParent();
        File alertsConfigFile = new File(propertiesFolder, alertsConfigFilename);

        // If it does not exist, try the default resource folder according to maven structure.
        if (!alertsConfigFile.exists()) {
            String alertsConfigFilepath = "src/main/resources/" + alertsConfigFilename;
            alertsConfigFile = new File(alertsConfigFilepath);
        }

        LOGGER.debug("Loading alerts config file from {}", alertsConfigFile.getAbsolutePath());
        JsonParser jsonParser = new JsonParser();
        alertsConfig = jsonParser.parse(new FileReader(alertsConfigFile)).getAsJsonObject();
        LOGGER.debug("Done.");
    } catch (IOException e) {
        throw new AlertConfigLoadException("Error loading alerts config at " + propertiesPath, e);
    } catch (JsonParseException e) {
        throw new AlertConfigLoadException("Error parsing config file at " + propertiesPath, e);
    }

    return alertsConfig;
}

From source file:org.paxml.core.ResourceLocator.java

/**
 * Find all resources with a spring path pattern, from the added resources.
 * //from   www. j a  va 2s.c  o  m
 * @param springPattern
 *            the spring path pattern.
 * @param baseFile
 *            the file used to resolve relative path with, can be null if
 *            the pattern is not relative.
 * @return all resource found, never null.
 */
public static Set<PaxmlResource> findResources(String springPattern, Resource baseFile) {

    springPattern = springPattern.trim();

    if (StringUtils.isBlank(springPattern)) {
        throw new PaxmlRuntimeException("Cannot have empty file pattern!");
    }

    Set<PaxmlResource> set = new LinkedHashSet<PaxmlResource>();
    if (!springPattern.startsWith("file:") && !springPattern.startsWith("classpath:")
            && !springPattern.startsWith("classpath*:")) {
        springPattern = getRelativeResource(baseFile, springPattern);
    }
    if (log.isInfoEnabled()) {
        log.info("Searching paxml resource with pattern: " + springPattern);
    }
    try {
        for (Resource res : new PathMatchingResourcePatternResolver().getResources(springPattern)) {

            if (res instanceof ClassPathResource) {
                set.add(new ClasspathResource((ClassPathResource) res));
            } else if (res instanceof UrlResource && res.getURI().toString().startsWith("jar:")) {

                set.add(new ClasspathResource(
                        new ClassPathResource(StringUtils.substringAfterLast(res.getURI().toString(), "!"))));

            } else {
                try {
                    File file = res.getFile();
                    if (file.isFile()) {
                        set.add(new FileSystemResource(file));
                    }
                } catch (IOException e) {
                    throw new PaxmlRuntimeException("Unsupported spring resource: " + res.getURI()
                            + ", of type: " + res.getClass().getName());
                }
            }
        }
    } catch (IOException e) {
        throw new PaxmlRuntimeException("Cannot find resources with spring pattern: " + springPattern, e);
    }
    return set;
}

From source file:org.paxml.table.excel.ExcelFileFactory.java

/**
 * {@inheritDoc}/*from   www.ja va  2 s.c  om*/
 */
@Override
public IFile load(Resource file) {
    try {
        return new ExcelTable(file.getFile(), null, null, false, false);
    } catch (IOException e) {
        throw new PaxmlRuntimeException("Cannot find the file of resource: " + PaxmlUtils.getResourceFile(file),
                e);
    }
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.FileSystemStorageTest.java

public void testDispose() throws Exception {
    Resource res = storage.getResource();
    File file = res.getFile();
    assertTrue(res.exists());/*from   w  ww .  j a v  a2s. c  om*/
    assertTrue(file.exists());
    storage.dispose();
    assertFalse(res.exists());
    assertFalse(file.exists());
}

From source file:org.jnap.core.assets.YuiCompressorAssetsHandler.java

@Override
protected Resource doCompression(Resource resource) throws IOException {
    File sourceFile = resource.getFile();
    Resource minifiedResource = new ServletContextResource(servletContext,
            format(((ServletContextResource) this.path).getPath() + compressedFilename,
                    FilenameUtils.getBaseName(sourceFile.getName()),
                    FilenameUtils.getExtension(sourceFile.getName())));
    resetResource(minifiedResource);/*from w  w  w  .  j  ava 2 s  .c  om*/
    runYuiCompressor(sourceFile, minifiedResource.getFile());
    return minifiedResource;
}

From source file:group.id.groovy.GroovyInvokerDelegate.java

public void setScript(Resource script) {
    try {//from  w  w  w  .  j  av  a2 s . co  m
        this.script = Files.toString(script.getFile(), Charset.defaultCharset());
    } catch (IOException e) {
        throw new TechnicalException(e);
    }
}

From source file:eu.databata.engine.spring.PropagatorSpringInstance.java

public void setChanges(Resource changesDir) throws IOException {
    this.changesDir = changesDir.getFile();
}

From source file:fr.acxio.tools.agia.file.pdf.MergePDFProcessorTest.java

@Test
public void testMerge() throws Exception {
    MergingPDDocumentFactory aFactory = new MergingPDDocumentFactory();
    ResourcesFactory aResourcesFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/content1.pdf"));
    Resource aFileResource2 = mock(Resource.class);
    when(aFileResource2.getFile()).thenReturn(new File("src/test/resources/testFiles/content2.pdf"));

    when(aResourcesFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1, aFileResource2 });

    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/M-content.pdf"));
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);

    MergePDFProcessor aProcessor = new MergePDFProcessor();
    aProcessor.setSourceFactory(aResourcesFactory);
    aProcessor.setDestinationFactory(aDestinationFactory);
    aProcessor.setDocumentFactory(aFactory);
    aProcessor.setKey("mergedPDF");

    Map<String, Object> aResult = aProcessor.process(new HashMap<String, Object>());
    assertNotNull(aResult);/*from www.  j  a va  2  s.  c  om*/
    assertEquals(aDestResource.getFile().getAbsolutePath(), aResult.get("mergedPDF"));
    assertTrue(new File((String) aResult.get("mergedPDF")).exists());

    aDestResource.getFile().delete();
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.FileSystemStorageTest.java

public void testResourceForTempFile() throws Exception {
    Resource res = storage.getResource();
    assertTrue(res.exists());/*from  w  w w. j  a v a2s . com*/
    File tempFile = res.getFile();
    assertTrue(tempFile.exists());
    assertTrue(tempFile.canRead());
    assertTrue(tempFile.canWrite());
}