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

nl.vpro.domain.classification.ClassificationServiceLocator Maven / Gradle / Ivy

Go to download

The classes needed for the 'classification' service used in POMS. This os based on ClassificationScheme xml's as provided by EBU. It at the moment is only used for genres, but it could in principle accommodate other types of classification based on a fixed list.

There is a newer version: 8.3.0
Show newest version
package nl.vpro.domain.classification;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import jakarta.inject.Inject;
import jakarta.inject.Provider;

/**
 * @author Michiel Meeuwissen
 * @since 3.2
 */
@Slf4j
public class ClassificationServiceLocator  {


    private static ClassificationServiceLocator singleton;

    private static boolean warned = false;

    private static List terms;

    @Inject
    private Provider classificationService = () -> EmptyClassificationService.INSTANCE;

    private ClassificationServiceLocator() {
        singleton = this;
        ServiceLoader.load(ClassificationService.class).iterator().forEachRemaining(c -> {
            // TODO load this stuff a bit more easily
            }
        );
    }
    private ClassificationServiceLocator(ClassificationService classificationService) {
        this();
        this.classificationService = () -> classificationService;
    }

    public static ClassificationService getInstance() {
        if (singleton == null || singleton.classificationService == null || singleton.classificationService.get() == null) {
            if (! warned) {
                log.warn("No classification service bean found, returning the empty one");
                warned = true;
            }
            return EmptyClassificationService.INSTANCE;
        } else {
            return singleton.classificationService.get();
        }
    }

    public static void setInstance(ClassificationService classificationService) {
        if (singleton == null) {
            new ClassificationServiceLocator(classificationService);
        } else {
            if (singleton.classificationService.get() != EmptyClassificationService.INSTANCE
                && !singleton.classificationService.get().equals(classificationService)
            ) {
                throw new IllegalStateException();
            }
            if (classificationService != null) {
                log.info("Using classification service {} with {} terms", classificationService, classificationService.getClassificationScheme().getTerms().size());
            }
            singleton.classificationService = () -> classificationService;
        }
        warned = false;
    }

    public static List getTerms() {
        if (terms == null) {
            terms = getInstance()
                .values()
                .stream()
                .map(Term::getTermId)
                .collect(Collectors.toList());
        }
        return terms;

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy