Example usage for org.springframework.core.io ResourceLoader getResource

List of usage examples for org.springframework.core.io ResourceLoader getResource

Introduction

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

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:sf.wicklet.site.gwt.test.db.TestSpringJDBCH201.java

protected EmbeddedDatabase createDatabase(final File dbfile, final String dbpath)
        throws ClassNotFoundException {
    final EmbeddedDatabaseFactory f = new EmbeddedDatabaseFactory();
    final ResourceDatabasePopulator p = new ResourceDatabasePopulator();
    final ResourceLoader l = new DefaultResourceLoader();
    if (!dbfile.exists()) {
        final String classname = getClass().getName().replace('.', '/');
        p.addScript(l.getResource(String.format("classpath:/%s-schema.sql", classname)));
        p.addScript(l.getResource(String.format("classpath:/%s-init.sql", classname)));
    }/*from  w ww . j a v a2  s. c  o m*/
    f.setDatabasePopulator(p);
    f.setDatabaseConfigurer(sf.wicklet.gwt.site.server.db.H2Configurator.getInstance(dbpath));
    // Database name now a dont' care.
    //        f.setDatabaseName("db01");
    return f.getDatabase();
}

From source file:com.yeahmobi.yunit.dataset.AbstractDataSetLoader.java

/**
 * Loads a {@link IDataSet dataset} from {@link Resource}s obtained from the specified <tt>location</tt>. Each
 * <tt>location</tt> can be mapped to a number of potential {@link #getResourceLocations resources}, the first
 * resource that {@link Resource#exists() exists} will be used. {@link Resource}s are loaded using the
 * {@link ResourceLoader} returned from {@link #getResourceLoader}.
 * <p>//from ww w  . j a v a2 s .  c o m
 * If no resource can be found then <tt>null</tt> will be returned.
 *
 * @see #createDataSet(Resource)
 * @see com.yeahmobi.yunit.dataset.DataSetLoader#loadDataSet(Class,Method,String,String) java.lang.String)
 */
public IDataSet loadDataSet(Class<?> testClass, Method testMethod, String location, String suffix)
        throws Exception {
    ResourceLoader[] resourceLoaders = getResourceLoader(testClass);
    for (ResourceLoader resourceLoader : resourceLoaders) {
        String[] resourceLocations = getResourceLocations(testClass, testMethod, location, suffix);
        for (String resourceLocation : resourceLocations) {
            Resource resource = resourceLoader.getResource(resourceLocation);
            if (resource.exists()) {
                return createDataSet(resource);
            }
        }
    }
    return null;
}

From source file:com.domingosuarez.boot.autoconfigure.jade4j.Jade4JTemplateAvailabilityProvider.java

@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
        ResourceLoader resourceLoader) {
    if (ClassUtils.isPresent("de.neuland.jade4j.spring.template.SpringTemplateLoader", classLoader)) {
        String prefix = environment.getProperty("spring.jade4j.prefix", Jade4JAutoConfiguration.DEFAULT_PREFIX);
        String suffix = environment.getProperty("spring.jade4j.suffix", Jade4JAutoConfiguration.DEFAULT_SUFFIX);
        return resourceLoader.getResource(prefix + view + suffix).exists();
    }/*  w w  w .j  av  a  2s  .c  o m*/

    return false;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.issues.IssuesReadersConfiguration.java

/**
 * Return a reader that gets a list of issues ids from a CSV file.
 * The CSV file must not have a header line for columns definition.
 * The file is loaded through Spring resource loader so the filepath can contains
 * definitions like classpath: and others.
 *
 * @param resourceLoader/* ww w . j  a  va2  s.  com*/
 *          Spring resource loader
 * @param filePath
 *          File path of the CSV file
 * @return the reader
 */
@Bean
@StepScope
public FlatFileItemReader<BugIdBean> csvIssuesReader(final ResourceLoader resourceLoader,
        @Value("#{jobParameters['mantis.filepath']}") final String filePath) {

    final FlatFileItemReader<BugIdBean> reader = new FlatFileItemReader<BugIdBean>();
    reader.setResource(resourceLoader.getResource(filePath));

    final DefaultLineMapper<BugIdBean> lineMapper = new DefaultLineMapper<BugIdBean>();

    final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(new String[] { "id" });
    lineMapper.setLineTokenizer(tokenizer);

    final BeanWrapperFieldSetMapper<BugIdBean> mapper = new BeanWrapperFieldSetMapper<BugIdBean>();
    mapper.setTargetType(BugIdBean.class);
    lineMapper.setFieldSetMapper(mapper);

    reader.setLineMapper(lineMapper);

    return reader;
}

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//ww  w . j av  a 2 s  .  c  o 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.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  w  ww.  ja v  a  2s.c  om
 * @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:ar.com.zauber.commons.web.proxy.impl.dao.properties.provider.JndiPropertiesProvider.java

/**
 * Creates the JndiPropertiesProvider.// ww  w.  java  2 s .co  m
 *
 * @param jndiVariable
 * @param resourceLoader
 * @param fallback
 * @throws IOException .
 */
public JndiPropertiesProvider(final String jndiVariable, final ResourceLoader resourceLoader,
        final PropertiesProvider fallback) throws IOException {
    Validate.notEmpty(jndiVariable);
    Validate.notNull(resourceLoader);
    Validate.notNull(fallback);

    try {
        final InitialContext initCtx = new InitialContext();
        final Context envCtx = (Context) initCtx.lookup("java:comp/env");
        final Resource resource = resourceLoader.getResource((String) envCtx.lookup(jndiVariable));
        target = new ResourcePropertiesProvider(resource);
    } catch (final NamingException e) {
        target = fallback;
    }
}

From source file:jp.gr.java_conf.bufferings.thymeleaf.spring.boot.Thymeleaf3TemplateAvailabilityProvider.java

@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
        ResourceLoader resourceLoader) {
    if (ClassUtils.isPresent("org.thymeleaf.spring4.SpringTemplateEngine", classLoader)
            && ClassUtils.isPresent("org.thymeleaf.Thymeleaf", classLoader)) {
        PropertyResolver resolver = new RelaxedPropertyResolver(environment, "spring.thymeleaf3.");
        String prefix = resolver.getProperty("prefix", Thymeleaf3Properties.DEFAULT_PREFIX);
        String suffix = resolver.getProperty("suffix", Thymeleaf3Properties.DEFAULT_SUFFIX);
        return resourceLoader.getResource(prefix + view + suffix).exists();
    }// www  . j a  v a  2s. co  m
    return false;
}

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

/**
 * Make sure we can get a valid job resource when all conditions are met.
 *
 * @throws IOException for any problem//from  ww  w .  j  a  va  2  s.c  o m
 */
@Test
public void canGetJobsDir() throws IOException {
    final ResourceLoader resourceLoader = Mockito.mock(ResourceLoader.class);
    final String jobsDirLocation = UUID.randomUUID().toString() + "/";
    final JobsProperties jobsProperties = new JobsProperties();
    jobsProperties.getLocations().setJobs(jobsDirLocation);

    final Resource jobsDirResource = Mockito.mock(Resource.class);
    Mockito.when(resourceLoader.getResource(jobsDirLocation)).thenReturn(jobsDirResource);
    Mockito.when(jobsDirResource.exists()).thenReturn(true);

    final File file = Mockito.mock(File.class);
    Mockito.when(jobsDirResource.getFile()).thenReturn(file);
    Mockito.when(file.isDirectory()).thenReturn(true);

    final Resource jobsDir = this.mvcConfig.jobsDir(resourceLoader, jobsProperties);
    Assert.assertNotNull(jobsDir);
}