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

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

Introduction

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

Prototype

@Override
public File getFile() throws IOException 

Source Link

Document

This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.

Usage

From source file:org.ngrinder.script.service.FileEntryService.java

/**
 * Load freemarker template for quick test.
 * //ww w .  ja va  2 s.c  o  m
 * @param user
 *            user
 * @param url
 *            url
 * @return generated test script
 */
public String loadFreeMarkerTemplate(User user, String url) {

    try {
        Configuration freemarkerConfig = new Configuration();
        ClassPathResource cpr = new ClassPathResource("script_template");
        freemarkerConfig.setDirectoryForTemplateLoading(cpr.getFile());
        freemarkerConfig.setObjectWrapper(new DefaultObjectWrapper());
        Template template = freemarkerConfig.getTemplate("basic_template.ftl");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", url);
        map.put("user", user);
        StringWriter writer = new StringWriter();
        template.process(map, writer);
        return writer.toString();
    } catch (Exception e) {
        LOG.error("Error while fetching template for quick start", e);
    }
    return "";
}

From source file:org.opentestsystem.shared.rest.ApiController.java

private void parseExamples() {
    if (apiRequestsByURI.isEmpty()) {
        try {//from  w  ww. j  av a2  s  .com
            ClassPathResource res = new ClassPathResource("api_examples.json");
            File apiExamples = res.getFile();
            String content = FileUtils.readFileToString(apiExamples);
            content = "[" + content.substring(0, content.length() - 1) + "]";
            ObjectMapper objectMapper = new ObjectMapper();
            String withWrapper = objectMapper.writeValueAsString(new ApiExampleList());
            withWrapper = withWrapper.replace("null", content);
            ApiExampleList values = objectMapper.readValue(withWrapper, ApiExampleList.class);
            for (ApiExample webRequest : values.getExamples()) {
                String uri = webRequest.getRequestUri();
                String context = uri;
                int index = uri.indexOf('/', 1);
                if (index > ZERO) {
                    context = uri.substring(0, index);
                }

                List<ApiExample> requests = apiRequestsByURI.get(context);
                if (requests == null) {
                    requests = new ArrayList<ApiExample>();
                    apiRequestsByURI.put(context, requests);
                }

                requests.add(webRequest);
                Collections.sort(requests);
            }
        } catch (IOException e) {
            LOGGER.error("", e);
        }
    }
}

From source file:org.springframework.security.config.doc.XsdDocumentedTests.java

/**
 * This will check to ensure that the expected number of xsd documents are found to ensure that we are validating
 * against the current xsd document. If this test fails, all that is needed is to update the schemaDocument
 * and the expected size for this test.//from w w w . ja va  2s . c o  m
 * @return
 */
@Test
public void sizeWhenReadingFilesystemThenIsCorrectNumberOfSchemaFiles() throws IOException {

    ClassPathResource resource = new ClassPathResource(this.schemaDocumentLocation);

    String[] schemas = resource.getFile().getParentFile().list((dir, name) -> name.endsWith(".xsd"));

    assertThat(schemas.length).isEqualTo(14)
            .withFailMessage("the count is equal to 14, if not then schemaDocument needs updating");
}

From source file:ro.nextreports.server.web.ApplicationLoaderListener.java

private void config() {

    LOG.info("NextReports Server " + ReleaseInfo.getVersion() + " starting ... ");

    CompositeConfiguration config = new CompositeConfiguration();
    config.addConfiguration(new SystemConfiguration());
    try {// www  .j ava  2  s.c  o m
        config.addConfiguration(new PropertiesConfiguration(getClass().getResource("/nextserver.properties")));
    } catch (ConfigurationException e) {
        // TODO
        e.printStackTrace();
    }

    File defaultNextServerHomeFolder = new File(System.getProperty("user.home"), ".nextserver");
    String defaultNextServerHome = defaultNextServerHomeFolder.getPath();
    String nextServerHome = config.getString("nextserver.home", defaultNextServerHome);
    LOG.info("nextserver.home = " + nextServerHome);

    String demo = System.getProperty("DEMO");
    LOG.info("DEMO=" + demo);
    boolean installWithDemo = Boolean.valueOf(demo);

    File nextServerHomeFolder = new File(nextServerHome);
    if (!nextServerHomeFolder.exists()) {
        try {
            if (installWithDemo) {
                deployDemoData(nextServerHome);
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Create nextserver home folder: '" + nextServerHomeFolder + "'");
                }
                FileUtils.forceMkdir(nextServerHomeFolder);
            }
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
    }

    System.setProperty("nextserver.home", nextServerHome);

    //      File defaultJaspersHomeFolder = new File(nextServerHome, "jaspers");
    //      String defaultJaspersHome = defaultJaspersHomeFolder.getPath();
    //      String jaspersHome = config.getString("jaspers.home", defaultJaspersHome);
    //      System.setProperty("jaspers.home", jaspersHome);
    //      if (LOG.isDebugEnabled()) {
    //         LOG.debug("jaspers.home = " + jaspersHome);         
    //      }      

    // indexing.file
    ClassPathResource classPathResource = new ClassPathResource("indexing.xml");
    String indexingFile = null;
    try {
        indexingFile = classPathResource.getFile().getAbsolutePath();
    } catch (IOException e) {
        // TODO
        e.printStackTrace();
    }
    System.setProperty("indexing.file", indexingFile);
    if (LOG.isDebugEnabled()) {
        LOG.debug("indexing.file = " + indexingFile);
    }

    //      String baseUrl = config.getString("nextserver.baseUrl");
    //      String reportsUrl = config.getString("reports.url");
    //      if (LOG.isDebugEnabled()) {
    //         LOG.debug("nextserver.baseUrl = " + baseUrl);
    //         LOG.debug("reports.url = " + reportsUrl);
    //      }
}