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:com.iterranux.droolsjbpmCore.runtime.build.impl.KieModuleBuilder.java

public void buildKmoduleForGrailsModule(String moduleName) {

    Resource kmoduleXml = new FileSystemResource("grails-app/conf/droolsjbpm/" + moduleName + "/kmodule.xml");
    if (kmoduleXml.exists()) {
        try {/*from  w ww  . j  a v a 2  s  . co  m*/
            buildKmoduleForGrailsModule(moduleName, kmoduleXml);
        } catch (IOException e) {
            log.error("Error: KieModule build for '" + moduleName + "' failed", e);
        }
    } else {
        log.error("Error: KieModule build for '" + moduleName
                + "' was tried, but no kmodule.xml could be found.");
    }

}

From source file:com.consol.citrus.admin.service.TestCaseService.java

/**
 * Find all tests in give source file. Method is finding tests by their annotation presence of @CitrusTest or @CitrusXmlTest.
 * @param sourceFile//w ww  .jav  a 2 s .co m
 * @param packageName
 * @param className
 * @return
 */
private List<Test> findTests(File sourceFile, String packageName, String className) {
    List<Test> tests = new ArrayList<>();

    try {
        String sourceCode = FileUtils.readToString(new FileSystemResource(sourceFile));

        Matcher matcher = Pattern.compile("[^/\\*]\\s@CitrusTest").matcher(sourceCode);
        while (matcher.find()) {
            Test test = new Test();
            test.setType(TestType.JAVA);
            test.setClassName(className);
            test.setPackageName(packageName);

            String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start()));
            snippet = snippet.substring(0, snippet.indexOf("){"));
            String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10);
            methodName = methodName.substring(0, methodName.indexOf("("));
            test.setMethodName(methodName);

            if (snippet.contains("@CitrusTest(name=")) {
                String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else {
                test.setName(className + "." + methodName);
            }

            tests.add(test);
        }

        matcher = Pattern.compile("[^/\\*]\\s@CitrusXmlTest").matcher(sourceCode);
        while (matcher.find()) {
            Test test = new Test();
            test.setType(TestType.XML);
            test.setClassName(className);
            test.setPackageName(packageName);

            String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start()));
            snippet = snippet.substring(0, snippet.indexOf('{', snippet.indexOf("publicvoid")));
            String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10);
            methodName = methodName.substring(0, methodName.indexOf("("));
            test.setMethodName(methodName);

            if (snippet.contains("@CitrusXmlTest(name=\"")) {
                String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else if (snippet.contains("@CitrusXmlTest(name={\"")) {
                String explicitName = snippet.substring(snippet.indexOf("name={\"") + 7);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else {
                test.setName(methodName);
            }

            if (snippet.contains("packageScan=\"")) {
                String packageScan = snippet.substring(snippet.indexOf("packageScan=\"") + 13);
                packageScan = packageScan.substring(0, packageScan.indexOf("\""));
                test.setPackageName(packageScan);
            }

            if (snippet.contains("packageName=\"")) {
                String explicitPackageName = snippet.substring(snippet.indexOf("packageName=\"") + 13);
                explicitPackageName = explicitPackageName.substring(0, explicitPackageName.indexOf("\""));
                test.setPackageName(explicitPackageName);
            }

            tests.add(test);
        }
    } catch (IOException e) {
        log.error("Failed to read test source file", e);
    }

    return tests;
}

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

@Test
public void testExecuteEmptyOrigin() throws Exception {
    FileCopyTasklet aTasklet = new FileCopyTasklet();
    aTasklet.setOrigin(new FileSystemResource("src/test/resources/testFiles/input.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy6.csv"));
    aTasklet.execute(null, null);/*from  w  ww. j  a v a2  s .  c o m*/

    assertTrue(aTasklet.getDestination().getFile().exists());
    assertTrue(aTasklet.getDestination().getFile().length() > 0);

    aTasklet.setEmptyOrigin(true);
    aTasklet.setOrigin(new FileSystemResource("target/input-copy6.csv"));
    aTasklet.setDestination(new FileSystemResource("target/input-copy7.csv"));
    aTasklet.execute(null, null);

    assertTrue(aTasklet.getDestination().getFile().exists());
    assertTrue(aTasklet.getOrigin().getFile().exists());
    assertEquals(0, aTasklet.getOrigin().getFile().length());
}

From source file:gov.nih.nci.cabig.caaers.tools.mail.CaaersJavaMailSender.java

/**
 * This method is used to send an email//from   w w w  .  j ava2s  . c om
 */
public void sendMail(String[] to, String[] cc, String subject, String content, String[] attachmentFilePaths) {
    if (to == null || to.length == 0 || to[0] == null) {
        return;
    }
    try {
        MimeMessage message = createMimeMessage();
        message.setSubject(subject);

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        if (cc != null && cc.length > 0) {
            helper.setCc(cc);
        }
        helper.setText(content);

        for (String attachmentPath : attachmentFilePaths) {
            if (StringUtils.isNotEmpty(attachmentPath)) {
                File f = new File(attachmentPath);
                FileSystemResource file = new FileSystemResource(f);
                helper.addAttachment(file.getFilename(), file);
            }
        }
        send(message);

    } catch (Exception e) {
        if (SUPRESS_MAIL_SEND_EXCEPTION)
            return; //supress the excetion related to email sending
        throw new CaaersSystemException("Error while sending email to " + to[0], e);
    }
}

From source file:cn.org.once.cstack.cli.utils.ModuleUtils.java

public String runScript(String moduleName, File file) {
    applicationUtils.checkConnectedAndApplicationSelected();

    Module module = findModule(moduleName);

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("file", new FileSystemResource(file));
    parameters.putAll(authenticationUtils.getMap());

    String url = String.format("%s%s%s/run-script", authenticationUtils.finalHost, urlLoader.modulePrefix,
            module.getName());/*from www.j a v a 2s  .  c  om*/

    log.info("Running script...");

    restUtils.sendPostForUpload(url, parameters);

    return "Done";
}

From source file:com.paxxis.cornerstone.messaging.service.shell.ServiceShell.java

private void doDatabaseUpdate(String[] inputs) {
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerShutdownHook();/*from   w w w.  jav  a  2 s. c o  m*/
    ctx.refresh();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    xmlReader.loadBeanDefinitions(new FileSystemResource(inputs[0]));

    DatabaseUpdater updater = ctx.getBean(DatabaseUpdater.class);

    String target = null;
    if (inputs.length == 3) {
        target = inputs[2];
    }

    updater.update(inputs[1], target);

    ctx.close();
}

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

@Test
public void testZipDirectory() throws Exception {
    FileUtils.forceMkdir(new File("target/Z-testfiles/source"));
    FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"),
            new File("target/Z-testfiles/source/CP0-input.csv"));
    FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"),
            new File("target/Z-testfiles/source/CP1-input.csv"));

    String aTargetFilename = "target/Z2-input.zip";
    ZipFilesTasklet aTasklet = new ZipFilesTasklet();
    aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/"));
    FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
    aSourceFactory.setPattern("file:target/Z-testfiles/source/");
    aTasklet.setSourceFactory(aSourceFactory);
    ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
    aDestinationFactory.setExpression(aTargetFilename);
    aTasklet.setDestinationFactory(aDestinationFactory);

    assertFalse(new File(aTargetFilename).exists());

    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(3)).incrementReadCount();
    verify(aStepContribution, times(3)).incrementWriteCount(1);

    assertTrue(new File(aTargetFilename).exists());
    ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
    Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
    assertTrue(aEntries.hasMoreElements());
    assertEquals("source", aEntries.nextElement().getName());
    assertTrue(aEntries.hasMoreElements());
    assertEquals("source/CP0-input.csv", aEntries.nextElement().getName());
    assertTrue(aEntries.hasMoreElements());
    assertEquals("source/CP1-input.csv", aEntries.nextElement().getName());
    assertFalse(aEntries.hasMoreElements());
    aZipFile.close();/*w  ww  . java2s .com*/
}

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

/**
 * Change the preferences defined in the map; this will use the accessors.
 * /*from www.j  a  v a  2  s.  com*/
 * @param prefs
 * @throws FileAccessException
 */
@Override
public void setPreferencesMap(Map<String, String> prefs) {
    if (prefs.containsKey(LocalStudioFileSystem.WMHOME_KEY)
            && prefs.get(LocalStudioFileSystem.WMHOME_KEY) != null) {
        try {
            LocalStudioFileSystem
                    .setWaveMakerHome(new FileSystemResource(prefs.get(LocalStudioFileSystem.WMHOME_KEY)));
        } catch (FileAccessException e) {
            throw new WMRuntimeException(e);
        }
    }
    if (prefs.containsKey(AbstractStudioFileSystem.DEMOHOME_KEY)
            && prefs.get(AbstractStudioFileSystem.DEMOHOME_KEY) != null) {
        this.fileSystem.setDemoDir(new File(prefs.get(AbstractStudioFileSystem.DEMOHOME_KEY)));
    }
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.restapi.PublisherController.java

/**
 * @deprecated This API endpoint will be removed.  
 *///from   w w w . j av a  2s.  c  om
@ApiOperation(value = "Deprecated route: Exposes H2O scoring engine model for download as JAR file", notes = "Privilege level: Any consumer of this endpoint must have a valid access token")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 500, message = "Internal server error, e.g. error building model") })
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", value = "/rest/downloads", produces = "application/java-archive")
@ResponseBody
@Deprecated
public FileSystemResource downloadEngine(@Valid @RequestBody DownloadRequest downloadRequest)
        throws EngineBuildingException {
    LOGGER.info("Got download request: " + downloadRequest);
    return new FileSystemResource(publisher
            .getScoringEngineJar(downloadRequest.getH2oCredentials(), downloadRequest.getModelName()).toFile());
}