InterpreterFactory implements InterpreterFactoryInterface { @Override public Interpreter getInterpreter(String replName, ExecutionContext executionContext) throws InterpreterNotFoundException { if (StringUtils.isBlank(replName)) { InterpreterSetting defaultSetting = interpreterSettingManager.getByName(executionContext.getDefaultInterpreterGroup()); return defaultSetting.getDefaultInterpreter(executionContext); } String[] replNameSplits = replName.split("\\."); if (replNameSplits.length == 2) { String group = replNameSplits[0]; String name = replNameSplits[1]; InterpreterSetting setting = interpreterSettingManager.getByName(group); if (null != setting) { Interpreter interpreter = setting.getInterpreter(executionContext, name); if (null != interpreter) { return interpreter; } throw new InterpreterNotFoundException("No such interpreter: " + replName); } throw new InterpreterNotFoundException("No interpreter setting named: " + group); } else if (replNameSplits.length == 1){ InterpreterSetting setting = interpreterSettingManager.getByName(executionContext.getDefaultInterpreterGroup()); if (setting != null) { Interpreter interpreter = setting.getInterpreter(executionContext, replName); if (null != interpreter) { return interpreter; } } setting = interpreterSettingManager.getByName(replName); if (null != setting) { return setting.getDefaultInterpreter(executionContext); } } throw new InterpreterNotFoundException("No such interpreter: " + replName); } @Inject  InterpreterFactory(InterpreterSettingManager interpreterSettingManager); @Override Interpreter getInterpreter(String replName,
                                    ExecutionContext executionContext);  }
@Test public void testGetFactory() throws InterpreterException { assertTrue(interpreterFactory.getInterpreter("", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()) instanceof RemoteInterpreter); RemoteInterpreter remoteInterpreter = (RemoteInterpreter) interpreterFactory.getInterpreter("", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); assertEquals(EchoInterpreter.class.getName(), remoteInterpreter.getClassName()); assertTrue(interpreterFactory.getInterpreter("double_echo", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()) instanceof RemoteInterpreter); remoteInterpreter = (RemoteInterpreter) interpreterFactory.getInterpreter("double_echo", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); assertEquals(DoubleEchoInterpreter.class.getName(), remoteInterpreter.getClassName()); assertTrue(interpreterFactory.getInterpreter("test", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()) instanceof RemoteInterpreter); remoteInterpreter = (RemoteInterpreter) interpreterFactory.getInterpreter("test", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); assertEquals(EchoInterpreter.class.getName(), remoteInterpreter.getClassName()); assertTrue(interpreterFactory.getInterpreter("test2", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()) instanceof RemoteInterpreter); remoteInterpreter = (RemoteInterpreter) interpreterFactory.getInterpreter("test2", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); assertEquals(EchoInterpreter.class.getName(), remoteInterpreter.getClassName()); assertTrue(interpreterFactory.getInterpreter("test2.double_echo", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()) instanceof RemoteInterpreter); remoteInterpreter = (RemoteInterpreter) interpreterFactory.getInterpreter("test2.double_echo", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); assertEquals(DoubleEchoInterpreter.class.getName(), remoteInterpreter.getClassName()); }
@Test public void testUnknownRepl1() { try { interpreterFactory.getInterpreter("test.unknown_repl", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); fail("should fail due to no such interpreter"); } catch (InterpreterNotFoundException e) { assertEquals("No such interpreter: test.unknown_repl", e.getMessage()); } }
@Test public void testUnknownRepl2() { try { interpreterFactory.getInterpreter("unknown_repl", new ExecutionContextBuilder().setUser("user1").setNoteId("note1").setDefaultInterpreterGroup("test").createExecutionContext()); fail("should fail due to no such interpreter"); } catch (InterpreterNotFoundException e) { assertEquals("No such interpreter: unknown_repl", e.getMessage()); } }