/* 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.activiti.engine.impl.interceptor;
import java.util.logging.Logger;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
/**
* @author Tom Baeyens
*/
public class CommandContextInterceptor extends CommandInterceptor {
private final Logger log = Logger.getLogger(CommandContextInterceptor.class.getName());
protected CommandContextFactory commandContextFactory;
protected ProcessEngineConfigurationImpl processEngineConfiguration;
protected boolean isContextReusePossible; // For backwards compatibility, the default is 'false'
public CommandContextInterceptor() {
}
public CommandContextInterceptor(CommandContextFactory commandContextFactory, ProcessEngineConfigurationImpl processEngineConfiguration) {
this.commandContextFactory = commandContextFactory;
this.processEngineConfiguration = processEngineConfiguration;
}
public <T> T execute(Command<T> command) {
CommandContext context = Context.getCommandContext();
boolean contextReused = false;
// We need to check the exception, because the transaction can be in a rollback state,
// and some other command is being fired to compensate (eg. decrementing job retries)
if (!isContextReusePossible || context == null || context.getException() != null) {
context = commandContextFactory.createCommandContext(command);
}
else {
log.fine("Valid context found. Reusing it for the current comment '" + command.getClass().getCanonicalName() + "'");
contextReused = true;
}
try {
// Push on stack
Context.setCommandContext(context);
Context.setProcessEngineConfiguration(processEngineConfiguration);
return next.execute(command);
} catch (Exception e) {
context.exception(e);
} finally {
try {
if (!contextReused) {
context.close();
}
} finally {
// Pop from stack
Context.removeCommandContext();
Context.removeProcessEngineConfiguration();
}
}
return null;
}
// public <T> T execute(Command<T> command) {
// CommandContext context = commandContextFactory.createCommandContext(command);
//
// try {
// Context.setCommandContext(context);
// Context.setProcessEngineConfiguration(processEngineConfiguration);
// return next.execute(command);
//
// } catch (Exception e) {
// context.exception(e);
//
// } finally {
// try {
// context.close();
// } finally {
// Context.removeCommandContext();
// Context.removeProcessEngineConfiguration();
// }
// }
//
// return null;
// }
public CommandContextFactory getCommandContextFactory() {
return commandContextFactory;
}
public void setCommandContextFactory(CommandContextFactory commandContextFactory) {
this.commandContextFactory = commandContextFactory;
}
public ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
return processEngineConfiguration;
}
public void setProcessEngineContext(ProcessEngineConfigurationImpl processEngineContext) {
this.processEngineConfiguration = processEngineContext;
}
public boolean isContextReusePossible() {
return isContextReusePossible;
}
public void setContextReusePossible(boolean isContextReusePossible) {
this.isContextReusePossible = isContextReusePossible;
}
}