Search in sources :

Example 1 with Execution

use of ratpack.exec.Execution in project ratpack by ratpack.

the class HystrixRegistryBackedRequestVariable method get.

@Override
@SuppressWarnings("unchecked")
public T get() {
    Execution execution = getExecution();
    HystrixCommandCache commandCache = execution.maybeGet(HystrixCommandCache.class).orElseGet(() -> {
        HystrixCommandCache cache = new HystrixCommandCache();
        execution.add(cache);
        return cache;
    });
    Object command = commandCache.get(this);
    if (command == null) {
        command = rv.initialValue();
        commandCache.putIfAbsent(this, command);
    }
    return (T) command;
}
Also used : Execution(ratpack.exec.Execution)

Example 2 with Execution

use of ratpack.exec.Execution in project ratpack by ratpack.

the class Transaction method unbind.

/**
   * Unbinds this transaction from the current execution.
   * <p>
   * If the transaction is not bound, this method is effectively a noop and returns false.
   *
   * @return whether this transaction was actually bound
   * @throws TransactionException if a different transaction is bound to the execution
   * @see #bind()
   */
default default boolean unbind() {
    Execution execution = Execution.current();
    Optional<Transaction> transaction = execution.maybeGet(Transaction.class);
    if (transaction.isPresent() && transaction.get() == this) {
        execution.remove(Transaction.class);
        return true;
    } else {
        return false;
    }
}
Also used : Execution(ratpack.exec.Execution) DefaultTransaction(ratpack.jdbctx.internal.DefaultTransaction)

Example 3 with Execution

use of ratpack.exec.Execution in project ratpack by ratpack.

the class DefaultContext method start.

public static void start(EventLoop eventLoop, final RequestConstants requestConstants, Registry registry, Handler[] handlers, Action<? super Execution> onComplete) {
    ChainIndex index = new ChainIndex(handlers, registry, true);
    requestConstants.indexes.push(index);
    DefaultContext context = new DefaultContext(requestConstants);
    requestConstants.context = context;
    context.pathBindings = new PathBindingStorage(new RootPathBinding(requestConstants.request.getPath()));
    requestConstants.applicationConstants.execController.fork().onError(throwable -> requestConstants.context.error(throwable instanceof HandlerException ? throwable.getCause() : throwable)).onComplete(onComplete).register(s -> s.add(Context.TYPE, context).add(Request.TYPE, requestConstants.request).add(Response.TYPE, requestConstants.response).add(PathBindingStorage.TYPE, context.pathBindings).addLazy(RequestId.TYPE, () -> registry.get(RequestId.Generator.TYPE).generate(requestConstants.request))).eventLoop(eventLoop).onStart(e -> DefaultRequest.setDelegateRegistry(requestConstants.request, e)).start(e -> {
        requestConstants.execution = e;
        context.joinedRegistry = new ContextRegistry(context).join(requestConstants.execution);
        context.next();
    });
}
Also used : RootPathBinding(ratpack.path.internal.RootPathBinding) ClientErrorHandler(ratpack.error.ClientErrorHandler) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) java.util(java.util) Types(ratpack.util.Types) FileSystemBinding(ratpack.file.FileSystemBinding) RootPathBinding(ratpack.path.internal.RootPathBinding) Execution(ratpack.exec.Execution) ResponseTransmitter(ratpack.file.internal.ResponseTransmitter) Status(ratpack.http.Status) LoggerFactory(org.slf4j.LoggerFactory) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) TypeToken(com.google.common.reflect.TypeToken) Parser(ratpack.parse.Parser) TypedData(ratpack.http.TypedData) NoSuchParserException(ratpack.parse.NoSuchParserException) TypeCaching(ratpack.registry.internal.TypeCaching) Request(ratpack.http.Request) Lists(com.google.common.collect.Lists) PathBindingStorage(ratpack.path.internal.PathBindingStorage) NoSuchRendererException(ratpack.render.NoSuchRendererException) ServerConfig(ratpack.server.ServerConfig) Registry(ratpack.registry.Registry) DefaultRequest(ratpack.http.internal.DefaultRequest) Parse(ratpack.parse.Parse) ratpack.handling(ratpack.handling) Path(java.nio.file.Path) HttpHeaderConstants(ratpack.http.internal.HttpHeaderConstants) Function(ratpack.func.Function) ServerErrorHandler(ratpack.error.ServerErrorHandler) RenderController(ratpack.render.internal.RenderController) Logger(org.slf4j.Logger) NotInRegistryException(ratpack.registry.NotInRegistryException) Promise(ratpack.exec.Promise) EventLoop(io.netty.channel.EventLoop) Block(ratpack.func.Block) Instant(java.time.Instant) Maps(com.google.common.collect.Maps) InvalidPathEncodingException(ratpack.path.InvalidPathEncodingException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) IF_MODIFIED_SINCE(io.netty.handler.codec.http.HttpHeaderNames.IF_MODIFIED_SINCE) Channel(io.netty.channel.Channel) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) Response(ratpack.http.Response) Action(ratpack.func.Action) Exceptions(ratpack.util.Exceptions) PathBinding(ratpack.path.PathBinding) ExecController(ratpack.exec.ExecController) PathBindingStorage(ratpack.path.internal.PathBindingStorage)

Example 4 with Execution

use of ratpack.exec.Execution in project ratpack by ratpack.

the class ExecutionBasedScope method getScopedObjectMap.

private Map<Key<?>, Object> getScopedObjectMap(Key<?> key) {
    try {
        Execution execution = Execution.current();
        if (!inScope(execution)) {
            throw new OutOfScopeException("Cannot access " + key + " outside of " + name);
        }
        return execution.maybeGet(storeType).orElseGet(() -> {
            S store = createStore();
            execution.add(storeType, store);
            return store;
        });
    } catch (UnmanagedThreadException e) {
        throw new OutOfScopeException("Cannot access " + key + " outside of " + name);
    }
}
Also used : Execution(ratpack.exec.Execution) UnmanagedThreadException(ratpack.exec.UnmanagedThreadException)

Example 5 with Execution

use of ratpack.exec.Execution in project ratpack by ratpack.

the class Transaction method bind.

/**
   * Binds this transaction to the current execution.
   * <p>
   * The instance is added to the current execution's registry.
   * <p>
   * It is typically not necessary to call this directly.
   * Transactions default to “auto binding”.
   * That is, this method is called implicitly when the transaction starts.
   *
   * @return {@code this}
   * @throws TransactionException if a different transaction is bound to the execution
   */
default default Transaction bind() throws TransactionException {
    Execution execution = Execution.current();
    execution.maybeGet(Transaction.class).ifPresent(t -> {
        if (t != this) {
            throw new TransactionException("A transaction is already bound");
        }
    });
    execution.add(Transaction.class, this);
    return this;
}
Also used : Execution(ratpack.exec.Execution) DefaultTransaction(ratpack.jdbctx.internal.DefaultTransaction)

Aggregations

Execution (ratpack.exec.Execution)5 DefaultTransaction (ratpack.jdbctx.internal.DefaultTransaction)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 TypeToken (com.google.common.reflect.TypeToken)1 Channel (io.netty.channel.Channel)1 EventLoop (io.netty.channel.EventLoop)1 IF_MODIFIED_SINCE (io.netty.handler.codec.http.HttpHeaderNames.IF_MODIFIED_SINCE)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 java.util (java.util)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ClientErrorHandler (ratpack.error.ClientErrorHandler)1 ServerErrorHandler (ratpack.error.ServerErrorHandler)1 ExecController (ratpack.exec.ExecController)1 Promise (ratpack.exec.Promise)1 UnmanagedThreadException (ratpack.exec.UnmanagedThreadException)1