<commit_before>package poussecafe.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import poussecafe.exception.PousseCafeException;
import poussecafe.journal.ConsumptionFailureRepository;
import poussecafe.journal.MessageReplayer;
import poussecafe.journal.MessagingJournal;
import poussecafe.messaging.JacksonMessageAdapter;
import poussecafe.messaging.MessageAdapter;
import poussecafe.messaging.MessageListener;
import poussecafe.messaging.MessageListenerRegistry;
import poussecafe.messaging.MessageListenerRoutingKey;
import poussecafe.messaging.MessageReceiver;
import poussecafe.messaging.MessageSender;
import poussecafe.messaging.internal.InternalMessageQueue;
import poussecafe.process.DomainProcess;
import poussecafe.storable.IdentifiedStorable;
import poussecafe.storable.IdentifiedStorableData;
import poussecafe.storable.IdentifiedStorableFactory;
import poussecafe.storable.IdentifiedStorableRepository;
import poussecafe.storable.PrimitiveFactory;
import poussecafe.storable.PrimitiveSpecification;
import poussecafe.storable.StorableDefinition;
import poussecafe.storable.StorableImplementation;
import poussecafe.storage.Storage;
import poussecafe.util.IdGenerator;

import static java.util.Collections.unmodifiableCollection;

public class MetaApplicationContext {

    private Environment environment;

    private PrimitiveFactory primitiveFactory;

    private Injector injector;

    private MessageListenerRegistry messageListenerRegistry;

    private ConsumptionFailureRepository consumptionFailureRepository;

    private MessagingJournal messagingJournal;

    private MessageReplayer messageReplayer;

    private TransactionRunnerLocator storageServiceLocator;

    private InternalMessageQueue inMemoryMessageQueue;

    private MessageSender messageSender;

    private MessageReceiver messageReceiver;

    private Map<Class<?>, StorableServices> storableServices = new HashMap<>();

    private MessageAdapter messageAdapter;

    public MetaApplicationContext() {
        environment = new Environment();

        primitiveFactory = new PrimitiveFactory();
        primitiveFactory.setEnvironment(environment);

        messageListenerRegistry = new MessageListenerRegistry();
        messagingJournal = new MessagingJournal();

        storageServiceLocator = new TransactionRunnerLocator();
        storageServiceLocator.setEnvironment(environment);

        inMemoryMessageQueue = new InternalMessageQueue();
        messageSender = inMemoryMessageQueue;
        messageReceiver = inMemoryMessageQueue;

        messageAdapter = new JacksonMessageAdapter();
    }

    public Environment environment() {
        return environment;
    }

    public void loadBundle(MetaApplicationBundle bundle) {
        for(StorableDefinition definition : bundle.getDefinitions()) {
            environment.defineStorable(definition);
        }
        for(StorableImplementation implementation : bundle.getImplementations()) {
            environment.implementStorable(implementation);
        }
        for(Class<? extends DomainProcess> processClass : bundle.getProcesses()) {
            environment.defineProcess(processClass);
        }
        for(Class<?> serviceClass : bundle.getServices()) {
            environment.defineService(serviceClass);
        }
    }

    public void start() {
        if(environment.isAbstract()) {
            throw new PousseCafeException("Cannot start meta-application with an abstract environment");
        }

        injector = new Injector();
        injector.registerInjectableService(new IdGenerator());
        injector.registerInjectableService(environment);
        injector.registerInjectableService(MessageAdapter.class, messageAdapter);
        injector.registerInjectableService(storageServiceLocator);
        injector.registerInjectableService(messageListenerRegistry);

        configureContext();
        injector.injectDependencies();
        startMessageHandling();
    }

    private void configureContext() {
        configureStorables();
        configureServices();
        configureProcesses();
        configureMessageEmissionPolicies();
        configureMessageJournal();
        configureMessageReceivers();
        configureMessageReplayer();
    }

    protected void configureStorables() {
        for (Class<?> storableClass : environment.getDefinedStorables()) {
            configureStorable(storableClass);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private void configureStorable(Class<?> storableClass) {
        StorableDefinition definition = environment.getStorableDefinition(storableClass);
        if(definition.hasFactory() && definition.hasRepository()) {<commit_msg>Add logging in case of error while starting abstract environment.<commit_after>package poussecafe.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import poussecafe.exception.PousseCafeException;
import poussecafe.journal.ConsumptionFailureRepository;
import poussecafe.journal.MessageReplayer;
import poussecafe.journal.MessagingJournal;
import poussecafe.messaging.JacksonMessageAdapter;
import poussecafe.messaging.MessageAdapter;
import poussecafe.messaging.MessageListener;
import poussecafe.messaging.MessageListenerRegistry;
import poussecafe.messaging.MessageListenerRoutingKey;
import poussecafe.messaging.MessageReceiver;
import poussecafe.messaging.MessageSender;
import poussecafe.messaging.internal.InternalMessageQueue;
import poussecafe.process.DomainProcess;
import poussecafe.storable.IdentifiedStorable;
import poussecafe.storable.IdentifiedStorableData;
import poussecafe.storable.IdentifiedStorableFactory;
import poussecafe.storable.IdentifiedStorableRepository;
import poussecafe.storable.PrimitiveFactory;
import poussecafe.storable.PrimitiveSpecification;
import poussecafe.storable.StorableDefinition;
import poussecafe.storable.StorableImplementation;
import poussecafe.storage.Storage;
import poussecafe.util.IdGenerator;

import static java.util.Collections.unmodifiableCollection;
import static org.slf4j.LoggerFactory.getLogger;

public class MetaApplicationContext {

    private Environment environment;

    private PrimitiveFactory primitiveFactory;

    private Injector injector;

    private MessageListenerRegistry messageListenerRegistry;

    private ConsumptionFailureRepository consumptionFailureRepository;

    private MessagingJournal messagingJournal;

    private MessageReplayer messageReplayer;

    private TransactionRunnerLocator storageServiceLocator;

    private InternalMessageQueue inMemoryMessageQueue;

    private MessageSender messageSender;

    private MessageReceiver messageReceiver;

    private Map<Class<?>, StorableServices> storableServices = new HashMap<>();

    private MessageAdapter messageAdapter;

    public MetaApplicationContext() {
        environment = new Environment();

        primitiveFactory = new PrimitiveFactory();
        primitiveFactory.setEnvironment(environment);

        messageListenerRegistry = new MessageListenerRegistry();
        messagingJournal = new MessagingJournal();

        storageServiceLocator = new TransactionRunnerLocator();
        storageServiceLocator.setEnvironment(environment);

        inMemoryMessageQueue = new InternalMessageQueue();
        messageSender = inMemoryMessageQueue;
        messageReceiver = inMemoryMessageQueue;

        messageAdapter = new JacksonMessageAdapter();
    }

    public Environment environment() {
        return environment;
    }

    public void loadBundle(MetaApplicationBundle bundle) {
        for(StorableDefinition definition : bundle.getDefinitions()) {
            environment.defineStorable(definition);
        }
        for(StorableImplementation implementation : bundle.getImplementations()) {
            environment.implementStorable(implementation);
        }
        for(Class<? extends DomainProcess> processClass : bundle.getProcesses()) {
            environment.defineProcess(processClass);
        }
        for(Class<?> serviceClass : bundle.getServices()) {
            environment.defineService(serviceClass);
        }
    }

    public void start() {
        if(environment.isAbstract()) {
            Set<Class<?>> abstractStorables = environment.getAbstractStorables();
            logger.error("{} abstract storable(s):", abstractStorables.size());
            for(Class<?> abstractStorableClass : abstractStorables) {
                logger.error("- {}", abstractStorableClass.getName());
            }
            throw new PousseCafeException("Cannot start meta-application with an abstract environment");
        }

        injector = new Injector();
        injector.registerInjectableService(new IdGenerator());
        injector.registerInjectableService(environment);
        injector.registerInjectableService(MessageAdapter.class, messageAdapter);
        injector.registerInjectableService(storageServiceLocator);
        injector.registerInjectableService(messageListenerRegistry);

        configureContext();
        injector.injectDependencies();
        startMessageHandling();
    }

    private Logger logger = getLogger(getClass());

    private void configureContext() {
        configureStorables();
        configureServices();
        configureProcesses();
        configureMessageEmissionPolicies();
        configureMessageJournal();
        configureMessageReceivers();
        configureMessageReplayer();
    }

    protected void configureStorables() {
        for (Class<?> storableClass : environment.getDefinedStorables()) {
            configureStorable(storableClass);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private void configureStorable(Class<?> storableClass) {
        StorableDefinition definition = environment.getStorableDefinition(storableClass);
        if(definition.hasFactory() && definition.hasRepository()) {
{'fraction_non_alphanumeric': 0.07642422428414647, 'fraction_numerical': 0.00019954105557218397, 'mean_word_length': 4.298097251585624, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3875471', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2390, 'n_words': 460}