From d1acea9b24af3bf04cfb46c90c64a43ffebd3129 Mon Sep 17 00:00:00 2001 From: David Caruana Date: Thu, 13 Jul 2006 17:58:11 +0000 Subject: [PATCH] Add support for writing jBPM plug-in actions which have access to Spring context. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3321 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- config/alfresco/workflow-context.xml | 10 +-- .../jbpm/JBPMSpringActionHandler.java | 53 ++++++++++++++++ .../repo/workflow/jbpm/JBPMSpringTest.java | 19 ++++-- .../jbpm/JBPMTestSpringActionHandler.java | 63 +++++++++++++++++++ 4 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringActionHandler.java create mode 100644 source/java/org/alfresco/repo/workflow/jbpm/JBPMTestSpringActionHandler.java diff --git a/config/alfresco/workflow-context.xml b/config/alfresco/workflow-context.xml index 44b2a55ebf..98abbb5311 100644 --- a/config/alfresco/workflow-context.xml +++ b/config/alfresco/workflow-context.xml @@ -4,14 +4,16 @@ - + - - + + - + + + diff --git a/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringActionHandler.java b/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringActionHandler.java new file mode 100644 index 0000000000..fd7d1449dd --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringActionHandler.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2005 Alfresco, Inc. + * + * Licensed under the Mozilla Public License version 1.1 + * with a permitted attribution clause. You may obtain a + * copy of the License at + * + * http://www.alfresco.org/legal/license.txt + * + * 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.alfresco.repo.workflow.jbpm; + +import org.jbpm.graph.def.ActionHandler; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.access.BeanFactoryLocator; +import org.springframework.beans.factory.access.BeanFactoryReference; +import org.springmodules.workflow.jbpm31.JbpmFactoryLocator; + + +/** + * Abstract base implementation of a Jbpm Action Hander with access to + * Alfresco Spring beans. + * + * @author davidc + */ +public abstract class JBPMSpringActionHandler implements ActionHandler +{ + + /** + * Construct + */ + protected JBPMSpringActionHandler() + { + // The following implementation is derived from Spring Modules v0.4 + BeanFactoryLocator factoryLocator = new JbpmFactoryLocator(); + BeanFactoryReference factory = factoryLocator.useBeanFactory(null); + initialiseHandler(factory.getFactory()); + } + + /** + * Initialise Action Handler + * + * @param factory Spring bean factory for accessing Alfresco beans + */ + protected abstract void initialiseHandler(BeanFactory factory); + +} diff --git a/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringTest.java b/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringTest.java index e49b18b9ad..15e3832ff2 100644 --- a/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringTest.java +++ b/source/java/org/alfresco/repo/workflow/jbpm/JBPMSpringTest.java @@ -18,6 +18,7 @@ package org.alfresco.repo.workflow.jbpm; import java.util.List; +import org.alfresco.service.descriptor.DescriptorService; import org.alfresco.util.BaseSpringTest; import org.jbpm.JbpmContext; import org.jbpm.db.GraphSession; @@ -35,13 +36,15 @@ import org.springmodules.workflow.jbpm31.JbpmTemplate; */ public class JBPMSpringTest extends BaseSpringTest { - private JbpmTemplate jbpmTemplate; + JbpmTemplate jbpmTemplate; + DescriptorService descriptorService; @Override protected void onSetUpInTransaction() throws Exception { - jbpmTemplate = (JbpmTemplate)applicationContext.getBean("jbpmTemplate"); + jbpmTemplate = (JbpmTemplate)applicationContext.getBean("jbpm.template"); + descriptorService = (DescriptorService)applicationContext.getBean("DescriptorService"); } @@ -100,13 +103,17 @@ public class JBPMSpringTest extends BaseSpringTest " " + " " + " " + - " " + + " " + + " " + + " a test value" + + " " + " " + - " " + + " " + " " + "" ); + jbpmTemplate.execute(new JbpmCallback() { public Object doInJbpm(JbpmContext context) @@ -137,6 +144,10 @@ public class JBPMSpringTest extends BaseSpringTest token.signal(); // Now the process is in the state 's'. assertEquals("s", token.getNode().getName()); + // Spring based action has been called, check the result by looking at the + // process variable set by the action + String result = "Repo: " + descriptorService.getServerDescriptor().getVersion() + ", Value: a test value, Node: s, Token: /"; + assertEquals(result, processInstance.getContextInstance().getVariable("jbpm.test.action.result")); context.save(processInstance); return null; diff --git a/source/java/org/alfresco/repo/workflow/jbpm/JBPMTestSpringActionHandler.java b/source/java/org/alfresco/repo/workflow/jbpm/JBPMTestSpringActionHandler.java new file mode 100644 index 0000000000..63de24123a --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jbpm/JBPMTestSpringActionHandler.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2005 Alfresco, Inc. + * + * Licensed under the Mozilla Public License version 1.1 + * with a permitted attribution clause. You may obtain a + * copy of the License at + * + * http://www.alfresco.org/legal/license.txt + * + * 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.alfresco.repo.workflow.jbpm; + +import org.alfresco.service.descriptor.DescriptorService; +import org.jbpm.graph.exe.ExecutionContext; +import org.springframework.beans.factory.BeanFactory; + + +/** + * Test Spring based Jbpm Action Handler + * + * @author davidc + */ +public class JBPMTestSpringActionHandler extends JBPMSpringActionHandler +{ + private static final long serialVersionUID = -7659883022289711381L; + + private DescriptorService descriptorService; + private String value; + + + /** + * Setter accessible from jBPM jPDL + * @param value + */ + public void setValue(String value) + { + this.value = value; + } + + /* + * (non-Javadoc) + * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext) + */ + public void execute(ExecutionContext arg0) throws Exception + { + String result = "Repo: " + descriptorService.getServerDescriptor().getVersion(); + result += ", Value: " + value + ", Node: " + arg0.getNode().getName() + ", Token: " + arg0.getToken().getFullName(); + arg0.getContextInstance().setVariable("jbpm.test.action.result", result); + } + + @Override + protected void initialiseHandler(BeanFactory factory) + { + descriptorService = (DescriptorService)factory.getBean("DescriptorService", DescriptorService.class); + } + +}