io.katharsis.resource.registry.ResourceRegistry Maven / Gradle / Ivy
package io.katharsis.resource.registry;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.katharsis.resource.exception.init.ResourceNotFoundInitializationException;
import io.katharsis.resource.information.ResourceInformation;
import io.katharsis.utils.java.Optional;
public class ResourceRegistry {
private final Map resources;
private final ServiceUrlProvider serviceUrlProvider;
private final Logger logger = LoggerFactory.getLogger(ResourceRegistry.class);
public ResourceRegistry(ServiceUrlProvider serviceUrlProvider) {
this.serviceUrlProvider = serviceUrlProvider;
this.resources = new HashMap();
}
public ResourceRegistry(Map resources, ServiceUrlProvider serviceUrlProvider) {
this.serviceUrlProvider = serviceUrlProvider;
this.resources = new HashMap(resources); // copying is slower but completely thread safe
}
/**
* Adds a new resource definition to a registry.
*
* @param resource class of a resource
* @param registryEntry resource information
* @param type of a resource
*/
public void addEntry(Class resource, RegistryEntry extends T> registryEntry) {
resources.put(resource, registryEntry);
registryEntry.initialize(this);
logger.debug("Added resource {} to ResourceRegistry", resource.getName());
}
/**
* Searches the registry for a resource identified by a JSON API resource type.
* If a resource cannot be found, null is returned.
*
* @param searchType resource type
* @return registry entry or null
*/
public RegistryEntry getEntry(String searchType) {
for (Map.Entry entry : resources.entrySet()) {
String type = getResourceType(entry.getKey());
if (type.equals(searchType)) {
return entry.getValue();
}
}
return null;
}
/**
* Searches the registry for a resource identified by a JSON API resource class.
* If a resource cannot be found, {@link ResourceNotFoundInitializationException} is thrown.
*
* @param clazz resource type
* @return registry entry
* @throws ResourceNotFoundInitializationException if resource is not found
*/
public RegistryEntry getEntry(Class clazz) {
return getEntry(clazz, false);
}
private RegistryEntry> getEntry(Class> clazz, boolean allowNull) {
Optional> resourceClazz = getResourceClass(clazz);
if (allowNull && !resourceClazz.isPresent())
return null;
else if (!resourceClazz.isPresent())
throw new ResourceNotFoundInitializationException(clazz.getCanonicalName());
return resources.get(resourceClazz.get());
}
/**
* Returns a JSON API resource type used by Katharsis. If a class cannot be found, null is returned.
* The value is fetched from {@link ResourceInformation#getResourceType()} attribute.
*
* @param clazz resource class
* @return resource type or null
*/
public String getResourceType(Class> clazz) {
RegistryEntry> entry = getEntry(clazz, true);
if (entry == null) {
return null;
}
ResourceInformation resourceInformation = entry.getResourceInformation();
return resourceInformation.getResourceType();
}
public Optional> getResourceClass(Object resource) {
return getResourceClass(resource.getClass());
}
public Optional> getResourceClass(Class> resourceClass) {
Class> currentClass = resourceClass;
while (currentClass != null && currentClass != Object.class) {
RegistryEntry> entry = resources.get(currentClass);
if (entry != null) {
return (Optional) Optional.of(currentClass);
}
currentClass = currentClass.getSuperclass();
}
return Optional.empty();
}
public String getResourceUrl(Class clazz) {
return serviceUrlProvider.getUrl() + "/" + getResourceType(clazz);
}
public String getServiceUrl() {
return serviceUrlProvider.getUrl();
}
/**
* Get a list of all registered resources by Katharsis.
*
* @return resources
*/
public Map getResources() {
return Collections.unmodifiableMap(resources);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy