Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jbpm.services.cdi.impl.manager;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.enterprise.context.spi.CreationalContext;
 import  javax.enterprise.inject.Any;
 import  javax.enterprise.inject.Instance;
 import  javax.enterprise.inject.UnsatisfiedResolutionException;
 import  javax.enterprise.inject.spi.Bean;
 import  javax.enterprise.inject.spi.BeanManager;
 import  javax.inject.Inject;
 
 import  org.drools.compiler.kie.builder.impl.KieContainerImpl;
 import  org.drools.compiler.kie.util.CDIHelper;
 import  org.drools.core.util.StringUtils;
 import  org.jbpm.process.audit.AbstractAuditLogger;
 import  org.jbpm.process.audit.AuditLoggerFactory;
 import  org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
 import  org.jbpm.process.audit.event.AuditEventBuilder;
 import  org.jbpm.process.instance.event.listeners.TriggerRulesEventListener;
 import  org.jbpm.runtime.manager.api.qualifiers.Agenda;
 import  org.jbpm.runtime.manager.api.qualifiers.Process;
 import  org.jbpm.runtime.manager.api.qualifiers.Task;
 import  org.jbpm.runtime.manager.api.qualifiers.WorkingMemory;
 import  org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory;
 import  org.jbpm.runtime.manager.impl.RuntimeEngineImpl;
 import  org.jbpm.runtime.manager.impl.jpa.EntityManagerFactoryManager;
 import  org.kie.api.builder.model.KieSessionModel;
 import  org.kie.api.event.process.ProcessEventListener;
 import  org.kie.api.event.rule.AgendaEventListener;
 import  org.kie.api.event.rule.RuleRuntimeEventListener;
 import  org.kie.api.runtime.KieContainer;
 import  org.kie.api.runtime.manager.RegisterableItemsFactory;
 import  org.kie.api.runtime.manager.RuntimeEngine;
 import  org.kie.api.runtime.manager.RuntimeManager;
 import  org.kie.api.runtime.process.WorkItemHandler;
 import  org.kie.api.task.TaskLifeCycleEventListener;
 import  org.kie.internal.executor.api.ExecutorService;
 import  org.kie.internal.runtime.conf.AuditMode;
 import  org.kie.internal.runtime.conf.DeploymentDescriptor;
 import  org.kie.internal.runtime.manager.EventListenerProducer;
 import  org.kie.internal.runtime.manager.GlobalProducer;
 import  org.kie.internal.runtime.manager.WorkItemHandlerProducer;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;

Implementation of RegisterableItemsFactory dedicated to CDI environments that allows us to get injections of following components:
  • ExternalTaskEventListener - required bean
  • WorkItemHandlerProducer - optional bean (0 or more)
  • EventListenerProducer<ProcessEventListener>> - optional bean (0 or more)
  • EventListenerProducer<AgendaEventListener>> - optional bean (0 or more)
  • EventListenerProducer<WorkingMemoryEventListener>> - optional bean (0 or more)
  • RuntimeFinder - optional required only when single CDI bean is going to manage many RuntimeManager instances
In addition to that, AbstractAuditLogger can be set after the bean has been injected if the default is not sufficient. Although this factory extends DefaultRegisterableItemsFactory, it will not use any of the listeners and handlers that come from the super class. It relies mainly on CDI injections where the only exception from this rule is AbstractAuditLogger
Even though this is a fully qualified bean for injection, it provides helper methods to build its instances using BeanManager in case more independent instances are required.
  • getFactory(BeanManager, AbstractAuditLogger)
  • getFactory(BeanManager, AbstractAuditLogger, KieContainer, String)
 
 public class InjectableRegisterableItemsFactory extends DefaultRegisterableItemsFactory {
 
     private static final String DEFAULT_KIE_SESSION = "defaultKieSession";
     private static final Logger logger = LoggerFactory.getLogger(InjectableRegisterableItemsFactory.class);
     
     // optional injections
     @Inject
    @Any
    private Instance<GlobalProducer> globalProducer
    @Inject
    @Any
    private Instance<WorkItemHandlerProducer> workItemHandlerProducer;  
    @Inject
    @Process
    private Instance<EventListenerProducer<ProcessEventListener>> processListenerProducer;
    @Inject
    @Agenda
    private Instance<EventListenerProducer<AgendaEventListener>> agendaListenerProducer;
    @Inject
    @WorkingMemory
    private Instance<EventListenerProducer<RuleRuntimeEventListener>> wmListenerProducer;
    @Inject
    @Task
    private Instance<EventListenerProducer<TaskLifeCycleEventListener>> taskListenerProducer;
    @Inject
    private Instance<ExecutorService> executorService;
    
    private AbstractAuditLogger auditlogger;
    
    // to handle kmodule approach
    private KieContainer kieContainer;
    private String ksessionName;
    
    @Override
    public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
        Map<String, WorkItemHandler> handler = new HashMap<String, WorkItemHandler>();
        handler.put("Human Task", getHTWorkItemHandler(runtime));
        
        RuntimeManager manager = ((RuntimeEngineImpl)runtime).getManager();
        Map<StringObjectparameters = new HashMap<StringObject>();
        parameters.put("ksession"runtime.getKieSession());
        parameters.put("taskService"runtime.getTaskService());
        parameters.put("runtimeManager"manager);
        parameters.put("kieContainer"getRuntimeManager().getKieContainer());
        try {
            parameters.put("executorService".get());
        } catch (Exception e) {
            .debug("Executor service not available due to {}"e.getMessage());
        }
        
        if ( != null) {
        	// add classloader as one of the parameters so it can be easily referenced
        	parameters.put("classLoader".getClassLoader());
            KieSessionModel ksessionModel = null;
            if(StringUtils.isEmpty()) {
                ksessionModel = ((KieContainerImpl)).getKieProject().getDefaultKieSession();
                if (ksessionModel == null) {
                    ksessionModel = ((KieContainerImpl)).getKieSessionModel();
                }
            } else {            
                ksessionModel = ((KieContainerImpl)).getKieSessionModel();
            }
            
            if (ksessionModel == null) {
                throw new IllegalStateException("Cannot find ksession, either it does not exist or there are multiple default ksession in kmodule.xml");
            }
            try {
                CDIHelper.wireListnersAndWIHs(ksessionModelruntime.getKieSession(), parameters);
            } catch (Throwable e) {
                // use fallback mechanism
                CDIHelper.wireListnersAndWIHs(ksessionModelruntime.getKieSession());
            }
        }
        try {
            for (WorkItemHandlerProducer producer : ) {
                handler.putAll(producer.getWorkItemHandlers(manager.getIdentifier(), parameters));
            }
        } catch (Exception e) {
            // do nothing as work item handler is considered optional
            .warn("Exception while evalutating work item handler prodcuers {}"e.getMessage());
        }
        // add handlers from descriptor
        handler.putAll(getWorkItemHandlersFromDescriptor(runtime));
        return handler;
    }
    
    @Override
    public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
        
        List<ProcessEventListener> defaultListeners = new ArrayList<ProcessEventListener>();
        if( != null) {
            defaultListeners.add();
        } else if (getAuditBuilder() != null) {
        	AbstractAuditLogger aLogger = getAuditLoggerInstance(runtime);
        	if (aLogger != null) {
        		defaultListeners.add(aLogger);
        	}
        }
        try {
            for (EventListenerProducer<ProcessEventListener> producer : ) {
                defaultListeners.addAll(producer.getEventListeners(((RuntimeEngineImpl)runtime).getManager().getIdentifier(), getParametersMap(runtime)));
            }
        } catch (Exception e) {
            .warn("Exception while evaluating ProcessEventListener producers {}"e.getMessage());
        }
        
        // add listeners from descriptor
        defaultListeners.addAll(getEventListenerFromDescriptor(runtime, ProcessEventListener.class)); 
        return defaultListeners;
    }
    
    @Override
    public List<RuleRuntimeEventListener> getRuleRuntimeEventListeners(RuntimeEngine runtime) {
        List<RuleRuntimeEventListener> defaultListeners = new ArrayList<RuleRuntimeEventListener>();
        try {
            for (EventListenerProducer<RuleRuntimeEventListener> producer : ) {
                defaultListeners.addAll(producer.getEventListeners(((RuntimeEngineImpl)runtime).getManager().getIdentifier(), getParametersMap(runtime)));
            }
        } catch (Exception e) {
            .warn("Exception while evaluating WorkingMemoryEventListener producers {}"e.getMessage());
        }
        // add listeners from descriptor
        defaultListeners.addAll(getEventListenerFromDescriptor(runtime, RuleRuntimeEventListener.class));
        return defaultListeners;
    }      
    @Override
    public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
        List<AgendaEventListener> defaultListeners = new ArrayList<AgendaEventListener>();
        defaultListeners.add(new TriggerRulesEventListener(runtime.getKieSession()));
        try {
            for (EventListenerProducer<AgendaEventListener> producer : ) {
                defaultListeners.addAll(producer.getEventListeners(((RuntimeEngineImpl)runtime).getManager().getIdentifier(), getParametersMap(runtime)));
            }
        } catch (Exception e) {
            .warn("Exception while evaluating WorkingMemoryEventListener producers {}"e.getMessage());
        }
        // add listeners from descriptor
        defaultListeners.addAll(getEventListenerFromDescriptor(runtime, AgendaEventListener.class)); 
        return defaultListeners;
    }   
    
    
    @Override
    public List<TaskLifeCycleEventListener> getTaskListeners() {
        List<TaskLifeCycleEventListener> defaultListeners = new ArrayList<TaskLifeCycleEventListener>();
        try {
            for ( EventListenerProducer<TaskLifeCycleEventListener> producer :  ) {
            	defaultListeners.addAll( producer.getEventListeners(nullnull) );
            }
        } catch ( Exception e ) {
            .warn( "Cannot add listeners to task service due to {}"e.getMessage() );
        }
        // add listeners from descriptor
        defaultListeners.addAll(getTaskListenersFromDescriptor());
        return defaultListeners;
    }  
    
    @Override
	public Map<StringObjectgetGlobals(RuntimeEngine runtime) {    	
    	Map<StringObjectglobals = new HashMap<StringObject>();
        
        RuntimeManager manager = ((RuntimeEngineImpl)runtime).getManager();
        Map<StringObjectparameters = new HashMap<StringObject>();
        parameters.put("ksession"runtime.getKieSession());
        parameters.put("taskService"runtime.getTaskService());
        parameters.put("runtimeManager"manager);
        parameters.put("kieContainer"getRuntimeManager().getKieContainer());
        try {
            parameters.put("executorService".get());
        } catch (Exception e) {
            .debug("Executor service not available due to {}"e.getMessage());
        }
        
        try {
            for (GlobalProducer producer : ) {
                globals.putAll(producer.getGlobals(manager.getIdentifier(), parameters));
            }
        } catch (Exception e) {
            // do nothing as work item handler is considered optional
            .warn("Exception while evalutating globals prodcuers {}"e.getMessage());
        }
        
	    // add globals from descriptor
	    globals.putAll(getGlobalsFromDescriptor(runtime));
        
        return globals;
	}


Allows us to create an instance of this class dynamically via BeanManager. This is useful in case multiple independent instances are required on runtime and that need cannot be satisfied with regular CDI practices.

Parameters:
beanManager - bean manager instance of the container
auditlogger - AbstractAuditLogger logger instance to be used, might be null
Returns:
new instance of the factory
    public static RegisterableItemsFactory getFactory(BeanManager beanManager, AbstractAuditLogger auditlogger) {
        InjectableRegisterableItemsFactory instance = getInstanceByType(beanManagerInjectableRegisterableItemsFactory.classnew Annotation[]{});
        instance.setAuditlogger(auditlogger);
        return instance;
    }
    
    
Allows us to create instance of this class dynamically via BeanManager. This is useful in case multiple independent instances are required on runtime and that need cannot be satisfied with regular CDI practices.

Parameters:
beanManager - bean manager instance of the container
auditlogger - AbstractAuditLogger logger instance to be used, might be null
kieContainer - KieContainer that the factory is built for
ksessionName - name of the ksession defined in kmodule to be used, if not given default ksession from kmodule will be used.
Returns:
    public static RegisterableItemsFactory getFactory(BeanManager beanManager, AbstractAuditLogger auditlogger, KieContainer kieContainerString ksessionName) {
        InjectableRegisterableItemsFactory instance = getInstanceByType(beanManagerInjectableRegisterableItemsFactory.classnew Annotation[]{});
        instance.setAuditlogger(auditlogger);
        instance.setKieContainer(kieContainer);
        instance.setKsessionName(ksessionName);
        return instance;
    }
    
Allows to create instance of this class dynamically via BeanManager. This is useful in case multiple independent instances are required on runtime and that need cannot be satisfied with regular CDI practices.

Parameters:
beanManager - bean manager instance of the container
eventBuilder - AuditEventBuilder logger builder instance to be used, might be null
Returns:
new instance of the factory
    public static RegisterableItemsFactory getFactory(BeanManager beanManager, AuditEventBuilder eventBuilder) {
        InjectableRegisterableItemsFactory instance = getInstanceByType(beanManagerInjectableRegisterableItemsFactory.classnew Annotation[]{});
        instance.setAuditBuilder(eventBuilder);
        return instance;
    }
    
    
Allows to create instance of this class dynamically via BeanManager. This is useful in case multiple independent instances are required on runtime and that need cannot be satisfied with regular CDI practices.

Parameters:
beanManager - bean manager instance of the container
eventBuilder - AbstractAuditLogger logger builder instance to be used, might be null
kieContainer - KieContainer that the factory is built for
ksessionName - name of the ksession defined in kmodule to be used, if not given default ksession from kmodule will be used.
Returns:
    public static RegisterableItemsFactory getFactory(BeanManager beanManager, AuditEventBuilder eventBuilder, KieContainer kieContainerString ksessionName) {
        InjectableRegisterableItemsFactory instance = getInstanceByType(beanManagerInjectableRegisterableItemsFactory.classnew Annotation[]{});
        instance.setAuditBuilder(eventBuilder);
        instance.setKieContainer(kieContainer);
        instance.setKsessionName(ksessionName);
        return instance;
    }
    
    
    protected static <T> T getInstanceByType(BeanManager managerClass<T> typeAnnotation... bindings) {
        final Bean<?> bean = manager.resolve(manager.getBeans(typebindings));
        if (bean == null) {
            throw new UnsatisfiedResolutionException("Unable to resolve a bean for " + type + " with bindings " + Arrays.asList(bindings));
        }
        CreationalContext<?> cc = manager.createCreationalContext(null);
        return type.cast(manager.getReference(beantypecc));
    }
    public AbstractAuditLogger getAuditlogger() {
        return ;
    }
    public void setAuditlogger(AbstractAuditLogger auditlogger) {
        this. = auditlogger;
    }
    public KieContainer getKieContainer() {
        return ;
    }
    public void setKieContainer(KieContainer kieContainer) {
        this. = kieContainer;
    }
    public String getKsessionName() {
        return ;
    }
    public void setKsessionName(String ksessionName) {
        this. = ksessionName;
    }

    
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:
  • jbpm.audit.jms.connection.factory.jndi - JNDI name of the connection factory to look up - type String
  • jbpm.audit.jms.queue.jndi - JNDI name of the queue to look up - type String

Returns:
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) {
                .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;
    }
    
New to GrepCode? Check out our FAQ X