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:org.paxml.tag.AbstractPaxmlEntity.java

@Override
public boolean isModified() {
    Resource res = getResource().getSpringResource();
    if (!res.exists()) {
        return true;
    }/*from   w ww .  j  av  a  2  s .c o  m*/
    final File file;
    try {
        file = res.getFile();

    } catch (IOException e) {
        // if file is not obtainable, it is not modifiable.
        return false;
    }
    return !file.exists() || file.lastModified() != timestamp;
}

From source file:grails.plugin.searchable.internal.compass.config.CompassXmlConfigurationSearchableCompassConfigurator.java

/**
 * Configure Compass ready for it to be built
 *
 * @param compassConfiguration runtime configuration instance
 * @param configurationContext a context allowing flexible parameter passing
 *//*  w  w  w.  j a  v a 2  s  . c om*/
public void configure(CompassConfiguration compassConfiguration, Map configurationContext) {
    Assert.notNull(compassConfiguration, "compassConfiguration cannot be null");
    Assert.notNull(resourceLoader, "resourceLoader cannot be null");

    Resource resource = resourceLoader.getResource("classpath:/compass.cfg.xml");
    if (resource.exists()) {
        Assert.notNull(configurationContext, "configurationContext cannot be null");
        try {
            LOG.debug("Configuring Compass with compass config file [" + resource.getURL() + "]");
            compassConfiguration.configure(resource.getURL());
        } catch (IOException ex) {
            LOG.error("Failed to configure Compass with classpath resource, even though it apparently exists: "
                    + ex, ex);
            throw new IllegalStateException(
                    "Failed to configure Compass with classpath resource, even though it apparently exists: "
                            + ex);
        }
        configurationContext.put(CONFIGURED, true);
    }
}

From source file:org.codehaus.groovy.grails.plugins.searchable.compass.config.CompassXmlConfigurationSearchableCompassConfigurator.java

/**
 * Configure Compass ready for it to be built
 *
 * @param compassConfiguration runtime configuration instance
 * @param configurationContext a context allowing flexible parameter passing
 *//*  ww  w. j av a2  s. c  om*/
public void configure(CompassConfiguration compassConfiguration, Map configurationContext) {
    Assert.notNull(compassConfiguration, "compassConfiguration cannot be null");
    Assert.notNull(resourceLoader, "resourceLoader cannot be null");

    Resource resource = resourceLoader.getResource("classpath:/compass.cfg.xml");
    if (resource.exists()) {
        Assert.notNull(configurationContext, "configurationContext cannot be null");
        try {
            LOG.debug("Configuring Compass with compass config file [" + resource.getURL().toString() + "]");
            compassConfiguration.configure(resource.getURL());
        } catch (IOException ex) {
            LOG.error("Failed to configure Compass with classpath resource, even though it apparently exists: "
                    + ex, ex);
            throw new IllegalStateException(
                    "Failed to configure Compass with classpath resource, even though it apparently exists: "
                            + ex);
        }
        configurationContext.put(CONFIGURED, Boolean.TRUE);
    }
}

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

private Resource newResource(boolean exists) {
    Resource resource = mock(Resource.class);
    when(resource.exists()).thenReturn(exists);
    return resource;
}

From source file:com.github.mrstampy.gameboot.util.resource.AbstractFallbackResourceCondition.java

private boolean hasOverride(ResourceLoader loader) {
    if (overrides == null || overrides.length == 0)
        return false;

    for (String override : overrides) {
        Resource o = loader.getResource(override);
        if (o.exists()) {
            ResourceLogger.log(override, fallback);
            return true;
        }//from w w  w. j  ava 2s  . c o  m
    }

    return false;
}

From source file:org.beanio.spring.BeanIOStreamFactory.java

/**
 * Creates a new {@link StreamFactory} and loads configured stream mapping resources.
 * @return the new <tt>StreamFactory</tt>
 * @throws BeanIOConfigurationException if a stream mapping resource does not exist
 *   or is invalid // w w w. j  av a2s.  c o m
 * @throws IOException if an I/O error occurs
 */
protected StreamFactory createStreamFactory() throws IOException, BeanIOConfigurationException {
    StreamFactory factory = StreamFactory.newInstance();

    if (streamMappings != null) {
        for (Resource res : streamMappings) {
            if (!res.exists()) {
                throw new BeanIOConfigurationException("Mapping file not found: " + res);
            }

            InputStream in = res.getInputStream();
            try {
                factory.load(in);
            } catch (BeanIOConfigurationException ex) {
                throw new BeanIOConfigurationException(
                        "Error in mapping file '" + res + "': " + ex.getMessage(), ex);
            } finally {
                IOUtil.closeQuietly(in);
            }
        }
    }

    return factory;
}

From source file:com.baifendian.swordfish.webserver.service.storage.FileSystemStorageService.java

@Override
public Resource loadAsResource(String filename) {
    try {//from ww w .  j a  v a  2 s. co m
        Path file = Paths.get(filename);

        Resource resource = new UrlResource(file.toUri());
        if (resource.exists() || resource.isReadable()) {
            return resource;
        } else {
            throw new StorageFileNotFoundException("Could not read file: " + filename);

        }
    } catch (MalformedURLException e) {
        throw new StorageFileNotFoundException("Could not read file: " + filename, e);
    }
}

From source file:org.obiba.onyx.core.domain.participant.ParticipantAttributeReader.java

@SuppressWarnings("unchecked")
public List<ParticipantAttribute> read() throws IOException {
    List<ParticipantElement> participantElements = new ArrayList<ParticipantElement>();

    for (int i = 0; i < this.resources.length; i++) {
        Resource resource = this.resources[i];

        if (resource.exists()) {
            participantElements.addAll((List<ParticipantElement>) xstream.fromXML(resource.getInputStream()));
        }/*from   w  w w. j  av a  2 s  . co  m*/
    }

    return flatten(participantElements);
}

From source file:com.google.api.ads.adwords.jaxws.extensions.kratu.restserver.reports.GenerateReportsRest.java

public Representation getHandler() {
    String result = null;//from   w  ww .  j a v a 2 s  .c  o  m
    try {
        getParameters();

        // Generate Reports at MCC level for dates
        if (topAccountId != null && dateStart != null && dateEnd != null) {

            getContext().getParameters().add("maxThreads", "512");

            Resource resource = new ClassPathResource(file);
            if (!resource.exists()) {
                resource = new FileSystemResource(file);
            }
            DynamicPropertyPlaceholderConfigurer.setDynamicResource(resource);
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);

            ReportProcessor processor = getApplicationContext().getBean(ReportProcessor.class);

            // Launching a new Service(Thread) to make the request async.
            RunnableReport runnableReport = new RunnableReport(topAccountId, processor, properties, dateStart,
                    dateEnd);

            taskService.submit(runnableReport);

            result = "OK - Task created, this usually takes 10-15mins for 1000 accounts/month";
        } else {
            taskService.getContext();
        }
    } catch (Exception exception) {
        return handleException(exception);
    }
    addReadOnlyHeaders();
    return createJsonResult(result);
}

From source file:com.github.instaweb.jade.web.JadeViewResolver.java

private String resolveFromLocale(String viewName, String locale) {
    String name = getPrefix() + viewName + locale + getSuffix();
    Resource resource = getApplicationContext().getResource(name);
    if (resource == null || !resource.exists()) {
        if (locale.isEmpty()) {
            return null;
        }//from  w w  w .  j  av  a  2 s  . co m
        int index = locale.lastIndexOf("_");
        return resolveFromLocale(viewName, locale.substring(0, index));
    }
    return name;
}