Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 JBoss by Red Hat.
   *
   * 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.producer;
 
 
 import  javax.enterprise.inject.AmbiguousResolutionException;
 import  javax.enterprise.inject.Any;
 import  javax.enterprise.inject.Instance;
 import  javax.enterprise.inject.Produces;
 import  javax.inject.Inject;
 import  javax.persistence.EntityManagerFactory;
 import  javax.persistence.PersistenceUnit;
 
 import  org.jbpm.services.task.HumanTaskConfigurator;
 import  org.jbpm.services.task.HumanTaskServiceFactory;
 import  org.jbpm.services.task.impl.command.CommandBasedTaskService;
 import  org.kie.api.task.TaskLifeCycleEventListener;
 import  org.kie.api.task.UserGroupCallback;
 import  org.kie.internal.task.api.InternalTaskService;
 import  org.kie.internal.task.api.UserInfo;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;

CDI producer for TaskService instances. By default it runs in new mode, meaning new TaskService instance for every injection point. This behavior can be altered by setting org.jbpm.cdi.taskservice.mode system property to one of the values.
  • none - disables producer to not return TaskService instances
  • singleton - produces only one instance of TaskService that will be shared
  • new - produces new instance for every injection point
This bean accept following injections:
  • UserGroupCallback
  • UserInfo
  • TaskLifeCycleEventListener
all of these are optional injections and if not available defaults will be used. Underneath it uses HumanTaskConfigurator for TaskService instances creations.

See also:
HumanTaskConfigurator
 
 public class HumanTaskServiceProducer {
 
     private static final Logger logger = LoggerFactory.getLogger( HumanTaskServiceProducer.class );
     final String mode = System.getProperty"org.jbpm.cdi.taskservice.mode""new" );
 
     @Inject
     private Instance<UserGroupCallback> userGroupCallback;
 
     @Inject
     private Instance<UserInfo> userInfo;
 
     @Inject
     @Any
     private Instance<TaskLifeCycleEventListener> taskListeners;
 
     @Inject
     @PersistenceUnit(unitName = "org.jbpm.domain")
     private EntityManagerFactory emf;
 
     // internal member to ensure only single instance of task service is produced
     private InternalTaskService taskService;
 
     @Produces
     public CommandBasedTaskService produceTaskService() {
         if ( .equalsIgnoreCase"none" ) ) {
             return null;
         }
         if (  == null ) {
             HumanTaskConfigurator configurator = HumanTaskServiceFactory.newTaskServiceConfigurator()
                     .entityManagerFactory(  )
                     .userGroupCallback( safeGet ) )
                     .userInfo( safeGet ) );
 
             try {
                 for ( TaskLifeCycleEventListener listener :  ) {
                     configurator.listener( listener );
                     .debug( "Registering listener {}"listener );
                 }
             } catch ( Exception e ) {
                 .debug( "Cannot add listeners to task service due to {}"e.getMessage() );
             }
            if ( .equalsIgnoreCase"singleton" ) ) {
                this. = (CommandBasedTaskService) configurator.getTaskService();
            } else {
                return (CommandBasedTaskService) configurator.getTaskService();
            }
        }
        return (CommandBasedTaskService) ;
    }
    protected <T> T safeGet( Instance<T> instance ) {
        try {
            T object = instance.get();
            .debug( "About to set object {} on task service"object );
            return object;
        } catch ( AmbiguousResolutionException e ) {
            // special handling in case cdi discovered multiple injections
            // that are actually same instances - e.g. weld on tomcat
            HashSet<T> available = new HashSet<T>();
            for ( T object : instance ) {
                available.addobject );
            }
            if ( available.size() == 1 ) {
                return available.iterator().next();
            } else {
                throw e;
            }
        } catch ( Throwable e ) {
            .debug( "Cannot get value of of instance {} due to {}"instancee.getMessage() );
        }
        return null;
    }
New to GrepCode? Check out our FAQ X