Search in sources :

Example 6 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-data-commons by spring-projects.

the class AnnotatedTypeScanner method findTypes.

public Set<Class<?>> findTypes(Iterable<String> basePackages) {
    ClassPathScanningCandidateComponentProvider provider = new InterfaceAwareScanner(considerInterfaces);
    if (resourceLoader != null) {
        provider.setResourceLoader(resourceLoader);
    }
    if (environment != null) {
        provider.setEnvironment(environment);
    }
    for (Class<? extends Annotation> annotationType : annotationTypess) {
        provider.addIncludeFilter(new AnnotationTypeFilter(annotationType, true, considerInterfaces));
    }
    Set<Class<?>> types = new HashSet<>();
    ResourceLoader loader = resourceLoader;
    ClassLoader classLoader = loader == null ? null : loader.getClassLoader();
    for (String basePackage : basePackages) {
        for (BeanDefinition definition : provider.findCandidateComponents(basePackage)) {
            String beanClassName = definition.getBeanClassName();
            if (beanClassName == null) {
                throw new IllegalStateException(String.format("Unable to obtain bean class name from bean definition %s!", definition));
            }
            try {
                types.add(ClassUtils.forName(beanClassName, classLoader));
            } catch (ClassNotFoundException o_O) {
                throw new IllegalStateException(o_O);
            }
        }
    }
    return types;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ResourceLoader(org.springframework.core.io.ResourceLoader) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) HashSet(java.util.HashSet)

Example 7 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-data-commons by spring-projects.

the class RepositoryBeanDefinitionParser method parse.

/*
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
	 */
@Nullable
public BeanDefinition parse(Element element, ParserContext parser) {
    XmlReaderContext readerContext = parser.getReaderContext();
    try {
        ResourceLoader resourceLoader = ConfigurationUtils.getRequiredResourceLoader(readerContext);
        Environment environment = readerContext.getEnvironment();
        BeanDefinitionRegistry registry = parser.getRegistry();
        XmlRepositoryConfigurationSource configSource = new XmlRepositoryConfigurationSource(element, parser, environment);
        RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configSource, resourceLoader, environment);
        RepositoryConfigurationUtils.exposeRegistration(extension, registry, configSource);
        for (BeanComponentDefinition definition : delegate.registerRepositoriesIn(registry, extension)) {
            readerContext.fireComponentRegistered(definition);
        }
    } catch (RuntimeException e) {
        handleError(e, element, readerContext);
    }
    return null;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) XmlReaderContext(org.springframework.beans.factory.xml.XmlReaderContext) Environment(org.springframework.core.env.Environment) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Nullable(org.springframework.lang.Nullable)

Example 8 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-security by spring-projects.

the class WebXmlJ2eeDefinedRolesRetrieverTests method testRole1To4Roles.

@Test
public void testRole1To4Roles() throws Exception {
    List<String> ROLE1TO4_EXPECTED_ROLES = Arrays.asList("Role1", "Role2", "Role3", "Role4");
    final Resource webXml = new ClassPathResource("webxml/Role1-4.web.xml");
    WebXmlMappableAttributesRetriever rolesRetriever = new WebXmlMappableAttributesRetriever();
    rolesRetriever.setResourceLoader(new ResourceLoader() {

        @Override
        public ClassLoader getClassLoader() {
            return Thread.currentThread().getContextClassLoader();
        }

        @Override
        public Resource getResource(String location) {
            return webXml;
        }
    });
    rolesRetriever.afterPropertiesSet();
    Set<String> j2eeRoles = rolesRetriever.getMappableAttributes();
    assertThat(j2eeRoles).containsAll(ROLE1TO4_EXPECTED_ROLES);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 9 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-security by spring-projects.

the class SecurityMockMvcRequestPostProcessors method x509.

/**
 * Finds an X509Cetificate using a resoureName and populates it on the request.
 * @param resourceName the name of the X509Certificate resource
 * @return the
 * {@link org.springframework.test.web.servlet.request.RequestPostProcessor} to use.
 * @throws IOException
 * @throws CertificateException
 */
public static RequestPostProcessor x509(String resourceName) throws IOException, CertificateException {
    ResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource(resourceName);
    InputStream inputStream = resource.getInputStream();
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(inputStream);
    return x509(certificate);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 10 with ResourceLoader

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

the class HttpServletBean method init.

/**
 * Map config parameters onto bean properties of this servlet, and
 * invoke subclass initialization.
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws ServletException {
    // Set bean properties from init parameters.
    PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            if (logger.isErrorEnabled()) {
                logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            }
            throw ex;
        }
    }
    // Let subclasses do whatever initialization they like.
    initServletBean();
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) ResourceEditor(org.springframework.core.io.ResourceEditor) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Aggregations

ResourceLoader (org.springframework.core.io.ResourceLoader)90 Resource (org.springframework.core.io.Resource)51 Test (org.junit.Test)35 ClassRelativeResourceLoader (org.springframework.core.io.ClassRelativeResourceLoader)34 File (java.io.File)33 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)26 Test (org.junit.jupiter.api.Test)15 IOException (java.io.IOException)9 Environment (org.springframework.core.env.Environment)9 ByteArrayResource (org.springframework.core.io.ByteArrayResource)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 HashMap (java.util.HashMap)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 InputStream (java.io.InputStream)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 InputStreamReader (java.io.InputStreamReader)3 Properties (java.util.Properties)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3