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

com.jslsolucoes.jpa.ee.discovery.TransactionCdiExtension Maven / Gradle / Ivy

There is a newer version: 1.0.32
Show newest version
package com.jslsolucoes.jpa.ee.discovery;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.WithAnnotations;
import javax.ws.rs.Path;

import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jslsolucoes.jpa.ee.PersistenceUnitType;
import com.jslsolucoes.jpa.ee.transaction.Transaction;
import com.jslsolucoes.jpa.ee.transaction.TransactionAnnotationLiteral;
import com.jslsolucoes.jpa.ee.transaction.TransactionUnitAnnotationLiteral;

public class TransactionCdiExtension implements Extension {

    private static final Logger logger = LoggerFactory.getLogger(TransactionCdiExtension.class);

    public TransactionCdiExtension() {

    }

    public  void processAnnotatedType(
	    @Observes @WithAnnotations(Path.class) ProcessAnnotatedType processAnnotatedType) {
	AnnotatedType annotatedType = processAnnotatedType.getAnnotatedType();
	logger.debug("Processing type {} for add transaction annotation", annotatedType);
	AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder().readFromType(annotatedType);
	for (AnnotatedMethod annotatedMethod : annotatedType.getMethods()) {
	    if (!annotatedMethod.isAnnotationPresent(Transaction.class)) {
		logger.debug("Adding transaction annotation to {}", annotatedMethod);
		builder.addToMethod(annotatedMethod,
			new TransactionAnnotationLiteral(new TransactionUnitAnnotationLiteral[] {
				new TransactionUnitAnnotationLiteral(30, PersistenceUnitType.DEFAULT) }));
	    } else {
		logger.debug("Transaction annotation already defined", annotatedType);
	    }
	}
	processAnnotatedType.setAnnotatedType(builder.create());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy