/** * If scripts are initialized, then Platypua.ja system will work in full * manner. Otherwise it will not perform some actions, such as data binding. * * @return True if scripts are fully initialized, false otherwise. */ public static boolean isInitialized() { Space space = getSpace(); return space != null && space.copyObjectFunc != null && space.restoreObjectFunc != null; }
public static CompletionHandler<?, ?> asCompletionHandler(JSObject aOnSuccess, JSObject aOnFailure) { final Space callingSpace = getSpace(); final LocalContext callingContext = getContext(); return new CompletionHandler<Integer, Object>() { @Override public void completed(Integer result, Object attachment) { callingSpace.process(callingContext, () -> { if (aOnSuccess != null) { aOnSuccess.call(null, new Object[]{result}); } }); } @Override public void failed(Throwable exc, Object attachment) { callingSpace.process(callingContext, () -> { if (aOnFailure != null) { aOnFailure.call(null, new Object[]{exc.toString()}); } }); } }; }
public static void initTasks(ExecutorService aExecutor) { class TasksExecutor implements Consumer<Runnable>, Function<Void, ExecutorService> { @Override public void accept(Runnable aTask) { aExecutor.submit(aTask); } @Override public ExecutorService apply(Void v) { return aExecutor; } } initTasks(new TasksExecutor()); }
public void loaded() { LocalContext oldContext = getContext(); setContext(context); try { onLoad.accept(null); } finally { setContext(oldContext); } }
protected void bind() { if (data != null && Scripts.isInitialized()) { boundToData = Scripts.getSpace().listen(data, "length", new AbstractJSObject() { @Override public Object call(Object thiz, Object... args) { enqueueElementsChanged(); return null; } }); } }
public void send(String from, String to, String subject, String body, JSObject aOnSuccess, JSObject aOnFailure) throws NamingException, AddressException, MessagingException { InitialContext initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup(lookup); Session session = (Session) envCtx.lookup(sessionName); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(MimeMessage.RecipientType.TO, to); message.setSubject(subject); message.setContent(body, contentType); Scripts.Space callingSpace = Scripts.getSpace(); Scripts.LocalContext callingContext = Scripts.getContext(); callingContext.incAsyncsCount(); Scripts.startBIO(() -> { try { Transport.send(message); if (aOnSuccess != null) { callingSpace.process(callingContext, () -> { aOnSuccess.call(null, new Object[]{}); }); } } catch (MessagingException ex) { Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex); if (aOnFailure != null) { callingSpace.process(callingContext, () -> { aOnFailure.call(null, new Object[]{ex}); }); } } }); }
public static void offerTask(Runnable aTask) { assert tasks != null : "Scripts tasks are not initialized"; if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } tasks.accept(aTask); }
GeneralResourceProvider.registerDrivers(); config.datasourcesArgs.registerDatasources(); Scripts.initBIO(config.threadsArgs.getMaxServicesTreads()); Scripts.initTasks((Runnable aTask) -> { EventQueue.invokeLater(aTask); }); Scripts.setOnlySpace(Scripts.createSpace()); } else { throw new IllegalArgumentException("Application url is missing. url is a required parameter.");
Scripts.initBIO(threadsConfig.getMaxServicesTreads()); serverProcessor.allowCoreThreadTimeOut(true); Scripts.initTasks(serverProcessor); serverCoreDbClient = new ScriptedDatabasesClient(defDatasource, indexer, true, tasksScanner.getValidators(), threadsConfig.getMaxJdbcTreads()); QueriesProxy<SqlQuery> queries = new LocalQueriesProxy(serverCoreDbClient, indexer);
public void process(Scripts.LocalContext context, Runnable aTask) { Runnable taskWrapper = () -> { setContext(context); try { setSpace(Space.this); try { aTask.run(); } finally { setSpace(null); setContext(null); offerTask(() -> {
public static boolean isValidJsIdentifier(final String aName) { if (aName != null && !aName.trim().isEmpty()) { try { FunctionNode astRoot = parseJs(String.format("function %s() {}", aName)).getAst(); return astRoot != null && !astRoot.getBody().getStatements().isEmpty(); } catch (Exception ex) { return false; } } return false; }
public void failed(Exception ex) { LocalContext oldContext = getContext(); setContext(context); try { onError.accept(ex); } finally { setContext(oldContext); } } }
protected void bindList() { if (displayList != null && Scripts.isInitialized()) { boundToList = Scripts.getSpace().listen(displayList, "length", new AbstractJSObject() { @Override public Object call(Object thiz, Object... args) { enqueueListChanged(); return null; } }); } }
public void process(Runnable aTask) { Scripts.LocalContext context = Scripts.getContext(); process(context, aTask); }
public void walk() { parsedSource = Scripts.parseJs(source); SymbolsResolver outerDefinedFinder = new SymbolsResolver(); parsedSource.getAst().accept(outerDefinedFinder);
public static void run(String aModuleName) { Scripts.getSpace().process(() -> { try { ScriptedResource._require(new String[]{aModuleName}, null, Scripts.getSpace(), new HashSet<>(), (Void v) -> { Logger.getLogger(PlatypusClientApplication.class.getName()).log(Level.INFO, "Platypus application started."); }, (Exception ex) -> { Logger.getLogger(PlatypusClientApplication.class.getName()).log(Level.SEVERE, null, ex); }); } catch (Exception ex) { Logger.getLogger(PlatypusClientApplication.class.getName()).log(Level.SEVERE, null, ex); } }); }
/** * For external API. * * @param aWrapped * @return BiConsumer<Object, Throwable> Object - result instance, * Throwable - exception raised while an operation. */ public static BiConsumer<Object, Throwable> inContext(BiConsumer<Object, Throwable> aWrapped) { Space callingSpace = getSpace(); LocalContext callingContext = getContext(); return (Object aResult, Throwable aReason) -> { callingSpace.process(callingContext, () -> { aWrapped.accept(aResult, aReason); }); }; }
private void startJdbcTask(Runnable aTask) { Scripts.LocalContext context = Scripts.getContext(); if (context != null) { context.incAsyncsCount(); } jdbcProcessor.submit(() -> { Scripts.setContext(context); try { aTask.run(); } finally { Scripts.setContext(null); } }); }
@Override public void handle(Session aSession, Consumer<CredentialRequest.Response> onSuccess, Consumer<Exception> onFailure) { if (onSuccess != null) { onSuccess.accept(new CredentialRequest.Response(((PlatypusPrincipal) Scripts.getContext().getPrincipal()).getName())); } } }