[jbpm-commits] JBoss JBPM SVN: r6386 - in jbpm3/branches/jbpm-3.2-soa/modules/core/src: test/java/org/jbpm and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Jun 1 04:29:01 EDT 2010


Author: alex.guizar at jboss.com
Date: 2010-06-01 04:29:00 -0400 (Tue, 01 Jun 2010)
New Revision: 6386

Added:
   jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/
   jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/JBPM2825Test.java
Modified:
   jbpm3/branches/jbpm-3.2-soa/modules/core/src/main/java/org/jbpm/instantiation/ProcessClassLoader.java
Log:
JBPM-2825: do not throw NPE from ProcessClassLoader when there is no current context

Modified: jbpm3/branches/jbpm-3.2-soa/modules/core/src/main/java/org/jbpm/instantiation/ProcessClassLoader.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/modules/core/src/main/java/org/jbpm/instantiation/ProcessClassLoader.java	2010-06-01 03:21:15 UTC (rev 6385)
+++ jbpm3/branches/jbpm-3.2-soa/modules/core/src/main/java/org/jbpm/instantiation/ProcessClassLoader.java	2010-06-01 08:29:00 UTC (rev 6386)
@@ -54,54 +54,54 @@
   }
 
   protected ProcessDefinition getProcessDefinition() {
-    return processDefinition != null ? processDefinition : JbpmContext.getCurrentJbpmContext()
-        .getGraphSession()
-        .loadProcessDefinition(processDefinitionId);
+    if (processDefinition != null) return processDefinition;
+
+    JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
+    if (jbpmContext != null) {
+      return jbpmContext.getGraphSession().loadProcessDefinition(processDefinitionId);
+    }
+
+    return null;
   }
 
   public URL findResource(String name) {
     ProcessDefinition processDefinition = getProcessDefinition();
-    FileDefinition fileDefinition = processDefinition.getFileDefinition();
-    if (fileDefinition != null) {
-      // we know that the leading slashes are removed in the names of the
-      // file definitions, therefore we skip the leading slashes
-      int off = 0;
-      for (int len = name.length(); off < len && name.charAt(off) == '/'; off++)
-        /* just increase offset */;
+    FileDefinition fileDefinition;
+    if (processDefinition == null
+      || (fileDefinition = processDefinition.getFileDefinition()) == null) return null;
 
-      // if the name of the resources is absolute (starts with one or more slashes)
-      if (off > 0) {
-        // then start searching from the root of the process archive
-        name = name.substring(off);
-      }
-      else {
-        // otherwise (if the resource is relative), look in the classes
-        // directory in the process archive
-        name = "classes/" + name;
-      }
+    // we know that the leading slashes are removed in the names of the
+    // file definitions, therefore we skip the leading slashes
+    int off = 0;
+    for (int len = name.length(); off < len && name.charAt(off) == '/'; off++)
+      /* just increase offset */;
 
-      byte[] bytes = null;
-      if (fileDefinition.hasFile(name)) {
-        bytes = fileDefinition.getBytes(name);
-      }
-      if (bytes != null) {
-        try {
-          return new URL(null, "processresource://"
-              + processDefinition.getName()
-              + "/classes/"
-              + name, new BytesUrlStreamHandler(bytes));
-        }
-        catch (MalformedURLException e) {
-          throw new JbpmException("couldn't create url", e);
-        }
-      }
+    // if the name of the resources is absolute (starts with one or more slashes)
+    if (off > 0) {
+      // then start searching from the root of the process archive
+      name = name.substring(off);
     }
-    return null;
+    else {
+      // otherwise (if the resource is relative), look in the classes
+      // directory of the file module definition
+      name = "classes/" + name;
+    }
+
+    byte[] bytes = fileDefinition.getBytes(name);
+    if (bytes == null) return null;
+
+    try {
+      return new URL("processresource", processDefinition.getName(), -1, "classes/" + name,
+        new BytesUrlStreamHandler(bytes));
+    }
+    catch (MalformedURLException e) {
+      throw new JbpmException("could not create url", e);
+    }
   }
 
   public static class BytesUrlStreamHandler extends URLStreamHandler {
 
-    byte[] bytes;
+    private byte[] bytes;
 
     public BytesUrlStreamHandler(byte[] bytes) {
       this.bytes = bytes;
@@ -114,7 +114,7 @@
 
   public static class BytesUrlConnection extends URLConnection {
 
-    byte[] bytes = null;
+    private byte[] bytes;
 
     public BytesUrlConnection(byte[] bytes, URL u) {
       super(u);
@@ -131,30 +131,30 @@
 
   public Class findClass(String className) throws ClassNotFoundException {
     ProcessDefinition processDefinition = getProcessDefinition();
-    FileDefinition fileDefinition = processDefinition.getFileDefinition();
-    if (fileDefinition != null) {
-      String fileName = "classes/" + className.replace('.', '/') + ".class";
-      byte[] classBytes = fileDefinition.getBytes(fileName);
+    FileDefinition fileDefinition;
+    if (processDefinition == null
+      || (fileDefinition = processDefinition.getFileDefinition()) == null) {
+      throw new ClassNotFoundException(className);
+    }
 
-      if (classBytes != null) {
-        // define the package before defining the class
-        // see https://jira.jboss.org/jira/browse/JBPM-1404
-        int packageIndex = className.lastIndexOf('.');
+    // look in the classes directory of the file module definition
+    String fileName = "classes/" + className.replace('.', '/') + ".class";
+    byte[] classBytes = fileDefinition.getBytes(fileName);
+    if (classBytes == null) throw new ClassNotFoundException(className);
 
-        if (packageIndex != -1) {
-          String packageName = className.substring(0, packageIndex);
+    // define the package before defining the class
+    // see https://jira.jboss.org/jira/browse/JBPM-1404
+    int packageIndex = className.lastIndexOf('.');
 
-          if (getPackage(packageName) == null) {
-            Package jbpmPackage = ProcessClassLoader.class.getPackage();
-            definePackage(packageName, processDefinition.getName(), 
-                Integer.toString(processDefinition.getVersion()), null, 
-                jbpmPackage.getImplementationTitle(), jbpmPackage.getImplementationVersion(),
-                jbpmPackage.getImplementationVersion(), null);
-          }
-        }
-        return defineClass(className, classBytes, 0, classBytes.length);
+    if (packageIndex != -1) {
+      String packageName = className.substring(0, packageIndex);
+
+      if (getPackage(packageName) == null) {
+        Package jbpmPackage = ProcessClassLoader.class.getPackage();
+        definePackage(packageName, null, null, null, jbpmPackage.getImplementationTitle(),
+          jbpmPackage.getImplementationVersion(), jbpmPackage.getImplementationVendor(), null);
       }
     }
-    throw new ClassNotFoundException(className);
+    return defineClass(className, classBytes, 0, classBytes.length);
   }
 }

Added: jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/JBPM2825Test.java
===================================================================
--- jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/JBPM2825Test.java	                        (rev 0)
+++ jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/JBPM2825Test.java	2010-06-01 08:29:00 UTC (rev 6386)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.jbpm2825;
+
+import org.jbpm.JbpmConfiguration;
+import org.jbpm.db.AbstractDbTestCase;
+import org.jbpm.file.def.FileDefinition;
+import org.jbpm.graph.def.ProcessDefinition;
+
+/**
+ * @author Alejandro Guizar
+ */
+public class JBPM2825Test extends AbstractDbTestCase {
+
+  public void testProcessClassLoaderOutsideContext() {
+    FileDefinition fileDefinition = new FileDefinition();
+    byte[] magicNumber = {
+      (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE
+    };
+    fileDefinition.addFile("classes/org/example/Undef", magicNumber);
+
+    ProcessDefinition processDefinition = new ProcessDefinition(getName());
+    processDefinition.addDefinition(fileDefinition);
+    jbpmContext.deployProcessDefinition(processDefinition);
+
+    ClassLoader procClassLoader = JbpmConfiguration.getProcessClassLoader(processDefinition);
+    String undefClassName = "org.example.Undef";
+
+    closeJbpmContext();
+    try {
+      Class.forName(undefClassName, false, procClassLoader);
+      fail("expected class " + undefClassName + " to not be found");
+    }
+    catch (ClassNotFoundException e) {
+      assertEquals(undefClassName, e.getMessage());
+    }
+    finally {
+      createJbpmContext();
+    }
+  }
+}


Property changes on: jbpm3/branches/jbpm-3.2-soa/modules/core/src/test/java/org/jbpm/jbpm2825/JBPM2825Test.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native



More information about the jbpm-commits mailing list