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:grails.plugin.searchable.internal.compass.config.mapping.CompassMappingXmlSearchableGrailsDomainClassMappingConfigurator.java

/**
 * Configure the Mapping in the CompassConfiguration for the given domain class
 *
 * @param compassConfiguration          the CompassConfiguration instance
 * @param configurationContext          a configuration context, for flexible parameter passing
 * @param searchableGrailsDomainClasses searchable domain classes to map
 * @param allSearchableGrailsDomainClasses all searchable domain classes, whether configured here or elsewhere
 *///from  www .  j  a v a  2s  . co  m
public void configureMappings(CompassConfiguration compassConfiguration, Map configurationContext,
        Collection searchableGrailsDomainClasses, Collection allSearchableGrailsDomainClasses) {
    Assert.notNull(resourceLoader, "resourceLoader cannot be null");
    if (configurationContext.containsKey(CompassXmlConfigurationSearchableCompassConfigurator.CONFIGURED)) {
        return;
    }

    for (Iterator iter = searchableGrailsDomainClasses.iterator(); iter.hasNext();) {
        GrailsDomainClass grailsDomainClass = (GrailsDomainClass) iter.next();
        Resource resource = getMappingResource(grailsDomainClass);
        Assert.isTrue(resource.exists(),
                "expected mapping resource [" + resource + "] to exist but it does not");
        try {
            compassConfiguration.addURL(resource.getURL());
        } catch (IOException ex) {
            String message = "Failed to configure Compass with mapping resource for class ["
                    + grailsDomainClass.getClazz().getName() + "] and resource ["
                    + getMappingResourceName(grailsDomainClass) + "]";
            LOG.error(message, ex);
            throw new IllegalStateException(message + ": " + ex);
        }
    }
}

From source file:com.haulmont.cuba.core.sys.ResourcesImplTest.java

public void testGetResource() {
    Resource resource;

    resource = resources.getResource("blablabla");
    assertNotNull(resource);/*from   w  ww . j a  va2s . c o m*/
    assertFalse(resource.exists());
    assertFalse(resource.isReadable());

    resource = resources.getResource("com/haulmont/cuba/core/sys/ResourcesImplTest.class");
    assertNotNull(resource);
    assertTrue(resource.exists());
    assertTrue(resource.isReadable());

    resource = resources.getResource("/com/haulmont/cuba/core/sys/ResourcesImplTest.class");
    assertNotNull(resource);
    assertTrue(resource.exists());
    assertTrue(resource.isReadable());
}

From source file:com.wavemaker.tools.project.AbstractStudioFileSystem.java

private synchronized void createCommonDir(Resource common) throws IOException {
    if (!common.exists()) {
        Resource templateFile = getStudioWebAppRoot().createRelative("lib/wm/" + COMMON_DIR);
        if (templateFile.exists()) {
            copyRecursive(templateFile, common, IOUtils.DEFAULT_EXCLUSION);
        }//from www . j  a  v  a  2s.c  om
    }
}

From source file:net.sf.jooreports.web.spring.controller.AbstractDocumentGenerator.java

private void renderDocument(Object model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    DocumentConverter converter = (DocumentConverter) getApplicationContext().getBean("documentConverter");
    DocumentFormatRegistry formatRegistry = (DocumentFormatRegistry) getApplicationContext()
            .getBean("documentFormatRegistry");
    String outputExtension = FilenameUtils.getExtension(request.getRequestURI());
    DocumentFormat outputFormat = formatRegistry.getFormatByFileExtension(outputExtension);
    if (outputFormat == null) {
        throw new ServletException("unsupported output format: " + outputExtension);
    }//from  ww w.j  av a 2 s .  c  om
    File templateFile = null;
    String documentName = FilenameUtils.getBaseName(request.getRequestURI());
    Resource templateDirectory = getTemplateDirectory(documentName);
    if (templateDirectory.exists()) {
        templateFile = templateDirectory.getFile();
    } else {
        templateFile = getTemplateFile(documentName).getFile();
        if (!templateFile.exists()) {
            throw new ServletException("template not found: " + documentName);
        }
    }

    DocumentTemplateFactory documentTemplateFactory = new DocumentTemplateFactory();
    DocumentTemplate template = documentTemplateFactory.getTemplate(templateFile);

    ByteArrayOutputStream odtOutputStream = new ByteArrayOutputStream();
    try {
        template.createDocument(model, odtOutputStream);
    } catch (DocumentTemplateException exception) {
        throw new ServletException(exception);
    }
    response.setContentType(outputFormat.getMimeType());
    response.setHeader("Content-Disposition",
            "inline; filename=" + documentName + "." + outputFormat.getFileExtension());

    if ("odt".equals(outputFormat.getFileExtension())) {
        // no need to convert
        response.getOutputStream().write(odtOutputStream.toByteArray());
    } else {
        ByteArrayInputStream odtInputStream = new ByteArrayInputStream(odtOutputStream.toByteArray());
        DocumentFormat inputFormat = formatRegistry.getFormatByFileExtension("odt");
        converter.convert(odtInputStream, inputFormat, response.getOutputStream(), outputFormat);
    }
}

From source file:com.searchbox.collection.pubmed.PubmedCollection.java

public ItemReader<Resource> reader() {
    return new ItemReader<Resource>() {
        boolean hasmore = true;

        @Override/*from  w w  w  .  ja  va  2 s  .c om*/
        public Resource read() {
            if (hasmore) {
                hasmore = false;
                Resource resource = context.getResource("classpath:data/pubmedIndex.xml");
                if (resource.exists()) {
                    LOGGER.info("Read has created this resource: " + resource.getFilename());
                    return resource;
                }
            }
            return null;
        }
    };
}

From source file:org.cloudfoundry.identity.uaa.integration.TestProfileEnvironment.java

private TestProfileEnvironment() {

    List<Resource> resources = new ArrayList<Resource>();

    for (String location : DEFAULT_PROFILE_CONFIG_FILE_LOCATIONS) {
        location = environment.resolvePlaceholders(location);
        Resource resource = recourceLoader.getResource(location);
        if (resource != null && resource.exists()) {
            resources.add(resource);/*from   ww w . j av a2 s  .  c om*/
        }
    }

    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(resources.toArray(new Resource[resources.size()]));
    factory.setDocumentMatchers(Collections.singletonMap("platform",
            environment.acceptsProfiles("postgresql") ? "postgresql" : "hsqldb"));
    factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);
    Properties properties = factory.getObject();

    logger.debug("Decoding environment properties: " + properties.size());
    if (!properties.isEmpty()) {
        for (Enumeration<?> names = properties.propertyNames(); names.hasMoreElements();) {
            String name = (String) names.nextElement();
            String value = properties.getProperty(name);
            if (value != null) {
                properties.setProperty(name, environment.resolvePlaceholders(value));
            }
        }
        if (properties.containsKey("spring_profiles")) {
            properties.setProperty(StandardEnvironment.ACTIVE_PROFILES_PROPERTY_NAME,
                    properties.getProperty("spring_profiles"));
        }
        // System properties should override the ones in the config file, so add it last
        environment.getPropertySources().addLast(new PropertiesPropertySource("uaa.yml", properties));
    }

    EnvironmentPropertiesFactoryBean environmentProperties = new EnvironmentPropertiesFactoryBean();
    environmentProperties.setEnvironment(environment);
    environmentProperties.setDefaultProperties(properties);
    Map<String, ?> debugProperties = environmentProperties.getObject();
    logger.debug("Environment properties: " + debugProperties);
}

From source file:org.esupportail.portlet.filemanager.services.ResourceUtils.java

public void afterPropertiesSet() throws Exception {

    try {//from   w w  w  .j  a  va2s  .c o m
        Resource iconsFolder = rl.getResource("img/icons");
        assert iconsFolder.exists();

        FileFilter fileFilter = new WildcardFileFilter("*.png");
        List<File> files = Arrays.asList(iconsFolder.getFile().listFiles(fileFilter));
        for (File icon : files) {
            String iconName = icon.getName();
            icons.put(iconName.substring(0, iconName.length() - 4),
                    "/esup-filemanager/img/icons/".concat(iconName));
        }

        log.debug("mimetypes incons retrieved : " + icons.toString());
    } catch (FileNotFoundException e) {
        log.error("FileNotFoundException getting icons ...", e);
    }

}

From source file:org.alfresco.encryption.SpringKeyResourceLoader.java

/**
 * Helper method to switch between application context resource loading or
 * simpler current classloader resource loading.
 *//*from w ww .j  a v  a2  s .c  om*/
private InputStream getSafeInputStream(String location) {
    try {
        final InputStream is;
        if (applicationContext != null) {
            Resource resource = applicationContext.getResource(location);
            if (resource.exists()) {
                is = new BufferedInputStream(resource.getInputStream());
            } else {
                // Fall back to conventional loading
                File file = ResourceUtils.getFile(location);
                if (file.exists()) {
                    is = new BufferedInputStream(new FileInputStream(file));
                } else {
                    is = null;
                }
            }
        } else {
            // Load conventionally (i.e. we are in a unit test)
            File file = ResourceUtils.getFile(location);
            if (file.exists()) {
                is = new BufferedInputStream(new FileInputStream(file));
            } else {
                is = null;
            }
        }

        return is;
    } catch (IOException e) {
        return null;
    }
}

From source file:org.carewebframework.ui.command.CommandShortcuts.java

public CommandShortcuts(Resource mappings) {
    put("help", "#f1");
    String filename = " '" + mappings.getFilename() + "'.";
    InputStream is;/*  www  .  j  ava  2 s. com*/

    try {
        if (!mappings.exists()) {
            log.info("No shortcut mappings found at" + filename);
            return;
        }

        is = mappings.getInputStream();

        if (is != null) {
            load(is);
            is.close();
            log.info("Shortcut mappings loaded from" + filename);
        }
    } catch (IOException e) {
        log.error("Error loading shortcut mappings from" + filename, e);
    }
}

From source file:org.paxml.core.PaxmlResource.java

public long getLastModified() {
    Resource res = getSpringResource();
    if (!res.exists()) {
        return -1;
    }/*from  w w  w .  j  a  va  2 s .co m*/
    final File file;
    try {
        file = res.getFile();
    } catch (IOException e) {
        return 0;
    }
    return file.lastModified();

}