<commit_before>/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.integration;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import io.spine.base.EventMessage;
import io.spine.core.BoundedContextName;
import io.spine.core.Event;
import io.spine.server.event.EventBus;
import io.spine.server.event.EventDispatcher;
import io.spine.server.transport.PublisherHub;
import io.spine.server.type.EventClass;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.protobuf.AnyPacker.unpack;
import static io.spine.util.Exceptions.newIllegalArgumentException;

/**
 * An adapter between the {@link IntegrationBus} and the {@link EventBus}.
 */
final class BusAdapter {

    /**
     * The wrapped local event bus.
     */
    private final EventBus targetBus;

    /**
     * The name of the bounded context, to which the wrapped local bus belongs.
     */
    private final BoundedContextName boundedContextName;

    /**
     * The publisher hub, used publish the messages dispatched from the local buses to external
     * collaborators.
     */
    private final PublisherHub publisherHub;

    private BusAdapter(Builder builder) {
        this.targetBus = builder.targetBus;
        this.boundedContextName = builder.boundedContextName;
        this.publisherHub = builder.publisherHub;
    }

    /**
     * Wraps a given {@code ExternalMessage} into an {@link ExternalMessageEnvelope}
     * to allow its processing inside of the {@code IntegrationBus}.
     *
     * @param message
     *         an external message to wrap
     * @return an {@code ExternalMessageEnvelope}, containing the given message
     */
    ExternalMessageEnvelope toExternalEnvelope(ExternalMessage message) {
        Message originalMessage = unpack(message.getOriginalMessage());
        return ExternalMessageEnvelope.of(message, originalMessage);
    }

    /**
     * Creates a dispatcher suitable for the wrapped local bus, dispatching the messages of
     * the given class.
     *
     * <p>The created dispatcher is serving as a listener, notifying the {@code IntegrationBus}
     * of the messages, that are requested by the collaborators outside of this bounded context.
     *
     * @param messageClass
     *         the class of message to be dispatched by the created dispatcher
     * @return a dispatcher for the local bus
     */
    private EventDispatcher createDispatcher(Class<? extends Message> messageClass) {
        @SuppressWarnings("unchecked") // Logically checked.
        Class<? extends EventMessage> eventClass = (Class<? extends EventMessage>) messageClass;
        EventClass eventType = EventClass.from(eventClass);
        EventDispatcher result = new DomesticEventPublisher(
                boundedContextName, publisherHub, eventType
        );<commit_msg>Fix what is supplied as the "original message"<commit_after>/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.integration;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import io.spine.base.EventMessage;
import io.spine.core.BoundedContextName;
import io.spine.core.Event;
import io.spine.server.event.EventBus;
import io.spine.server.event.EventDispatcher;
import io.spine.server.transport.PublisherHub;
import io.spine.server.type.EventClass;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.protobuf.AnyPacker.unpack;
import static io.spine.util.Exceptions.newIllegalArgumentException;

/**
 * An adapter between the {@link IntegrationBus} and the {@link EventBus}.
 */
final class BusAdapter {

    /**
     * The wrapped local event bus.
     */
    private final EventBus targetBus;

    /**
     * The name of the bounded context, to which the wrapped local bus belongs.
     */
    private final BoundedContextName boundedContextName;

    /**
     * The publisher hub, used publish the messages dispatched from the local buses to external
     * collaborators.
     */
    private final PublisherHub publisherHub;

    private BusAdapter(Builder builder) {
        this.targetBus = builder.targetBus;
        this.boundedContextName = builder.boundedContextName;
        this.publisherHub = builder.publisherHub;
    }

    /**
     * Wraps a given {@code ExternalMessage} into an {@link ExternalMessageEnvelope}
     * to allow its processing inside of the {@code IntegrationBus}.
     *
     * @param message
     *         an external message to wrap
     * @return an {@code ExternalMessageEnvelope}, containing the given message
     */
    ExternalMessageEnvelope toExternalEnvelope(ExternalMessage message) {
        Event event = unpack(message.getOriginalMessage(), Event.class);
        return ExternalMessageEnvelope.of(message, event.enclosedMessage());
    }

    /**
     * Creates a dispatcher suitable for the wrapped local bus, dispatching the messages of
     * the given class.
     *
     * <p>The created dispatcher is serving as a listener, notifying the {@code IntegrationBus}
     * of the messages, that are requested by the collaborators outside of this bounded context.
     *
     * @param messageClass
     *         the class of message to be dispatched by the created dispatcher
     * @return a dispatcher for the local bus
     */
    private EventDispatcher createDispatcher(Class<? extends Message> messageClass) {
        @SuppressWarnings("unchecked") // Logically checked.
        Class<? extends EventMessage> eventClass = (Class<? extends EventMessage>) messageClass;
        EventClass eventType = EventClass.from(eventClass);
        EventDispatcher result = new DomesticEventPublisher(
                boundedContextName, publisherHub, eventType
        );
{'fraction_non_alphanumeric': 0.07336460776648424, 'fraction_numerical': 0.0010424811050299713, 'mean_word_length': 3.560308972073678, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3522208', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1883, 'n_words': 752}