3333 */
3434public class SparkRInterpreter extends Interpreter {
3535 private static final Logger logger = LoggerFactory .getLogger (SparkRInterpreter .class );
36+ protected static final String KINIT_CMD = ".zres <- knit2html(text=.zcmd)" ;
3637
3738 static {
3839 Interpreter .register (
@@ -55,17 +56,21 @@ public SparkRInterpreter(Properties property) {
5556
5657 @ Override
5758 public void open () {
58- ZeppelinR .open (getProperty ("spark.master" ), getProperty ("spark.home" ), getSparkInterpreter ());
59+ zeppelinR () .open (getProperty ("spark.master" ), getProperty ("spark.home" ), getSparkInterpreter ());
5960 }
6061
6162 @ Override
6263 public InterpreterResult interpret (String lines , InterpreterContext contextInterpreter ) {
6364
65+ if (contextInterpreter == null ) {
66+ throw new NullPointerException ("Please use a non null contextInterpreter" );
67+ }
68+
6469 try {
6570
66- ZeppelinR .set (".zcmd" , "\n ```{r comment=NA, echo=FALSE}\n " + lines + "\n ```" );
67- ZeppelinR .eval (".zres <- knit2html(text=.zcmd)" );
68- String html = ZeppelinR .getS0 (".zres" );
71+ zeppelinR () .set (".zcmd" , "\n ```{r comment=NA, echo=FALSE}\n " + lines + "\n ```" );
72+ zeppelinR () .eval (KINIT_CMD );
73+ String html = zeppelinR () .getS0 (".zres" );
6974
7075 // Only keep the bare results.
7176 String htmlOut = html .substring (html .indexOf ("<body>" ) + 7 , html .indexOf ("</body>" ) - 1 )
@@ -75,6 +80,7 @@ public InterpreterResult interpret(String lines, InterpreterContext contextInter
7580 .replaceAll ("<pre>" , "<p class='text'>" ).replaceAll ("</pre>" , "</p>" );
7681
7782 return new InterpreterResult (InterpreterResult .Code .SUCCESS , "%html\n " + htmlOut );
83+
7884 } catch (Exception e ) {
7985 logger .error ("Exception while connecting to R" , e );
8086 return new InterpreterResult (InterpreterResult .Code .ERROR , e .getMessage ());
@@ -84,7 +90,7 @@ public InterpreterResult interpret(String lines, InterpreterContext contextInter
8490
8591 @ Override
8692 public void close () {
87- ZeppelinR .close ();
93+ zeppelinR () .close ();
8894 }
8995
9096 @ Override
@@ -127,4 +133,51 @@ private SparkInterpreter getSparkInterpreter() {
127133 return null ;
128134 }
129135
136+ protected static ZeppelinRFactory zeppelinR () {
137+ return ZeppelinRFactory .instance ();
138+ }
139+
140+ /**
141+ * Java Factory to support tests with Mockito
142+ * (mockito can not mock the zeppelinR final scala class).
143+ */
144+ protected static class ZeppelinRFactory {
145+ private static ZeppelinRFactory instance ;
146+ private static ZeppelinR zeppelinR ;
147+
148+ private ZeppelinRFactory () {
149+ // Singleton
150+ }
151+
152+ protected static synchronized ZeppelinRFactory instance () {
153+ if (instance == null ) instance = new ZeppelinRFactory ();
154+ return instance ;
155+ }
156+
157+ protected void open (String master , String sparkHome , SparkInterpreter sparkInterpreter ) {
158+ zeppelinR .open (master , sparkHome , sparkInterpreter );
159+ }
160+
161+ protected Object eval (String command ) {
162+ return zeppelinR .eval (command );
163+ }
164+
165+ protected void set (String key , Object value ) {
166+ zeppelinR .set (key , value );
167+ }
168+
169+ protected Object get (String key ) {
170+ return zeppelinR .get (key );
171+ }
172+
173+ protected String getS0 (String key ) {
174+ return zeppelinR .getS0 (key );
175+ }
176+
177+ protected void close () {
178+ zeppelinR .close ();
179+ }
180+
181+ }
182+
130183}
0 commit comments