Author: rebody
Date: 2010-10-05 05:27:02 -0400 (Tue, 05 Oct 2010)
New Revision: 6741
Added:
jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java
jbpm4/trunk/modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml
Modified:
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/repository/DeploymentObjectInputStream.java
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/util/ReflectUtil.java
Log:
JBPM-2929 fixed deployment classloader class cast issue.
Modified:
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/repository/DeploymentObjectInputStream.java
===================================================================
---
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/repository/DeploymentObjectInputStream.java 2010-10-05
08:42:00 UTC (rev 6740)
+++
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/repository/DeploymentObjectInputStream.java 2010-10-05
09:27:02 UTC (rev 6741)
@@ -52,8 +52,7 @@
catch (ClassNotFoundException e) {
// trying to get it from deployment
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
- ClassLoader deploymentClassLoader =
- new DeploymentClassLoader(contextClassLoader, deploymentId);
+ ClassLoader deploymentClassLoader =
ReflectUtil.getDeploymentClassLoader(deploymentId, contextClassLoader);
return Class.forName(desc.getName(), false, deploymentClassLoader);
}
}
Modified:
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/util/ReflectUtil.java
===================================================================
---
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/util/ReflectUtil.java 2010-10-05
08:42:00 UTC (rev 6740)
+++
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/util/ReflectUtil.java 2010-10-05
09:27:02 UTC (rev 6741)
@@ -393,16 +393,20 @@
Thread currentThread = Thread.currentThread();
ClassLoader original = currentThread.getContextClassLoader();
+ DeploymentClassLoader deploymentClassLoader = getDeploymentClassLoader(deploymentId,
original);
+ currentThread.setContextClassLoader(deploymentClassLoader);
+
+ return original;
+ }
+
+ public static DeploymentClassLoader getDeploymentClassLoader(String deploymentId,
ClassLoader original) {
RepositoryCache repositoryCache =
EnvironmentImpl.getFromCurrent(RepositoryCache.class);
DeploymentClassLoader deploymentClassLoader =
repositoryCache.getDeploymentClassLoader(deploymentId, original);
if (deploymentClassLoader == null) {
deploymentClassLoader = new DeploymentClassLoader(original, deploymentId);
repositoryCache.setDeploymentClassLoader(deploymentId, original,
deploymentClassLoader);
}
-
- currentThread.setContextClassLoader(deploymentClassLoader);
-
- return original;
+ return deploymentClassLoader;
}
public static void uninstallDeploymentClassLoader(ClassLoader original) {
Added:
jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java
===================================================================
---
jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java
(rev 0)
+++
jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/variables/DeploymentSerializeTest.java 2010-10-05
09:27:02 UTC (rev 6741)
@@ -0,0 +1,156 @@
+package org.jbpm.test.variables;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.util.List;
+
+import org.codehaus.janino.DebuggingInformation;
+import org.codehaus.janino.util.StringPattern;
+import org.codehaus.janino.util.enumerator.EnumeratorSet;
+import org.jbpm.api.ProcessInstance;
+import org.jbpm.api.task.Task;
+import org.jbpm.test.JbpmTestCase;
+
+public class DeploymentSerializeTest extends JbpmTestCase {
+ private File targetDir;
+ private String deploymentId;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ this.targetDir = new File("target/generated/org/jbpm/test/variables");
+
+ generateClasses();
+
+ this.deploymentId = repositoryService.createDeployment()
+
.addResourceFromClasspath("org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml")
+
.addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeBean.class",
new FileInputStream(new File(targetDir, "DeploymentSerializeBean.class")))
+
.addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeCustom1.class",
new FileInputStream(new File(targetDir, "DeploymentSerializeCustom1.class")))
+
.addResourceFromInputStream("org/jbpm/test/variables/DeploymentSerializeCustom2.class",
new FileInputStream(new File(targetDir, "DeploymentSerializeCustom2.class")))
+ .deploy();
+ }
+
+ protected void tearDown() throws Exception {
+ repositoryService.deleteDeploymentCascade(deploymentId);
+ super.tearDown();
+ }
+
+ public void testDeploymentVariableSerialization() {
+ ProcessInstance processInstance =
executionService.startProcessInstanceByKey("DeploymentSerializeTest");
+ String processInstanceId = processInstance.getId();
+
+ List<Task> tasks = taskService.findPersonalTasks("alex");
+ assertTrue(tasks.size() == 1);
+ Task task = tasks.get(0);
+
+ assertNotNull(taskService.getVariable(task.getId(), "bean"));
+ assertNotNull(executionService.getVariable(processInstanceId, "bean"));
+ assertEquals(executionService.getVariable(processInstanceId,
"bean").toString(), "DeploymentSerializeBean[value=42]");
+
+ taskService.completeTask(task.getId());
+ }
+
+ private void generateClasses() {
+ try {
+ if (!targetDir.exists()) {
+ targetDir.mkdirs();
+ }
+
+ File[] sourceFiles = {
+ writeFile(targetDir, "DeploymentSerializeBean.java", getBeanSource()),
+ writeFile(targetDir, "DeploymentSerializeCustom1.java",
getCustom1Source()),
+ writeFile(targetDir, "DeploymentSerializeCustom2.java",
getCustom2Source()),
+ };
+
+ compileClasses(sourceFiles);
+ }
+ catch (Exception e) {
+ log.error("Error while creating additional resources", e);
+ }
+ }
+
+ /**
+ * This method is used to generate classes that will be used within a process but
should not be
+ * on class path while executing process but retrieved from db.
+ *
+ * Uses Janinio to compile the sources.
+ */
+ private void compileClasses(File[] sourceFiles) throws Exception {
+ log.debug("Inside compileClasses method");
+
+ File destinationDirectory = org.codehaus.janino.Compiler.NO_DESTINATION_DIRECTORY;
+ File[] optionalSourcePath = null;
+ File[] classPath = { new File("."), new
File("../api/target/classes") };
+ File[] optionalExtDirs = null;
+ File[] optionalBootClassPath = null;
+ String optionalCharacterEncoding = null;
+ boolean verbose = false;
+ EnumeratorSet debuggingInformation =
DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION;
+ StringPattern[] warningHandlePatterns =
org.codehaus.janino.Compiler.DEFAULT_WARNING_HANDLE_PATTERNS;
+ boolean rebuild = false;
+
+ log.debug("About to run Janinio compiler");
+
+ org.codehaus.janino.Compiler javac = new
org.codehaus.janino.Compiler(optionalSourcePath, classPath, optionalExtDirs,
optionalBootClassPath,
+ destinationDirectory, optionalCharacterEncoding, verbose,
debuggingInformation, warningHandlePatterns, rebuild);
+ javac.compile(sourceFiles);
+
+ log.debug("Class compiled successfully");
+ }
+
+ private File writeFile(File directory, String filename, String content) throws
Exception {
+ File sourceFile = new File(directory, filename);
+ FileWriter writer = new FileWriter(sourceFile);
+ writer.write(content);
+ writer.close();
+
+ log.debug("Source file '" + filename + "' created in
'" + directory.getAbsolutePath() + "'");
+
+ return sourceFile;
+ }
+
+ private String getBeanSource() {
+ StringBuffer source = new StringBuffer();
+ source.append("package org.jbpm.test.variables;\n");
+ source.append("import java.io.Serializable;\n");
+ source.append("public class DeploymentSerializeBean implements Serializable
{\n");
+ source.append(" private int value;\n");
+ source.append(" public int getValue() { return value; }\n");
+ source.append(" public void setValue(int value) { this.value = value;
}\n");
+ source.append(" public String toString() { return
\"DeploymentSerializeBean[value=\" + value + \"]\"; }\n");
+ source.append("}\n");
+ return source.toString();
+ }
+
+ private String getCustom1Source() {
+ StringBuffer source = new StringBuffer();
+ source.append("package org.jbpm.test.variables;\n");
+ source.append("import org.jbpm.api.activity.ActivityBehaviour;\n");
+ source.append("import org.jbpm.api.activity.ActivityExecution;\n");
+ source.append("public class DeploymentSerializeCustom1 implements
ActivityBehaviour {\n");
+ source.append(" public void execute(ActivityExecution execution) throws
Exception {\n");
+ source.append(" DeploymentSerializeBean bean = new
DeploymentSerializeBean();\n");
+ source.append(" bean.setValue(42);\n");
+ source.append(" execution.setVariable(\"bean\", bean);\n");
+ source.append(" }\n");
+ source.append("}\n");
+ return source.toString();
+ }
+
+ private String getCustom2Source() {
+ StringBuffer source = new StringBuffer();
+ source.append("package org.jbpm.test.variables;\n");
+ source.append("import org.jbpm.api.activity.ActivityBehaviour;\n");
+ source.append("import org.jbpm.api.activity.ActivityExecution;\n");
+ source.append("public class DeploymentSerializeCustom2 implements
ActivityBehaviour {\n");
+ source.append(" public void execute(ActivityExecution execution) throws
Exception {\n");
+ source.append(" DeploymentSerializeBean bean =
(DeploymentSerializeBean)execution.getVariable(\"bean\");\n");
+ source.append(" if (bean.getValue() != 42) {\n");
+ source.append(" throw new Exception(\"Value must be
42!\");\n");
+ source.append(" }\n");
+ source.append(" }\n");
+ source.append("}\n");
+ return source.toString();
+ }
+}
Added:
jbpm4/trunk/modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml
===================================================================
---
jbpm4/trunk/modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml
(rev 0)
+++
jbpm4/trunk/modules/test-db/src/test/resources/org/jbpm/test/variables/DeploymentSerializeTest.jpdl.xml 2010-10-05
09:27:02 UTC (rev 6741)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<process name="DeploymentSerializeTest"
xmlns="http://jbpm.org/4.4/jpdl">
+ <start name="start1">
+ <transition to="custom1"/>
+ </start>
+ <custom name="custom1"
class="org.jbpm.test.variables.DeploymentSerializeCustom1">
+ <transition to="task1"/>
+ </custom>
+ <task name="task1" assignee="alex">
+ <transition to="custom2"/>
+ </task>
+ <custom name="custom2"
class="org.jbpm.test.variables.DeploymentSerializeCustom2">
+ <transition to="end1"/>
+ </custom>
+ <end name="end1"/>
+</process>