Search in sources :

Example 1 with StaticResourceLoader

use of org.grails.core.io.StaticResourceLoader in project grails-core by grails.

the class StaticResourceLoaderTests method testGetResource.

public void testGetResource() throws Exception {
    StaticResourceLoader srl = new StaticResourceLoader();
    srl.setBaseResource(new UrlResource("http://grails.org"));
    Resource r = srl.getResource("/Home");
    assertEquals("http://grails.org/Home", r.getURL().toString());
}
Also used : StaticResourceLoader(org.grails.core.io.StaticResourceLoader) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource)

Example 2 with StaticResourceLoader

use of org.grails.core.io.StaticResourceLoader in project grails-core by grails.

the class StaticResourceLoaderTests method testIllegalState.

public void testIllegalState() {
    StaticResourceLoader srl = new StaticResourceLoader();
    try {
        srl.getResource("/foo");
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ise) {
    // expected
    }
}
Also used : StaticResourceLoader(org.grails.core.io.StaticResourceLoader)

Example 3 with StaticResourceLoader

use of org.grails.core.io.StaticResourceLoader in project grails-core by grails.

the class BinaryGrailsPlugin method getProperties.

/**
     * Obtains all properties for this binary plugin for the given locale.
     *
     * Note this method does not cache so clients should in general cache the results of this method.
     *
     * @param locale The locale
     * @return The properties or null if non exist
     */
public Properties getProperties(final Locale locale) {
    Resource url = this.baseResourcesResource;
    Properties properties = null;
    if (url != null) {
        StaticResourceLoader resourceLoader = new StaticResourceLoader();
        resourceLoader.setBaseResource(url);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
        try {
            // first load all properties
            Resource[] resources = resolver.getResources('*' + PROPERTIES_EXTENSION);
            resources = resources.length > 0 ? filterResources(resources, locale) : resources;
            if (resources.length > 0) {
                properties = new Properties();
                // message bundles are locale specific. The more underscores the locale has the more specific the locale
                // so we order by the number of underscores present so that the most specific appears
                Arrays.sort(resources, new Comparator<Resource>() {

                    @Override
                    public int compare(Resource o1, Resource o2) {
                        String f1 = o1.getFilename();
                        String f2 = o2.getFilename();
                        int firstUnderscoreCount = StringUtils.countOccurrencesOf(f1, "_");
                        int secondUnderscoreCount = StringUtils.countOccurrencesOf(f2, "_");
                        if (firstUnderscoreCount == secondUnderscoreCount) {
                            return 0;
                        } else {
                            return firstUnderscoreCount > secondUnderscoreCount ? 1 : -1;
                        }
                    }
                });
                loadFromResources(properties, resources);
            }
        } catch (IOException e) {
            return null;
        }
    }
    return properties;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) StaticResourceLoader(org.grails.core.io.StaticResourceLoader) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

StaticResourceLoader (org.grails.core.io.StaticResourceLoader)3 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 IOException (java.io.IOException)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)1