Search in sources :

Example 36 with FilterBuilder

use of org.reflections.util.FilterBuilder in project oap by oaplatform.

the class Resources method urls.

public static List<URL> urls(String atPackage, String ext) {
    final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("reflections-%d").build());
    try {
        final ConfigurationBuilder configuration = new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(atPackage)).setScanners(new ResourcesScanner()).filterInputsBy(new FilterBuilder().includePackage(atPackage)).setExecutorService(executorService);
        final Reflections reflections = new Reflections(configuration);
        final Set<String> resources = reflections.getResources(in -> in.endsWith("." + ext));
        return new ArrayList<>(Sets.map(resources, r -> Thread.currentThread().getContextClassLoader().getResource(r)));
    } finally {
        executorService.shutdown();
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) URL(java.net.URL) Reflections(org.reflections.Reflections) Strings(oap.util.Strings) Executors(oap.concurrent.Executors) ArrayList(java.util.ArrayList) FilterBuilder(org.reflections.util.FilterBuilder) Sets(oap.util.Sets) Lists(oap.util.Lists) Stream(oap.util.Stream) Path(java.nio.file.Path) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ExecutorService(java.util.concurrent.ExecutorService) Set(java.util.Set) IOException(java.io.IOException) ClasspathHelper(org.reflections.util.ClasspathHelper) UncheckedIOException(java.io.UncheckedIOException) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Try(oap.util.Try) List(java.util.List) Paths(java.nio.file.Paths) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) Collections(java.util.Collections) InputStream(java.io.InputStream) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FilterBuilder(org.reflections.util.FilterBuilder) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 37 with FilterBuilder

use of org.reflections.util.FilterBuilder in project startup-os by google.

the class ClassScanner method scanPackage.

public void scanPackage(String packagePrefix, Map<String, FlagData> flags) {
    // TODO - figure out configuration builder.
    Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packagePrefix))).setUrls(ClasspathHelper.forPackage(packagePrefix)).setScanners(new FieldAnnotationsScanner()));
    Set<Field> fields = reflections.getFieldsAnnotatedWith(FlagDesc.class);
    for (Field field : fields) {
        Class<?> clazz = field.getType();
        if (!Flag.class.isAssignableFrom(clazz)) {
            throw new IllegalArgumentException("Annotation '" + field + "' does not annotate a flag.");
        }
        if ((field.getModifiers() & Modifier.STATIC) == 0) {
            throw new IllegalArgumentException("Flag '" + field + "' should be static but is not.");
        }
        Class<?> declaringClass = field.getDeclaringClass();
        Flag<?> flag = getFlagMember(declaringClass, field);
        FlagData flagData = createFlagData(declaringClass, field, flag);
        if (flags.containsKey(flagData.getName())) {
            throw new IllegalArgumentException(String.format("Flag '%s' is already defined here:\n%s", field, flags.get(flagData.getName())));
        }
        flags.put(flagData.getName(), flagData);
        flag.setName(flagData.getName());
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Field(java.lang.reflect.Field) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) FilterBuilder(org.reflections.util.FilterBuilder) Reflections(org.reflections.Reflections)

Example 38 with FilterBuilder

use of org.reflections.util.FilterBuilder in project drill by axbaretto.

the class ClassPathScanner method scan.

/**
 * @param pathsToScan the locations to scan for .class files
 * @param packagePrefixes the whitelist of package prefixes to scan
 * @param parentResult if there was a prescan, its result
 * @return the merged scan
 */
static ScanResult scan(Collection<URL> pathsToScan, Collection<String> packagePrefixes, Collection<String> scannedClasses, Collection<String> scannedAnnotations, ScanResult parentResult) {
    Stopwatch watch = Stopwatch.createStarted();
    try {
        AnnotationScanner annotationScanner = new AnnotationScanner(scannedAnnotations);
        SubTypesScanner subTypesScanner = new SubTypesScanner(parentResult.getImplementations());
        if (packagePrefixes.size() > 0) {
            final FilterBuilder filter = new FilterBuilder();
            for (String prefix : packagePrefixes) {
                filter.include(FilterBuilder.prefix(prefix));
            }
            ConfigurationBuilder conf = new ConfigurationBuilder().setUrls(pathsToScan).setMetadataAdapter(// Scanners depend on this
            METADATA_ADAPTER).filterInputsBy(filter).setScanners(annotationScanner, subTypesScanner);
            // scans stuff, but don't use the funky storage layer
            new Reflections(conf);
        }
        List<ParentClassDescriptor> implementations = new ArrayList<>();
        for (String baseTypeName : scannedClasses) {
            implementations.add(new ParentClassDescriptor(baseTypeName, new ArrayList<>(subTypesScanner.getChildrenOf(baseTypeName))));
        }
        List<AnnotatedClassDescriptor> annotated = annotationScanner.getAnnotatedClasses();
        verifyClassUnicity(annotated, pathsToScan);
        return new ScanResult(packagePrefixes, scannedClasses, scannedAnnotations, annotated, implementations);
    } finally {
        logger.info(format("Scanning packages %s in locations %s took %dms", packagePrefixes, pathsToScan, watch.elapsed(MILLISECONDS)));
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FilterBuilder(org.reflections.util.FilterBuilder) ParentClassDescriptor(org.apache.drill.common.scanner.persistence.ParentClassDescriptor) Reflections(org.reflections.Reflections)

Example 39 with FilterBuilder

use of org.reflections.util.FilterBuilder in project nd4j by deeplearning4j.

the class BasicNDArrayCompressor method loadCompressors.

protected void loadCompressors() {
    /*
            We scan classpath for NDArrayCompressor implementations and add them one by one to codecs map
         */
    codecs = new ConcurrentHashMap<>();
    Set<Class<? extends NDArrayCompressor>> classes = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.nd4j")).exclude(// Consider only .class files (to avoid debug messages etc. on .dlls, etc
    "^(?!.*\\.class$).*$")).setUrls(ClasspathHelper.forPackage("org.nd4j")).setScanners(new SubTypesScanner())).getSubTypesOf(NDArrayCompressor.class);
    for (Class<? extends NDArrayCompressor> impl : classes) {
        if (Modifier.isAbstract(impl.getModifiers()) || impl.isInterface())
            continue;
        try {
            NDArrayCompressor compressor = impl.newInstance();
            codecs.put(compressor.getDescriptor().toUpperCase(), compressor);
        } catch (InstantiationException i) {
            // we need catch there, to avoid exceptions at abstract classes
            ;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections)

Example 40 with FilterBuilder

use of org.reflections.util.FilterBuilder in project cas by apereo.

the class OidcProfileScopeToAttributesFilter method configureAttributeReleasePoliciesByScope.

/**
 * Configure attribute release policies by scope.
 */
protected void configureAttributeReleasePoliciesByScope() {
    val oidc = casProperties.getAuthn().getOidc();
    val packageName = BaseOidcScopeAttributeReleasePolicy.class.getPackage().getName();
    val reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().includePackage(packageName)).setUrls(ClasspathHelper.forPackage(packageName)));
    val subTypes = reflections.getSubTypesOf(BaseOidcScopeAttributeReleasePolicy.class);
    subTypes.forEach(Unchecked.consumer(t -> {
        if (ClassUtils.hasConstructor(t)) {
            val ex = t.getDeclaredConstructor().newInstance();
            if (oidc.getDiscovery().getScopes().contains(ex.getScopeType())) {
                LOGGER.trace("Found standard OpenID Connect scope [{}] to filter attributes", ex.getScopeType());
                this.attributeReleasePolicies.put(ex.getScopeType(), ex);
            } else {
                LOGGER.debug("OpenID Connect scope [{}] is not configured for use and will be ignored", ex.getScopeType());
            }
        }
    }));
    if (!userScopes.isEmpty()) {
        LOGGER.debug("Configuring attributes release policies for user-defined scopes [{}]", userScopes);
        userScopes.forEach(t -> attributeReleasePolicies.put(t.getScopeName(), t));
    }
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RegisteredServiceAttributeReleasePolicyContext(org.apereo.cas.services.RegisteredServiceAttributeReleasePolicyContext) OidcCustomScopeAttributeReleasePolicy(org.apereo.cas.oidc.claims.OidcCustomScopeAttributeReleasePolicy) Reflections(org.reflections.Reflections) BaseOidcScopeAttributeReleasePolicy(org.apereo.cas.oidc.claims.BaseOidcScopeAttributeReleasePolicy) LinkedHashMap(java.util.LinkedHashMap) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) DefaultOAuth20ProfileScopeToAttributesFilter(org.apereo.cas.support.oauth.profile.DefaultOAuth20ProfileScopeToAttributesFilter) FilterBuilder(org.reflections.util.FilterBuilder) Map(java.util.Map) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) LinkedHashSet(java.util.LinkedHashSet) Unchecked(org.jooq.lambda.Unchecked) ClassUtils(org.springframework.util.ClassUtils) OAuth20Utils(org.apereo.cas.support.oauth.util.OAuth20Utils) OidcConstants(org.apereo.cas.oidc.OidcConstants) Collection(java.util.Collection) lombok.val(lombok.val) RegisteredService(org.apereo.cas.services.RegisteredService) ClasspathHelper(org.reflections.util.ClasspathHelper) OAuth20AccessToken(org.apereo.cas.ticket.accesstoken.OAuth20AccessToken) OidcAttributeReleasePolicyFactory(org.apereo.cas.oidc.scopes.OidcAttributeReleasePolicyFactory) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) OidcRegisteredService(org.apereo.cas.services.OidcRegisteredService) Service(org.apereo.cas.authentication.principal.Service) Principal(org.apereo.cas.authentication.principal.Principal) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) BaseOidcScopeAttributeReleasePolicy(org.apereo.cas.oidc.claims.BaseOidcScopeAttributeReleasePolicy) FilterBuilder(org.reflections.util.FilterBuilder) Reflections(org.reflections.Reflections)

Aggregations

FilterBuilder (org.reflections.util.FilterBuilder)46 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)23 Reflections (org.reflections.Reflections)19 Test (org.junit.Test)17 SubTypesScanner (org.reflections.scanners.SubTypesScanner)9 ResourcesScanner (org.reflections.scanners.ResourcesScanner)8 Test (org.junit.jupiter.api.Test)7 ArrayList (java.util.ArrayList)6 URL (java.net.URL)4 lombok.val (lombok.val)4 Field (java.lang.reflect.Field)2 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Set (java.util.Set)2 BeforeClass (org.junit.BeforeClass)2 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)2 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)2 ClasspathHelper (org.reflections.util.ClasspathHelper)2 Stopwatch (com.google.common.base.Stopwatch)1