Skip to content

Commit e810da4

Browse files
committed
Move getEditorSetting to InterpreterFactory class
1 parent fd7896e commit e810da4

File tree

7 files changed

+95
-82
lines changed

7 files changed

+95
-82
lines changed

zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void onMessage(NotebookSocket conn, String msg) {
251251
saveInterpreterBindings(conn, messagereceived);
252252
break;
253253
case EDITOR_SETTING:
254-
getEditorSetting(conn, notebook, messagereceived);
254+
getEditorSetting(conn, messagereceived);
255255
break;
256256
default:
257257
break;
@@ -1581,13 +1581,12 @@ public void onRemove(String interpreterGroupId, String name, String noteId, Stri
15811581
}
15821582
}
15831583

1584-
private void getEditorSetting(NotebookSocket conn, Notebook notebook, Message fromMessage)
1584+
private void getEditorSetting(NotebookSocket conn, Message fromMessage)
15851585
throws IOException {
15861586
String replName = (String) fromMessage.get("magic");
15871587
String noteId = getOpenNoteId(conn);
1588-
Note note = notebook.getNote(noteId);
15891588
Message resp = new Message(OP.EDITOR_SETTING);
1590-
resp.put("editor", note.getEditorSetting(replName));
1589+
resp.put("editor", notebook().getInterpreterFactory().getEditorSetting(noteId, replName));
15911590
conn.send(serializeMessage(resp));
15921591
return;
15931592
}

zeppelin-zengine/src/main/java/org/apache/zeppelin/interpreter/InterpreterFactory.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
import com.google.common.base.Preconditions;
5252
import com.google.common.collect.ImmutableMap;
53+
import com.google.common.collect.Maps;
5354
import com.google.gson.Gson;
5455
import com.google.gson.GsonBuilder;
5556
import com.google.gson.reflect.TypeToken;
@@ -1279,6 +1280,35 @@ public void setEnv(Map<String, String> env) {
12791280
this.env = env;
12801281
}
12811282

1283+
public Map<String, Object> getEditorSetting(String noteId, String replName) {
1284+
Interpreter intp = getInterpreter(noteId, replName);
1285+
Map<String, Object> editor = Maps.newHashMap(
1286+
ImmutableMap.<String, Object>builder()
1287+
.put("language", "text").build());
1288+
String defaultSettingName = getDefaultInterpreterSetting(noteId).getName();
1289+
String group = StringUtils.EMPTY;
1290+
try {
1291+
List<InterpreterSetting> intpSettings = getInterpreterSettings(noteId);
1292+
for (InterpreterSetting intpSetting : intpSettings) {
1293+
String[] replNameSplit = replName.split("\\.");
1294+
if (replNameSplit.length == 2) {
1295+
group = replNameSplit[0];
1296+
}
1297+
// when replName is 'name' of interpreter
1298+
if (defaultSettingName.equals(intpSetting.getName())) {
1299+
editor = getEditorFromSettingByClassName(intpSetting, intp.getClassName());
1300+
}
1301+
// when replName is 'alias name' of interpreter or 'group' of interpreter
1302+
if (replName.equals(intpSetting.getName()) || group.equals(intpSetting.getName())) {
1303+
editor = getEditorFromSettingByClassName(intpSetting, intp.getClassName());
1304+
break;
1305+
}
1306+
}
1307+
} catch (NullPointerException e) {
1308+
logger.warn("Couldn't get interpreter editor language");
1309+
}
1310+
return editor;
1311+
}
12821312

12831313
private Interpreter getDevInterpreter() {
12841314
if (devInterpreter == null) {

zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.util.concurrent.ScheduledThreadPoolExecutor;
3131
import java.util.concurrent.TimeUnit;
3232

33-
import com.google.common.collect.ImmutableMap;
34-
import com.google.common.collect.Maps;
3533
import com.google.gson.Gson;
3634
import org.apache.commons.lang.StringUtils;
3735
import org.slf4j.Logger;
@@ -652,40 +650,6 @@ public void setInfo(Map<String, Object> info) {
652650
this.info = info;
653651
}
654652

655-
public Interpreter getRepl(String name) {
656-
return factory.getInterpreter(getId(), name);
657-
}
658-
659-
public Map<String, Object> getEditorSetting(String replName) {
660-
Interpreter intp = getRepl(replName);
661-
Map<String, Object> editor = Maps.newHashMap(
662-
ImmutableMap.<String, Object>builder()
663-
.put("language", "text").build());
664-
String defaultSettingName = factory.getDefaultInterpreterSetting(this.getId()).getName();
665-
String group = StringUtils.EMPTY;
666-
try {
667-
List<InterpreterSetting> intpSettings = factory.getInterpreterSettings(this.getId());
668-
for (InterpreterSetting intpSetting : intpSettings) {
669-
String[] replNameSplit = replName.split("\\.");
670-
if (replNameSplit.length == 2) {
671-
group = replNameSplit[0];
672-
}
673-
// when replName is 'name' of interpreter
674-
if (defaultSettingName.equals(intpSetting.getName())) {
675-
editor = factory.getEditorFromSettingByClassName(intpSetting, intp.getClassName());
676-
}
677-
// when replName is 'alias name' of interpreter or 'group' of interpreter
678-
if (replName.equals(intpSetting.getName()) || group.equals(intpSetting.getName())) {
679-
editor = factory.getEditorFromSettingByClassName(intpSetting, intp.getClassName());
680-
break;
681-
}
682-
}
683-
} catch (NullPointerException e) {
684-
logger.warn("Couldn't get interpreter editor language");
685-
}
686-
return editor;
687-
}
688-
689653
@Override
690654
public void beforeStatusChange(Job job, Status before, Status after) {
691655
if (jobListenerFactory != null) {

zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Paragraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static String getScriptBody(String text) {
193193
}
194194

195195
public Interpreter getRepl(String name) {
196-
return note.getRepl(name);
196+
return factory.getInterpreter(note.getId(), name);
197197
}
198198

199199
public Interpreter getCurrentRepl() {

zeppelin-zengine/src/test/java/org/apache/zeppelin/interpreter/InterpreterFactoryTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,42 @@
3434
import org.apache.zeppelin.interpreter.mock.MockInterpreter1;
3535
import org.apache.zeppelin.interpreter.mock.MockInterpreter2;
3636
import org.apache.zeppelin.interpreter.remote.RemoteInterpreter;
37+
import org.apache.zeppelin.notebook.JobListenerFactory;
38+
import org.apache.zeppelin.notebook.Note;
39+
import org.apache.zeppelin.notebook.Notebook;
40+
import org.apache.zeppelin.notebook.repo.NotebookRepo;
41+
import org.apache.zeppelin.notebook.repo.VFSNotebookRepo;
42+
import org.apache.zeppelin.scheduler.SchedulerFactory;
43+
import org.apache.zeppelin.search.SearchService;
3744
import org.junit.After;
3845
import org.junit.Before;
3946
import org.junit.Test;
47+
import org.quartz.SchedulerException;
4048
import org.sonatype.aether.RepositoryException;
4149

4250
import static org.junit.Assert.*;
51+
import static org.mockito.Mockito.mock;
52+
import org.mockito.Mock;
4353

4454
public class InterpreterFactoryTest {
4555

4656
private InterpreterFactory factory;
4757
private File tmpDir;
4858
private ZeppelinConfiguration conf;
4959
private InterpreterContext context;
60+
private Notebook notebook;
61+
private NotebookRepo notebookRepo;
5062
private DependencyResolver depResolver;
63+
private SchedulerFactory schedulerFactory;
64+
@Mock
65+
private JobListenerFactory jobListenerFactory;
5166

5267
@Before
5368
public void setUp() throws Exception {
5469
tmpDir = new File(System.getProperty("java.io.tmpdir")+"/ZeppelinLTest_"+System.currentTimeMillis());
5570
tmpDir.mkdirs();
5671
new File(tmpDir, "conf").mkdirs();
72+
FileUtils.copyDirectory(new File("src/test/resources/interpreter"), new File(tmpDir, "interpreter"));
5773

5874
Map<String, InterpreterProperty> propertiesMockInterpreter1 = new HashMap<String, InterpreterProperty>();
5975
propertiesMockInterpreter1.put("PROPERTY_1", new InterpreterProperty("PROPERTY_1", "", "VALUE_1", "desc"));
@@ -62,11 +78,22 @@ public void setUp() throws Exception {
6278
MockInterpreter2.register("mock2", "org.apache.zeppelin.interpreter.mock.MockInterpreter2");
6379

6480
System.setProperty(ConfVars.ZEPPELIN_HOME.getVarName(), tmpDir.getAbsolutePath());
65-
System.setProperty(ConfVars.ZEPPELIN_INTERPRETERS.getVarName(), "org.apache.zeppelin.interpreter.mock.MockInterpreter1,org.apache.zeppelin.interpreter.mock.MockInterpreter2");
81+
System.setProperty(ConfVars.ZEPPELIN_INTERPRETERS.getVarName(),
82+
"org.apache.zeppelin.interpreter.mock.MockInterpreter1," +
83+
"org.apache.zeppelin.interpreter.mock.MockInterpreter2," +
84+
"org.apache.zeppelin.interpreter.mock.MockInterpreter11");
85+
System.setProperty(ConfVars.ZEPPELIN_INTERPRETER_GROUP_ORDER.getVarName(),
86+
"mock1,mock2,mock11,dev");
6687
conf = new ZeppelinConfiguration();
88+
schedulerFactory = new SchedulerFactory();
6789
depResolver = new DependencyResolver(tmpDir.getAbsolutePath() + "/local-repo");
6890
factory = new InterpreterFactory(conf, new InterpreterOption(false), null, null, null, depResolver);
6991
context = new InterpreterContext("note", "id", "title", "text", null, null, null, null, null, null, null);
92+
93+
SearchService search = mock(SearchService.class);
94+
notebookRepo = new VFSNotebookRepo(conf);
95+
notebook = new Notebook(conf, notebookRepo, schedulerFactory, factory, jobListenerFactory, search,
96+
null, null);
7097
}
7198

7299
@After
@@ -128,7 +155,7 @@ public void testRemoteRepl() throws Exception {
128155
public void testFactoryDefaultList() throws IOException, RepositoryException {
129156
// get default settings
130157
List<String> all = factory.getDefaultInterpreterSettingList();
131-
assertTrue(factory.getRegisteredInterpreterList().size() >= all.size());
158+
assertTrue(factory.get().size() >= all.size());
132159
}
133160

134161
@Test
@@ -196,4 +223,29 @@ public void testInvalidInterpreterSettingName() {
196223
assertEquals("'.' is invalid for InterpreterSetting name.", e.getMessage());
197224
}
198225
}
226+
227+
228+
@Test
229+
public void getEditorSetting() throws IOException, RepositoryException, SchedulerException {
230+
List<String> intpIds = new ArrayList<>();
231+
for(InterpreterSetting intpSetting: factory.get()) {
232+
if (intpSetting.getName().startsWith("mock1")) {
233+
intpIds.add(intpSetting.getId());
234+
}
235+
}
236+
Note note = notebook.createNote(intpIds, null);
237+
238+
// get editor setting from interpreter-setting.json
239+
Map<String, Object> editor = factory.getEditorSetting(note.getId(), "mock11");
240+
assertEquals("java", editor.get("language"));
241+
242+
// when interpreter is not loaded via interpreter-setting.json
243+
// or editor setting doesn't exit
244+
editor = factory.getEditorSetting(note.getId(), "mock1");
245+
assertEquals(null, editor.get("language"));
246+
247+
// when interpreter is not bound to note
248+
editor = factory.getEditorSetting(note.getId(), "mock2");
249+
assertEquals("text", editor.get("language"));
250+
}
199251
}

zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NoteTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.apache.zeppelin.interpreter.Interpreter;
2121
import org.apache.zeppelin.interpreter.InterpreterFactory;
22-
import org.apache.zeppelin.interpreter.InterpreterSetting;
2322
import org.apache.zeppelin.notebook.repo.NotebookRepo;
2423
import org.apache.zeppelin.scheduler.Scheduler;
2524
import org.apache.zeppelin.search.SearchService;
@@ -28,7 +27,6 @@
2827
import org.junit.runner.RunWith;
2928
import org.mockito.ArgumentCaptor;
3029
import org.mockito.Mock;
31-
import org.mockito.Mockito;
3230
import org.mockito.runners.MockitoJUnitRunner;
3331

3432
import static org.junit.Assert.*;

zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,11 @@ public void setUp() throws Exception {
7474
new File(tmpDir, "conf").mkdirs();
7575
notebookDir = new File(tmpDir + "/notebook");
7676
notebookDir.mkdirs();
77-
FileUtils.copyDirectory(new File("src/test/resources/interpreter"), new File(tmpDir, "interpreter"));
7877

7978
System.setProperty(ConfVars.ZEPPELIN_CONF_DIR.getVarName(), tmpDir.toString() + "/conf");
8079
System.setProperty(ConfVars.ZEPPELIN_HOME.getVarName(), tmpDir.getAbsolutePath());
8180
System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_DIR.getVarName(), notebookDir.getAbsolutePath());
82-
System.setProperty(ConfVars.ZEPPELIN_INTERPRETERS.getVarName(),
83-
"org.apache.zeppelin.interpreter.mock.MockInterpreter1," +
84-
"org.apache.zeppelin.interpreter.mock.MockInterpreter2," +
85-
"org.apache.zeppelin.interpreter.mock.MockInterpreter11");
86-
System.setProperty(ConfVars.ZEPPELIN_INTERPRETER_GROUP_ORDER.getVarName(),
87-
"mock1,mock2,mock11,dev");
81+
System.setProperty(ConfVars.ZEPPELIN_INTERPRETERS.getVarName(), "org.apache.zeppelin.interpreter.mock.MockInterpreter1,org.apache.zeppelin.interpreter.mock.MockInterpreter2");
8882

8983
conf = ZeppelinConfiguration.create();
9084

@@ -238,7 +232,7 @@ public void testClearParagraphOutput() throws IOException, SchedulerException{
238232
p1.setText("hello world");
239233
note.run(p1.getId());
240234

241-
while(p1.isTerminated()==false || p1.getResult()==null) Thread.yield();
235+
while(p1.isTerminated() == false || p1.getResult() == null) Thread.yield();
242236
assertEquals("repl1: hello world", p1.getResult().message());
243237

244238
// clear paragraph output/result
@@ -273,7 +267,7 @@ public void testRunAll() throws IOException {
273267
note.runAll();
274268

275269
// wait for finish
276-
while(p3.isTerminated()==false) {
270+
while(p3.isTerminated() == false) {
277271
Thread.yield();
278272
}
279273

@@ -285,7 +279,7 @@ public void testRunAll() throws IOException {
285279
}
286280

287281
@Test
288-
public void testSchedule() throws InterruptedException, IOException{
282+
public void testSchedule() throws InterruptedException, IOException {
289283
// create a note and a paragraph
290284
Note note = notebook.createNote(null);
291285
factory.setInterpreters(note.getId(), factory.getDefaultInterpreterSettingList());
@@ -482,8 +476,8 @@ public void testResourceRemovealOnParagraphNoteRemove() throws IOException {
482476
p2.setText("%mock2 world");
483477

484478
note.runAll();
485-
while(p1.isTerminated()==false || p1.getResult()==null) Thread.yield();
486-
while(p2.isTerminated()==false || p2.getResult()==null) Thread.yield();
479+
while (p1.isTerminated() == false || p1.getResult() == null) Thread.yield();
480+
while (p2.isTerminated() == false || p2.getResult() == null) Thread.yield();
487481

488482
assertEquals(2, ResourcePoolUtils.getAllResources().size());
489483

@@ -622,7 +616,7 @@ public void testPermissions() throws IOException {
622616
assertEquals(notebookAuthorization.isWriter(note.getId(),
623617
new HashSet<String>(Arrays.asList("user2"))), false);
624618
assertEquals(notebookAuthorization.isWriter(note.getId(),
625-
new HashSet<String>(Arrays.asList("user1"))), true);
619+
new HashSet<String>(Arrays.asList("user1"))), true);
626620

627621
// Test clearing of permssions
628622
notebookAuthorization.setReaders(note.getId(), Sets.<String>newHashSet());
@@ -892,30 +886,6 @@ else if(file.isDirectory()){
892886
}
893887
}
894888

895-
@Test
896-
public void getEditorSetting() throws IOException, RepositoryException, SchedulerException {
897-
List<String> intpIds = new ArrayList<>();
898-
for(InterpreterSetting intpSetting: factory.get()) {
899-
if (intpSetting.getName().startsWith("mock1")) {
900-
intpIds.add(intpSetting.getId());
901-
}
902-
}
903-
Note note = notebook.createNote(intpIds, null);
904-
905-
// get editor setting from interpreter-setting.json
906-
Map<String, Object> editor = note.getEditorSetting("mock11");
907-
assertEquals("java", editor.get("language"));
908-
909-
// when interpreter is not loaded via interpreter-setting.json
910-
// or editor setting doesn't exit
911-
editor = note.getEditorSetting("mock1");
912-
assertEquals(null, editor.get("language"));
913-
914-
// when interpreter is not bound to note
915-
editor = note.getEditorSetting("mock2");
916-
assertEquals("text", editor.get("language"));
917-
}
918-
919889
@Override
920890
public ParagraphJobListener getParagraphJobListener(Note note) {
921891
return new ParagraphJobListener(){

0 commit comments

Comments
 (0)