Example usage for org.springframework.core.io Resource exists

List of usage examples for org.springframework.core.io Resource exists

Introduction

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

Prototype

boolean exists();

Source Link

Document

Determine whether this resource actually exists in physical form.

Usage

From source file:com.netflix.genie.web.configs.GenieApiAutoConfiguration.java

/**
 * Get the jobs dir as a Spring Resource. Will create if it doesn't exist.
 *
 * @param resourceLoader The resource loader to use
 * @param jobsProperties The jobs properties to use
 * @return The job dir as a resource/* w  w w  .  j  a va 2  s  .  co m*/
 * @throws IOException on error reading or creating the directory
 */
@Bean
@ConditionalOnMissingBean(name = "jobsDir", value = Resource.class)
public Resource jobsDir(final ResourceLoader resourceLoader, final JobsProperties jobsProperties)
        throws IOException {
    final String jobsDirLocation = jobsProperties.getLocations().getJobs();
    final Resource tmpJobsDirResource = resourceLoader.getResource(jobsDirLocation);
    if (tmpJobsDirResource.exists() && !tmpJobsDirResource.getFile().isDirectory()) {
        throw new IllegalStateException(jobsDirLocation + " exists but isn't a directory. Unable to continue");
    }

    // We want the resource to end in a slash for use later in the generation of URL's
    final String slash = "/";
    String localJobsDir = jobsDirLocation;
    if (!jobsDirLocation.endsWith(slash)) {
        localJobsDir = localJobsDir + slash;
    }
    final Resource jobsDirResource = resourceLoader.getResource(localJobsDir);

    if (!jobsDirResource.exists()) {
        final File file = jobsDirResource.getFile();
        if (!file.mkdirs()) {
            throw new IllegalStateException(
                    "Unable to create jobs directory " + jobsDirLocation + " and it doesn't exist.");
        }
    }

    return jobsDirResource;
}

From source file:com.github.mybatis.repository.autoconfig.MybatisAutoConfiguration.java

@PostConstruct
public void checkConfigFileExists() {
    if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
        Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
        Assert.state(resource.exists(), "Cannot find config location: " + resource
                + " (please add config file or check your Mybatis configuration)");
    }//w w  w .j  a  v  a2 s. c o  m
}

From source file:com.github.mjeanroy.springmvc.view.mustache.core.DefaultMustacheTemplateLoader.java

@Override
public Reader getTemplate(String name) {
    final String templateName = resolve(name);
    final Resource resource = resourceLoader.getResource(templateName);

    if (!resource.exists()) {
        log.error("Mustache template '{}' does not exist, template is not found", templateName);
        throw new MustacheTemplateNotFoundException(templateName);
    }//from w w w  . jav  a  2  s  . c  om

    try {
        final InputStream inputStream = resource.getInputStream();
        return new InputStreamReader(inputStream);
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
        throw new MustacheTemplateException(ex);
    }
}

From source file:org.apache.james.container.spring.lifecycle.ConfigurationProviderImpl.java

/**
 * @see ConfigurationProvider#getConfiguration(java.lang.String)
 *///from  w  ww  . ja v a 2s .  com
public HierarchicalConfiguration getConfiguration(String name) throws ConfigurationException {

    HierarchicalConfiguration conf = configurations.get(name);

    // Simply return the configuration if it is already loaded.
    if (conf != null) {
        return conf;
    }

    // Load the configuration.
    else {

        // Compute resourceName and configPart (if any, configPart can
        // remain null).
        int i = name.indexOf(".");
        String resourceName;
        String configPart = null;

        if (i > -1) {
            resourceName = name.substring(0, i);
            configPart = name.substring(i + 1);
        } else {
            resourceName = name;
        }

        Resource resource = loader.getResource(getConfigPrefix() + resourceName + CONFIGURATION_FILE_SUFFIX);

        if (resource.exists()) {
            try {
                HierarchicalConfiguration config = getConfig(resource);
                if (configPart != null) {
                    return config.configurationAt(configPart);
                } else {
                    return config;
                }

            } catch (Exception e) {
                throw new ConfigurationException("Unable to load configuration for component " + name, e);
            }
        }
    }

    // Configuration was not loaded, throw exception.
    throw new ConfigurationException("Unable to load configuration for component " + name);

}

From source file:com.netflix.genie.web.configs.MvcConfig.java

/**
 * Get the jobs dir as a Spring Resource. Will create if it doesn't exist.
 *
 * @param resourceLoader The resource loader to use
 * @param jobsProperties The jobs properties to use
 * @return The job dir as a resource/*from   ww w .  j a va  2 s. co  m*/
 * @throws IOException on error reading or creating the directory
 */
@Bean
@ConditionalOnMissingBean
public Resource jobsDir(final ResourceLoader resourceLoader, final JobsProperties jobsProperties)
        throws IOException {
    final String jobsDirLocation = jobsProperties.getLocations().getJobs();
    final Resource tmpJobsDirResource = resourceLoader.getResource(jobsDirLocation);
    if (tmpJobsDirResource.exists() && !tmpJobsDirResource.getFile().isDirectory()) {
        throw new IllegalStateException(jobsDirLocation + " exists but isn't a directory. Unable to continue");
    }

    // We want the resource to end in a slash for use later in the generation of URL's
    final String slash = "/";
    String localJobsDir = jobsDirLocation;
    if (!jobsDirLocation.endsWith(slash)) {
        localJobsDir = localJobsDir + slash;
    }
    final Resource jobsDirResource = resourceLoader.getResource(localJobsDir);

    if (!jobsDirResource.exists()) {
        final File file = jobsDirResource.getFile();
        if (!file.mkdirs()) {
            throw new IllegalStateException(
                    "Unable to create jobs directory " + jobsDirLocation + " and it doesn't exist.");
        }
    }

    return jobsDirResource;
}

From source file:com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheTemplateLoaderFactoryBeanTest.java

@Test
public void classpath_resource_loader_should_load_resource_from_classpath_and_remove_prefix() throws Exception {
    Resource resource = classpathResourceLoader.getResource("classpath:/templates/foo.template.html");
    assertThat(resource).isNotNull();/*from   ww w .j a v a2 s.  c  o m*/
    assertThat(resource.exists()).isTrue();
    assertThat(resource.getURI().toString()).startsWith("file:/")
            .endsWith("/target/test-classes/templates/foo.template.html");
}

From source file:com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheTemplateLoaderFactoryBeanTest.java

@Test
public void classpath_resource_loader_should_load_resource_from_classpath_and_return_resource_if_it_does_not_exist()
        throws Exception {
    Resource resource = classpathResourceLoader.getResource("classpath:/templates/fake.template.html");
    assertThat(resource).isNotNull();/*from w  ww  .  j  av a2s  . c  om*/
    assertThat(resource.exists()).isFalse();
}

From source file:com.enonic.cms.core.structure.SitePropertiesServiceImpl.java

private SiteProperties loadSiteProperties(final SiteKey siteKey) {
    final Properties properties = new Properties(defaultProperties);
    properties.setProperty("sitekey", String.valueOf(siteKey));

    final String relativePathToCmsHome = "/config/site-" + siteKey + ".properties";
    boolean custom = false;
    try {//from  ww  w  . j ava2  s.c o  m
        String resourcePath = this.homeDir.toURI().toURL() + relativePathToCmsHome;
        Resource resource = resourceLoader.getResource(resourcePath);
        boolean useCustomProperties = resource.exists();
        if (useCustomProperties) {
            InputStream stream = resource.getInputStream();
            properties.load(stream);
            properties.setProperty("customSiteProperties", "true");
            custom = true;
            stream.close();
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to load site properties file: " + relativePathToCmsHome, e);
    }

    properties.setProperty(SitePropertyNames.URL_DEFAULT_CHARACTER_ENCODING, this.characterEncoding);

    final SiteProperties siteProperties = new SiteProperties(siteKey, properties);
    sitePropertiesMap.put(siteKey, siteProperties);

    if (custom) {
        LOG.info("Loaded custom properties for site #{}", siteKey);
    } else {
        LOG.info("Loaded default properties for site #{}", siteKey);
    }

    return siteProperties;
}

From source file:it.publisys.ims.discovery.job.EntityTasks.java

@Scheduled(fixedRate = 600000, initialDelay = 5000)
public void reloadEntities() {
    log.debug("Reload Entities");

    log.debug("ServletContext: " + servletContext);

    try {/*from   ww  w.j  a  va 2 s  .  c  om*/
        Resource resourceDir = new ClassPathResource(METADATA_DIR + "/" + GUARDS_DIR);

        if (!resourceDir.exists()) {
            throw new FileNotFoundException(
                    String.format("Directory dei Medatada non presente. [%s]", resourceDir));
        }

        List<File> files = loadMetadataXml(resourceDir.getFile());
        EntitiesDescriptorDocument entitiesDescriptorDocument = storeEntitiesFile(files);

        EntityDescriptorType[] entityDescriptorTypes = loadAndCacheEntities(entitiesDescriptorDocument);
        loadEntities(entityDescriptorTypes);

    } catch (FileNotFoundException fnfe) {
        log.warn(fnfe.getMessage(), fnfe);
    } catch (IOException ioe) {
        log.warn("Caricamento Metadata non riuscito.", ioe);
    }

}