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

io.quarkiverse.ironjacamar.runtime.endpoint.DefaultMessageEndpointFactory Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package io.quarkiverse.ironjacamar.runtime.endpoint;

import java.lang.reflect.Method;
import java.util.Objects;

import javax.transaction.xa.XAResource;

import jakarta.resource.spi.UnavailableException;
import jakarta.resource.spi.endpoint.MessageEndpoint;
import jakarta.resource.spi.endpoint.MessageEndpointFactory;
import jakarta.transaction.Transactional;

import io.quarkiverse.ironjacamar.Defaults;
import io.quarkiverse.ironjacamar.ResourceAdapterFactory;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.smallrye.common.annotation.Identifier;

public class DefaultMessageEndpointFactory implements MessageEndpointFactory {

    private final Class endpointClass;
    private final String identifier;
    private final ResourceAdapterFactory resourceAdapterSupport;
    private Boolean transacted;

    public DefaultMessageEndpointFactory(Class endpointClass, String identifier, ResourceAdapterFactory adapterFactory) {
        this.endpointClass = endpointClass;
        this.identifier = identifier;
        this.resourceAdapterSupport = adapterFactory;
    }

    @Override
    public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException {
        if (transacted == null) {
            Method endpointClassMethod = endpointClass.getMethod(method.getName(), method.getParameterTypes());
            Transactional annotation = endpointClassMethod.getAnnotation(Transactional.class);
            if (annotation == null) {
                transacted = Boolean.FALSE;
            } else {
                transacted = annotation.value() != Transactional.TxType.NEVER &&
                        annotation.value() != Transactional.TxType.NOT_SUPPORTED;
            }
        }
        return transacted;
    }

    @Override
    public MessageEndpoint createEndpoint(XAResource xaResource, long timeout) throws UnavailableException {
        // TODO: Implement timeout
        return createEndpoint(xaResource);
    }

    @Override
    public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException {
        final MessageEndpoint endpoint;
        if (xaResource == null) {
            endpoint = NoopMessageEndpoint.INSTANCE;
        } else {
            endpoint = new TransactionAwareMessageEndpoint(xaResource);
        }
        return resourceAdapterSupport.wrap(endpoint, getEndpointInstance());
    }

    @Override
    public String getActivationName() {
        return endpointClass.getName();
    }

    @Override
    public Class getEndpointClass() {
        return endpointClass;
    }

    private Object getEndpointInstance() {
        Object instance;
        ArcContainer container = Arc.container();
        if (Defaults.DEFAULT_RESOURCE_ADAPTER_NAME.equals(identifier)) {
            // Try with default identifier and fallback to default if null
            instance = container.select(endpointClass, Identifier.Literal.of(identifier)).orElse(null);
            if (instance == null) {
                instance = container.select(endpointClass).get();
            }
        } else {
            instance = container.select(endpointClass, Identifier.Literal.of(identifier)).get();
        }
        return Objects.requireNonNull(instance, "Unable to find endpoint instance for " + endpointClass.getName()
                + " with identifier " + identifier + " in Arc container");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy