Example usage for org.springframework.core.io.support ResourcePatternResolver CLASSPATH_ALL_URL_PREFIX

List of usage examples for org.springframework.core.io.support ResourcePatternResolver CLASSPATH_ALL_URL_PREFIX

Introduction

In this page you can find the example usage for org.springframework.core.io.support ResourcePatternResolver CLASSPATH_ALL_URL_PREFIX.

Prototype

String CLASSPATH_ALL_URL_PREFIX

To view the source code for org.springframework.core.io.support ResourcePatternResolver CLASSPATH_ALL_URL_PREFIX.

Click Source Link

Document

Pseudo URL prefix for all matching resources from the class path: "classpath*:" This differs from ResourceLoader's classpath URL prefix in that it retrieves all matching resources for a given name (e.g.

Usage

From source file:Main.java

public static Stream<Class> scan(String packageName) {
    try {/*from  ww  w  . j  a  va 2 s  .c o m*/
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                + convertClassNameToResourcePath(resolvePlaceholders(packageName)) + "/**/*.class";

        return Stream.of(resourcePatternResolver.getResources(packageSearchPath)).filter(Resource::isReadable)
                .map(r -> getClassOrNull(readerFactory, r)).filter(c -> c != null);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.baomidou.mybatisplus.toolkit.PackageHelper.java

/**
 * <p>//from  www .j a  v  a2  s. c  o  m
 * ???
 * </p>
 * <p>
 * <property name="typeAliasesPackage" value="com.baomidou.*.entity"/>
 * </p>
 * 
 * @param typeAliasesPackage
 *            ??
 * @return
 */
public static String[] convertTypeAliasesPackage(String typeAliasesPackage) {
    ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
    String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/*.class";

    /*
     * ??Resource
     * ClassLoader.getResource("META-INF")?????????
     */
    try {
        Set<String> set = new HashSet<String>();
        Resource[] resources = resolver.getResources(pkg);
        if (resources != null && resources.length > 0) {
            MetadataReader metadataReader = null;
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    metadataReader = metadataReaderFactory.getMetadataReader(resource);
                    set.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
                            .getName());
                }
            }
        }
        if (!set.isEmpty()) {
            return set.toArray(new String[] {});
        } else {
            throw new MybatisPlusException("not find typeAliasesPackage:" + pkg);
        }
    } catch (Exception e) {
        throw new MybatisPlusException("not find typeAliasesPackage:" + pkg, e);
    }
}

From source file:com.baomidou.framework.spring.SpringScanPackage.java

/**
 * <p>/*from ww  w  .  ja va2 s . co m*/
 * ???,?
 * </p>
 *
 * @param scanPackages
 *            ??package
 * @return
 */
public static Set<String> findPackageClass(String scanPackages) {
    Set<String> clazzSet = new HashSet<String>();
    ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
    String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + ClassUtils.convertClassNameToResourcePath(scanPackages) + "/**/*.class";
    /*
     * ??Resource
     * ClassLoader.getResource("META-INF")?????????
     */
    try {
        Resource[] resources = resolver.getResources(pkg);
        if (resources != null && resources.length > 0) {
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                    if (metadataReader != null) {
                        clazzSet.add(metadataReader.getClassMetadata().getClassName());
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return clazzSet;
}

From source file:spring.osgi.utils.OsgiResourceUtils.java

/**
 * Return the search type to be used for the give string based on the
 * prefix.//ww  w.  ja va  2  s .co  m
 *
 * @param path path
 * @return type
 */
public static int getSearchType(String path) {
    Assert.notNull(path);
    int type;
    String prefix = getPrefix(path);

    // no prefix is treated just like osgibundle:
    if (!StringUtils.hasText(prefix))
        type = PREFIX_TYPE_NOT_SPECIFIED;
    else if (prefix.startsWith(OsgiBundleResource.BUNDLE_URL_PREFIX))
        type = PREFIX_TYPE_BUNDLE_SPACE;
    else if (prefix.startsWith(OsgiBundleResource.BUNDLE_JAR_URL_PREFIX))
        type = PREFIX_TYPE_BUNDLE_JAR;
    else if (prefix.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX))
        type = PREFIX_TYPE_CLASS_SPACE;
    else if (prefix.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX))
        type = PREFIX_TYPE_CLASS_ALL_SPACE;

    else
        type = PREFIX_TYPE_UNKNOWN;

    return type;
}

From source file:gov.nih.nci.cabig.caaers.domain.DomainObjectTest.java

private List<Class> findMyTypes(String basePackage) throws IOException, ClassNotFoundException {
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

    List<Class> candidates = new ArrayList<Class>();
    String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + resolveBasePackage(basePackage) + "/" + "**/*.class";
    Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
    for (Resource resource : resources) {
        if (resource.isReadable()) {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
            if (isCandidate(metadataReader))
                candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
        }//  w  w  w. j a v a 2s .c o  m
    }
    return candidates;
}

From source file:com.fantasy.Application.java

private static List<Class> findTypes(String basePackage) throws IOException, ClassNotFoundException {
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

    List<Class> candidates = new ArrayList();
    String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + resolveBasePackage(basePackage) + "/" + "**/*.class";
    Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
    for (Resource resource : resources) {
        if (resource.isReadable()) {
            MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
            if (isCandidate(metadataReader)) {
                candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
            }/*from   w w w .  j  a  va2  s  .  com*/
        }
    }
    return candidates;
}

From source file:com.bluexml.side.framework.alfresco.commons.configurations.RepositoryResourcePatternResolver.java

protected boolean isClassPathLocation(String location) {
    return location.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)
            || location.startsWith(ResourcePatternResolver.CLASSPATH_URL_PREFIX);
}

From source file:com.zht.common.generator.util.LoadPackageClasses.java

/**
 * ??BeanClass??/*from ww  w  . j a  v a2 s  .c  o  m*/
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public Set<Class<?>> getClassSet() throws IOException, ClassNotFoundException {
    this.classSet.clear();
    if (!this.packagesList.isEmpty()) {
        for (String pkg : this.packagesList) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
            Resource[] resources = this.resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                    this.resourcePatternResolver);
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    if (matchesEntityTypeFilter(reader, readerFactory)) {
                        this.classSet.add(Class.forName(className));
                    }
                }
            }
        }
    }
    //
    //      if (logger.isInfoEnabled()){
    //         for (Class<?> clazz : this.classSet) {
    //            //logger.info(String.format("Found class:%s", clazz.getName()));
    //         }
    //      }
    return this.classSet;
}

From source file:com.sesxh.hoschange.common.util.LoadPackageClasses.java

/**
 * ??BeanClass??//from   w ww  . j a  v a  2s. c o  m
 * 
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public Set<Class<?>> getClassSet() throws IOException, ClassNotFoundException {
    this.classSet.clear();
    if (!this.packagesList.isEmpty()) {
        for (String pkg : this.packagesList) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
            Resource[] resources = this.resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                    this.resourcePatternResolver);
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    if (matchesEntityTypeFilter(reader, readerFactory)) {
                        this.classSet.add(Class.forName(className));
                    }
                }
            }
        }
    }
    // 
    // if (logger.isInfoEnabled()){
    // for (Class<?> clazz : this.classSet) {
    // //logger.info(String.format("Found class:%s", clazz.getName()));
    // }
    // }
    return this.classSet;
}

From source file:cn.wanghaomiao.seimi.core.SeimiScanner.java

@SafeVarargs
public final Set<Class<?>> scan(String[] confPkgs, Class<? extends Annotation>... annotationTags) {
    Set<Class<?>> resClazzSet = new HashSet<>();
    List<AnnotationTypeFilter> typeFilters = new LinkedList<>();
    if (ArrayUtils.isNotEmpty(annotationTags)) {
        for (Class<? extends Annotation> annotation : annotationTags) {
            typeFilters.add(new AnnotationTypeFilter(annotation, false));
        }/* w  w w  .  ja v a2 s  . com*/
    }
    if (ArrayUtils.isNotEmpty(confPkgs)) {
        for (String pkg : confPkgs) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + String.format(RESOURCE_PATTERN, ClassUtils.convertClassNameToResourcePath(pkg));
            try {
                Resource[] resources = this.resourcePatternResolver.getResources(pattern);
                MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                        this.resourcePatternResolver);
                for (Resource resource : resources) {
                    if (resource.isReadable()) {
                        MetadataReader reader = readerFactory.getMetadataReader(resource);
                        String className = reader.getClassMetadata().getClassName();
                        if (ifMatchesEntityType(reader, readerFactory, typeFilters)) {
                            Class<?> curClass = Thread.currentThread().getContextClassLoader()
                                    .loadClass(className);
                            context.register(curClass);
                            resClazzSet.add(curClass);
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("?????[{}][{}]", pattern,
                        StringUtils.join(typeFilters, ","));
            }
        }
    }
    if (!context.isActive()) {
        context.refresh();
    }
    return resClazzSet;
}