/** Clear all paragraph output of note */ public void clearAllParagraphOutput() { synchronized (paragraphs) { for (Paragraph p : paragraphs) { p.setReturn(null, null); } } }
private InterpreterContext getInterpreterContext() { AngularObjectRegistry registry = null; if (!getNoteReplLoader().getInterpreterSettings().isEmpty()) { InterpreterSetting intpGroup = getNoteReplLoader().getInterpreterSettings().get(0); registry = intpGroup.getInterpreterGroup().getAngularObjectRegistry(); } List<InterpreterContextRunner> runners = new LinkedList<InterpreterContextRunner>(); for (Paragraph p : note.getParagraphs()) { runners.add(new ParagraphRunner(note, note.id(), p.getId())); } InterpreterContext interpreterContext = new InterpreterContext( note.id(), getId(), this.getTitle(), this.getText(), this.getConfig(), this.settings, registry, runners); return interpreterContext; }
void setInterpreterFactory(InterpreterFactory factory) { this.factory = factory; synchronized (paragraphs) { for (Paragraph p : paragraphs) { p.setInterpreterFactory(factory); } } }
private InterpreterContext getInterpreterContext() { AngularObjectRegistry registry = null; ResourcePool resourcePool = null; if (!getNoteReplLoader().getInterpreterSettings().isEmpty()) { InterpreterSetting intpGroup = getNoteReplLoader().getInterpreterSettings().get(0); registry = intpGroup.getInterpreterGroup().getAngularObjectRegistry(); resourcePool = intpGroup.getInterpreterGroup().getResourcePool(); } List<InterpreterContextRunner> runners = new LinkedList<InterpreterContextRunner>(); for (Paragraph p : note.getParagraphs()) { runners.add(new ParagraphRunner(note, note.id(), p.getId())); } final Paragraph self = this; InterpreterContext interpreterContext = new InterpreterContext( note.id(), getId(), this.getTitle(), this.getText(), this.getConfig(), this.settings, registry, resourcePool, runners, new InterpreterOutput( new InterpreterOutputListener() { @Override public void onAppend(InterpreterOutput out, byte[] line) { updateParagraphResult(out); ((ParagraphJobListener) getListener()) .onOutputAppend(self, out, new String(line)); } @Override public void onUpdate(InterpreterOutput out, byte[] output) { updateParagraphResult(out); ((ParagraphJobListener) getListener()) .onOutputUpdate(self, out, new String(output)); } private void updateParagraphResult(InterpreterOutput out) { // update paragraph result Throwable t = null; String message = null; try { message = new String(out.toByteArray()); } catch (IOException e) { logger().error(e.getMessage(), e); t = e; } setReturn(new InterpreterResult(Code.SUCCESS, out.getType(), message), t); } })); return interpreterContext; }
public Map<String, String> generateSingleParagraphInfo(String paragraphId) { synchronized (paragraphs) { for (Paragraph p : paragraphs) { if (p.getId().equals(paragraphId)) { return populateParagraphInfo(p); } } return new HashMap<>(); } }
public Paragraph getParagraph(String paragraphId) { synchronized (paragraphs) { for (Paragraph p : paragraphs) { if (p.getId().equals(paragraphId)) { return p; } } } return null; }
/** Check whether all paragraphs belongs to this note has terminated */ boolean isTerminated() { synchronized (paragraphs) { for (Paragraph p : paragraphs) { if (!p.isTerminated()) { return false; } } } return true; }
/** * Clear paragraph output by id. * * @param paragraphId ID of paragraph * @return Paragraph */ public Paragraph clearParagraphOutput(String paragraphId) { synchronized (paragraphs) { for (Paragraph p : paragraphs) { if (p.getId().equals(paragraphId)) { p.setReturn(null, null); return p; } } } return null; }
/** * Insert paragraph in given index. * * @param index index of paragraphs */ public Paragraph insertParagraph(int index, AuthenticationInfo authenticationInfo) { Paragraph p = new Paragraph(this, this, factory); p.setAuthenticationInfo(authenticationInfo); setParagraphMagic(p, index); synchronized (paragraphs) { paragraphs.add(index, p); } if (noteEventListener != null) { noteEventListener.onParagraphCreate(p); } return p; }
/** Run all paragraphs sequentially. */ public synchronized void runAll() { String cronExecutingUser = (String) getConfig().get("cronExecutingUser"); if (null == cronExecutingUser) { cronExecutingUser = "******"; } for (Paragraph p : getParagraphs()) { if (!p.isEnabled()) { continue; } AuthenticationInfo authenticationInfo = new AuthenticationInfo(); authenticationInfo.setUser(cronExecutingUser); p.setAuthenticationInfo(authenticationInfo); run(p.getId()); } }
/** * Return new note for specific user. this inserts and replaces user paragraph which doesn't * exists in original paragraph * * @param user specific user * @return new Note for the user */ public Note getUserNote(String user) { Note newNote = new Note(); newNote.id = getId(); newNote.config = getConfig(); newNote.angularObjects = getAngularObjects(); Paragraph newParagraph; for (Paragraph p : paragraphs) { newParagraph = p.getUserParagraph(user); if (null == newParagraph) { newParagraph = p.cloneParagraphForUser(user); } newNote.paragraphs.add(newParagraph); } return newNote; }
/** * Remove paragraph by id. * * @param paragraphId ID of paragraph * @return a paragraph that was deleted, or <code>null</code> otherwise */ public Paragraph removeParagraph(String user, String paragraphId) { removeAllAngularObjectInParagraph(user, paragraphId); ResourcePoolUtils.removeResourcesBelongsToParagraph(getId(), paragraphId); synchronized (paragraphs) { Iterator<Paragraph> i = paragraphs.iterator(); while (i.hasNext()) { Paragraph p = i.next(); if (p.getId().equals(paragraphId)) { index.deleteIndexDoc(this, p); i.remove(); if (noteEventListener != null) { noteEventListener.onParagraphRemove(p); } return p; } } } return null; }
private InterpreterContext getInterpreterContext(InterpreterOutput output) { AngularObjectRegistry registry = null; ResourcePool resourcePool = null; if (!factory.getInterpreterSettings(note.getId()).isEmpty()) { InterpreterSetting intpGroup = factory.getInterpreterSettings(note.getId()).get(0); registry = intpGroup.getInterpreterGroup(getUser(), note.getId()).getAngularObjectRegistry(); resourcePool = intpGroup.getInterpreterGroup(getUser(), note.getId()).getResourcePool(); } List<InterpreterContextRunner> runners = new LinkedList<>(); for (Paragraph p : note.getParagraphs()) { runners.add(new ParagraphRunner(note, note.getId(), p.getId())); } final Paragraph self = this; Credentials credentials = note.getCredentials(); if (authenticationInfo != null) { UserCredentials userCredentials = credentials.getUserCredentials(authenticationInfo.getUser()); authenticationInfo.setUserCredentials(userCredentials); } InterpreterContext interpreterContext = new InterpreterContext( note.getId(), getId(), getRequiredReplName(), this.getTitle(), this.getText(), this.getAuthenticationInfo(), this.getConfig(), this.settings, registry, resourcePool, runners, output); return interpreterContext; }
private void setParagraphMagic(Paragraph p, int index) { if (paragraphs.size() > 0) { String magic; if (index == 0) { magic = paragraphs.get(0).getMagic(); } else { magic = paragraphs.get(index - 1).getMagic(); } if (StringUtils.isNotEmpty(magic)) { p.setText(magic + "\n"); } } }
private Map<String, String> populateParagraphInfo(Paragraph p) { Map<String, String> info = new HashMap<>(); info.put("id", p.getId()); info.put("status", p.getStatus().toString()); if (p.getDateStarted() != null) { info.put("started", p.getDateStarted().toString()); } if (p.getDateFinished() != null) { info.put("finished", p.getDateFinished().toString()); } if (p.getStatus().isRunning()) { info.put("progress", String.valueOf(p.progress())); } else { info.put("progress", String.valueOf(100)); } return info; }
public void initializeJobListenerForParagraph(Paragraph paragraph) { final Note paragraphNote = paragraph.getNote(); if (paragraphNote.getId().equals(this.getId())) { throw new IllegalArgumentException( format( "The paragraph %s from note %s " + "does not belong to note %s", paragraph.getId(), paragraphNote.getId(), this.getId())); } boolean foundParagraph = false; for (Paragraph ownParagraph : paragraphs) { if (paragraph.getId().equals(ownParagraph.getId())) { paragraph.setListener(this.jobListenerFactory.getParagraphJobListener(this)); foundParagraph = true; } } if (!foundParagraph) { throw new IllegalArgumentException( format( "Cannot find paragraph %s " + "from note %s", paragraph.getId(), paragraphNote.getId())); } }
public List<InterpreterCompletion> completion(String paragraphId, String buffer, int cursor) { Paragraph p = getParagraph(paragraphId); p.setListener(jobListenerFactory.getParagraphJobListener(this)); return p.completion(buffer, cursor); }
/** * Run a single paragraph. * * @param paragraphId ID of paragraph */ public void run(String paragraphId) { Paragraph p = getParagraph(paragraphId); if (p.isBlankParagraph()) { logger.info("skip to run blank paragraph. {}", p.getId()); return; } p.setListener(jobListenerFactory.getParagraphJobListener(this)); String requiredReplName = p.getRequiredReplName(); Interpreter intp = factory.getInterpreter(p.getUser(), getId(), requiredReplName); if (intp == null) { String intpExceptionMsg = p.getJobName() + "'s Interpreter " + requiredReplName + " not found"; InterpreterException intpException = new InterpreterException(intpExceptionMsg); InterpreterResult intpResult = new InterpreterResult(InterpreterResult.Code.ERROR, intpException.getMessage()); p.setReturn(intpResult, intpException); p.setStatus(Job.Status.ERROR); throw intpException; } if (p.getConfig().get("enabled") == null || (Boolean) p.getConfig().get("enabled")) { p.setAuthenticationInfo(p.getAuthenticationInfo()); intp.getScheduler().submit(p); } }
/** * Clone paragraph and add it to note. * * @param srcParagraph source paragraph */ void addCloneParagraph(Paragraph srcParagraph) { // Keep paragraph original ID final Paragraph newParagraph = new Paragraph(srcParagraph.getId(), this, this, factory); Map<String, Object> config = new HashMap<>(srcParagraph.getConfig()); Map<String, Object> param = new HashMap<>(srcParagraph.settings.getParams()); Map<String, Input> form = new HashMap<>(srcParagraph.settings.getForms()); newParagraph.setConfig(config); newParagraph.settings.setParams(param); newParagraph.settings.setForms(form); newParagraph.setText(srcParagraph.getText()); newParagraph.setTitle(srcParagraph.getTitle()); try { Gson gson = new Gson(); String resultJson = gson.toJson(srcParagraph.getReturn()); InterpreterResult result = gson.fromJson(resultJson, InterpreterResult.class); newParagraph.setReturn(result, null); } catch (Exception e) { // 'result' part of Note consists of exception, instead of actual interpreter results logger.warn( "Paragraph " + srcParagraph.getId() + " has a result with exception. " + e.getMessage()); } synchronized (paragraphs) { paragraphs.add(newParagraph); } if (noteEventListener != null) { noteEventListener.onParagraphCreate(newParagraph); } }