Search in sources :

Example 51 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project tomee by apache.

the class SpringClasspathScanner method findClassesInternal.

@Override
protected Map<Class<? extends Annotation>, Collection<Class<?>>> findClassesInternal(Collection<String> basePackages, List<Class<? extends Annotation>> annotations, ClassLoader loader) throws IOException, ClassNotFoundException {
    ResourcePatternResolver resolver = getResolver(loader);
    MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
    final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = new HashMap<>();
    final Map<Class<? extends Annotation>, Collection<String>> matchingInterfaces = new HashMap<>();
    final Map<String, String[]> nonMatchingClasses = new HashMap<>();
    for (Class<? extends Annotation> annotation : annotations) {
        classes.put(annotation, new HashSet<>());
        matchingInterfaces.put(annotation, new HashSet<>());
    }
    if (basePackages == null || basePackages.isEmpty()) {
        return classes;
    }
    for (final String basePackage : basePackages) {
        final boolean scanAllPackages = basePackage.equals(WILDCARD);
        final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES;
        final Resource[] resources = resolver.getResources(packageSearchPath);
        for (final Resource resource : resources) {
            final MetadataReader reader = factory.getMetadataReader(resource);
            final AnnotationMetadata metadata = reader.getAnnotationMetadata();
            if (scanAllPackages && shouldSkip(metadata.getClassName())) {
                continue;
            }
            for (Class<? extends Annotation> annotation : annotations) {
                boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
                if (metadata.isAnnotated(annotation.getName())) {
                    if (concreteClass) {
                        classes.get(annotation).add(loadClass(metadata.getClassName(), loader));
                    } else {
                        matchingInterfaces.get(annotation).add(metadata.getClassName());
                    }
                } else if (concreteClass && metadata.getInterfaceNames().length > 0) {
                    nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames());
                }
            }
        }
    }
    if (!nonMatchingClasses.isEmpty()) {
        for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) {
            for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
                for (String intName : e2.getValue()) {
                    if (e1.getValue().contains(intName)) {
                        classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
                        break;
                    }
                }
            }
        }
    }
    for (Map.Entry<Class<? extends Annotation>, Collection<String>> e : matchingInterfaces.entrySet()) {
        if (classes.get(e.getKey()).isEmpty()) {
            for (String intName : e.getValue()) {
                classes.get(e.getKey()).add(loadClass(intName, loader));
            }
        }
    }
    return classes;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) HashMap(java.util.HashMap) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project Asqatasun by Asqatasun.

the class WebAppConfiguration method reloadableResourceBundleMessageSource.

// /**
// * Redirect '/' to login page
// * @param registry
// */
// @Override
// public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("").setViewName("login");
// registry.addViewController("/").setViewName("login");
// }
/**
 * Load all i18n file from the classpath
 * @return
 */
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource() {
    ClassLoader cl = this.getClass().getClassLoader();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
    Resource[] resources = new Resource[0];
    // load dynamically all i18 files. They have to be in the classpath under an i18n folder.
    try {
        resources = resolver.getResources("classpath*:i18n/*I18N.properties");
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<String> baseNames = new ArrayList<>();
    for (Resource resource : resources) {
        baseNames.add("classpath:i18n/" + StringUtils.remove(resource.getFilename(), ".properties"));
    }
    ReloadableResourceBundleMessageSource bundleMessageSource = new ReloadableResourceBundleMessageSource();
    bundleMessageSource.setDefaultEncoding("UTF-8");
    bundleMessageSource.setBasenames(baseNames.toArray(new String[baseNames.size()]));
    return bundleMessageSource;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ReloadableResourceBundleMessageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Bean(org.springframework.context.annotation.Bean)

Example 53 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project mybatis.flying by limeng32.

the class SpringBootVFS method list.

@Override
protected List<String> list(URL url, String path) throws IOException {
    ClassLoader cl = this.getClass().getClassLoader();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
    Resource[] resources = resolver.getResources(path + "/**/*.class");
    List<Resource> resources1 = Arrays.asList(resources);
    List<String> resourcePaths = new ArrayList<String>();
    for (Resource resource : resources1) {
        resourcePaths.add(preserveSubpackageName(resource.getURI(), path));
    }
    return resourcePaths;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 54 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project cxf by apache.

the class SpringClasspathScanner method findClassesInternal.

@Override
protected Map<Class<? extends Annotation>, Collection<Class<?>>> findClassesInternal(Collection<String> basePackages, List<Class<? extends Annotation>> annotations, ClassLoader loader) throws IOException, ClassNotFoundException {
    ResourcePatternResolver resolver = getResolver(loader);
    MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
    final Map<Class<? extends Annotation>, Collection<Class<?>>> classes = new HashMap<>();
    final Map<Class<? extends Annotation>, Collection<String>> matchingInterfaces = new HashMap<>();
    final Map<String, String[]> nonMatchingClasses = new HashMap<>();
    for (Class<? extends Annotation> annotation : annotations) {
        classes.put(annotation, new HashSet<>());
        matchingInterfaces.put(annotation, new HashSet<>());
    }
    if (basePackages == null || basePackages.isEmpty()) {
        return classes;
    }
    for (final String basePackage : basePackages) {
        final boolean scanAllPackages = basePackage.equals(WILDCARD);
        final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES;
        final Resource[] resources = resolver.getResources(packageSearchPath);
        for (final Resource resource : resources) {
            final MetadataReader reader = factory.getMetadataReader(resource);
            final AnnotationMetadata metadata = reader.getAnnotationMetadata();
            if (scanAllPackages && shouldSkip(metadata.getClassName())) {
                continue;
            }
            for (Class<? extends Annotation> annotation : annotations) {
                boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
                if (metadata.isAnnotated(annotation.getName())) {
                    if (concreteClass) {
                        classes.get(annotation).add(loadClass(metadata.getClassName(), loader));
                    } else {
                        matchingInterfaces.get(annotation).add(metadata.getClassName());
                    }
                } else if (concreteClass && metadata.getInterfaceNames().length > 0) {
                    nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames());
                }
            }
        }
    }
    if (!nonMatchingClasses.isEmpty()) {
        for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) {
            for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
                for (String intName : e2.getValue()) {
                    if (e1.getValue().contains(intName)) {
                        classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
                        break;
                    }
                }
            }
        }
    }
    for (Map.Entry<Class<? extends Annotation>, Collection<String>> e : matchingInterfaces.entrySet()) {
        if (classes.get(e.getKey()).isEmpty()) {
            for (String intName : e.getValue()) {
                classes.get(e.getKey()).add(loadClass(intName, loader));
            }
        }
    }
    return classes;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) HashMap(java.util.HashMap) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project sinsim by WilsonHu.

the class MybatisConfig method sqlSessionFactoryBean.

@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTypeAliasesPackage(MODEL_PACKAGE + "/*");
    // 配置分页插件,详情请查阅官方文档
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    // 分页尺寸为0时查询所有纪录不再执行分页
    properties.setProperty("pageSizeZero", "true");
    // 页码<=0 查询第一页,页码>=总页数查询最后一页
    properties.setProperty("reasonable", "true");
    // 支持通过 Mapper 接口参数来传递分页参数
    properties.setProperty("supportMethodsArguments", "true");
    pageHelper.setProperties(properties);
    // 添加插件
    factory.setPlugins(new Interceptor[] { pageHelper });
    // 添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
    // /通过Java配置的方式是通过setConfiguration/setConfigLocation,而setConfigurationProperties只是
    // 配置文件中的一个property。所以mybatis的配置,要不通过XML的location方式,要不就是setConfiguration后
    // 加上property的方式Properties
    // mybatisProperties = new Properties();
    // mybatisProperties.setProperty("mapUnderscoreToCamelCase", "true");
    // factory.setConfigurationProperties(mybatisProperties);
    factory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
    return factory.getObject();
}
Also used : PageHelper(com.github.pagehelper.PageHelper) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Properties(java.util.Properties) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)68 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)65 Resource (org.springframework.core.io.Resource)52 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)11 Bean (org.springframework.context.annotation.Bean)11 PageHelper (com.github.pagehelper.PageHelper)9 File (java.io.File)9 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)9 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)9 MetadataReader (org.springframework.core.type.classreading.MetadataReader)9 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)9 Properties (java.util.Properties)8 InputStream (java.io.InputStream)6 FileOutputStream (java.io.FileOutputStream)5 URL (java.net.URL)5 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 SocketChannel (io.netty.channel.socket.SocketChannel)2