mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 14:05:44 +01:00
implemented LinkEnricher registration via Enrich annotation
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation to specify the source of an enricher.
|
||||||
|
*
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Enrich {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Source mapping class.
|
||||||
|
*
|
||||||
|
* @return source mapping class
|
||||||
|
*/
|
||||||
|
Class<?> value();
|
||||||
|
}
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import sonia.scm.plugin.ExtensionPoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link LinkEnricher} can be used to append hateoas links to a specific json response.
|
* A {@link LinkEnricher} can be used to append hateoas links to a specific json response.
|
||||||
|
* To register an enricher use the {@link Enrich} annotation or the {@link LinkEnricherRegistry} which is available
|
||||||
|
* via injection.
|
||||||
|
*
|
||||||
|
* <b>Warning:</b> enrichers are always registered as singletons.
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
|
@ExtensionPoint
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface LinkEnricher {
|
public interface LinkEnricher {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import sonia.scm.plugin.Extension;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers every {@link LinkEnricher} which is annotated with an {@link Enrich} annotation.
|
||||||
|
*/
|
||||||
|
@Extension
|
||||||
|
public class LinkEnricherAutoRegistration implements ServletContextListener {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(LinkEnricherAutoRegistration.class);
|
||||||
|
|
||||||
|
private final LinkEnricherRegistry registry;
|
||||||
|
private final Set<LinkEnricher> enrichers;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public LinkEnricherAutoRegistration(LinkEnricherRegistry registry, Set<LinkEnricher> enrichers) {
|
||||||
|
this.registry = registry;
|
||||||
|
this.enrichers = enrichers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
|
for (LinkEnricher enricher : enrichers) {
|
||||||
|
Enrich annotation = enricher.getClass().getAnnotation(Enrich.class);
|
||||||
|
if (annotation != null) {
|
||||||
|
registry.register(annotation.value(), enricher);
|
||||||
|
} else {
|
||||||
|
LOG.warn("found LinkEnricher extension {} without Enrich annotation", enricher.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent sce) {
|
||||||
|
// nothing todo
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Java6Assertions.assertThat;
|
||||||
|
|
||||||
|
class LinkEnricherAutoRegistrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRegisterAllAvailableLinkEnrichers() {
|
||||||
|
LinkEnricher one = new One();
|
||||||
|
LinkEnricher two = new Two();
|
||||||
|
LinkEnricher three = new Three();
|
||||||
|
LinkEnricher four = new Four();
|
||||||
|
Set<LinkEnricher> enrichers = ImmutableSet.of(one, two, three, four);
|
||||||
|
|
||||||
|
LinkEnricherRegistry registry = new LinkEnricherRegistry();
|
||||||
|
|
||||||
|
LinkEnricherAutoRegistration autoRegistration = new LinkEnricherAutoRegistration(registry, enrichers);
|
||||||
|
autoRegistration.contextInitialized(null);
|
||||||
|
|
||||||
|
assertThat(registry.allByType(String.class)).containsOnly(one, two);
|
||||||
|
assertThat(registry.allByType(Integer.class)).containsOnly(three);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Enrich(String.class)
|
||||||
|
public static class One implements LinkEnricher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enrich(LinkEnricherContext context, LinkAppender appender) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Enrich(String.class)
|
||||||
|
public static class Two implements LinkEnricher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enrich(LinkEnricherContext context, LinkAppender appender) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Enrich(Integer.class)
|
||||||
|
public static class Three implements LinkEnricher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enrich(LinkEnricherContext context, LinkAppender appender) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Four implements LinkEnricher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enrich(LinkEnricherContext context, LinkAppender appender) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user