IDEMPIERE-598 Different errors when running Groovy scripts from Swing or Webui

This commit is contained in:
jan thielemann 2013-02-28 12:50:36 -05:00
parent e0279e57d4
commit 325f48f7ac
3 changed files with 52 additions and 6 deletions

View File

@ -24,6 +24,7 @@ import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.UUID;
import java.util.logging.Level;
import javax.script.ScriptEngine;
@ -159,8 +160,52 @@ public final class ProcessUtil {
process = Core.getProcess(className);
if (process == null) {
pi.setSummary("Failed to create new process instance for " + className, true);
//Get Class
Class<?> processClass = null;
//use context classloader if available
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null)
{
try
{
processClass = classLoader.loadClass(className);
}
catch (ClassNotFoundException ex)
{
log.log(Level.FINE, className, ex);
}
}
if (processClass == null)
{
classLoader = ProcessUtil.class.getClassLoader();
try
{
processClass = classLoader.loadClass(className);
}
catch (ClassNotFoundException ex)
{
log.log(Level.WARNING, className, ex);
pi.setSummary ("ClassNotFound", true);
return false;
}
}
if (processClass == null) {
pi.setSummary("No Instance for " + pi.getClassName(), true);
return false;
}
//Get Process
try
{
process = (ProcessCall)processClass.newInstance();
}
catch (Exception ex)
{
log.log(Level.WARNING, "Instance for " + className, ex);
pi.setSummary ("InstanceError", true);
return false;
}
}
boolean success = false;
@ -223,7 +268,7 @@ public final class ProcessUtil {
// now add the method arguments to the engine
engine.put(MRule.ARGUMENTS_PREFIX + "Ctx", ctx);
if (trx == null)
trx = Trx.get(pi.getTitle()+"_"+pi.getAD_PInstance_ID(), true);
trx = Trx.get(pi.getTitle()+"_"+pi.getAD_PInstance_ID() + "_" + UUID.randomUUID(), true);
engine.put(MRule.ARGUMENTS_PREFIX + "Trx", trx);
engine.put(MRule.ARGUMENTS_PREFIX + "TrxName", trx.getTrxName());
engine.put(MRule.ARGUMENTS_PREFIX + "Record_ID", pi.getRecord_ID());

View File

@ -26,6 +26,7 @@ import java.util.Properties;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.compiere.Adempiere;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.Msg;
@ -195,7 +196,7 @@ public class MRule extends X_AD_Rule
* @return ScriptEngine
*/
public ScriptEngine getScriptEngine() {
factory = new ScriptEngineManager();
factory = new ScriptEngineManager(Adempiere.class.getClassLoader());
String engineName = getEngineName();
if (engineName != null)
engine = factory.getEngineByName(getEngineName());

View File

@ -116,11 +116,11 @@ public class WProcessInfo extends ProcessInfo {
//try append W prefix to class name
if (zkName == null)
{
int lastdot = className.lastIndexOf(".");
zkName = className.substring(0, lastdot) + ".W" + className.substring(lastdot+1);
try {
int lastdot = className.lastIndexOf(".");
zkName = className.substring(0, lastdot) + ".W" + className.substring(lastdot+1);
this.getClass().getClassLoader().loadClass(zkName);
} catch (ClassNotFoundException e) {
} catch (Exception e) {
zkName = null;
}
}