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:com.joshlong.esb.springintegration.modules.net.sftp.SFTPInboundSynchronizer.java

@SuppressWarnings("ignored")
private boolean copyFromRemoteToLocalDirectory(SFTPSession sftpSession, ChannelSftp.LsEntry entry,
        Resource localDir) throws Exception {
    logger.debug(String.format("attempting to sync remote file %s/%s to local file %s", remotePath,
            entry.getFilename(), localDir.getFile().getAbsolutePath()));

    File fileForLocalDir = localDir.getFile();

    File localFile = new File(fileForLocalDir, entry.getFilename());

    if (!localFile.exists()) {
        InputStream in = null;//from   ww w .j  av  a 2  s  . c  o  m
        FileOutputStream fos = null;

        try {
            File tmpLocalTarget = new File(localFile.getAbsolutePath() + INCOMPLETE_EXTENSION);

            fos = new FileOutputStream(tmpLocalTarget);

            String remoteFqPath = this.remotePath + "/" + entry.getFilename();
            in = sftpSession.getChannel().get(remoteFqPath);
            IOUtils.copy(in, fos);

            if (tmpLocalTarget.renameTo(localFile)) {
                // last step
                if (isShouldDeleteDownloadedRemoteFiles()) {
                    logger.debug(String.format("isShouldDeleteDownloadedRemoteFiles == true; "
                            + "attempting to remove remote path '%s'", remoteFqPath));
                    sftpSession.getChannel().rm(remoteFqPath);
                }
            }

            return true;
        } catch (Throwable th) {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(fos);
        }
    } else {
        logger.debug(String.format("local file %s already exists. Not re-downloading it.",
                localFile.getAbsolutePath()));

        return true;
    }

    return false;
}

From source file:architecture.ee.component.core.lifecycle.RepositoryImpl.java

public ConfigRoot getConfigRoot() {
    try {//w ww.j  av  a 2  s.  co m

        File file = new File(getRootResource().getFile(), "config");
        Resource child = new FileSystemResource(file);

        log.debug("config root:" + child.getURI());

        if (!child.exists()) {
            child.getFile().mkdirs();
        }
        return new ConfigRootImpl(child);
    } catch (Exception e) {
    }
    return null;
}

From source file:fr.acxio.tools.agia.tasks.ZipFilesTasklet.java

protected void zipResource(Resource sSourceResource, ZipArchiveOutputStream sZipArchiveOutputStream,
        StepContribution sContribution, ChunkContext sChunkContext) throws IOException, ZipFilesException {
    // TODO : use a queue to reduce the callstack overhead
    if (sSourceResource.exists()) {
        File aSourceFile = sSourceResource.getFile();
        String aSourcePath = aSourceFile.getCanonicalPath();

        if (!aSourcePath.startsWith(sourceBaseDirectoryPath)) {
            throw new ZipFilesException(
                    "Source file " + aSourcePath + " does not match base directory " + sourceBaseDirectoryPath);
        }/* ww  w  .  j a  va2s .c o  m*/

        if (sContribution != null) {
            sContribution.incrementReadCount();
        }
        String aZipEntryName = aSourcePath.substring(sourceBaseDirectoryPath.length() + 1);
        sZipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(aZipEntryName));
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Zipping {} to {}", sSourceResource.getFile().getCanonicalPath(), aZipEntryName);
        }
        if (aSourceFile.isFile()) {
            InputStream aInputStream = sSourceResource.getInputStream();
            IOUtils.copy(aInputStream, sZipArchiveOutputStream);
            aInputStream.close();
            sZipArchiveOutputStream.closeArchiveEntry();
        } else {
            sZipArchiveOutputStream.closeArchiveEntry();
            for (File aFile : aSourceFile
                    .listFiles((FileFilter) (recursive ? TrueFileFilter.TRUE : FileFileFilter.FILE))) {
                zipResource(new FileSystemResource(aFile), sZipArchiveOutputStream, sContribution,
                        sChunkContext);
            }
        }
        if (sContribution != null) {
            sContribution.incrementWriteCount(1);
        }
    } else if (LOGGER.isInfoEnabled()) {
        LOGGER.info("{} does not exist", sSourceResource.getFilename());
    }
}

From source file:net.sourceforge.vulcan.metrics.XmlMetricsPlugin.java

private void loadTransformers() throws IOException {
    LOG.info("Reloading XSL transformers because they have changed.");

    transformers.clear();//  www.  ja v a  2s . co  m
    for (Resource r : resourceResolver.getResources(transformSourcePath)) {
        try {
            final long lastModified = r.getFile().lastModified();
            if (lastModified > newestTransformDate) {
                newestTransformDate = lastModified;
            }

            final SAXSource source = new SAXSource(XMLReaderFactory.createXMLReader(),
                    new InputSource(r.getInputStream()));

            transformers.put(r.getFilename(), transformerFactory.newTransformer(source));
        } catch (Exception e) {
            eventHandler.reportEvent(
                    new ErrorEvent(this, "metrics.errors.load.transform", new Object[] { e.getMessage() }, e));
        }
    }
}

From source file:org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean.java

private String getResourceName(Resource resource) {
    String name;/*from w w w.j  a  va  2 s  .  c om*/
    if (resource instanceof ContextResource) {
        name = ((ContextResource) resource).getPathWithinContext();
    } else if (resource instanceof ByteArrayResource) {
        name = resource.getDescription();
    } else {
        try {
            name = resource.getFile().getAbsolutePath();
        } catch (IOException e) {
            name = resource.getFilename();
        }
    }
    return name;
}

From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public boolean isDirectory(Resource resource) {
    try {/* w ww.  j  ava 2  s .  c o  m*/
        return resource.getFile().isDirectory();
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }
}

From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public void prepareForWriting(Resource resource) {
    try {//w  w w. j  a  v a 2s.  co  m
        if (resource.getFile().isDirectory()) {
            resource.getFile().mkdirs();
        } else {
            resource.getFile().getParentFile().mkdirs();
        }
    } catch (IOException ex) {
        throw new WMRuntimeException(ex);
    }
}

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

protected void resetResource(Resource resource) throws IOException {
    File file = null;/*w  w  w .  jav  a  2 s. c  o  m*/
    if (!resource.exists()) {
        ServletContextResource servletResource = (ServletContextResource) resource;
        file = new File(WebUtils.getRealPath(servletContext, "/") + servletResource.getPath());
    } else {
        file = resource.getFile();
    }
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
}

From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public OutputStream getOutputStream(Resource resource) {
    try {//from  w  w  w .jav  a  2s  .  c  o m
        Assert.isTrue(!resource.getFile().isDirectory(), "Cannot get an output stream for an invalid file.");
        prepareForWriting(resource);
        return new FileOutputStream(resource.getFile());
    } catch (FileNotFoundException ex) {
        throw new WMRuntimeException(ex);
    } catch (IOException ex) {
        throw new WMRuntimeException(ex);
    }
}