All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.katharsis.resource.registry.DefaultResourceLookup Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.katharsis.resource.registry;

import io.katharsis.repository.RelationshipRepository;
import io.katharsis.repository.ResourceRepository;
import io.katharsis.repository.annotations.JsonApiRelationshipRepository;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.resource.annotations.JsonApiResource;
import org.reflections.Reflections;

import java.util.HashSet;
import java.util.Set;

/**
 * Scans all classes in provided package and finds all resources and repositories associated with found resource.
 */
public class DefaultResourceLookup implements ResourceLookup {

	private String packageName;
	private Reflections reflections;

	public DefaultResourceLookup(String packageName) {
		this.packageName = packageName;
        if (packageName != null) {
            String[] packageNames = packageName.split(",");
            reflections = new Reflections(packageNames);
        } else {
            reflections = new Reflections(packageName);
        }
	}

	@Override
	public Set> getResourceClasses() {
        return reflections.getTypesAnnotatedWith(JsonApiResource.class);
	}
	
	@Override
	public Set> getResourceRepositoryClasses() {
		Set> annotatedResourceRepositories = reflections.getTypesAnnotatedWith(JsonApiResourceRepository.class);
		Set> annotatedRelationshipRepositories = reflections.getTypesAnnotatedWith(JsonApiRelationshipRepository.class);
		Set> resourceRepositories = reflections.getSubTypesOf(ResourceRepository.class);
		Set> relationshipRepositories = reflections.getSubTypesOf(RelationshipRepository.class);
		
		Set> result = new HashSet<>();
		result.addAll(annotatedResourceRepositories);
		result.addAll(annotatedRelationshipRepositories);
		result.addAll(resourceRepositories);
		result.addAll(relationshipRepositories);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy