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.teiid.spring.autoconfigure.MultiDataSourceInitializer.java

List<Resource> getResources(String propertyName, List<String> locations, boolean validate) {
    List<Resource> resources = new ArrayList<Resource>();
    for (String location : locations) {
        for (Resource resource : doGetResources(location)) {
            if (resource.exists()) {
                resources.add(resource);
            } else if (validate) {
                throw new ResourceNotFoundException(propertyName, resource);
            }/*  www.  jav a  2 s.co  m*/
        }
    }
    return resources;
}

From source file:org.teiid.spring.autoconfigure.TeiidInitializer.java

private static List<Resource> getResources(String propertyName, List<String> locations, boolean validate,
        ApplicationContext context) {// w  w w  .  j  av a 2  s.co  m
    List<Resource> resources = new ArrayList<Resource>();
    for (String location : locations) {
        for (Resource resource : doGetResources(location, context)) {
            if (resource.exists()) {
                resources.add(resource);
            } else if (validate) {
                throw new ResourceNotFoundException(propertyName, resource);
            }
        }
    }
    return resources;
}

From source file:org.tinygroup.jdbctemplatedslsession.SimpleDslSession.java

public SimpleDslSession(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
    simpleJdbcTemplate = new SimpleJdbcTemplate(jdbcTemplate);
    provider = new DefaultTableMetaDataProvider();
    dbType = provider.getDbType(dataSource);
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    Resource resource = resourceLoader.getResource("tinydsl.xml");
    if (resource.exists()) {
        try {/*from   w ww .  j  a v a2 s.c om*/
            ConfigurationBuilder builder = new ConfigurationBuilder(resource.getInputStream());
            configuration = builder.parse();
        } catch (IOException e) {
            LOGGER.errorMessage("??{}?", e,
                    e.getMessage());
            throw new DslRuntimeException(e);
        }
    } else {
        configuration = new Configuration();
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalUserDataServiceImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    log.debug("Load Resource from Location {}", properties.getLocation());
    Resource resource = resourceLoader.getResource(properties.getLocation());
    Validate.notNull(resource);/*from   www .j a va2s.c o m*/
    Validate.isTrue(resource.exists());
    Validate.isTrue(resource.isReadable());

    log.debug("Read the Resource {} to the Class {}", resource.getFilename(),
            OwncloudLocalUserData.class.getName());
    OwncloudLocalUserData resourceData = xmlMapper.readValue(resource.getInputStream(),
            OwncloudLocalUserData.class);
    checkGroupReferences(resourceData);

    log.trace("Clear the Users Map");
    users.clear();

    log.debug("Read the Users as a Map");
    if (CollectionUtils.isNotEmpty(resourceData.getUsers())) {
        for (OwncloudLocalUserData.User user : resourceData.getUsers()) {
            users.put(user.getUsername(), user);
        }
    }

    log.trace("Clear the Groups Map");
    groups.clear();
    log.debug("Read the Groups as a Map");
    if (CollectionUtils.isNotEmpty(resourceData.getGroups())) {
        groups.addAll(resourceData.getGroups());
    }

    log.info("User Information from Resource Location {} successfully loaded", properties.getLocation());
}

From source file:stroom.util.config.StroomProperties.java

private static void loadResource(final DefaultResourceLoader resourceLoader, final String resourceName,
        final Source source) {
    try {//w  w w.  ja v a  2  s.  c  o  m
        final Resource resource = StroomResourceLoaderUtil.getResource(resourceLoader, resourceName);

        if (resource != null && resource.exists()) {
            final InputStream is = resource.getInputStream();
            final Properties properties = new Properties();
            properties.load(is);
            CloseableUtil.close(is);

            for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
                final String key = (String) entry.getKey();
                final String value = (String) entry.getValue();
                if (value != null) {
                    setProperty(key, value, source);
                }
            }

            String path = "";
            try {
                final File file = resource.getFile();
                final File dir = file.getParentFile();
                path = dir.getPath();
                path = dir.getCanonicalPath();
            } catch (final Exception e) {
                // Ignore.
            }

            LOGGER.info("Using properties '%s' from '%s'", resourceName, path);

            // Is this this web app property file?
            if (Source.WAR.equals(source)) {
                try {
                    final File resourceFile = resource.getFile();
                    propertiesDir = resourceFile.getParentFile();
                } catch (final Exception ex) {
                    LOGGER.warn("Unable to locate properties dir ... maybe running in maven?");
                }
            }
        } else {
            LOGGER.info("Properties not found at '%s'", resourceName);
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

From source file:test.jdbc.datasource.DataSourceInitializer.java

private void doExecuteScript(final Resource scriptResource) {
    if (scriptResource == null || !scriptResource.exists())
        return;/*from ww w .j  a  va  2s . c  o m*/
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String[] scripts;
    try {
        String[] list = StringUtils.delimitedListToStringArray(
                stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";");
        scripts = list;
    } catch (IOException e) {
        throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
    }
    for (int i = 0; i < scripts.length; i++) {
        final String script = scripts[i].trim();
        TransactionTemplate transactionTemplate = new TransactionTemplate(
                new DataSourceTransactionManager(dataSource));
        transactionTemplate.execute(new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus status) {
                if (StringUtils.hasText(script)) {
                    try {
                        jdbcTemplate.execute(script);
                    } catch (DataAccessException e) {
                        if (!script.toUpperCase().startsWith("DROP")) {
                            throw e;
                        }
                    }
                }
                return null;
            }

        });
    }

}