Search in sources :

Example 6 with AbstractAuditLogger

use of org.jbpm.process.audit.AbstractAuditLogger in project jbpm by kiegroup.

the class AsyncAuditLogProducerTest method testAsyncAuditLoggerComplete.

@Test
public void testAsyncAuditLoggerComplete() throws Exception {
    Environment env = createEnvironment(context);
    // load the process
    KieBase kbase = createKnowledgeBase();
    // create a new session
    KieSession session = createSession(kbase, env);
    Map<String, Object> jmsProps = new HashMap<String, Object>();
    jmsProps.put("jbpm.audit.jms.transacted", false);
    jmsProps.put("jbpm.audit.jms.connection.factory", factory);
    jmsProps.put("jbpm.audit.jms.queue", queue);
    AbstractAuditLogger logger = AuditLoggerFactory.newInstance(Type.JMS, session, jmsProps);
    Assertions.assertThat(logger).isNotNull();
    Assertions.assertThat((logger instanceof AsyncAuditLogProducer)).isTrue();
    // start process instance
    ProcessInstance processInstance = session.startProcess("com.sample.ruleflow");
    MessageReceiver receiver = new MessageReceiver();
    receiver.receiveAndProcess(queue, ((EntityManagerFactory) env.get(EnvironmentName.ENTITY_MANAGER_FACTORY)), 2000, 11);
    // validate if everything is stored in db
    AuditLogService logService = new JPAAuditLogService(env);
    List<ProcessInstanceLog> processInstances = logService.findProcessInstances("com.sample.ruleflow");
    Assertions.assertThat(processInstances.size()).isEqualTo(1);
    List<NodeInstanceLog> nodeInstances = logService.findNodeInstances(processInstance.getId());
    Assertions.assertThat(nodeInstances.size()).isEqualTo(6);
    for (NodeInstanceLog nodeInstance : nodeInstances) {
        Assertions.assertThat(processInstance.getId()).isEqualTo(nodeInstance.getProcessInstanceId().longValue());
        Assertions.assertThat(nodeInstance.getProcessId()).isEqualTo("com.sample.ruleflow");
        Assertions.assertThat(nodeInstance.getDate()).isNotNull();
    }
    logService.clear();
    processInstances = logService.findProcessInstances("com.sample.ruleflow");
    logService.dispose();
    Assertions.assertThat(processInstances).isEmpty();
}
Also used : JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) AuditLogService(org.jbpm.process.audit.AuditLogService) NodeInstanceLog(org.jbpm.process.audit.NodeInstanceLog) HashMap(java.util.HashMap) JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUtil.createEnvironment(org.jbpm.persistence.util.PersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) AbstractAuditLogServiceTest.createKieSession(org.jbpm.process.audit.AbstractAuditLogServiceTest.createKieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) ProcessInstanceLog(org.jbpm.process.audit.ProcessInstanceLog) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 7 with AbstractAuditLogger

use of org.jbpm.process.audit.AbstractAuditLogger in project jbpm by kiegroup.

the class AsyncAuditLogProducerTest method testAsyncAuditLoggerCompleteWithVariables.

@Test
public void testAsyncAuditLoggerCompleteWithVariables() throws Exception {
    Environment env = createEnvironment(context);
    // load the process
    KieBase kbase = createKnowledgeBase();
    // create a new session
    KieSession session = createSession(kbase, env);
    Map<String, Object> jmsProps = new HashMap<String, Object>();
    jmsProps.put("jbpm.audit.jms.transacted", false);
    jmsProps.put("jbpm.audit.jms.connection.factory", factory);
    jmsProps.put("jbpm.audit.jms.queue", queue);
    AbstractAuditLogger logger = AuditLoggerFactory.newInstance(Type.JMS, session, jmsProps);
    Assertions.assertThat(logger).isNotNull();
    Assertions.assertThat((logger instanceof AsyncAuditLogProducer)).isTrue();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("s", "test value");
    // start process instance
    ProcessInstance processInstance = session.startProcess("com.sample.ruleflow3", params);
    MessageReceiver receiver = new MessageReceiver();
    receiver.receiveAndProcess(queue, ((EntityManagerFactory) env.get(EnvironmentName.ENTITY_MANAGER_FACTORY)), 3000, 13);
    // validate if everything is stored in db
    AuditLogService logService = new JPAAuditLogService(env);
    List<ProcessInstanceLog> processInstances = logService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances.size()).isEqualTo(1);
    List<NodeInstanceLog> nodeInstances = logService.findNodeInstances(processInstance.getId());
    Assertions.assertThat(nodeInstances.size()).isEqualTo(6);
    for (NodeInstanceLog nodeInstance : nodeInstances) {
        Assertions.assertThat(nodeInstance.getProcessInstanceId().longValue()).isEqualTo(processInstance.getId());
        Assertions.assertThat(nodeInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
        Assertions.assertThat(nodeInstance.getDate()).isNotNull();
    }
    // verify variables
    List<VariableInstanceLog> variables = logService.findVariableInstances(processInstance.getId());
    Assertions.assertThat(variables).isNotNull();
    Assertions.assertThat(variables).hasSize(2);
    VariableInstanceLog var = variables.get(0);
    // initial value from rule flow definition
    Assertions.assertThat(var.getValue()).isEqualTo("InitialValue");
    Assertions.assertThat(var.getOldValue()).isIn("", " ", null);
    Assertions.assertThat(var.getProcessInstanceId().longValue()).isEqualTo(processInstance.getId());
    Assertions.assertThat(var.getProcessId()).isEqualTo(processInstance.getProcessId());
    Assertions.assertThat(var.getVariableId()).isEqualTo("s");
    Assertions.assertThat(var.getVariableInstanceId()).isEqualTo("s");
    // value given at process start
    var = variables.get(1);
    // initial value from rule flow definition
    Assertions.assertThat(var.getValue()).isEqualTo("test value");
    Assertions.assertThat(var.getOldValue()).isEqualTo("InitialValue");
    Assertions.assertThat(var.getProcessInstanceId().longValue()).isEqualTo(processInstance.getId());
    Assertions.assertThat(var.getProcessId()).isEqualTo(processInstance.getProcessId());
    Assertions.assertThat(var.getVariableId()).isEqualTo("s");
    Assertions.assertThat(var.getVariableInstanceId()).isEqualTo("s");
    logService.clear();
    processInstances = logService.findProcessInstances("com.sample.ruleflow3");
    logService.dispose();
    Assertions.assertThat(processInstances).isNullOrEmpty();
}
Also used : JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) AuditLogService(org.jbpm.process.audit.AuditLogService) NodeInstanceLog(org.jbpm.process.audit.NodeInstanceLog) HashMap(java.util.HashMap) JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) VariableInstanceLog(org.jbpm.process.audit.VariableInstanceLog) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUtil.createEnvironment(org.jbpm.persistence.util.PersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) AbstractAuditLogServiceTest.createKieSession(org.jbpm.process.audit.AbstractAuditLogServiceTest.createKieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) ProcessInstanceLog(org.jbpm.process.audit.ProcessInstanceLog) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 8 with AbstractAuditLogger

use of org.jbpm.process.audit.AbstractAuditLogger in project jbpm by kiegroup.

the class AsyncAuditLogProducerTest method testAsyncAuditProducerTransactionalWithRollback.

@Test
public void testAsyncAuditProducerTransactionalWithRollback() throws Exception {
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    Environment env = createEnvironment(context);
    // load the process
    KieBase kbase = createKnowledgeBase();
    // create a new session
    KieSession session = createSession(kbase, env);
    Map<String, Object> jmsProps = new HashMap<String, Object>();
    jmsProps.put("jbpm.audit.jms.transacted", true);
    jmsProps.put("jbpm.audit.jms.connection.factory", factory);
    jmsProps.put("jbpm.audit.jms.queue", queue);
    AbstractAuditLogger logger = AuditLoggerFactory.newInstance(Type.JMS, session, jmsProps);
    Assertions.assertThat(logger).isNotNull();
    Assertions.assertThat((logger instanceof AsyncAuditLogProducer)).isTrue();
    // start process instance
    long processInstanceId = session.startProcess("com.sample.ruleflow").getId();
    ut.rollback();
    MessageReceiver receiver = new MessageReceiver();
    List<Message> messages = receiver.receive(queue);
    Assertions.assertThat(messages).isNotNull();
    Assertions.assertThat(messages.size()).isEqualTo(0);
}
Also used : UserTransaction(javax.transaction.UserTransaction) Message(javax.jms.Message) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) PersistenceUtil.createEnvironment(org.jbpm.persistence.util.PersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) AbstractAuditLogServiceTest.createKieSession(org.jbpm.process.audit.AbstractAuditLogServiceTest.createKieSession) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 9 with AbstractAuditLogger

use of org.jbpm.process.audit.AbstractAuditLogger in project jbpm by kiegroup.

the class InjectableRegisterableItemsFactory method getAuditLoggerInstance.

/**
 * Provides  AuditLogger implementation, JPA or JMS.
 * JPA is the default one and JMS requires to have configuration file (.properties)
 * to be available on classpath under 'jbpm.audit.jms.properties' name.
 * This file must have following properties defined:
 * <ul>
 *  <li>jbpm.audit.jms.connection.factory.jndi - JNDI name of the connection factory to look up - type String</li>
 *  <li>jbpm.audit.jms.queue.jndi - JNDI name of the queue to look up - type String</li>
 * </ul>
 * @return instance of the audit logger
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected AbstractAuditLogger getAuditLoggerInstance(RuntimeEngine engine) {
    DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
    AbstractAuditLogger auditLogger = null;
    if ("true".equals(System.getProperty("jbpm.audit.jms.enabled")) || descriptor.getAuditMode() == AuditMode.JMS) {
        try {
            Properties properties = new Properties();
            properties.load(getRuntimeManager().getEnvironment().getClassLoader().getResourceAsStream("/jbpm.audit.jms.properties"));
            auditLogger = AuditLoggerFactory.newJMSInstance((Map) properties);
        } catch (IOException e) {
            logger.error("Unable to load jms audit properties from {}", "/jbpm.audit.jms.properties", e);
        }
        auditLogger.setBuilder(getAuditBuilder(engine));
    } else if (descriptor.getAuditMode() == AuditMode.JPA) {
        if (descriptor.getPersistenceUnit().equals(descriptor.getAuditPersistenceUnit())) {
            auditLogger = AuditLoggerFactory.newJPAInstance(engine.getKieSession().getEnvironment());
        } else {
            auditLogger = new JPAWorkingMemoryDbLogger(EntityManagerFactoryManager.get().getOrCreate(descriptor.getAuditPersistenceUnit()));
        }
        auditLogger.setBuilder(getAuditBuilder(engine));
    }
    return auditLogger;
}
Also used : DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) IOException(java.io.IOException) Properties(java.util.Properties) JPAWorkingMemoryDbLogger(org.jbpm.process.audit.JPAWorkingMemoryDbLogger) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with AbstractAuditLogger

use of org.jbpm.process.audit.AbstractAuditLogger in project jbpm by kiegroup.

the class DefaultRegisterableItemsFactory method getProcessEventListeners.

@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
    List<ProcessEventListener> defaultListeners = new ArrayList<ProcessEventListener>();
    DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
    if (descriptor == null) {
        // register JPAWorkingMemoryDBLogger
        AbstractAuditLogger logger = AuditLoggerFactory.newJPAInstance(runtime.getKieSession().getEnvironment());
        logger.setBuilder(getAuditBuilder(runtime));
        defaultListeners.add(logger);
    } else if (descriptor.getAuditMode() == AuditMode.JPA) {
        // register JPAWorkingMemoryDBLogger
        AbstractAuditLogger logger = null;
        if (descriptor.getPersistenceUnit().equals(descriptor.getAuditPersistenceUnit())) {
            logger = AuditLoggerFactory.newJPAInstance(runtime.getKieSession().getEnvironment());
        } else {
            Environment env = EnvironmentFactory.newEnvironment();
            env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, EntityManagerFactoryManager.get().getOrCreate(descriptor.getAuditPersistenceUnit()));
            logger = AuditLoggerFactory.newJPAInstance(env);
        }
        logger.setBuilder(getAuditBuilder(runtime));
        defaultListeners.add(logger);
    } else if (descriptor.getAuditMode() == AuditMode.JMS) {
        try {
            if (jmsLogger == null) {
                Properties properties = new Properties();
                InputStream input = getRuntimeManager().getEnvironment().getClassLoader().getResourceAsStream("/jbpm.audit.jms.properties");
                // required for junit test
                if (input == null) {
                    input = getRuntimeManager().getEnvironment().getClassLoader().getResourceAsStream("jbpm.audit.jms.properties");
                }
                properties.load(input);
                logger.debug("Creating AsyncAuditLogProducer {}", properties);
                jmsLogger = AuditLoggerFactory.newJMSInstance((Map) properties);
                jmsLogger.setBuilder(getAuditBuilder(runtime));
            }
            defaultListeners.add(jmsLogger);
        } catch (IOException e) {
            logger.error("Unable to load jms audit properties from {}", "/jbpm.audit.jms.properties", e);
        }
    }
    // add any custom listeners
    defaultListeners.addAll(super.getProcessEventListeners(runtime));
    // add listeners from descriptor
    defaultListeners.addAll(getEventListenerFromDescriptor(runtime, ProcessEventListener.class));
    return defaultListeners;
}
Also used : InputStream(java.io.InputStream) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) IOException(java.io.IOException) Properties(java.util.Properties) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AbstractAuditLogger (org.jbpm.process.audit.AbstractAuditLogger)13 Test (org.junit.Test)11 KieSession (org.kie.api.runtime.KieSession)10 HashMap (java.util.HashMap)9 Environment (org.kie.api.runtime.Environment)9 PersistenceUtil.createEnvironment (org.jbpm.persistence.util.PersistenceUtil.createEnvironment)8 AbstractAuditLogServiceTest.createKieSession (org.jbpm.process.audit.AbstractAuditLogServiceTest.createKieSession)8 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)8 KieBase (org.kie.api.KieBase)8 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 Message (javax.jms.Message)4 EntityManagerFactory (javax.persistence.EntityManagerFactory)4 AuditLogService (org.jbpm.process.audit.AuditLogService)4 JPAAuditLogService (org.jbpm.process.audit.JPAAuditLogService)4 NodeInstanceLog (org.jbpm.process.audit.NodeInstanceLog)4 ProcessInstanceLog (org.jbpm.process.audit.ProcessInstanceLog)4 ArrayList (java.util.ArrayList)3 UserTransaction (javax.transaction.UserTransaction)3 ServicesAwareAuditEventBuilder (org.jbpm.kie.services.impl.audit.ServicesAwareAuditEventBuilder)3 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)3