public static void offerTask(Runnable aTask) { assert tasks != null : "Scripts tasks are not initialized"; if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } tasks.accept(aTask); }
public void process(Runnable aTask) { Scripts.LocalContext context = Scripts.getContext(); process(context, aTask); }
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 void loaded() { LocalContext oldContext = getContext(); setContext(context); try { onLoad.accept(null); } finally { setContext(oldContext); } }
public void failed(Exception ex) { LocalContext oldContext = getContext(); setContext(context); try { onError.accept(ex); } finally { setContext(oldContext); } } }
/** * 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); } }); }
public static void startBIO(Runnable aBioTask) { LocalContext context = getContext(); if (context != null) { context.incAsyncsCount(); } bio.submit(() -> { setContext(context); try { aBioTask.run(); } finally { 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())); } } }
@Override public <R extends Response> void enqueueRequest(Request aRequest, Scripts.Space aSpace, Consumer<R> onSuccess, Consumer<Exception> onFailure) { if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } Attempts attemps = new Attempts(); Consumer<Response> responseHandler = (Response response) -> { if (response instanceof ExceptionResponse) { if (onFailure != null) { Exception cause = handleErrorResponse((ExceptionResponse) response, aSpace); onFailure.accept(cause); } } else if (onSuccess != null) { if (aRequest instanceof LogoutRequest) { credentials = null; sessionTicket = null; } onSuccess.accept((R) response); } }; retry(aRequest, aSpace, responseHandler, attemps); }
public void schedule(JSObject aJsTask, long aTimeout) { Scripts.LocalContext context = Scripts.getContext(); bio.submit(() -> { try { Thread.sleep(aTimeout); Scripts.setContext(context); try { process(() -> { aJsTask.call(null, new Object[]{}); }); } finally { Scripts.setContext(null); } } catch (InterruptedException ex) { Logger.getLogger(Scripts.class.getName()).log(Level.SEVERE, null, ex); } }); }
@Override public Object callServerModuleMethod(String aModuleName, String aMethodName, Scripts.Space aSpace, JSObject onSuccess, JSObject onFailure, Object... aArguments) throws Exception { if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } assert Scripts.getSpace() == aSpace; serverCore.executeMethod(aModuleName, aMethodName, aArguments, false, onSuccess != null ? (Object aResult) -> { onSuccess.call(null, new Object[]{aSpace.toJs(aResult)}); } : null, (Exception ex) -> { if (onFailure != null) { onFailure.call(null, new Object[]{ex instanceof JsObjectException ? ((JsObjectException) ex).getData() : ex.toString()}); } else { Logger.getLogger(LocalServerModulesProxy.class.getName()).log(Level.SEVERE, ex.toString()); } }); return null;// Only asynchronous form is allowed. }
@Override public String preparationContext() throws Exception { Scripts.LocalContext context = Scripts.getContext(); if (context != null && context.getPrincipal() != null) { return ((PlatypusPrincipal) context.getPrincipal()).getContext(); } else { 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}); }); } } }); }
/** * Checks module roles. * * @param anAllowedRoles * @param aSubjectName * @throws AccessControlException */ public static void checkPrincipalPermission(Set<String> anAllowedRoles, String aSubjectName) throws AccessControlException { if (anAllowedRoles != null && !anAllowedRoles.isEmpty()) { try { PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (principal == null || !principal.hasAnyRole(anAllowedRoles)) { throw new AccessControlException(String.format("Access denied to %s for '%s'.",//NOI18N aSubjectName, principal != null ? principal.getName() : null), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null); } } catch (Exception ex) { if (ex instanceof AccessControlException) { throw ex; } else { throw new AccessControlException(ex.getMessage()); } } } } }
Scripts.LocalContext context = Scripts.getContext(); try { return selector.call();
private <R extends Response> void retry(Request aRequest, Scripts.Space aSpace, Consumer<Response> responseHandler, Attempts attemps) { Credentials sentCreds = credentials; Scripts.LocalContext context = Scripts.getContext(); RequestEnvelope requestEnv = new RequestEnvelope(aRequest, credentials != null ? credentials.userName : null, credentials != null ? credentials.password : null, sessionTicket, (Response response) -> { Scripts.setContext(context);
private void checkModuleRoles(String aModuleName) throws Exception { ScriptDocument.ModuleDocument moduleDoc = serverCore.lookupModuleDocument(aModuleName); if (moduleDoc != null) { Set<String> rolesAllowed = moduleDoc.getAllowedRoles(); PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (rolesAllowed != null && !principal.hasAnyRole(rolesAllowed)) { throw new AccessControlException(String.format(ACCESS_DENIED_MSG, aModuleName, getRequest().getModuleName(), principal.getName()), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null); } } } }
((LocalQueriesProxy) serverCore.getQueries()).getQuery(change.entityName, Scripts.getSpace(), (SqlQuery aQuery) -> { if (aQuery.isPublicAccess()) { AccessControlException aex = checkWritePrincipalPermission((PlatypusPrincipal) Scripts.getContext().getPrincipal(), change.entityName, aQuery.getWriteRoles()); if (aex != null) { process.complete(null, null, aex, null);
PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (rolesAllowed != null && !principal.hasAnyRole(rolesAllowed)) { throw new AccessControlException(String.format(ACCESS_DENIED_MSG, query.getEntityName(), principal.getName()), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null);