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:test.gov.nih.nci.logging.api.persistence.TestSpringLocationSessionFactoryBean.java

/**
 * @param args//  www. j a  va  2  s  .  co  m
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    java.io.InputStream in = FileLoader.getInstance().getFileAsStream("commons-logging.properties");
    Resource resource = new InputStreamResource(in);
    System.out.println(resource.exists());

    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    String mappingResources[] = { "gov/nih/nci/logging/api/domain/LogMessage.hbm.xml",
            "gov/nih/nci/logging/api/domain/ObjectAttribute.hbm.xml" };
    localSessionFactoryBean.setMappingResources(mappingResources);
    BasicDataSource ds = setDataSourceProperties();
    localSessionFactoryBean.setDataSource(ds);
    localSessionFactoryBean.setHibernateProperties(getJDBCProperties());

    try {
        localSessionFactoryBean.afterPropertiesSet();
    } catch (HibernateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    SessionFactory sf = (SessionFactory) localSessionFactoryBean.getObject();
    //SessionFactory sessionFactory = (SessionFactory) ObjectFactory.getObject(ObjectFactory.HibernateSessionFactory);
}

From source file:config.ResourceUtils.java

/**
 * Checks if a resource with the given relative path exists on the file
 * system./*from ww w  .ja va  2  s  . co  m*/
 *
 * @param relativePath the relative path of the resource
 * @return the is existing boolean
 */
public static boolean isExistingFile(String relativePath) {
    boolean isExistingResource = Boolean.FALSE;
    Resource resource = new FileSystemResource(relativePath);
    if (resource.exists()) {
        isExistingResource = true;
    }

    return isExistingResource;
}

From source file:config.ResourceUtils.java

/**
 * Gets a resource by its relative path. If the resource is not found on the
 * file system, the classpath is searched. If nothing is found, null is
 * returned./*w  ww . j  a va  2s.c  om*/
 *
 * @param fileName the name of the resource
 * @return the found resource
 */
public static Resource getResourceByRelativePath(String fileName) {
    Resource resource = new FileSystemResource(RESOURCES_FOLDER + File.separator + fileName);
    if (!resource.exists()) {
        //try to find it on the classpath
        resource = new ClassPathResource(fileName);
        if (!resource.exists()) {
            // making sure to run on Netbeans..
            resource = new FileSystemResource("src" + File.separator + "main" + File.separator
                    + RESOURCES_FOLDER + File.separator + fileName);
            if (!resource.exists()) {
                resource = null;
            }
        }
    }
    return resource;
}

From source file:org.sakaiproject.genericdao.springutil.ResourceFinder.java

private static List<Resource> makeResources(List<String> paths) {
    List<Resource> rs = new ArrayList<Resource>();
    if (paths != null && !paths.isEmpty()) {
        ClassLoader cl = ResourceFinder.class.getClassLoader();
        for (String path : paths) {
            Resource r = new ClassPathResource(path, cl);
            if (r.exists()) {
                rs.add(r);/* ww w  .  j a  v a 2  s. c om*/
            }
        }
    }
    return rs;
}

From source file:com.trigonic.utils.spring.beans.ImportHelper.java

private static void importAbsoluteResourcePattern(XmlBeanDefinitionReader reader, String location,
        Set<Resource> actualResources, ResourcePatternResolver resourceLoader) {
    try {//from ww w  .  ja  v a  2  s .c o m
        List<Resource> resources = new ArrayList<Resource>(
                Arrays.asList(resourceLoader.getResources(location)));
        int loadCount = 0;
        for (Resource resource : resources) {
            if (resource.exists()) {
                loadCount += reader.loadBeanDefinitions(resource);
                actualResources.add(resource);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
        }
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException(
                "Could not resolve bean definition resource pattern [" + location + "]", ex);
    }
}

From source file:com.trigonic.utils.spring.beans.ImportHelper.java

private static void importSingleAbsoluteResource(XmlBeanDefinitionReader reader, String location,
        Set<Resource> actualResources, ResourceLoader resourceLoader) {
    Resource resource = resourceLoader.getResource(location);
    if (resource.exists()) {
        int loadCount = reader.loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }/*from  w ww . jav  a  2  s  .c om*/
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
        }
    }
}

From source file:gov.nih.nci.integration.util.CommonsPropertyLoaderUtil.java

/**
 * fill properties from given resource//  www .j  a v a2s .  co m
 * 
 * @param resource - resource
 * @param properties - properties to fill
 */
private static void fillProperties(Resource resource, Properties properties) {
    try {
        if (resource.exists()) {
            LOG.info("merging properties form : " + resource.getDescription());
            PropertiesLoaderUtils.fillProperties(properties, resource);
        } else {
            LOG.info(String.format("can not merge property from %s as resource does not exists.",
                    resource.getDescription()));
        }
    } catch (IOException e) {
        final String message = "error while loading properties from resource" + resource;
        LOG.error(message, e);
        throw new RuntimeException(message, e);// NOPMD
    }
}

From source file:gov.nih.nci.cacis.common.util.CommonsPropertyLoaderUtil.java

/**
 * fill properties from given resource//from w  ww  .jav  a2s. com
 *
 * @param resource   - resource
 * @param properties - properties to fill
 */
private static void fillProperties(Resource resource, Properties properties) {
    try {
        if (resource.exists()) {
            LOG.info("merging properties form : " + resource.getDescription());
            PropertiesLoaderUtils.fillProperties(properties, resource);
        } else {
            LOG.info(String.format("can not merge property from %s as resource does not exists.",
                    resource.getDescription()));
        }
    } catch (IOException e) {
        final String message = "error while loading properties from resource" + resource;
        LOG.error(message, e);
        throw new ApplicationRuntimeException(message, e);
    }
}

From source file:magoffin.matt.sobriquet.test.DirectoryServerStatement.java

public static InMemoryDirectoryServer startServer(final int port, final String baseDN, final String authDN,
        final String authPassword, final String[] ldifFiles, final String[] schemaFiles)
        throws LDIFException, LDAPException, IOException {
    final InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig("default", port);
    final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(new DN(baseDN));
    config.setListenerConfigs(listenerConfig);
    config.addAdditionalBindCredentials(authDN, authPassword);
    if (schemaFiles != null && schemaFiles.length > 0) {
        // merge custom schemas with default standard schemas
        List<File> files = new ArrayList<File>(schemaFiles.length);
        for (String name : schemaFiles) {
            // try fs first
            Resource r = new FileSystemResource(name);
            if (r.exists()) {
                files.add(r.getFile());// www.  ja  v a  2 s  . com
            } else {
                r = new ClassPathResource(name);
                if (r.exists()) {
                    files.add(r.getFile());
                }
            }
        }
        if (files.size() > 0) {
            config.setSchema(Schema.mergeSchemas(Schema.getDefaultStandardSchema(), Schema.getSchema(files)));
        }
    }
    final InMemoryDirectoryServer server = new InMemoryDirectoryServer(config);
    server.add(new Entry(baseDN, new Attribute("objectclass", "domain", "top")));
    server.startListening();
    if (ldifFiles != null) {
        for (final String ldifFile : ldifFiles) {
            DirectoryServerUtils.loadData(server, ldifFile);
        }
    }
    return server;
}

From source file:com.trigonic.utils.spring.beans.ImportHelper.java

private static void importRelativeResource(XmlBeanDefinitionReader reader, Resource sourceResource,
        String location, Set<Resource> actualResources) {
    int importCount = 0;
    try {/*  w w  w  .j  a va 2  s . co m*/
        Resource relativeResource = sourceResource.createRelative(location);
        if (relativeResource.exists()) {
            importCount = reader.loadBeanDefinitions(relativeResource);
            actualResources.add(relativeResource);
        }
    } catch (IOException e) {
        throw new BeanDefinitionStoreException("Could not resolve current location [" + location + "]", e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
    }
}