Search in sources :

Example 6 with ResourceLoader

use of io.micronaut.core.io.ResourceLoader in project micronaut-core by micronaut-projects.

the class RequiresCondition method matchesPresenceOfResources.

private boolean matchesPresenceOfResources(ConditionContext context, AnnotationValue<Requires> requirements) {
    if (requirements.contains(MEMBER_RESOURCES)) {
        final String[] resourcePaths = requirements.stringValues(MEMBER_RESOURCES);
        if (ArrayUtils.isNotEmpty(resourcePaths)) {
            final BeanContext beanContext = context.getBeanContext();
            ResourceResolver resolver;
            final List<ResourceLoader> resourceLoaders;
            if (beanContext instanceof ApplicationContext) {
                ResourceLoader resourceLoader = ((ApplicationContext) beanContext).getEnvironment();
                resourceLoaders = Arrays.asList(resourceLoader, FileSystemResourceLoader.defaultLoader());
            } else {
                resourceLoaders = Arrays.asList(ClassPathResourceLoader.defaultLoader(beanContext.getClassLoader()), FileSystemResourceLoader.defaultLoader());
            }
            resolver = new ResourceResolver(resourceLoaders);
            for (String resourcePath : resourcePaths) {
                if (!resolver.getResource(resourcePath).isPresent()) {
                    context.fail("Resource [" + resourcePath + "] does not exist");
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : FileSystemResourceLoader(io.micronaut.core.io.file.FileSystemResourceLoader) ResourceLoader(io.micronaut.core.io.ResourceLoader) ClassPathResourceLoader(io.micronaut.core.io.scan.ClassPathResourceLoader) ResourceResolver(io.micronaut.core.io.ResourceResolver)

Aggregations

ResourceLoader (io.micronaut.core.io.ResourceLoader)6 FileSystemResourceLoader (io.micronaut.core.io.file.FileSystemResourceLoader)3 ClassPathResourceLoader (io.micronaut.core.io.scan.ClassPathResourceLoader)3 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)2 ResourceResolver (io.micronaut.core.io.ResourceResolver)2 URL (java.net.URL)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 BeanContext (io.micronaut.context.BeanContext)1 PropertiesPropertySourceLoader (io.micronaut.context.env.PropertiesPropertySourceLoader)1 DefaultFileSystemResourceLoader (io.micronaut.core.io.file.DefaultFileSystemResourceLoader)1 HttpStatus (io.micronaut.http.HttpStatus)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1