Skip to content

Commit

Permalink
changing class name to StaticRepl and adding some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mfelgamal committed Aug 9, 2016
1 parent 0f73387 commit b88ff75
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 98 deletions.
5 changes: 0 additions & 5 deletions beam/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
<dependencies>


<dependency>
<groupId>com.javax0</groupId>
<artifactId>jscc</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,21 @@ public void close() {
File dir = new File(".");
for (int i = 0; i < dir.list().length; i++) {
File f = dir.listFiles()[i];
// System.out.println(f.getAbsolutePath());
if (f.getAbsolutePath().contains(".class"))
f.delete();
}
}

@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
public InterpreterResult interpret(String code, InterpreterContext context) {

String className = "C" + UUID.randomUUID().toString().replace("-", "");

try {
String msg = CompileSourceInMemory.execute(className, st);
String msg = StaticRepl.execute(className, code);
return new InterpreterResult(InterpreterResult.Code.SUCCESS, msg);
} catch (Exception e) {
// e.printStackTrace();
// e.printStackTrace();
return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

/**
* @author Mahmoud
*
*
*/
public class CompileSourceInMemory {
public class StaticRepl {
public static String execute(String className, String code) throws Exception {

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Expand Down Expand Up @@ -64,11 +64,9 @@ public static String execute(String className, String code) throws Exception {

out.println(code);
out.close();



JavaFileObject file = new JavaSourceFromString(className, writer.toString());

Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);

ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
Expand All @@ -88,29 +86,28 @@ public static String execute(String className, String code) throws Exception {
boolean success = task.call();
if (!success) {
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
if (diagnostic.getLineNumber() == -1) continue;
System.out.println("line "+ diagnostic.getLineNumber()+ " : " +diagnostic.getMessage(null));
if (diagnostic.getLineNumber() == -1)
continue;
System.out.println("line " + diagnostic.getLineNumber() + " : "
+ diagnostic.getMessage(null));
}
}
if (success) {
try {

URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
//URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
Class.forName(className, true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });

URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI()
.toURL() });
Class.forName(className, true, classLoader)
.getDeclaredMethod("main", new Class[] { String[].class })
.invoke(null, new Object[] { null });

// Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
// .invoke(null, new Object[] { null });

System.out.flush();
System.err.flush();

System.setOut(oldOut);
System.setErr(oldErr);



return baosOut.toString();
} catch (ClassNotFoundException e) {
e.printStackTrace(newErr);
Expand All @@ -128,19 +125,19 @@ public static String execute(String className, String code) throws Exception {
e.printStackTrace(newErr);
System.err.println("Invocation target: " + e);
throw new Exception(baosErr.toString());
} finally{
System.out.flush();
System.err.flush();

System.setOut(oldOut);
System.setErr(oldErr);
}
} else {
System.out.flush();
} finally {
System.out.flush();
System.err.flush();

System.setOut(oldOut);
System.setErr(oldErr);
}
} else {
System.out.flush();
System.err.flush();

System.setOut(oldOut);
System.setErr(oldErr);
throw new Exception(baosOut.toString());
}
}
Expand Down
129 changes: 66 additions & 63 deletions beam/src/main/test/org/apache/zeppelin/beam/BeamInterpreterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,79 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.zeppelin.beam.BeamInterpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author admin
*
*/
public class BeamInterpreterTest {

private static BeamInterpreter beam;
private static InterpreterContext context;

@BeforeClass
public static void setUp() {
Properties p = new Properties();
beam = new BeamInterpreter(p);
beam.open();
context = new InterpreterContext(null, null, null, null, null, null,
null, null, null, null, null);
}

@AfterClass
public static void tearDown() {
beam.close();
}

@Test
public void testStaticRepl() {

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();

InterpreterResult res = beam.interpret(writer.toString(), context);

assertEquals(InterpreterResult.Code.SUCCESS, res.code());
}
@Test
public void testStaticReplWithoutMain() {

StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
@Test
public void testStaticReplWithSyntaxError() {

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.prin(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);

assertEquals(InterpreterResult.Code.ERROR, res.code());
}
private static BeamInterpreter beam;
private static InterpreterContext context;

@BeforeClass
public static void setUp() {
Properties p = new Properties();
beam = new BeamInterpreter(p);
beam.open();
context = new InterpreterContext(null, null, null, null, null, null, null, null, null, null,
null);
}

@AfterClass
public static void tearDown() {
beam.close();
}

@Test
public void testStaticRepl() {

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();

InterpreterResult res = beam.interpret(writer.toString(), context);

assertEquals(InterpreterResult.Code.SUCCESS, res.code());
}

@Test
public void testStaticReplWithoutMain() {

StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}

@Test
public void testStaticReplWithSyntaxError() {

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.prin(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);

assertEquals(InterpreterResult.Code.ERROR, res.code());
}

}
2 changes: 1 addition & 1 deletion zeppelin-web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<script src="bower_components/angular-xeditable/dist/js/xeditable.js"></script>
<script src="bower_components/highlightjs/highlight.pack.js"></script>
<script src="bower_components/lodash/lodash.js"></script>
<script src="bower_components/angular-filter/dist/angular-filter.js"></script>
<script src="bower_components/angular-filter/dist/angular-filter.min.js"></script>
<script src="bower_components/ngtoast/dist/ngToast.js"></script>
<script src="bower_components/ng-focus-if/focusIf.js"></script>
<script src="bower_components/bootstrap3-dialog/dist/js/bootstrap-dialog.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion zeppelin-web/test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function(config) {
'bower_components/angular-xeditable/dist/js/xeditable.js',
'bower_components/highlightjs/highlight.pack.js',
'bower_components/lodash/lodash.js',
'bower_components/angular-filter/dist/angular-filter.js',
'bower_components/angular-filter/dist/angular-filter.min.js',
'bower_components/ngtoast/dist/ngToast.js',
'bower_components/ng-focus-if/focusIf.js',
'bower_components/bootstrap3-dialog/dist/js/bootstrap-dialog.min.js',
Expand Down

0 comments on commit b88ff75

Please sign in to comment.