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.codehaus.grepo.procedure.AbstractProcedureRepositoryTest.java

/**
 * @param fileName The file name.//ww w.  j av a 2  s. c  om
 * @throws FileNotFoundException in case of errors.
 * @throws IOException in case of errors.
 */
protected void executeSqlFromFile(String fileName) throws FileNotFoundException, IOException {
    DefaultResourceLoader drl = new DefaultResourceLoader();
    Resource resource = drl.getResource(fileName);
    BufferedReader br = new BufferedReader(new FileReader(resource.getFile()));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
        sb.append(line).append("\n");
    }
    br.close();
    getJdbcTemplate().execute(sb.toString());
}

From source file:csns.test.Setup.java

/**
 * Spring's executeSqlScript() splits the script into statements and
 * executes each statement individually. The problem is that the split is
 * based on simple delimiters like semicolon and it does not recognize the
 * syntax of create function/procedure. So in order to run csns-create.sql,
 * we have to read the file into a string and pass the whole thing to the
 * JDBC driver./*w ww .ja v  a 2s.co  m*/
 */
@SuppressWarnings("deprecation")
private void executeSqlScript(String path) {
    try {
        StringBuilder sb = new StringBuilder();
        Resource resource = applicationContext.getResource(path);
        Scanner in = new Scanner(resource.getFile());
        while (in.hasNextLine()) {
            sb.append(in.nextLine());
            sb.append("\n");
        }
        in.close();
        simpleJdbcTemplate.update(sb.toString());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:slina.mb.parsing.LogParserTests.java

@Test
public void testLog4jFile() throws IOException {

    LogFileReader reader = new LogFileReaderImpl();

    Resource res = new ClassPathResource(this.poolLog);
    File file = res.getFile();
    String var = file.getAbsolutePath();

    List<String> filelist = reader.readFile(var);
    assertTrue(filelist.size() > 0);/* w w  w. j  a  v  a2s.co  m*/
    System.out.println(filelist.size());

    List<LogEvent> eventsList = this.stdLogParser.createLogEvents(2, filelist);

    assertTrue(eventsList.size() > 0);
    System.out.println(eventsList.size());
    System.out.println("\n\n");

}

From source file:slina.mb.parsing.LogParserTests.java

@Test
public void testIBMFile() throws IOException {

    Resource res = new ClassPathResource(this.ibmFile);
    File file = res.getFile();
    String var = file.getAbsolutePath();

    LogFileReader reader = new LogFileReaderImpl();
    List<String> filelist = reader.readFile(var);
    assertTrue(filelist.size() > 0);/*from  ww w  .  ja va  2s .  c o m*/
    System.out.println(filelist.size());

    IBMParserImpl parser = new IBMParserImpl();
    this.createLogIBMLevelMap(parser.getLogLevelLokup());

    List<LogEvent> eventsList = this.stdLogParser.createLogEvents(0, filelist);
    assertTrue(eventsList.size() > 0);
    System.out.println(eventsList.size());

}

From source file:fr.acxio.tools.agia.io.FilesOperationProcessorTest.java

@Test
public void testExecuteCopy() throws Exception {
    FilesOperationProcessor aProcessor = new FilesOperationProcessor();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());

    aProcessor.setSourceFactory(aSourceFactory);
    aProcessor.setDestinationFactory(aDestinationFactory);
    aProcessor.setOperation(Operation.COPY);
    aProcessor.setKey("outputFiles");
    aProcessor.afterPropertiesSet();// ww  w.  ja  v  a 2  s  . c om

    Map<String, Object> aData = new HashMap<String, Object>();
    aData.put("k1", "v1");

    Map<String, Object> aResult = aProcessor.process(aData);
    assertNotNull(aResult);
    assertNotNull(aResult.get("outputFiles"));

    String aOutputFilePath = ((List<String>) aResult.get("outputFiles")).get(0);
    assertEquals(new File("target/CP-input.csv").getCanonicalPath(), aOutputFilePath);

    assertTrue(aDestResource.getFile().exists());
    assertTrue(new File(aOutputFilePath).exists());
}

From source file:com.camp.web.converter.XMLConverter.java

public void convertFromObjectToXML(Object object, Resource resource) throws IOException {

    FileOutputStream os = null;//from  w w w.ja  va  2  s.c o  m
    try {
        File file = resource.getFile();
        os = new FileOutputStream(file);
        getMarshaller().marshal(object, new StreamResult(os));
    } finally {
        if (null != os) {
            os.close();
        }
    }
}

From source file:com.example.securelogin.selenium.loginform.page.account.AccountCreatePage.java

public AccountCreatePage inputValidationFailure(String username, String firstName, String lastName,
        String email, String confirmEmail, String url, String image, String profile) throws IOException {
    webDriverOperations.overrideText(id("username"), username);
    webDriverOperations.overrideText(id("firstName"), firstName);
    webDriverOperations.overrideText(id("lastName"), lastName);
    webDriverOperations.overrideText(id("email"), email);
    webDriverOperations.overrideText(id("confirmEmail"), confirmEmail);
    webDriverOperations.overrideText(id("url"), url);
    webDriverOperations.overrideText(id("profile"), profile);
    Resource resource = resourceLoader.getResource(image);
    webDriverOperations.referUploadFile(id("image"), resource.getFile());
    webDriverOperations.click(id("confirm"));
    waitDefaultInterval();/*from w w  w  . ja v  a 2s  .  c o m*/
    return this;
}

From source file:it.geosolutions.geobatch.catalog.dao.file.xstream.XStreamFlowConfigurationDAOTest.java

@Test
public void testDAO() throws IOException {

    Resource resource = context.getResource("data");
    File dir = resource.getFile();
    assertTrue(dir.exists());/*from  w  w w  .  j av a2s.c o  m*/

    File file = new File(dir, "flow1.xml");
    assertTrue(file.exists());

    XStreamFlowConfigurationDAO dao = new XStreamFlowConfigurationDAO(dir.getAbsolutePath(), createAlias());
    FileBasedFlowConfiguration fbfc = dao.find("flow1", false);

    assertNotNull(fbfc);

    assertEquals(fbfc.getId(), "flow1");
    assertEquals(fbfc.getName(), "flow1name");
    assertEquals(fbfc.getDescription(), "flow1desc");

    FileBasedEventGeneratorConfiguration fbegc = (FileBasedEventGeneratorConfiguration) fbfc
            .getEventGeneratorConfiguration();
    assertNotNull(fbegc);

    FileBasedEventConsumerConfiguration fbecc = (FileBasedEventConsumerConfiguration) fbfc
            .getEventConsumerConfiguration();
    assertNotNull(fbecc);

    List<? extends ActionConfiguration> lac = fbecc.getActions();
    assertNull(lac);

}

From source file:org.focusns.web.widget.interceptor.PluginWidgetInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    ///*w  w w .ja v a  2 s.  co m*/
    if (modelAndView == null) {
        return;
    }
    //
    String viewName = modelAndView.getViewName();
    if (viewName != null && viewName.startsWith("plugin:")) {
        //
        viewName = viewName.substring("plugin:".length());
        Resource resource = resourceLoader.getResource("/WEB-INF/plugins");
        File[] pluginDirs = resource.getFile().listFiles();
        for (File pluginDir : pluginDirs) {
            File targetFile = new File(pluginDir, "/META-INF/widgets/" + viewName + ".jsp");
            if (targetFile.exists()) {
                viewName = pluginDir.getName() + "/META-INF/widgets/" + viewName;
            }
        }
        //
        modelAndView.setViewName(viewName);
    }
}

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

@Override
public PDDocumentContainer fromParts(ResourcesFactory sResourcesFactory, Map<String, Object> sParameters)
        throws PDDocumentFactoryException {
    PDDocumentContainer aContainer = null;
    if (sResourcesFactory != null) {
        Map<String, Object> aParameters = getMergedParameters(sParameters);
        PDDocument aPart = null;/*  w  ww .  j av  a 2s .co m*/
        List<PDDocument> aParts = new ArrayList<PDDocument>();
        try {
            Resource[] aResources = sResourcesFactory.getResources(aParameters);
            if (aResources != null) {
                for (Resource aResource : aResources) {
                    aPart = loadDocument(aResource.getFile(), aParameters);
                    aParts.add(aPart);
                }
            }
            aContainer = new BasicPDDocumentContainer(null, aParts);

        } catch (Exception e) {
            Exception aException = e;
            try {
                if (aContainer != null) {
                    aContainer.close();
                } else if (aPart != null) {
                    aPart.close();
                }
            } catch (Exception ex) {
                aException = new PDDocumentFactoryException(ex);
            }
            throw new PDDocumentFactoryException(aException);
        }
    }
    return aContainer;
}