Example usage for org.springframework.core.io FileSystemResource FileSystemResource

List of usage examples for org.springframework.core.io FileSystemResource FileSystemResource

Introduction

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

Prototype

public FileSystemResource(Path filePath) 

Source Link

Document

Create a new FileSystemResource from a Path handle, performing all file system interactions via NIO.2 instead of File .

Usage

From source file:org.echocat.jomon.spring.application.XmlBasedApplicationContextRequirement.java

@Nonnull
public XmlBasedApplicationContextRequirement withConfigurations(@Nonnull File... files) {
    for (final File file : files) {
        _configurationFiles.add(new FileSystemResource(file));
    }/*from w w w .  j  a  va 2s. c  o  m*/
    return this;
}

From source file:com.devnexus.ting.common.SystemInformationUtils.java

public static Properties getConfigProperties(final String filePath) {

    Resource resource = new FileSystemResource(getConfigFile(filePath));
    try {/*from  w  ww  . jav a  2s .  co m*/
        return PropertiesLoaderUtils.loadProperties(resource);
    } catch (IOException e) {
        throw new IllegalStateException("Error loading properties.", e);
    }

}

From source file:gov.nih.nci.cagrid.identifiers.service.IdentifiersNAServiceImpl.java

public IdentifiersNAServiceImpl() throws RemoteException {
    super();/*from   w ww .ja  va 2  s  .  c  om*/

    try {
        String naConfigurationFile = getConfiguration().getNaConfigurationFile();
        String naProperties = getConfiguration().getNaPropertiesFile();
        FileSystemResource naConfResource = new FileSystemResource(naConfigurationFile);
        FileSystemResource naPropertiesResource = new FileSystemResource(naProperties);

        XmlBeanFactory factory = new XmlBeanFactory(naConfResource);
        PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
        cfg.setLocation(naPropertiesResource);
        cfg.postProcessBeanFactory(factory);

        this.namingAuthority = (MaintainerNamingAuthority) factory.getBean(NA_BEAN_NAME,
                MaintainerNamingAuthority.class);

    } catch (Exception e) {
        String message = "Problem inititializing NamingAuthority while loading configuration:" + e.getMessage();
        LOG.error(message, e);
        throw new RemoteException(message, e);
    }
}

From source file:com.ethlo.geodata.util.ResourceUtil.java

private Resource openConnection(String urlStr) throws IOException {
    final String[] urlParts = StringUtils.split(urlStr, "|");

    if (urlStr.startsWith("file:")) {
        String path = urlParts[0].substring(7);
        if (path.startsWith("~" + File.separator)) {
            path = System.getProperty("user.home") + path.substring(1);
        }//from   w w  w .  j  ava 2  s .c  o  m
        final File file = new File(path);
        if (!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        return new FileSystemResource(file);
    } else if (urlStr.startsWith("classpath:")) {
        return new ClassPathResource(urlParts[0].substring(10));
    }

    return new UrlResource(urlParts[0]);
}

From source file:de.langmi.spring.batch.examples.readers.file.peekable.SimplePeekableItemReaderTest.java

/**
 * Test should read succesfully./*from w  w w.j  a  va2s . c  o m*/
 *
 * @throws Exception 
 */
@Test
public void testSuccessfulReading() throws Exception {
    // init delegate
    delegateReader.setLineMapper(new PassThroughLineMapper());
    delegateReader.setResource(new FileSystemResource(INPUT_FILE));
    // init peekable
    SingleItemPeekableItemReader<String> peekable = new SingleItemPeekableItemReader<String>();
    peekable.setDelegate(delegateReader);
    // open, provide "mock" ExecutionContext
    peekable.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext());
    // read
    try {
        int count = 0;
        String line;
        while ((line = peekable.read()) != null) {
            assertEquals(String.valueOf(count), line);
            count++;
        }
        assertEquals(EXPECTED_COUNT, count);
    } catch (Exception e) {
        throw e;
    } finally {
        peekable.close();
    }
}

From source file:de.codecentric.boot.admin.actuate.LogfileMvcEndpoint.java

@RequestMapping(method = RequestMethod.GET)
public void invoke(HttpServletResponse response) throws IOException {
    if (!isAvailable()) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return;//from w  ww . j av a  2s. com
    }

    Resource file = new FileSystemResource(logfile);
    response.setContentType(MediaType.TEXT_PLAIN_VALUE);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\"");
    FileCopyUtils.copy(file.getInputStream(), response.getOutputStream());
}

From source file:com.miserablemind.butter.domain.service.email.EmailService.java

/**
 * Sends a mime mail with a specified Velocity template that may contain HTML and attachments.
 *
 * @param emailMessage prepared message object to be sent. Usually prepared by {@link EmailManager}
 *///  www .  j  ava  2  s  . c  o  m
public void sendMimeMail(final EmailMessage emailMessage) {

    MimeMessagePreparator preparedMessage = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {

            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

            message.setTo(emailMessage.getToEmail());
            message.setFrom(emailMessage.getFromAddress());
            message.setReplyTo(emailMessage.getFromAddress());
            message.setSubject(emailMessage.getSubject());
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    emailMessage.getTemplatePath(), "UTF-8", emailMessage.getModel());

            message.setText(body, true);

            if (!StringUtils.isBlank(emailMessage.getAttachmentPath())) {
                FileSystemResource file = new FileSystemResource(emailMessage.getAttachmentPath());
                message.addAttachment(emailMessage.getAttachmentName(), file);
            }
        }
    };

    try {
        this.mailSender.send(preparedMessage);
    } catch (MailException e) {
        logger.error("Email Service Exception Send Mime Mail: " + e.getMessage(), e);
    }

}

From source file:com.consol.citrus.admin.service.report.JUnitTestReportService.java

/**
 * Access file resource representing the TestNG results file.
 * @param activeProject//from w w  w .  j  a va 2s  .  c  o  m
 * @return
 */
private Resource getTestResultsFile(Project activeProject) {
    return new FileSystemResource(
            activeProject.getProjectHome() + "/target/failsafe-reports/TEST-TestSuite.xml");
}

From source file:org.jasig.ssp.util.importer.job.csv.RawItemCsvWriter.java

@Override
public void setResource(Resource resource) {
    String fileName = resource.getFilename();
    try {//from  w  ww  . j a  v  a 2  s . co  m
        File file = new File(writeDirectory.getFile(), fileName);
        file.createNewFile();
        if (file.exists())
            logger.info("File created for writing file name: " + file);
        else
            logger.error("File not created for writing file name: " + file);
        super.setResource(new FileSystemResource(file));
    } catch (IOException e) {
        logger.error("Error attempting to create file " + fileName + "for writing raw output.", e);
        e.printStackTrace();
    }

}

From source file:org.jwebsocket.spring.JWebSocketBeanFactory.java

/**
 * Load beans from a configuration file into a specific bean factory
 *
 * @param aNamespace// ww  w. j a  va  2 s  . c o  m
 * @param aPath
 * @param aClassLoader
 */
public static void load(String aNamespace, String aPath, ClassLoader aClassLoader) {
    String lPath = Tools.expandEnvVarsAndProps(aPath);

    XmlBeanDefinitionReader lXmlReader;
    if (null != aNamespace) {
        lXmlReader = new XmlBeanDefinitionReader(getInstance(aNamespace));
    } else {
        lXmlReader = new XmlBeanDefinitionReader(getInstance());
    }

    lXmlReader.setBeanClassLoader(aClassLoader);

    // if no JWEBSOCKET_HOME environment variable set 
    // then use the classpath resource, otherwise the file system resource
    // System.out.println("getJWebSocketHome: '" + JWebSocketConfig.getJWebSocketHome() + "'...");
    if (JWebSocketConfig.getJWebSocketHome().isEmpty()) {
        // System.out.println("Loading resource from classpath: " + aPath + "...");
        lXmlReader.loadBeanDefinitions(new ClassPathResource(lPath));
    } else {
        // System.out.println("Loading resource from filesystem: " + aPath + "...");
        lXmlReader.loadBeanDefinitions(new FileSystemResource(lPath));
    }
}