package com.bonus.mq.rocket.tx;

import com.bonus.mq.tx.MQTxCheck;
import com.bonus.mq.tx.MQTxCheckComposite;
import com.bonus.mq.tx.MQTxChecker;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class RocketTxConfiguration {
   public RocketTxConfiguration(ApplicationContext applicationContext) {
      Map<String, Object> beans = (Map)applicationContext.getBeansWithAnnotation(MQTxChecker.class).entrySet().stream().filter((entryx) -> {
         return !ScopedProxyUtils.isScopedTarget((String)entryx.getKey());
      }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
      Iterator var3 = beans.entrySet().iterator();

      while(var3.hasNext()) {
         Map.Entry<String, Object> entry = (Map.Entry)var3.next();
         Class<?> clazz = AopProxyUtils.ultimateTargetClass(entry.getValue());
         if (!MQTxCheck.class.isAssignableFrom(entry.getValue().getClass())) {
            String var10002 = String.valueOf(clazz);
            throw new IllegalStateException(var10002 + " is not instance of " + MQTxCheck.class.getName());
         }

         RocketMQTemplate rocketMQTemplate = (RocketMQTemplate)applicationContext.getBean(RocketMQTemplate.class);
         ((MQTxCheckComposite)((TransactionMQProducer)rocketMQTemplate.getProducer()).getTransactionCheckListener()).addTrxExecutor((MQTxCheck)entry.getValue());
      }

   }
}
