Search in sources :

Example 26 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class GenericGroovyApplicationContext method load.

/**
	 * Load bean definitions from the given Groovy scripts or XML files.
	 * <p>Note that ".xml" files will be parsed as XML content; all other kinds
	 * of resources will be parsed as Groovy scripts.
	 * @param relativeClass class whose package will be used as a prefix when
	 * loading each specified resource name
	 * @param resourceNames relatively-qualified names of resources to load
	 */
public void load(Class<?> relativeClass, String... resourceNames) {
    Resource[] resources = new Resource[resourceNames.length];
    for (int i = 0; i < resourceNames.length; i++) {
        resources[i] = new ClassPathResource(resourceNames[i], relativeClass);
    }
    load(resources);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 27 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class LocalValidatorFactoryBean method afterPropertiesSet.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void afterPropertiesSet() {
    Configuration<?> configuration;
    if (this.providerClass != null) {
        ProviderSpecificBootstrap bootstrap = Validation.byProvider(this.providerClass);
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    } else {
        GenericBootstrap bootstrap = Validation.byDefaultProvider();
        if (this.validationProviderResolver != null) {
            bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
        }
        configuration = bootstrap.configure();
    }
    // Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
    if (this.applicationContext != null) {
        try {
            Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
            ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
        } catch (NoSuchMethodException ex) {
        // Ignore - no Hibernate Validator 5.2+ or similar provider
        }
    }
    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
        targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
    if (this.traversableResolver != null) {
        configuration.traversableResolver(this.traversableResolver);
    }
    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
        targetConstraintValidatorFactory = new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
        configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }
    if (this.parameterNameDiscoverer != null) {
        configureParameterNameProviderIfPossible(configuration);
    }
    if (this.mappingLocations != null) {
        for (Resource location : this.mappingLocations) {
            try {
                configuration.addMapping(location.getInputStream());
            } catch (IOException ex) {
                throw new IllegalStateException("Cannot read mapping resource: " + location);
            }
        }
    }
    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
        configuration.addProperty(entry.getKey(), entry.getValue());
    }
    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);
    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
}
Also used : ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) Resource(org.springframework.core.io.Resource) Method(java.lang.reflect.Method) IOException(java.io.IOException) ProviderSpecificBootstrap(javax.validation.bootstrap.ProviderSpecificBootstrap) GenericBootstrap(javax.validation.bootstrap.GenericBootstrap) HashMap(java.util.HashMap) Map(java.util.Map) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(javax.validation.MessageInterpolator)

Example 28 with Resource

use of org.springframework.core.io.Resource in project spring-boot by spring-projects.

the class ClassLoaderFilesResourcePatternResolver method getAdditionalResources.

private List<Resource> getAdditionalResources(String locationPattern) throws MalformedURLException {
    List<Resource> additionalResources = new ArrayList<>();
    String trimmedLocationPattern = trimLocationPattern(locationPattern);
    for (SourceFolder sourceFolder : this.classLoaderFiles.getSourceFolders()) {
        for (Entry<String, ClassLoaderFile> entry : sourceFolder.getFilesEntrySet()) {
            String name = entry.getKey();
            ClassLoaderFile file = entry.getValue();
            if (file.getKind() == Kind.ADDED && this.antPathMatcher.match(trimmedLocationPattern, name)) {
                URL url = new URL("reloaded", null, -1, "/" + name, new ClassLoaderFileURLStreamHandler(file));
                UrlResource resource = new UrlResource(url);
                additionalResources.add(resource);
            }
        }
    }
    return additionalResources;
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) ClassLoaderFileURLStreamHandler(org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler) UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) AbstractResource(org.springframework.core.io.AbstractResource) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL)

Example 29 with Resource

use of org.springframework.core.io.Resource in project spring-boot by spring-projects.

the class ClassLoaderFilesResourcePatternResolver method getResources.

@Override
public Resource[] getResources(String locationPattern) throws IOException {
    List<Resource> resources = new ArrayList<>();
    Resource[] candidates = this.patternResolverDelegate.getResources(locationPattern);
    for (Resource candidate : candidates) {
        if (!isDeleted(candidate)) {
            resources.add(candidate);
        }
    }
    resources.addAll(getAdditionalResources(locationPattern));
    return resources.toArray(new Resource[resources.size()]);
}
Also used : ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) AbstractResource(org.springframework.core.io.AbstractResource)

Example 30 with Resource

use of org.springframework.core.io.Resource in project spring-boot by spring-projects.

the class SpringApplicationBannerPrinter method getTextBanner.

private Banner getTextBanner(Environment environment) {
    String location = environment.getProperty(BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION);
    Resource resource = this.resourceLoader.getResource(location);
    if (resource.exists()) {
        return new ResourceBanner(resource);
    }
    return null;
}
Also used : Resource(org.springframework.core.io.Resource)

Aggregations

Resource (org.springframework.core.io.Resource)541 Test (org.junit.Test)248 ClassPathResource (org.springframework.core.io.ClassPathResource)217 IOException (java.io.IOException)86 FileSystemResource (org.springframework.core.io.FileSystemResource)67 File (java.io.File)60 UrlResource (org.springframework.core.io.UrlResource)60 ArrayList (java.util.ArrayList)49 ByteArrayResource (org.springframework.core.io.ByteArrayResource)46 InputStream (java.io.InputStream)33 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)24 URL (java.net.URL)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16 ServletContextResource (org.springframework.web.context.support.ServletContextResource)16 URI (java.net.URI)15 ResourceLoader (org.springframework.core.io.ResourceLoader)15 HashMap (java.util.HashMap)13 LinkedHashSet (java.util.LinkedHashSet)13