Search in sources :

Example 11 with ConfigurationBuilder

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

the class JpaTicketRegistryConfiguration method ticketPackagesToScan.

@Bean
public List<String> ticketPackagesToScan() {
    final Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(CentralAuthenticationService.NAMESPACE)).setScanners(new SubTypesScanner(false)));
    final Set<Class<?>> subTypes = (Set) reflections.getSubTypesOf(AbstractTicket.class);
    final List<String> packages = subTypes.stream().map(t -> t.getPackage().getName()).collect(Collectors.toList());
    return packages;
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) JpaLockingStrategy(org.apereo.cas.ticket.registry.support.JpaLockingStrategy) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) Autowired(org.springframework.beans.factory.annotation.Autowired) Beans(org.apereo.cas.configuration.support.Beans) Reflections(org.reflections.Reflections) TicketRegistryProperties(org.apereo.cas.configuration.model.core.ticket.registry.TicketRegistryProperties) StringUtils(org.apache.commons.lang3.StringUtils) EnableTransactionManagement(org.springframework.transaction.annotation.EnableTransactionManagement) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) TicketRegistry(org.apereo.cas.ticket.registry.TicketRegistry) LockingStrategy(org.apereo.cas.ticket.registry.support.LockingStrategy) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Qualifier(org.springframework.beans.factory.annotation.Qualifier) DataSource(javax.sql.DataSource) InetAddressUtils(org.apereo.cas.util.InetAddressUtils) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) TicketCatalog(org.apereo.cas.ticket.TicketCatalog) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) JpaTicketRegistryProperties(org.apereo.cas.configuration.model.support.jpa.ticketregistry.JpaTicketRegistryProperties) JpaConfigDataHolder(org.apereo.cas.configuration.model.support.jpa.JpaConfigDataHolder) Set(java.util.Set) CoreTicketUtils(org.apereo.cas.util.CoreTicketUtils) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) SubTypesScanner(org.reflections.scanners.SubTypesScanner) JpaTicketRegistry(org.apereo.cas.ticket.registry.JpaTicketRegistry) Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Lazy(org.springframework.context.annotation.Lazy) AbstractTicket(org.apereo.cas.ticket.AbstractTicket) Bean(org.springframework.context.annotation.Bean) JpaBeans(org.apereo.cas.configuration.support.JpaBeans) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Set(java.util.Set) AbstractTicket(org.apereo.cas.ticket.AbstractTicket) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 12 with ConfigurationBuilder

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

the class CasEmbeddedContainerUtils method getCasBannerInstance.

/**
 * Gets cas banner instance.
 *
 * @return the cas banner instance
 */
public static Banner getCasBannerInstance() {
    final String packageName = CasEmbeddedContainerUtils.class.getPackage().getName();
    final Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().includePackage(packageName)).setUrls(ClasspathHelper.forPackage(packageName)).setScanners(new SubTypesScanner(true)));
    final Set<Class<? extends AbstractCasBanner>> subTypes = reflections.getSubTypesOf(AbstractCasBanner.class);
    subTypes.remove(DefaultCasBanner.class);
    if (subTypes.isEmpty()) {
        return new DefaultCasBanner();
    }
    try {
        final Class<? extends AbstractCasBanner> clz = subTypes.iterator().next();
        LOGGER.debug("Created banner [{}]", clz);
        return clz.getDeclaredConstructor().newInstance();
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new DefaultCasBanner();
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) AbstractCasBanner(org.apereo.cas.util.spring.boot.AbstractCasBanner) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) DefaultCasBanner(org.apereo.cas.util.spring.boot.DefaultCasBanner) UtilityClass(lombok.experimental.UtilityClass) Reflections(org.reflections.Reflections)

Example 13 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project che by eclipse.

the class TypeScriptDtoGenerator method init.

/**
     * Init stuff is responsible to grab all DTOs found in classpath (classloader) and setup model for String Template
     */
protected void init() {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder().setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
    if (useClassPath) {
        configurationBuilder.setUrls(forJavaClassPath());
    } else {
        configurationBuilder.setUrls(forClassLoader());
    }
    // keep only DTO interfaces
    Reflections reflections = new Reflections(configurationBuilder);
    List<Class<?>> annotatedWithDtos = new ArrayList<>(reflections.getTypesAnnotatedWith(DTO.class));
    List<Class<?>> interfacesDtos = annotatedWithDtos.stream().filter(clazz -> clazz.isInterface()).collect(Collectors.toList());
    interfacesDtos.stream().forEach(this::analyze);
}
Also used : Resources(com.google.common.io.Resources) URL(java.net.URL) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Reflections(org.reflections.Reflections) Collectors(java.util.stream.Collectors) SubTypesScanner(org.reflections.scanners.SubTypesScanner) ArrayList(java.util.ArrayList) List(java.util.List) DtoModel(org.eclipse.che.plugin.typescript.dto.model.DtoModel) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ST(org.stringtemplate.v4.ST) ClasspathHelper.forJavaClassPath(org.reflections.util.ClasspathHelper.forJavaClassPath) DTO(org.eclipse.che.dto.shared.DTO) ClasspathHelper.forClassLoader(org.reflections.util.ClasspathHelper.forClassLoader) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ArrayList(java.util.ArrayList) DTO(org.eclipse.che.dto.shared.DTO) Reflections(org.reflections.Reflections)

Example 14 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project che by eclipse.

the class DtoGenerator method generate.

public void generate() {
    Set<URL> urls = getClasspathForPackages(dtoPackages);
    genFileName = genFileName.replace('/', File.separatorChar);
    String outputFilePath = genFileName;
    // Extract the name of the output file that will contain all the DTOs and its package.
    String myPackageBase = packageBase;
    if (!myPackageBase.endsWith("/")) {
        myPackageBase += "/";
    }
    myPackageBase = myPackageBase.replace('/', File.separatorChar);
    int packageStart = outputFilePath.lastIndexOf(myPackageBase) + myPackageBase.length();
    int packageEnd = outputFilePath.lastIndexOf(File.separatorChar);
    String fileName = outputFilePath.substring(packageEnd + 1);
    String className = fileName.substring(0, fileName.indexOf(".java"));
    String packageName = outputFilePath.substring(packageStart, packageEnd).replace(File.separatorChar, '.');
    File outFile = new File(outputFilePath);
    try {
        DtoTemplate dtoTemplate = new DtoTemplate(packageName, className, impl);
        Reflections reflection = new Reflections(new ConfigurationBuilder().setUrls(urls).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()));
        List<Class<?>> dtos = new ArrayList<>(reflection.getTypesAnnotatedWith(DTO.class));
        // We sort alphabetically to ensure deterministic order of routing types.
        Collections.sort(dtos, new ClassesComparator());
        for (Class<?> clazz : dtos) {
            // DTO are interface
            if (clazz.isInterface()) {
                dtoTemplate.addInterface(clazz);
            }
        }
        reflection = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader()).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()));
        List<Class<?>> dtosDependencies = new ArrayList<>(reflection.getTypesAnnotatedWith(DTO.class));
        dtosDependencies.removeAll(dtos);
        reflection = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader()).setScanners(new SubTypesScanner()));
        for (Class<?> clazz : dtosDependencies) {
            for (Class impl : reflection.getSubTypesOf(clazz)) {
                if (!(impl.isInterface() || urls.contains(impl.getProtectionDomain().getCodeSource().getLocation()))) {
                    if ("client".equals(dtoTemplate.getImplType())) {
                        if (isClientImpl(impl)) {
                            dtoTemplate.addImplementation(clazz, impl);
                        }
                    } else if ("server".equals(dtoTemplate.getImplType())) {
                        if (isServerImpl(impl)) {
                            dtoTemplate.addImplementation(clazz, impl);
                        }
                    }
                }
            }
        }
        // Emit the generated file.
        Files.createDirectories(outFile.toPath().getParent());
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFile))) {
            writer.write(dtoTemplate.toString());
        }
        if ("server".equals(impl)) {
            // Create file in META-INF/services/
            File outServiceFile = new File(myPackageBase + "META-INF/services/" + DtoFactoryVisitor.class.getCanonicalName());
            Files.createDirectories(outServiceFile.toPath().getParent());
            try (BufferedWriter serviceFileWriter = new BufferedWriter(new FileWriter(outServiceFile))) {
                serviceFileWriter.write(packageName + "." + className);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) File(java.io.File) DTO(org.eclipse.che.dto.shared.DTO) Reflections(org.reflections.Reflections)

Example 15 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project Gaffer by gchq.

the class StreamUtil method openStreams.

public static InputStream[] openStreams(final Class clazz, final String folderPath, final boolean logErrors) {
    if (null == folderPath) {
        return new InputStream[0];
    }
    String folderPathChecked = folderPath;
    if (!folderPathChecked.endsWith("/")) {
        folderPathChecked = folderPathChecked + "/";
    }
    if (folderPathChecked.startsWith("/")) {
        folderPathChecked = folderPathChecked.substring(1);
    }
    final Set<String> schemaFiles = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(ClasspathHelper.forClass(clazz))).getResources(Pattern.compile(".*"));
    final Iterator<String> itr = schemaFiles.iterator();
    while (itr.hasNext()) {
        if (!itr.next().startsWith(folderPathChecked)) {
            itr.remove();
        }
    }
    int index = 0;
    final InputStream[] schemas = new InputStream[schemaFiles.size()];
    for (final String schemaFile : schemaFiles) {
        schemas[index] = openStream(clazz, schemaFile, logErrors);
        index++;
    }
    return schemas;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) InputStream(java.io.InputStream) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Aggregations

ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)29 Reflections (org.reflections.Reflections)26 SubTypesScanner (org.reflections.scanners.SubTypesScanner)16 URL (java.net.URL)7 ArrayList (java.util.ArrayList)7 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)7 FilterBuilder (org.reflections.util.FilterBuilder)7 HashSet (java.util.HashSet)5 List (java.util.List)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Slf4j (lombok.extern.slf4j.Slf4j)5 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)4 ResourcesScanner (org.reflections.scanners.ResourcesScanner)4 ClasspathHelper (org.reflections.util.ClasspathHelper)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Map (java.util.Map)3 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)3 IOException (java.io.IOException)2