提交 664ce00f 编写于 作者: T tombaeyens

ACT-127 start restructuring the modules

上级 6f1ea38f
/* 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.examples.pvm;
import org.activiti.pvm.activity.ActivityBehavior;
import org.activiti.pvm.activity.ActivityExecution;
import org.activiti.pvm.process.PvmTransition;
/**
* @author Tom Baeyens
*/
public class Automatic implements ActivityBehavior {
public void execute(ActivityExecution activityContext) throws Exception {
PvmTransition defaultOutgoingTransition = activityContext.getActivity().getOutgoingTransitions().get(0);
activityContext.take(defaultOutgoingTransition);
}
}
/* 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.examples.pvm;
import org.activiti.pvm.activity.ActivityBehavior;
import org.activiti.pvm.activity.ActivityExecution;
import org.activiti.pvm.process.PvmTransition;
/**
* @author Tom Baeyens
*/
public class Decision implements ActivityBehavior {
public void execute(ActivityExecution execution) throws Exception {
PvmTransition transition = null;
String creditRating = (String) execution.getVariable("creditRating");
if (creditRating.equals("AAA+")) {
transition = execution.getActivity().findOutgoingTransition("wow");
} else if (creditRating.equals("Aaa-")) {
transition = execution.getActivity().findOutgoingTransition("nice");
} else {
transition = execution.getActivity().findOutgoingTransition("default");
}
execution.take(transition);
}
}
/* 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.examples.pvm;
import junit.framework.TestCase;
import org.activiti.pvm.ProcessDefinitionBuilder;
import org.activiti.pvm.process.PvmProcessDefinition;
import org.activiti.pvm.runtime.PvmExecution;
import org.activiti.pvm.runtime.PvmProcessInstance;
/**
* @author Tom Baeyens
*/
public class PvmTest extends TestCase {
public void testPvmWaitState() {
PvmProcessDefinition processDefinition = new ProcessDefinitionBuilder()
.createActivity("a")
.initial()
.behavior(new WaitState())
.transition("b")
.endActivity()
.createActivity("b")
.behavior(new WaitState())
.transition("c")
.endActivity()
.createActivity("c")
.behavior(new WaitState())
.endActivity()
.buildProcessDefinition();
PvmProcessInstance processInstance = processDefinition.createProcessInstance();
processInstance.start();
PvmExecution activityInstance = processInstance.findExecution("a");
assertNotNull(activityInstance);
activityInstance.signal(null, null);
activityInstance = processInstance.findExecution("b");
assertNotNull(activityInstance);
activityInstance.signal(null, null);
activityInstance = processInstance.findExecution("c");
assertNotNull(activityInstance);
}
public void testPvmAutomatic() {
PvmProcessDefinition processDefinition = new ProcessDefinitionBuilder()
.createActivity("a")
.initial()
.behavior(new Automatic())
.transition("b")
.endActivity()
.createActivity("b")
.behavior(new Automatic())
.transition("c")
.endActivity()
.createActivity("c")
.behavior(new WaitState())
.endActivity()
.buildProcessDefinition();
PvmProcessInstance processInstance = processDefinition.createProcessInstance();
processInstance.start();
assertNotNull(processInstance.findExecution("c"));
}
public void testPvmDecision() {
PvmProcessDefinition processDefinition = new ProcessDefinitionBuilder()
.createActivity("start")
.initial()
.behavior(new Automatic())
.transition("checkCredit")
.endActivity()
.createActivity("checkCredit")
.behavior(new Decision())
.transition("askDaughterOut", "wow")
.transition("takeToGolf", "nice")
.transition("ignore", "default")
.endActivity()
.createActivity("takeToGolf")
.behavior(new WaitState())
.endActivity()
.createActivity("askDaughterOut")
.behavior(new WaitState())
.endActivity()
.createActivity("ignore")
.behavior(new WaitState())
.endActivity()
.buildProcessDefinition();
PvmProcessInstance processInstance = processDefinition.createProcessInstance();
processInstance.setVariable("creditRating", "Aaa-");
processInstance.start();
assertNotNull(processInstance.findExecution("takeToGolf"));
processInstance = processDefinition.createProcessInstance();
processInstance.setVariable("creditRating", "AAA+");
processInstance.start();
assertNotNull(processInstance.findExecution("askDaughterOut"));
processInstance = processDefinition.createProcessInstance();
processInstance.setVariable("creditRating", "bb-");
processInstance.start();
assertNotNull(processInstance.findExecution("ignore"));
}
}
/* 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.examples.pvm;
import org.activiti.pvm.activity.ActivityExecution;
import org.activiti.pvm.activity.SignallableActivityBehavior;
import org.activiti.pvm.process.PvmTransition;
/**
* @author Tom Baeyens
*/
public class WaitState implements SignallableActivityBehavior {
public void execute(ActivityExecution execution) {
// By default, the execution will not propagate.
// So if no method like take(Transition) is called on execution
// then the activity will behave as a wait state. The execution is currently
// pointing to the activity. The original call to execution.start()
// or execution.event() will return. Then the execution object will
// remain pointing to the current activity until execution.event(Object) is called.
// That method will delegate to the method below.
}
public void signal(ActivityExecution execution, String signalName, Object event) {
PvmTransition transition = findTransition(execution, signalName);
execution.take(transition);
}
protected PvmTransition findTransition(ActivityExecution execution, String signalName) {
for (PvmTransition transition: execution.getActivity().getOutgoingTransitions()) {
if (signalName==null) {
if (transition.getId()==null) {
return transition;
}
} else {
if (signalName.equals(transition.getId())) {
return transition;
}
}
}
throw new RuntimeException("no transition for signalName '"+signalName+"' in WaitState '"+execution.getActivity().getId()+"'");
}
}
/* 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.spring;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import org.springframework.context.ApplicationContext;
/**
* @author Tom Baeyens
*/
public class ApplicationContextElResolver extends ELResolver {
protected ApplicationContext applicationContext;
public ApplicationContextElResolver(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object getValue(ELContext context, Object base, Object property) {
if (base == null) {
// according to javadoc, can only be a String
String key = (String) property;
if (applicationContext.containsBean(key)) {
context.setPropertyResolved(true);
return applicationContext.getBean(key);
}
}
return null;
}
public boolean isReadOnly(ELContext context, Object base, Object property) {
return true;
}
public void setValue(ELContext context, Object base, Object property, Object value) {
}
public Class< ? > getCommonPropertyType(ELContext context, Object arg) {
return Object.class;
}
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object arg) {
return null;
}
public Class< ? > getType(ELContext context, Object arg1, Object arg2) {
return Object.class;
}
}
/* 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.spring;
import java.io.IOException;
import java.util.zip.ZipInputStream;
import javax.sql.DataSource;
import org.activiti.engine.DbSchemaStrategy;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.activiti.engine.impl.cfg.ProcessEngineConfiguration;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.interceptor.DefaultCommandExecutor;
import org.activiti.engine.impl.jobexecutor.JobExecutor;
import org.activiti.engine.impl.variable.VariableTypes;
import org.activiti.engine.repository.DeploymentBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ContextResource;
import org.springframework.core.io.Resource;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Dave Syer
* @author Christian Stettler
* @author Tom Baeyens
*/
public class ProcessEngineFactoryBean implements FactoryBean<ProcessEngine>, DisposableBean, ApplicationContextAware {
protected ProcessEngineConfiguration processEngineConfiguration = new ProcessEngineConfiguration();
protected PlatformTransactionManager transactionManager;
protected ApplicationContext applicationContext;
protected String deploymentName = "SpringAutoDeployment";
protected Resource[] deploymentResources = new Resource[0];
protected ProcessEngineImpl processEngine;
public void destroy() throws Exception {
if (processEngine != null) {
processEngine.close();
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public ProcessEngine getObject() throws Exception {
initializeSpringTransactionInterceptor();
initializeExpressionManager();
processEngine = (ProcessEngineImpl) processEngineConfiguration.buildProcessEngine();
if (deploymentResources.length > 0) {
autoDeployResources();
}
return processEngine;
}
private void initializeSpringTransactionInterceptor() {
processEngineConfiguration.setLocalTransactions(transactionManager == null);
if (transactionManager != null) {
DefaultCommandExecutor commandExecutor = (DefaultCommandExecutor) processEngineConfiguration.getCommandExecutor();
commandExecutor.addCommandInterceptor(new SpringTransactionInterceptor(transactionManager));
processEngineConfiguration.setCommandExecutor(commandExecutor);
}
}
protected void initializeExpressionManager() {
processEngineConfiguration.setExpressionManager(new SpringExpressionManager(applicationContext));
}
public Class< ? > getObjectType() {
return ProcessEngine.class;
}
public boolean isSingleton() {
return true;
}
protected void autoDeployResources() throws IOException {
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilder deploymentBuilder = repositoryService
.createDeployment()
.enableDuplicateFiltering()
.name(deploymentName);
for (Resource resource : deploymentResources) {
String resourceName = null;
if (resource instanceof ContextResource) {
resourceName = ((ContextResource) resource).getPathWithinContext();
} else if (resource instanceof ByteArrayResource) {
resourceName = resource.getDescription();
} else {
try {
resourceName = resource.getFile().getAbsolutePath();
} catch (IOException e) {
resourceName = resource.getFilename();
}
}
if ( resourceName.endsWith(".bar")
|| resourceName.endsWith(".zip")
|| resourceName.endsWith(".jar") ) {
deploymentBuilder.addZipInputStream(new ZipInputStream(resource.getInputStream()));
} else {
deploymentBuilder.addInputStream(resourceName, resource.getInputStream());
}
}
deploymentBuilder.deploy();
}
// getters and setters //////////////////////////////////////////////////////
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void setDeploymentName(String deploymentName) {
this.deploymentName = deploymentName;
}
public void setDeploymentResources(Resource[] deploymentResources) {
this.deploymentResources = deploymentResources;
}
public void setCommandExecutor(CommandExecutor commandExecutor) {
processEngineConfiguration.setCommandExecutor(commandExecutor);
}
public void setDataBaseName(String dataBaseName) {
processEngineConfiguration.setDatabaseName(dataBaseName);
}
public void setDataSource(DataSource dataSource) {
processEngineConfiguration.setDataSource(dataSource);
}
public void setDbSchemaStrategy(DbSchemaStrategy dbSchemaStrategy) {
processEngineConfiguration.setDbSchemaStrategy(dbSchemaStrategy);
}
public void setHistoricDataService(HistoryService historicDataService) {
processEngineConfiguration.setHistoricDataService(historicDataService);
}
public void setIdentityService(IdentityService identityService) {
processEngineConfiguration.setIdentityService(identityService);
}
public void setIdGenerator(IdGenerator idGenerator) {
processEngineConfiguration.setIdGenerator(idGenerator);
}
public void setJobExecutor(JobExecutor jobExecutor) {
processEngineConfiguration.setJobExecutor(jobExecutor);
}
public void setJobExecutorAutoActivate(boolean jobExecutorAutoActivate) {
processEngineConfiguration.setJobExecutorAutoActivate(jobExecutorAutoActivate);
}
public void setProcessEngineName(String processEngineName) {
processEngineConfiguration.setProcessEngineName(processEngineName);
}
public void setVariableTypes(VariableTypes variableTypes) {
processEngineConfiguration.setVariableTypes(variableTypes);
}
}
/* 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.spring;
import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
import javax.el.ELResolver;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import org.activiti.engine.impl.el.ExecutionVariableElResolver;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.pvm.impl.runtime.ExecutionImpl;
import org.springframework.context.ApplicationContext;
/**
* @author Tom Baeyens
*/
public class SpringExpressionManager extends ExpressionManager {
protected ApplicationContext applicationContext;
public SpringExpressionManager(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
protected ELResolver createElResolver(ExecutionImpl execution) {
CompositeELResolver compositeElResolver = new CompositeELResolver();
compositeElResolver.add(new ExecutionVariableElResolver(execution));
compositeElResolver.add(new ApplicationContextElResolver(applicationContext));
compositeElResolver.add(new ArrayELResolver());
compositeElResolver.add(new ListELResolver());
compositeElResolver.add(new MapELResolver());
compositeElResolver.add(new BeanELResolver());
return compositeElResolver;
}
}
/* 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.spring;
import java.io.InputStream;
import java.util.Map;
import java.util.logging.Logger;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.test.ProcessEngineTestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Tom Baeyens
*/
public class SpringProcessEngineTestCase extends ProcessEngineTestCase {
private static Logger log = Logger.getLogger(SpringProcessEngineTestCase.class.getName());
protected String springApplicationContextConfigurationResource;
protected ApplicationContext applicationContext;
public SpringProcessEngineTestCase() {
if (getResourceAsStream("applicationContext.xml")!=null) {
this.springApplicationContextConfigurationResource = "applicationContext.xml";
this.configurationResource = null;
}
}
public SpringProcessEngineTestCase(String springApplicationContextConfigurationResource) {
this.springApplicationContextConfigurationResource = springApplicationContextConfigurationResource;
this.configurationResource = null;
}
InputStream getResourceAsStream(String resourceName) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
}
protected void initializeProcessEngine() {
if (configurationResource!=null) {
super.initializeProcessEngine();
} else if (springApplicationContextConfigurationResource!=null) {
log.fine("==== BUILDING SPRING APPLICATION CONTEXT AND PROCESS ENGINE =========================================");
this.applicationContext = new ClassPathXmlApplicationContext(springApplicationContextConfigurationResource);
Map<String, ProcessEngine> beansOfType = applicationContext.getBeansOfType(ProcessEngine.class);
if ( (beansOfType==null)
|| (beansOfType.isEmpty())
) {
throw new ActivitiException("no "+ProcessEngine.class.getName()+" defined in the application context "+springApplicationContextConfigurationResource);
}
processEngine = beansOfType.values().iterator().next();
log.fine("==== SPRING PROCESS ENGINE CREATED ==================================================================");
}
}
}
/* 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.spring;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.interceptor.CommandInterceptor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
/**
* @author Dave Syer
* @author Tom Baeyens
*/
public class SpringTransactionInterceptor implements CommandInterceptor {
protected PlatformTransactionManager transactionManager;
public SpringTransactionInterceptor(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public <T> T invoke(final CommandExecutor next, final Command<T> command) {
@SuppressWarnings("unchecked")
T result = (T) new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return next.execute(command);
}
});
return result;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册