/*
* This thread performs all the evaluation
*/
public void run() {
DynamicClassLoader loader = null;;
jobDescriptor j = null;
JavaSourceCompiler javaSourceCompiler;
javaSourceCompiler = new JavaSourceCompilerImpl();
while(!exit) {
try {
// wait for work
syncObject.acquire();
// check if we must create or update class loader
if(loader==null || updateLoader) {
loader=new DynamicClassLoader(outDir);
for(String pt : classPath) {
loader.add(pt);
}
}
// get next job descriptor
j = jobQueue.poll();
if(j==null)
continue;
j.outputObject.started();
Pattern p;
Matcher m;
String pname = packageId;
JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit(new File(outDir));
// build the compiler class path
String classpath = System.getProperty("java.class.path");
String[] classpathEntries = classpath.split(File.pathSeparator);
if(classpathEntries!=null && classpathEntries.length>0)
compilationUnit.addClassPathEntries(Arrays.asList(classpathEntries));
if(!classPath.isEmpty())
compilationUnit.addClassPathEntries(classPath);
compilationUnit.addClassPathEntry(outDir);
// normalize and analyze code
String code = normalizeCode(j.codeToBeExecuted);
String [] codev = code.split("\n");
int ci = 0;
StringBuilder javaSourceCode = new StringBuilder();
p = Pattern.compile("\\s*package\\s+((?:[a-zA-Z]\\w*)(?:\\.[a-zA-Z]\\w*)*);.*");
m = p.matcher(codev[ci]);
if(m.matches()) {
pname = m.group(1);
ci++;
}
javaSourceCode.append("package ");
javaSourceCode.append(pname);
javaSourceCode.append(";\n");
for(String i : imports) {
javaSourceCode.append("import ");
javaSourceCode.append(i);
javaSourceCode.append(";\n");
}
p = Pattern.compile("\\s*import\\s+((?:[a-zA-Z]\\w*)(?:\\.[a-zA-Z]\\w*)*(?:\\.\\*)?);.*");
m = p.matcher(codev[ci]);
while(m.matches()) {
String impstr = m.group(1);
ci++;
m = p.matcher(codev[ci]);
javaSourceCode.append("import ");
javaSourceCode.append(impstr);
javaSourceCode.append(";\n");
}
p = Pattern.compile("(?:^|.*\\s+)class\\s+([a-zA-Z]\\w*).*");
m = p.matcher(codev[ci]);
if(m.matches()) {
// this is a class definition
String cname = m.group(1);
for(; ci<codev.length; ci++)
javaSourceCode.append(codev[ci]);
compilationUnit.addJavaSource(pname+"."+cname, javaSourceCode.toString());
try {
javaSourceCompiler.compile(compilationUnit);
javaSourceCompiler.persistCompiledClasses(compilationUnit);
j.outputObject.finished(null);
} catch(Exception e) { j.outputObject.error("ERROR:\n"+e.toString()); }
} else {
String ret = "void";
if(codev[codev.length-1].matches("(^|.*\\s+)return\\s+.*"))
ret = "Object";
// this is an expression evaluation
javaSourceCode.append("public class Foo {\n");
javaSourceCode.append("public static ");
javaSourceCode.append(ret);
javaSourceCode.append(" beakerRun() throws InterruptedException {\n");
for(; ci<codev.length; ci++)
javaSourceCode.append(codev[ci]);
javaSourceCode.append("}\n");
javaSourceCode.append("}\n");
compilationUnit.addJavaSource(pname+".Foo", javaSourceCode.toString());
loader.clearCache();
javaSourceCompiler.compile(compilationUnit);
javaSourceCompiler.persistCompiledClasses(compilationUnit);
Class<?> fooClass = loader.loadClass(pname+".Foo");
Method mth = fooClass.getDeclaredMethod("beakerRun", (Class[]) null);
Object o = mth.invoke(null, (Object[])null);
if(ret.equals("Object")) {
j.outputObject.finished(o);
} else {