Search in sources :

Example 1 with InstanceHandle

use of io.quarkus.arc.InstanceHandle in project quarkus by quarkusio.

the class HttpSessionContext method get.

@SuppressWarnings("unchecked")
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    HttpServletRequest request = servletRequest();
    if (request == null) {
        throw new ContextNotActiveException();
    }
    InjectableBean<T> bean = (InjectableBean<T>) contextual;
    ComputingCache<Key, ContextInstanceHandle<?>> contextualInstances = getContextualInstances(request);
    if (creationalContext != null) {
        return (T) contextualInstances.getValue(new Key(creationalContext, bean.getIdentifier())).get();
    } else {
        InstanceHandle<T> handle = (InstanceHandle<T>) contextualInstances.getValueIfPresent(new Key(null, bean.getIdentifier()));
        return handle != null ? handle.get() : null;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) InjectableBean(io.quarkus.arc.InjectableBean) ContextInstanceHandle(io.quarkus.arc.ContextInstanceHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) ContextInstanceHandle(io.quarkus.arc.ContextInstanceHandle)

Example 2 with InstanceHandle

use of io.quarkus.arc.InstanceHandle in project quarkus by quarkusio.

the class MicroProfileRestClientEnricher method forClass.

@Override
public void forClass(MethodCreator constructor, AssignableResultHandle webTargetBase, ClassInfo interfaceClass, IndexView index) {
    ResultHandle clientHeadersFactory = null;
    AnnotationInstance registerClientHeaders = interfaceClass.classAnnotation(REGISTER_CLIENT_HEADERS);
    boolean useDefaultHeaders = true;
    if (registerClientHeaders != null) {
        String headersFactoryClass = registerClientHeaders.valueWithDefault(index).asClass().name().toString();
        if (!headersFactoryClass.equals(DEFAULT_HEADERS_FACTORY)) {
            // Arc.container().instance(...).get():
            ResultHandle containerHandle = constructor.invokeStaticMethod(MethodDescriptor.ofMethod(Arc.class, "container", ArcContainer.class));
            ResultHandle instanceHandle = constructor.invokeInterfaceMethod(MethodDescriptor.ofMethod(ArcContainer.class, "instance", InstanceHandle.class, Class.class, Annotation[].class), containerHandle, constructor.loadClassFromTCCL(headersFactoryClass), constructor.newArray(Annotation.class, 0));
            clientHeadersFactory = constructor.invokeInterfaceMethod(MethodDescriptor.ofMethod(InstanceHandle.class, "get", Object.class), instanceHandle);
            useDefaultHeaders = false;
        }
    }
    if (useDefaultHeaders) {
        clientHeadersFactory = constructor.newInstance(MethodDescriptor.ofConstructor(DEFAULT_HEADERS_FACTORY));
    }
    ResultHandle restClientFilter = constructor.newInstance(MethodDescriptor.ofConstructor(MicroProfileRestClientRequestFilter.class, ClientHeadersFactory.class), clientHeadersFactory);
    constructor.assign(webTargetBase, constructor.invokeInterfaceMethod(MethodDescriptor.ofMethod(Configurable.class, "register", Configurable.class, Object.class), webTargetBase, restClientFilter));
}
Also used : Arc(io.quarkus.arc.Arc) ArcContainer(io.quarkus.arc.ArcContainer) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) ClientHeadersFactory(org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory) InstanceHandle(io.quarkus.arc.InstanceHandle) MicroProfileRestClientRequestFilter(io.quarkus.rest.client.reactive.runtime.MicroProfileRestClientRequestFilter) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Annotation(java.lang.annotation.Annotation)

Example 3 with InstanceHandle

use of io.quarkus.arc.InstanceHandle in project quarkus by quarkusio.

the class MongoHealthCheck method configure.

public void configure(MongodbConfig config) {
    Iterable<InstanceHandle<MongoClient>> handle = Arc.container().select(MongoClient.class, Any.Literal.INSTANCE).handles();
    Iterable<InstanceHandle<ReactiveMongoClient>> reactiveHandlers = Arc.container().select(ReactiveMongoClient.class, Any.Literal.INSTANCE).handles();
    if (config.defaultMongoClientConfig != null) {
        MongoClient client = getClient(handle, null);
        ReactiveMongoClient reactiveClient = getReactiveClient(reactiveHandlers, null);
        if (client != null) {
            checks.add(new MongoClientCheck(CLIENT_DEFAULT, client, config.defaultMongoClientConfig));
        }
        if (reactiveClient != null) {
            checks.add(new ReactiveMongoClientCheck(CLIENT_DEFAULT_REACTIVE, reactiveClient, config.defaultMongoClientConfig));
        }
    }
    config.mongoClientConfigs.forEach(new BiConsumer<String, MongoClientConfig>() {

        @Override
        public void accept(String name, MongoClientConfig cfg) {
            MongoClient client = getClient(handle, name);
            ReactiveMongoClient reactiveClient = getReactiveClient(reactiveHandlers, name);
            if (client != null) {
                checks.add(new MongoClientCheck(name, client, config.defaultMongoClientConfig));
            }
            if (reactiveClient != null) {
                checks.add(new ReactiveMongoClientCheck(name, reactiveClient, config.defaultMongoClientConfig));
            }
        }
    });
}
Also used : MongoClient(com.mongodb.client.MongoClient) ReactiveMongoClient(io.quarkus.mongodb.reactive.ReactiveMongoClient) ReactiveMongoClient(io.quarkus.mongodb.reactive.ReactiveMongoClient) InstanceHandle(io.quarkus.arc.InstanceHandle) MongoClientConfig(io.quarkus.mongodb.runtime.MongoClientConfig)

Example 4 with InstanceHandle

use of io.quarkus.arc.InstanceHandle in project quarkus by quarkusio.

the class RepositoryDataAccessImplementor method getRepositoryInstance.

/**
 * Implements getting repository from Arc container.
 * <code>Arc.container().instance(Repository.class, new Annotation[0]).get()</code>
 */
private ResultHandle getRepositoryInstance(BytecodeCreator creator) {
    ResultHandle arcContainer = creator.invokeStaticMethod(ofMethod(Arc.class, "container", ArcContainer.class));
    ResultHandle instanceHandle = creator.invokeInterfaceMethod(ofMethod(ArcContainer.class, "instance", InstanceHandle.class, Class.class, Annotation[].class), arcContainer, creator.loadClassFromTCCL(repositoryClassName), creator.newArray(Annotation.class, 0));
    ResultHandle instance = creator.invokeInterfaceMethod(ofMethod(InstanceHandle.class, "get", Object.class), instanceHandle);
    creator.ifNull(instance).trueBranch().throwException(RuntimeException.class, repositoryClassName + " instance was not found");
    return instance;
}
Also used : Arc(io.quarkus.arc.Arc) ArcContainer(io.quarkus.arc.ArcContainer) ResultHandle(io.quarkus.gizmo.ResultHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) Annotation(java.lang.annotation.Annotation)

Example 5 with InstanceHandle

use of io.quarkus.arc.InstanceHandle in project quarkus by quarkusio.

the class RepositoryDataAccessImplementor method getRepositoryInstance.

/**
 * Implements getting repository from Arc container.
 * <code>Arc.container().instance(Repository.class, new Annotation[0]).get()</code>
 */
private ResultHandle getRepositoryInstance(BytecodeCreator creator) {
    ResultHandle arcContainer = creator.invokeStaticMethod(ofMethod(Arc.class, "container", ArcContainer.class));
    ResultHandle instanceHandle = creator.invokeInterfaceMethod(ofMethod(ArcContainer.class, "instance", InstanceHandle.class, Class.class, Annotation[].class), arcContainer, creator.loadClass(repositoryClassName), creator.newArray(Annotation.class, 0));
    ResultHandle instance = creator.invokeInterfaceMethod(ofMethod(InstanceHandle.class, "get", Object.class), instanceHandle);
    creator.ifNull(instance).trueBranch().throwException(RuntimeException.class, repositoryClassName + " instance was not found");
    return instance;
}
Also used : Arc(io.quarkus.arc.Arc) ArcContainer(io.quarkus.arc.ArcContainer) ResultHandle(io.quarkus.gizmo.ResultHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) Annotation(java.lang.annotation.Annotation)

Aggregations

InstanceHandle (io.quarkus.arc.InstanceHandle)13 Arc (io.quarkus.arc.Arc)10 ArcContainer (io.quarkus.arc.ArcContainer)10 ResultHandle (io.quarkus.gizmo.ResultHandle)10 Annotation (java.lang.annotation.Annotation)7 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)4 InjectableBean (io.quarkus.arc.InjectableBean)2 ClassCreator (io.quarkus.gizmo.ClassCreator)2 MethodCreator (io.quarkus.gizmo.MethodCreator)2 UpdateExecutor (io.quarkus.rest.data.panache.runtime.UpdateExecutor)2 ClassInfo (org.jboss.jandex.ClassInfo)2 MethodInfo (org.jboss.jandex.MethodInfo)2 MongoClient (com.mongodb.client.MongoClient)1 ContextInstanceHandle (io.quarkus.arc.ContextInstanceHandle)1 BeanInfo (io.quarkus.arc.processor.BeanInfo)1 ElasticsearchClientConfig (io.quarkus.elasticsearch.restclient.lowlevel.ElasticsearchClientConfig)1 BranchResult (io.quarkus.gizmo.BranchResult)1 BytecodeCreator (io.quarkus.gizmo.BytecodeCreator)1 CatchBlockCreator (io.quarkus.gizmo.CatchBlockCreator)1 FieldDescriptor (io.quarkus.gizmo.FieldDescriptor)1