ProcessEngineConfiguration requiring Spring by default?

8 posts / 0 new
Last post
egore911
Offline
Last seen: 2 years 9 months ago
Joined: 09/23/2010 - 07:14
Posts: 9
ProcessEngineConfiguration requiring Spring by default?

From what I understood the ProcessEngineConfiguration seems to require Spring for its configuration starting with Activiti 5.0. Is that really necessary to wire a Spring that deep into the engine? Or are there other possibilities to run Activiti without Spring?

Thanks in advance,
Chris

frederikheremans
frederikheremans's picture
Offline
Last seen: 2 months 4 days ago
Joined: 08/26/2010 - 06:59
Posts: 3087

The engine is indeed using 'spring-beans' dependency for it's configuration. This way we don't have to keep 3 ways of configuration in sync (spring-context, activiti.cfg.xml and programmatic with old ProcessEngineBuilder).

On top of that, this change makes future pluggability extensions of the engine much easier and cleaner. We don't want to write our own DI-container.

To answer your last question: since spring-beans is used in ProcessEngineConfiguration, there is no way of not using spring. But not because spring is used for OUR configuration, that you should use spring to wire your application or get hold of the ProcessEngine, it's still embeddable in non-spring envs.

Activiti Core Developer

egore911
Offline
Last seen: 2 years 9 months ago
Joined: 09/23/2010 - 07:14
Posts: 9

I already use Seam to create my processEngineConfiguration which works quite fine. The issue is that I still have to put spring-beans (and therefore spring-core) into my classpath.

I think it would work if "parseProcessEngineConfiguration" (plus friends) would be moved from ProcessEngineConfiguration to
a subclass named "SpringProcessEngineConfiguration" (or similar). This way I could extend the ProcessEngineConfiguration with my own Implementation without depending on Spring.

As far as I can tell the ProcessEngineConfiguration is the only place where Spring is used until now. I would have attached a patch that exactly does what I stated above, but no file-extension I tested was allowed (.patch, .txt, .nfo).

egore911
Offline
Last seen: 2 years 9 months ago
Joined: 09/23/2010 - 07:14
Posts: 9

From 045cbdbdb59235f055a3c9854cd57dc186be905a Mon Sep 17 00:00:00 2001
From: Christoph Brill <egore911@egore911.de>
Date: Wed, 1 Dec 2010 12:27:57 +0100
Subject: [PATCH] Split Spring-dependent code from the ProcessEngineConfiguration
 
This way it is possible to create a subclass of ProcessEngineConfiguration that
does not depend on Spring.
---
 .../engine/ProcessEngineConfiguration.java         |   36 ------------
 .../java/org/activiti/engine/ProcessEngines.java   |    2 +-
 .../engine/SpringProcessEngineConfiguration.java   |   59 ++++++++++++++++++++
 .../impl/test/DefaultProcessEngineInitializer.java |    4 +-
 .../engine/impl/test/ResourceActivitiTestCase.java |    4 +-
 .../org/activiti/engine/impl/test/TestHelper.java  |    4 +-
 .../engine/test/db/ConnectionPoolTest.java         |    4 +-
 .../examples/variables/jpa/JPAVariableTest.java    |    4 +-
 .../initialization/NoDbConnectionTest.java         |    4 +-
 .../ProcessEngineInitializationTest.java           |    7 +-
 10 files changed, 76 insertions(+), 52 deletions(-)
 create mode 100644 modules/activiti-engine/src/main/java/org/activiti/engine/SpringProcessEngineConfiguration.java
 
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngineConfiguration.java b/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngineConfiguration.java
index 53dbca6..4f573a6 100644
--- a/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngineConfiguration.java
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngineConfiguration.java
@@ -19,12 +19,6 @@ import javax.sql.DataSource;
 
 import org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
 import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
-import org.springframework.beans.factory.support.DefaultListableBeanFactory;
-import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.InputStreamResource;
-import org.springframework.core.io.Resource;
-
 
 /** Configuration information from which a process engine can be build.
  * 
@@ -143,36 +137,6 @@ public abstract class ProcessEngineConfiguration {
 
   public abstract ProcessEngine buildProcessEngine();
 
-  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResourceDefault() {
-    return createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration");
-  }
-
-  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource) {
-    return createProcessEngineConfigurationFromResource(resource, "processEngineConfiguration");
-  }
-
-  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource, String beanName) {
-    Resource springResource = new ClassPathResource(resource);
-    return parseProcessEngineConfiguration(springResource, beanName);
-  }
-  
-  public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream) {
-    return createProcessEngineConfigurationFromInputStream(inputStream, "processEngineConfiguration");
-  }
-
-  public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
-    Resource springResource = new InputStreamResource(inputStream);
-    return parseProcessEngineConfiguration(springResource, beanName);
-  }
-
-  protected static ProcessEngineConfiguration parseProcessEngineConfiguration(Resource springResource, String beanName) {
-    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
-    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
-    xmlBeanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
-    xmlBeanDefinitionReader.loadBeanDefinitions(springResource);
-    return (ProcessEngineConfiguration) beanFactory.getBean(beanName);
-  }
-  
   public static ProcessEngineConfiguration createStandaloneProcessEngineConfiguration() {
     return new StandaloneProcessEngineConfiguration();
   }
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngines.java b/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngines.java
index 7f7281f..1d2561b 100644
--- a/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngines.java
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/ProcessEngines.java
@@ -147,7 +147,7 @@ public abstract class ProcessEngines {
     InputStream inputStream = null;
     try {
       inputStream = resource.openStream();
-      ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
+      ProcessEngineConfiguration processEngineConfiguration = SpringProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
       return processEngineConfiguration.buildProcessEngine();
 
     } catch (IOException e) {
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/SpringProcessEngineConfiguration.java b/modules/activiti-engine/src/main/java/org/activiti/engine/SpringProcessEngineConfiguration.java
new file mode 100644
index 0000000..e911c8b
--- /dev/null
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/SpringProcessEngineConfiguration.java
@@ -0,0 +1,59 @@
+/* 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
+ * 
+ *      <a href="http://www.apache.org/licenses/LICENSE-2.0
+" rel="nofollow">http://www.apache.org/licenses/LICENSE-2.0
+</a> * 
+ * 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;
+
+import java.io.InputStream;
+
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.core.io.Resource;
+
+/**
+ * @author Christoph Brill
+ */
+public abstract class SpringProcessEngineConfiguration extends ProcessEngineConfiguration {
+
+  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResourceDefault() {
+    return createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration");
+  }
+
+  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource) {
+    return createProcessEngineConfigurationFromResource(resource, "processEngineConfiguration");
+  }
+
+  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource, String beanName) {
+    Resource springResource = new ClassPathResource(resource);
+    return parseProcessEngineConfiguration(springResource, beanName);
+  }
+  
+  public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream) {
+    return createProcessEngineConfigurationFromInputStream(inputStream, "processEngineConfiguration");
+  }
+
+  public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
+    Resource springResource = new InputStreamResource(inputStream);
+    return parseProcessEngineConfiguration(springResource, beanName);
+  }
+
+  protected static ProcessEngineConfiguration parseProcessEngineConfiguration(Resource springResource, String beanName) {
+    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
+    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
+    xmlBeanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
+    xmlBeanDefinitionReader.loadBeanDefinitions(springResource);
+    return (ProcessEngineConfiguration) beanFactory.getBean(beanName);
+  }
+  
+}
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/DefaultProcessEngineInitializer.java b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/DefaultProcessEngineInitializer.java
index fd7e26a..2687ae4 100644
--- a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/DefaultProcessEngineInitializer.java
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/DefaultProcessEngineInitializer.java
@@ -16,7 +16,7 @@ package org.activiti.engine.impl.test;
 import java.util.logging.Logger;
 
 import org.activiti.engine.ProcessEngine;
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 
 
 /**
@@ -28,7 +28,7 @@ public class DefaultProcessEngineInitializer implements ProcessEngineInitializer
 
   public ProcessEngine getProcessEngine() {
     log.fine("==== BUILDING PROCESS ENGINE ========================================================================");
-    ProcessEngine processEngine = ProcessEngineConfiguration
+    ProcessEngine processEngine = SpringProcessEngineConfiguration
       .createProcessEngineConfigurationFromResourceDefault()
       .buildProcessEngine();
     log.fine("==== PROCESS ENGINE CREATED =========================================================================");
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/ResourceActivitiTestCase.java b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/ResourceActivitiTestCase.java
index 8a6fe30..00be2ce 100644
--- a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/ResourceActivitiTestCase.java
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/ResourceActivitiTestCase.java
@@ -17,7 +17,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.activiti.engine.ProcessEngine;
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 
 
 /**
@@ -37,7 +37,7 @@ public class ResourceActivitiTestCase extends AbstractActivitiTestCase {
   protected void initializeProcessEngine() {
     processEngine = cachedProcessEngines.get(activitiConfigurationResource);
     if (processEngine==null) {
-      processEngine = ProcessEngineConfiguration
+      processEngine = SpringProcessEngineConfiguration
         .createProcessEngineConfigurationFromResource(activitiConfigurationResource)
         .buildProcessEngine();
       cachedProcessEngines.put(activitiConfigurationResource, processEngine);
diff --git a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/TestHelper.java b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/TestHelper.java
index f692a84..59a1955 100644
--- a/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/TestHelper.java
+++ b/modules/activiti-engine/src/main/java/org/activiti/engine/impl/test/TestHelper.java
@@ -26,7 +26,7 @@ import junit.framework.AssertionFailedError;
 
 import org.activiti.engine.ActivitiException;
 import org.activiti.engine.ProcessEngine;
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 import org.activiti.engine.impl.ProcessEngineImpl;
 import org.activiti.engine.impl.bpmn.deployer.BpmnDeployer;
 import org.activiti.engine.impl.db.DbSqlSessionFactory;
@@ -197,7 +197,7 @@ public abstract class TestHelper {
     ProcessEngine processEngine = processEngines.get(configurationResource);
     if (processEngine==null) {
       log.fine("==== BUILDING PROCESS ENGINE ========================================================================");
-      processEngine = ProcessEngineConfiguration
+      processEngine = SpringProcessEngineConfiguration
         .createProcessEngineConfigurationFromResource(configurationResource)
         .buildProcessEngine();
       log.fine("==== PROCESS ENGINE CREATED =========================================================================");
diff --git a/modules/activiti-engine/src/test/java/org/activiti/engine/test/db/ConnectionPoolTest.java b/modules/activiti-engine/src/test/java/org/activiti/engine/test/db/ConnectionPoolTest.java
index 0935b50..7989975 100644
--- a/modules/activiti-engine/src/test/java/org/activiti/engine/test/db/ConnectionPoolTest.java
+++ b/modules/activiti-engine/src/test/java/org/activiti/engine/test/db/ConnectionPoolTest.java
@@ -15,7 +15,7 @@ package org.activiti.engine.test.db;
 
 import javax.sql.DataSource;
 
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
 import org.activiti.engine.impl.test.PvmTestCase;
 import org.apache.ibatis.datasource.pooled.PooledDataSource;
@@ -27,7 +27,7 @@ import org.apache.ibatis.datasource.pooled.PooledDataSource;
 public class ConnectionPoolTest extends PvmTestCase {
 
   public void testMyBatisConnectionPoolProperlyConfigured() {
-    ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration
+    ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) SpringProcessEngineConfiguration
       .createProcessEngineConfigurationFromResource("org/activiti/engine/test/db/connection-pool.activiti.cfg.xml");
 
     config.buildProcessEngine();
diff --git a/modules/activiti-engine/src/test/java/org/activiti/examples/variables/jpa/JPAVariableTest.java b/modules/activiti-engine/src/test/java/org/activiti/examples/variables/jpa/JPAVariableTest.java
index 8537dbf..014506d 100644
--- a/modules/activiti-engine/src/test/java/org/activiti/examples/variables/jpa/JPAVariableTest.java
+++ b/modules/activiti-engine/src/test/java/org/activiti/examples/variables/jpa/JPAVariableTest.java
@@ -28,7 +28,7 @@ import junit.framework.Assert;
 
 import org.activiti.engine.ActivitiException;
 import org.activiti.engine.ProcessEngine;
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
 import org.activiti.engine.impl.test.AbstractActivitiTestCase;
 import org.activiti.engine.impl.variable.EntityManagerSession;
@@ -70,7 +70,7 @@ public class JPAVariableTest extends AbstractActivitiTestCase {
 
   protected void initializeProcessEngine() {
     if (cachedProcessEngine==null) {
-      ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration
+      ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) SpringProcessEngineConfiguration
         .createProcessEngineConfigurationFromResource("org/activiti/examples/variables/jpa/activiti.cfg.xml");
 
       processEngine = processEngineConfiguration.buildProcessEngine();
diff --git a/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/NoDbConnectionTest.java b/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/NoDbConnectionTest.java
index 0fd0313..2c3e7bd 100644
--- a/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/NoDbConnectionTest.java
+++ b/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/NoDbConnectionTest.java
@@ -14,7 +14,7 @@ package org.activiti.standalone.initialization;
 
 import java.sql.SQLException;
 
-import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 import org.activiti.engine.impl.test.PvmTestCase;
 
 /**
@@ -24,7 +24,7 @@ public class NoDbConnectionTest extends PvmTestCase {
 
   public void testNoDbConnection() {
     try {
-      ProcessEngineConfiguration
+      SpringProcessEngineConfiguration
         .createProcessEngineConfigurationFromResource("org/activiti/standalone/initialization/nodbconnection.activiti.cfg.xml")
         .buildProcessEngine();
       fail("expected exception");
diff --git a/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/ProcessEngineInitializationTest.java b/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/ProcessEngineInitializationTest.java
index 751cec4..06a7016 100644
--- a/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/ProcessEngineInitializationTest.java
+++ b/modules/activiti-engine/src/test/java/org/activiti/standalone/initialization/ProcessEngineInitializationTest.java
@@ -19,6 +19,7 @@ import org.activiti.engine.ActivitiException;
 import org.activiti.engine.ActivitiWrongDbException;
 import org.activiti.engine.ProcessEngine;
 import org.activiti.engine.ProcessEngineConfiguration;
+import org.activiti.engine.SpringProcessEngineConfiguration;
 import org.activiti.engine.impl.ProcessEngineImpl;
 import org.activiti.engine.impl.db.DbSqlSession;
 import org.activiti.engine.impl.db.DbSqlSessionFactory;
@@ -33,7 +34,7 @@ public class ProcessEngineInitializationTest extends PvmTestCase {
 
   public void testNoTables() {
     try {
-      ProcessEngineConfiguration
+      SpringProcessEngineConfiguration
       .createProcessEngineConfigurationFromResource("org/activiti/standalone/initialization/notables.activiti.cfg.xml")
         .buildProcessEngine();
       fail("expected exception");
@@ -45,7 +46,7 @@ public class ProcessEngineInitializationTest extends PvmTestCase {
 
   public void testVersionMismatch() {
     // first create the schema
-    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngineConfiguration
+    ProcessEngineImpl processEngine = (ProcessEngineImpl) SpringProcessEngineConfiguration
       .createProcessEngineConfigurationFromResource("org/activiti/standalone/initialization/notables.activiti.cfg.xml")
       .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP)
       .buildProcessEngine();
@@ -78,7 +79,7 @@ public class ProcessEngineInitializationTest extends PvmTestCase {
     try {
       // now we can see what happens if when a process engine is being
       // build with a version mismatch between library and db tables
-      ProcessEngineConfiguration
+      SpringProcessEngineConfiguration
         .createProcessEngineConfigurationFromResource("org/activiti/standalone/initialization/notables.activiti.cfg.xml")
         .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
         .buildProcessEngine();
-- 
1.7.1

tombaeyens
Offline
Last seen: 2 years 4 days ago
Joined: 04/27/2010 - 11:41
Posts: 440

Spring is not wired deep into the engine. The only hard dependency we have right now on a couple of spring libs is to parse the configuration file. We could build our own parser, but that would take us a couple of days. And for now we prefer to spend our time on useful features instead. But if these lib dependencies keep annoying our users, then we might build our own parser for it.

Beyond the configuration file parsing, there is also a more deep spring integration but that is all optional.

regards, tom.

egore911
Offline
Last seen: 2 years 9 months ago
Joined: 09/23/2010 - 07:14
Posts: 9

You're right. Writing a parser yourself is currently a waste time. And I think the patch I posted does all we need here. You still get the possibility to load from a configuration file plus you gain the ability not use it and instantiate the configuration "programmatically" (or using any other IOC container). It's just moving code around a bit to allow the top level class of the hierarchie not to depend on Spring.

tombaeyens
Offline
Last seen: 2 years 4 days ago
Joined: 04/27/2010 - 11:41
Posts: 440

regards, tom.

egore911
Offline
Last seen: 2 years 9 months ago
Joined: 09/23/2010 - 07:14
Posts: 9

Perfect smiley I attached my patch to the issue.