Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * 
   *      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.task.persistence;
 
 
 import  org.drools.core.command.CommandService;
 import  org.drools.core.command.Interceptor;
 import  org.drools.core.command.impl.AbstractInterceptor;
 import  org.drools.persistence.OrderedTransactionSynchronization;
 import  org.drools.persistence.TransactionManager;
 import  org.drools.persistence.TransactionManagerHelper;
 import  org.drools.persistence.jta.JtaTransactionManager;
 import  org.kie.api.command.Command;
 import  org.kie.api.runtime.Environment;
 import  org.kie.api.runtime.EnvironmentName;
 import  org.kie.api.task.UserGroupCallback;
 import  org.kie.api.task.model.Task;
 import  org.kie.internal.command.Context;
 import  org.kie.internal.command.World;
 import  org.kie.internal.task.api.TaskContext;
 import  org.kie.internal.task.api.TaskPersistenceContext;
 import  org.kie.internal.task.api.TaskPersistenceContextManager;
 import  org.kie.internal.task.api.model.InternalPeopleAssignments;
 import  org.kie.internal.task.api.model.InternalTask;
 import  org.kie.internal.task.exception.TaskException;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;
 
 public class TaskTransactionInterceptor extends AbstractInterceptor {
 
 	private static Logger logger = LoggerFactory.getLogger(TaskTransactionInterceptor.class);
     private static String SPRING_TM_CLASSNAME = "org.springframework.transaction.support.AbstractPlatformTransactionManager";
 	
 	private CommandService             commandService;
     private TransactionManager         txm;
     private TaskPersistenceContextManager  tpm;
     private boolean eagerDisabled = false;
     
     public TaskTransactionInterceptor(Environment environment) {
     	this. = Boolean.getBoolean("jbpm.ht.eager.disabled");
     	initTransactionManager(environment);
     }
 	
 	public synchronized <T> T execute(Command<T> command) {
 		boolean transactionOwner = false;
 		T result = null;
 		
         try {
             transactionOwner = .begin();
             .beginCommandScopedEntityManager();
             TransactionManagerHelper.registerTransactionSyncInContainer(this.new TaskSynchronizationImplthis ));
                 
             result = executeNext((Command<T>) command);
             postInit(result);
             .commit( transactionOwner );
 
             return result;
 
         } catch (TaskException e) {
         	// allow to handle TaskException as business exceptions on caller side
         	// if transaction is owned by other component like process engine
         	if (transactionOwner) {
         		rollbackTransactionetransactionOwner );
         		e.setRecoverable(false);
         		throw e;
         	} else {
         		throw e;
         	}
         }
         catch ( RuntimeException re ) {
             rollbackTransactionretransactionOwner );
             throw re;
         } catch ( Exception t1 ) {
             rollbackTransactiont1,  transactionOwner );
             throw new RuntimeException"Wrapped exception see cause"t1 );
         }
 		
 	}
 	
 	private void rollbackTransaction(Exception t1boolean transactionOwner) {
 		try {
 			.warn("Could not commit session"t1);
 			.rollback(transactionOwner);
catch (Exception t2) {
			.error("Could not rollback"t2);
			throw new RuntimeException("Could not commit session or rollback"t2);
		}
	}
	public void addInterceptor(Interceptor interceptor) {
        interceptor.setNext( this. == null ? this : this. );
        this. = interceptor;
    }
	public Context getContext() {
		final TaskPersistenceContext persistenceContext = .getPersistenceContext(); 
		persistenceContext.joinTransaction();
        return new TaskContext() {
			public void set(String identifierObject value) {	
				.putResource(identifiervalue);
			}
			public void remove(String identifier) {
			}
			public String getName() {
				return null;
			}
			public World getContextManager() {
				return null;
			}
			public Object get(String identifier) {
				return .getResource(identifier);
			}
			public void setPersistenceContext(TaskPersistenceContext context) {
			}
			public TaskPersistenceContext getPersistenceContext() {
				return persistenceContext;
			}
			public UserGroupCallback getUserGroupCallback() {
				return null;
			}
		};
	}
	public void initTransactionManager(Environment env) {
        Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );
        if ( env.get( EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER ) != null &&
             env.get( EnvironmentName.TRANSACTION_MANAGER ) != null ) {
            this. = (TransactionManager) tm;
            this. = (TaskPersistenceContextManager) env.get( EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER );
        } else {
            if ( tm != null && isSpringTransactionManager(tm.getClass()) ) {
                try {
                    .debug( "Instantiating KieSpringTransactionManager" );
                    Class< ? > cls = Class.forName"org.kie.spring.persistence.KieSpringTransactionManager" );
                    Constructor< ? > con = cls.getConstructors()[0];
                    this. = (TransactionManager) con.newInstancetm );
                    env.set( EnvironmentName.TRANSACTION_MANAGER, this. );
                    cls = Class.forName"org.kie.spring.persistence.KieSpringTaskJpaManager" );
                    con = cls.getConstructors()[0];
                    this. = (TaskPersistenceContextManager) con.newInstancenew Object[]{env} );
                } catch ( Exception e ) {
    
                    .warn( "Could not instantiate DroolsSpringTransactionManager" );
                    throw new RuntimeException"Could not instantiate org.kie.container.spring.beans.persistence.DroolsSpringTransactionManager"e );
                }
            } else {
                .debug( "Instantiating JtaTransactionManager" );
                this. = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ),
                                                      env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),
                                                      tm );
                env.set( EnvironmentName.TRANSACTION_MANAGER, this. );
                try {
                     this. = new JPATaskPersistenceContextManagerenv );
                } catch ( Exception e ) {
                    throw new RuntimeException"Error creating JPATaskPersistenceContextManager"e );
                }
            }
            env.set( EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER,
                     this. );
            env.set( EnvironmentName.TRANSACTION_MANAGER,
                     this. );
        }
    }
    public boolean isSpringTransactionManagerClass<?> clazz ) {
        if ( .equals(clazz.getName()) ) {
            return true;
        }
        // Try to find from the ancestors
        if (clazz.getSuperclass() != null)
        {
            return isSpringTransactionManager(clazz.getSuperclass());
        }
        return false;
    }
    
    private void postInit(Object result) {
    	if (result instanceof Task) {
    		Task task = (Task) result;
    		if (task != null && !) {
    			task.getNames().size();
    			task.getDescriptions().size();
    			task.getSubjects().size();
    			task.getPeopleAssignments().getBusinessAdministrators().size();
    			task.getPeopleAssignments().getPotentialOwners().size();
    			((InternalPeopleAssignments) task.getPeopleAssignments()).getRecipients().size();
    			((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners().size();
    			((InternalPeopleAssignments) task.getPeopleAssignments()).getTaskStakeholders().size();
    			task.getTaskData().getAttachments().size();
    			task.getTaskData().getComments().size();
    			((InternalTask)task).getDeadlines().getStartDeadlines().size();
    			((InternalTask)task).getDeadlines().getEndDeadlines().size();
    		}
    	} else if (result instanceof Collection<?>) {
            ((Collection<?>) result).size();
    	}	
    }
    
	private static class TaskSynchronizationImpl extends
			OrderedTransactionSynchronization {
			super(1, "TaskService-"+service.toString());
			this. = service;
		}
		public void afterCompletion(int status) {
			this...endCommandScopedEntityManager();
		}
		public void beforeCompletion() {
			// not used
		}
	}
New to GrepCode? Check out our FAQ X