com.webbfontaine.valuewebb.irms.factory.risk.RiskActionFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.irms.factory.risk.RiskActionFactory.java

Source

/*
 * Copyrights 2002-2013 Webb Fontaine
 * Developer: Sargis Harutyunyan
 * Date: 09 avr. 2013
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 */
package com.webbfontaine.valuewebb.irms.factory.risk;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.webbfontaine.valuewebb.irms.annotations.Selectivity;
import com.webbfontaine.valuewebb.irms.factory.ActionFactory;
import com.webbfontaine.valuewebb.irms.factory.ActionProcessor;
import com.webbfontaine.valuewebb.irms.impl.risk.repository.RiskRuleRepository;
import com.webbfontaine.valuewebb.irms.repo.CriteriaRepository;
import com.webbfontaine.valuewebb.irms.utils.ConcurrentUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;
import java.lang.annotation.Annotation;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Name("riskActionsFactory")
@Scope(ScopeType.APPLICATION)
public class RiskActionFactory implements ActionFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(RiskActionFactory.class);

    private static final Integer RISK_ACTION_FACTORY_ID = Integer.valueOf(2);

    private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(getExecutorService());

    private CriteriaRepository criteriaRepository;

    private RiskRuleRepository riskRuleRepository;

    @Override
    public Integer getId() {
        return RISK_ACTION_FACTORY_ID;
    }

    @Override
    public String getDescription() {
        return "Risk Type";
    }

    @Override
    public boolean isSupporting(Annotation annotation) {
        return annotation instanceof Selectivity;
    }

    @Override
    public ActionProcessor getActionProcessor() {
        RiskActionProcessor riskActionProcessor = new RiskActionProcessor(RISK_ACTION_FACTORY_ID, executor);
        riskActionProcessor.setCriteriaRepository(criteriaRepository);
        riskActionProcessor.setRiskRuleRepository(riskRuleRepository);

        return riskActionProcessor;
    }

    @PreDestroy
    public void destroy() {
        LOGGER.info("Shutting down Risk rule handler executor, please wait...");
        ConcurrentUtils.shutdownExecutor(executor, 30L, TimeUnit.SECONDS);
    }

    @In(create = true)
    public void setCriteriaRepository(CriteriaRepository criteriaRepository) {
        this.criteriaRepository = criteriaRepository;
    }

    @In(create = true)
    public void setRiskRuleRepository(RiskRuleRepository riskRuleRepository) {
        this.riskRuleRepository = riskRuleRepository;
    }

    private static ExecutorService getExecutorService() {
        return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
                new BasicThreadFactory.Builder().namingPattern("RiskRuleHandler-%d")
                        .uncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                            @Override
                            public void uncaughtException(Thread t, Throwable e) {
                                LOGGER.error("", e);
                            }
                        }).build());
    }

}