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

com.github.lontime.extcopper.provider.AbstractProvider Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.github.lontime.extcopper.provider;

import java.util.ArrayList;
import java.util.List;

import com.github.lontime.base.commonj.components.AbstractComponentLifecycle;
import com.github.lontime.base.commonj.components.ComponentShutdownPriority;
import com.github.lontime.extcopper.common.ProcessingEngineWrapper;
import com.github.lontime.extcopper.container.DefaultPersistentEngineContainer;
import com.github.lontime.extcopper.container.DefaultTransientEngineContainer;
import com.github.lontime.extcopper.container.EngineContainer;
import org.copperengine.core.DependencyInjector;
import org.copperengine.core.audit.BatchingAuditTrail;
import org.copperengine.core.audit.ScottyAuditTrailQueryEngine;

/**
 * AbstractProvider.
 *
 * @author lontime
 * @since 1.0
 */
public abstract class AbstractProvider extends AbstractComponentLifecycle implements Provider {

    private final List containers = new ArrayList<>();

    private DependencyInjector dependencyInjector;

    @Override
    public void withDependencyInjector(DependencyInjector dependencyInjector) {
        this.dependencyInjector = dependencyInjector;
    }

    public AbstractProvider() {
        containers.add(new DefaultPersistentEngineContainer());
        containers.add(new DefaultTransientEngineContainer());
    }

    @Override
    public int getShutdownPriority() {
        return ComponentShutdownPriority.LIB_LOW.getValue();
    }

    @Override
    public BatchingAuditTrail getBatchingAuditTrail() {
        return containers.stream().filter(s -> s.getName()
                        .equals(EngineContainer.DEFAULT_PERSISTENT_ENGINE_NAME))
                .map(s -> s.getBatchingAuditTrail())
                .findAny().orElse(null);
    }

    @Override
    public ScottyAuditTrailQueryEngine getAuditTrailQueryEngine() {
        return containers.stream().filter(s -> s.getName()
                        .equals(EngineContainer.DEFAULT_PERSISTENT_ENGINE_NAME))
                .map(s -> s.getAuditTrailQueryEngine())
                .findAny().orElse(null);
    }

    @Override
    public void initialize() {
        if (dependencyInjector != null) {
            containers.forEach(c -> c.setDependencyInjector(dependencyInjector));
        }
        containers.forEach(EngineContainer::initialize);
    }

    @Override
    public void start() {
        containers.forEach(EngineContainer::start);
    }

    @Override
    public void stop() {
        containers.forEach(EngineContainer::stop);
    }

    @Override
    public void destroy() {
        containers.forEach(EngineContainer::destroy);
    }

    @Override
    @SuppressWarnings("all")
    public ProcessingEngineWrapper getProcessingEngine(String name) {
        return containers.stream().filter(s -> s.getName().equals(name))
                .map(s -> s.getEngine())
                .findAny().orElse(null);
    }

    @Override
    public void close() {
        reentrantStopAndRemoveHooklet();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy