helper for printing javascript and inject $z

This commit is contained in:
Lee moon soo 2016-05-12 11:59:05 -07:00
parent 8186daf58e
commit b6e4141bad
5 changed files with 78 additions and 2 deletions

View file

@ -48,4 +48,48 @@ public abstract class Application {
* this method is invoked just before application is removed
*/
public abstract void unload() throws ApplicationException;
public void print(String string) throws IOException {
context.out.write(string);
}
public void println(String string) throws IOException {
print(string + "\n");
}
public void printResource(String resourceName) throws IOException {
context.out.writeResource(resourceName);
}
public void printResourceAsJavascript(String resourceName) throws IOException {
beginJavascript();
context.out.writeResource(resourceName);
endJavascript();
}
public void printStringAsJavascript(String js) throws IOException {
beginJavascript();
context.out.write(js);
endJavascript();
}
private void beginJavascript() throws IOException {
StringBuffer js = new StringBuffer();
js.append("\n<script id=\"app_js_" + js.hashCode() + "\">\n");
js.append("(function() {\n");
js.append("var $z = {\n");
js.append("id : \"" + context.getApplicationInstanceId() + "\",\n");
js.append("scope : angular.element(\"#app_js_" + js.hashCode() + "\").scope()\n");
js.append("};\n");
js.append("$z.result = ($z.scope._devmodeResult) ? " +
"$z.scope._devmodeResult : $z.scope.$parent.paragraph.result;\n");
context.out.write(js.toString());
}
private void endJavascript() throws IOException {
StringBuffer js = new StringBuffer();
js.append("\n})();\n");
js.append("</script>\n");
context.out.write(js.toString());
}
}

View file

@ -25,15 +25,19 @@ import org.apache.zeppelin.interpreter.InterpreterOutput;
public class ApplicationContext {
private final String noteId;
private final String paragraphId;
private final String applicationInstanceId;
private final HeliumAppAngularObjectRegistry angularObjectRegistry;
public final InterpreterOutput out;
public ApplicationContext(String noteId,
String paragraphId,
String applicationInstanceId,
HeliumAppAngularObjectRegistry angularObjectRegistry,
InterpreterOutput out) {
this.noteId = noteId;
this.paragraphId = paragraphId;
this.applicationInstanceId = applicationInstanceId;
this.angularObjectRegistry = angularObjectRegistry;
this.out = out;
}
@ -46,6 +50,10 @@ public class ApplicationContext {
return paragraphId;
}
public String getApplicationInstanceId() {
return applicationInstanceId;
}
public HeliumAppAngularObjectRegistry getAngularObjectRegistry() {
return angularObjectRegistry;
}

View file

@ -21,6 +21,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import com.google.gson.Gson;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
@ -29,6 +30,7 @@ import org.apache.zeppelin.interpreter.*;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterEventClient;
import org.apache.zeppelin.resource.ResourceSet;
import org.apache.zeppelin.resource.WellKnownResourceName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -94,6 +96,7 @@ public class ZeppelinApplicationDevServer extends ZeppelinDevServer {
logger.info("Run " + className);
app.context().out.clear();
app.context().out.setType(InterpreterResult.Type.ANGULAR);
transferTableResultDataToFrontend();
app.run(resourceSet);
} catch (IOException | ApplicationException e) {
logger.error(e.getMessage(), e);
@ -102,10 +105,29 @@ public class ZeppelinApplicationDevServer extends ZeppelinDevServer {
return new InterpreterResult(Code.SUCCESS, "");
}
private void transferTableResultDataToFrontend() throws IOException {
ResourceSet results = resourceSet.filterByClassname(InterpreterResult.class.getName());
if (results.size() == 0) {
return;
}
InterpreterResult result = (InterpreterResult) results.get(0).get();
Gson gson = new Gson();
String resultJson = gson.toJson(result);
StringBuffer transferResult = new StringBuffer();
transferResult.append("$z.result = " + resultJson + ";\n");
if (result.type() == InterpreterResult.Type.TABLE) {
transferResult.append("$z.scope.loadTableData($z.result);\n");
}
transferResult.append("$z.scope._devmodeResult = $z.result;\n");
app.printStringAsJavascript(transferResult.toString());
}
ApplicationContext getApplicationContext(InterpreterContext interpreterContext) {
return new ApplicationContext(
interpreterContext.getNoteId(),
interpreterContext.getParagraphId(),
"app_" + this.hashCode(),
new HeliumAppAngularObjectRegistry(
interpreterContext.getAngularObjectRegistry(),
interpreterContext.getNoteId(),

View file

@ -783,6 +783,7 @@ public class RemoteInterpreterServer
return new ApplicationContext(
noteId,
paragraphId,
applicationInstanceId,
new HeliumAppAngularObjectRegistry(angularObjectRegistry, noteId, applicationInstanceId),
out);
}

View file

@ -53,7 +53,7 @@ public class ApplicationLoaderTest {
ApplicationLoader appLoader = new ApplicationLoader(resourcePool, dep);
HeliumPackage pkg1 = createPackageInfo(MockApplication1.class.getName(), "artifact1");
ApplicationContext context1 = createContext("note1", "paragraph1");
ApplicationContext context1 = createContext("note1", "paragraph1", "app1");
// when load application
MockApplication1 app = (MockApplication1) ((ClassLoaderApplication)
@ -82,10 +82,11 @@ public class ApplicationLoaderTest {
return app1;
}
public ApplicationContext createContext(String noteId, String paragraphId) {
public ApplicationContext createContext(String noteId, String paragraphId, String appInstanceId) {
ApplicationContext context1 = new ApplicationContext(
noteId,
paragraphId,
appInstanceId,
null,
new InterpreterOutput(new InterpreterOutputListener() {
@Override