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:com.github.mjeanroy.springmvc.view.mustache.core.CompositeResourceLoader.java

@Override
public Resource getResource(String location) {
    log.debug("Get resource: {}", location);

    Resource resource = null;

    for (ResourceLoader resourceLoader : resourceLoaders) {
        log.trace("Test for resourceLoader: {}", resourceLoader);
        resource = resourceLoader.getResource(location);

        if (resource.exists()) {
            log.trace("Resource {} exist, return it", resource);
            return resource;
        }//from w  ww .j av  a2s  .c  o m
    }

    // Return last resource
    log.trace("Resource {} not found, return last computed value", resource);
    return resource;
}

From source file:com.haulmont.restapi.config.RestJsonTransformations.java

protected void init() {
    String configName = AppContext.getProperty(CUBA_REST_JSON_TRANSFORMATION_CONFIG_PROP_NAME);
    StrTokenizer tokenizer = new StrTokenizer(configName);
    for (String location : tokenizer.getTokenArray()) {
        Resource resource = resources.getResource(location);
        if (resource.exists()) {
            InputStream stream = null;
            try {
                stream = resource.getInputStream();
                loadConfig(Dom4j.readDocument(stream).getRootElement());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(stream);
            }/* w w  w. j a v  a 2s .com*/
        } else {
            log.warn("Resource " + location + " not found, ignore it");
        }
    }
}

From source file:edu.jhuapl.openessence.i18n.InspectableResourceBundleMessageSource.java

public Collection<Locale> getLocales() throws IOException {
    String basename = basenames[0];
    Resource resource = resourceLoader.getResource(basename + ".properties");
    if (!resource.exists()) {
        return Collections.emptyList();
    }/*from   w w w  .  j a v a 2  s  .  c  o m*/

    File baseFile = resource.getFile();
    final String bundleName = FilenameUtils.getBaseName(baseFile.getPath());
    File[] files = resource.getFile().getParentFile().listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(bundleName + "_") && name.endsWith(".properties");
        }
    });

    List<Locale> locales = new ArrayList<Locale>();
    for (File f : files) {
        String prefix = bundleName + "_";
        String code = f.getName().substring(prefix.length()).replace(".properties", "");

        locales.add(StringUtils.parseLocaleString(code));
    }

    return locales;
}

From source file:edu.jhuapl.openessence.i18n.InspectableResourceBundleMessageSource.java

/**
 * <p> Find a supported locale that is the best match to the given locale. For example, if we only support "en" and
 * "fr", then the best match for "en_US" would be "en". </p> The available locales are given by the properties files
 * for the 0th basename. For example, if the 0th element of the basenames array is "file:/home/messages", then this
 * method looks for all properties files in /home that begin with "messages". So if that directory has the files
 * "messages_en.properties" and "messages_fr.properties", then the available locales are "en" and "fr". The "best"
 * locale is given by the order of values returned by {@link #calculateFilenamesForLocale(String, Locale)}.
 *
 * @param locale the locale to match//from  w w w .  j a  v a2  s  .c  o  m
 * @return best match or default locale if no best match
 */
public Locale getBestMatchingLocale(Locale locale) {
    if (basenames == null || basenames.length == 0) {
        throw new IllegalStateException("no basenames set");
    }

    List<String> filenames = calculateFilenamesForLocale(basenames[0], locale);
    if (filenames.isEmpty()) {
        throw new IllegalStateException("no filenames found for locale " + locale);
    }

    for (String filename : filenames) {
        // only support properties files since that's all we use
        Resource resource = resourceLoader.getResource(filename + ".properties");
        if (resource.exists()) {
            String prefix = basenames[0] + "_";
            String code = filename.substring(prefix.length());

            return StringUtils.parseLocaleString(code);
        }
    }

    return Locale.getDefault();
}

From source file:org.globus.security.stores.CertKeyCredential.java

public CertKeyCredential(Resource certResource, Resource keyResource, X509Credential credential)
        throws ResourceStoreException {
    this.certFile = certResource;
    try {//from w w w. ja v  a  2  s .  c om
        if (!certResource.exists()) {
            FileUtils.touch(certResource.getFile());
            this.certLastModified = certResource.lastModified();
        }
        this.keyFile = keyResource;
        if (!keyResource.exists()) {
            FileUtils.touch(keyResource.getFile());
            this.keyLastModified = keyResource.lastModified();
        }
    } catch (IOException e) {
        throw new ResourceStoreException(e);
    }
    this.credential = credential;
}

From source file:jp.go.aist.six.util.core.config.spring.SpringContext.java

private Properties _getConfigProperties() {
    if (_configProperties == null) {
        _configProperties = new Properties();
        if (_configPropertyLocations != null) {
            for (String location : _configPropertyLocations) {
                try {
                    Resource resource = _getContext().getResource(location);
                    _LOG_.info("property resource: " + (resource.exists() ? "exist - " : "inexist - ")
                            + resource.getFile());
                    if (resource.exists()) {
                        _configProperties.load(resource.getInputStream());
                    }/*from   w  ww . j a v a2s  .c  o m*/
                } catch (IOException ex) {
                    _LOG_.warn("property resource ERROR (skip): " + ex);
                }

                //                _configProperties.list( System.out );
            }
        }
    }

    return _configProperties;
}

From source file:org.eclipse.gemini.blueprint.test.provisioning.internal.LocalFileSystemMavenRepository.java

/**
 * Returns the <code>localRepository</code> settings as indicated by the
 * <code>settings.xml</code> file.
 * //from   ww  w. j a v a2s .  co m
 * @return local repository as indicated by a Maven settings.xml file
 */
String getMavenSettingsLocalRepository(Resource m2Settings) {
    // no file found, return null to continue the discovery process
    if (!m2Settings.exists())
        return null;

    try {
        DocumentLoader docLoader = new DefaultDocumentLoader();
        Document document = docLoader.loadDocument(new InputSource(m2Settings.getInputStream()), null, null,
                XmlValidationModeDetector.VALIDATION_NONE, false);

        return (DomUtils.getChildElementValueByTagName(document.getDocumentElement(), LOCAL_REPOSITORY_ELEM));
    } catch (Exception ex) {
        throw (RuntimeException) new RuntimeException(
                new ParserConfigurationException("error parsing resource=" + m2Settings).initCause(ex));
    }
}

From source file:com.iterranux.droolsjbpmCore.runtime.build.impl.KieModuleBuilder.java

public void buildKmoduleForGrailsModule(String moduleName) {

    Resource kmoduleXml = new FileSystemResource("grails-app/conf/droolsjbpm/" + moduleName + "/kmodule.xml");
    if (kmoduleXml.exists()) {
        try {// ww w . j av a2  s .  com
            buildKmoduleForGrailsModule(moduleName, kmoduleXml);
        } catch (IOException e) {
            log.error("Error: KieModule build for '" + moduleName + "' failed", e);
        }
    } else {
        log.error("Error: KieModule build for '" + moduleName
                + "' was tried, but no kmodule.xml could be found.");
    }

}

From source file:com.googlecode.deadalus.server.internal.LocalObjectFactoryRegistry.java

public void addJarFile(File jarFile) throws IOException {
    // add the file to the classloader
    if (jarFile.exists())
        System.out.println("jarFile = " + jarFile);
    classLoader.addJarFile(jarFile);/*from   ww  w  .  j av a 2 s.  c om*/
    // get the resources
    Resource springCtxResource = new ClassPathResource("META-INF/factories.xml", classLoader);
    // @todo: do we need to reset this at the end?
    Thread.currentThread().setContextClassLoader(classLoader);
    if (springCtxResource.exists()) {
        // create the spring context for the application
        String[] locations = new String[] { "/META-INF/factories.xml" };
        ClassPathXmlApplicationContext springCtx = new ClassPathXmlApplicationContext(locations, parentCtx);
        Map<String, ObjectFactory> factories = springCtx.getBeansOfType(ObjectFactory.class);
        // now register them with the registry
        for (Map.Entry<String, ObjectFactory> factoryEntry : factories.entrySet()) {
            UUID clsId = factoryEntry.getValue().getClassIdentifier();
            // double check if we already had a factory registered for this UUID
            if (!registry.containsKey(clsId)) {
                registry.put(clsId, factoryEntry.getValue());
            } else {
                // @todo: what to do with already registered ObjectFactory instances? overwrite?
            }
        }

    }
}