Search in sources :

Example 16 with Instance

use of javax.enterprise.inject.Instance in project kernel by exoplatform.

the class WeldContainer method getComponentAdapter.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <T> ComponentAdapter<T> getComponentAdapter(final Object componentKey, Class<T> bindType, boolean autoRegistration) {
    ComponentAdapter<T> result = super.getComponentAdapter(componentKey, bindType, autoRegistration);
    if (weld != null && result == null) {
        if (componentKey instanceof Class<?> && !((Class<?>) componentKey).isAnnotation()) {
            return getAdapterOfType((Class<T>) componentKey);
        } else if (componentKey instanceof String) {
            Set<Bean<?>> beans = container.getBeanManager().getBeans(bindType, createNamed((String) componentKey));
            if (beans != null && !beans.isEmpty()) {
                return createComponentAdapter(bindType, (Instance<T>) container.instance().select(beans.iterator().next().getBeanClass()));
            }
        } else if (componentKey instanceof Class<?>) {
            final Class<? extends Annotation> annotationType = (Class<? extends Annotation>) componentKey;
            Annotation annotation = createAnnotation(annotationType);
            Set<Bean<?>> beans = container.getBeanManager().getBeans(bindType, annotation);
            if (beans != null && !beans.isEmpty()) {
                return createComponentAdapter(bindType, (Instance<T>) container.instance().select(beans.iterator().next().getBeanClass()));
            }
        }
    }
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Instance(javax.enterprise.inject.Instance) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean)

Example 17 with Instance

use of javax.enterprise.inject.Instance in project kie-wb-common by kiegroup.

the class VerifierWebWorkerRegistryTest method noWebWorkerProviders.

@Test
public void noWebWorkerProviders() throws Exception {
    final Instance verifierWebWorkerProviders = mock(Instance.class);
    doReturn(new ArrayList<>().iterator()).when(verifierWebWorkerProviders).iterator();
    assertFalse(new VerifierWebWorkerRegistry(verifierWebWorkerProviders).get("something").isPresent());
}
Also used : Instance(javax.enterprise.inject.Instance) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 18 with Instance

use of javax.enterprise.inject.Instance in project tomee by apache.

the class TomEEOpenAPIExtension method createOpenApi.

private OpenAPI createOpenApi(final Class<?> application, final Stream<Class<?>> beans) {
    final CDI<Object> current = CDI.current();
    final OpenAPI api = ofNullable(config.read(OASConfig.MODEL_READER, null)).map(value -> newInstance(current, value)).map(it -> OASModelReader.class.cast(it).buildModel()).orElseGet(() -> current.select(DefaultLoader.class).get().loadDefaultApi());
    final BeanManager beanManager = current.getBeanManager();
    processor.processApplication(api, new ElementImpl(beanManager.createAnnotatedType(application)));
    if (skipScan) {
        return api.paths(new PathsImpl());
    }
    // adds the context path to the base
    final Instance<ServletContext> servletContextInstance = current.select(ServletContext.class);
    final boolean appendContextPath = Boolean.valueOf(config.read("application.append-context-path", "true"));
    String contextPath = "";
    if (appendContextPath && !servletContextInstance.isAmbiguous() && !servletContextInstance.isUnsatisfied()) {
        contextPath = servletContextInstance.get().getContextPath();
    }
    final String base = contextPath + processor.getApplicationBinding(application);
    processor.beforeProcessing();
    beans.filter(c -> (excludeClasses == null || !excludeClasses.contains(c.getName()))).filter(c -> (excludePackages == null || excludePackages.stream().noneMatch(it -> c.getName().startsWith(it)))).map(beanManager::createAnnotatedType).forEach(at -> processor.processClass(base, api, new ElementImpl(at), at.getMethods().stream().map(MethodElementImpl::new)));
    return ofNullable(config.read(OASConfig.FILTER, null)).map(it -> newInstance(current, it)).map(i -> new FilterImpl(OASFilter.class.cast(i)).filter(api)).orElse(api);
}
Also used : Array(java.lang.reflect.Array) OASModelReader(org.eclipse.microprofile.openapi.OASModelReader) Path(javax.ws.rs.Path) ProcessBean(javax.enterprise.inject.spi.ProcessBean) HashMap(java.util.HashMap) Application(javax.ws.rs.core.Application) AnnotatedTypeElement(org.apache.geronimo.microprofile.openapi.impl.processor.AnnotatedTypeElement) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) AnnotationProcessor(org.apache.geronimo.microprofile.openapi.impl.processor.AnnotationProcessor) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) Observes(javax.enterprise.event.Observes) Map(java.util.Map) Yaml(org.apache.geronimo.microprofile.openapi.impl.loader.yaml.Yaml) Instance(javax.enterprise.inject.Instance) Collectors.toSet(java.util.stream.Collectors.toSet) GeronimoOpenAPIConfig(org.apache.geronimo.microprofile.openapi.config.GeronimoOpenAPIConfig) AnnotatedMethodElement(org.apache.geronimo.microprofile.openapi.impl.processor.AnnotatedMethodElement) NamingStrategy(org.apache.geronimo.microprofile.openapi.impl.processor.spi.NamingStrategy) Annotated(javax.enterprise.inject.spi.Annotated) Extension(javax.enterprise.inject.spi.Extension) Optional.ofNullable(java.util.Optional.ofNullable) FilterImpl(org.apache.geronimo.microprofile.openapi.impl.filter.FilterImpl) Collection(java.util.Collection) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) CDI(javax.enterprise.inject.spi.CDI) JacksonOpenAPIYamlBodyWriter(org.apache.geronimo.microprofile.openapi.jaxrs.JacksonOpenAPIYamlBodyWriter) InvocationTargetException(java.lang.reflect.InvocationTargetException) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) PathsImpl(org.apache.geronimo.microprofile.openapi.impl.model.PathsImpl) DefaultLoader(org.apache.geronimo.microprofile.openapi.impl.loader.DefaultLoader) Annotation(java.lang.annotation.Annotation) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) ServletContext(javax.servlet.ServletContext) OASConfig(org.eclipse.microprofile.openapi.OASConfig) OASFilter(org.eclipse.microprofile.openapi.OASFilter) Bean(javax.enterprise.inject.spi.Bean) GeronimoOpenAPIExtension(org.apache.geronimo.microprofile.openapi.cdi.GeronimoOpenAPIExtension) RegisterRestClient(org.eclipse.microprofile.rest.client.inject.RegisterRestClient) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) BeanManager(javax.enterprise.inject.spi.BeanManager) AnnotatedElement(java.lang.reflect.AnnotatedElement) FilterImpl(org.apache.geronimo.microprofile.openapi.impl.filter.FilterImpl) OASModelReader(org.eclipse.microprofile.openapi.OASModelReader) PathsImpl(org.apache.geronimo.microprofile.openapi.impl.model.PathsImpl) ServletContext(javax.servlet.ServletContext) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) DefaultLoader(org.apache.geronimo.microprofile.openapi.impl.loader.DefaultLoader) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 19 with Instance

use of javax.enterprise.inject.Instance in project ART-TIME by Artezio.

the class TrackingSystemProducerTest method testGetTeamTrackingSystem.

@Test
public void testGetTeamTrackingSystem() throws NoSuchFieldException {
    Settings settings = createMock(Settings.class);
    Instance instance = createMock(Instance.class);
    trackingSystemProducer = createMockBuilder(TrackingSystemProducer.class).addMockedMethod("findImplementationByName", Instance.class, String.class).createMock();
    setField(trackingSystemProducer, "teamTrackingSystems", instance);
    setField(trackingSystemProducer, "settings", settings);
    TeamTrackingSystem expectedImpl = createMock(TeamTrackingSystem.class);
    expect(trackingSystemProducer.findImplementationByName(instance, "ExpectedImpl")).andReturn(expectedImpl);
    expect(settings.getTeamTrackingSystemName()).andReturn("ExpectedImpl");
    replayAll(instance, settings, expectedImpl, trackingSystemProducer);
    TeamTrackingSystem actual = trackingSystemProducer.getTeamTrackingSystem();
    PowerMock.verifyAll();
    assertEquals(expectedImpl, actual);
}
Also used : Instance(javax.enterprise.inject.Instance) Settings(com.artezio.arttime.config.Settings) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with Instance

use of javax.enterprise.inject.Instance in project ART-TIME by Artezio.

the class TrackingSystemProducerTest method testGetEmployeeTrackingSystemNames.

@Test
public void testGetEmployeeTrackingSystemNames() throws NoSuchFieldException {
    Instance instance = createMock(Instance.class);
    trackingSystemProducer = createMockBuilder(TrackingSystemProducer.class).addMockedMethod("getTrackingSystemsNames", Instance.class).createMock();
    setField(trackingSystemProducer, "employeeTrackingSystems", instance);
    Set<String> expected = Sets.newHashSet("A", "B");
    expect(trackingSystemProducer.getTrackingSystemsNames(instance)).andReturn(expected);
    replayAll(instance, trackingSystemProducer);
    Set<String> actual = trackingSystemProducer.getEmployeeTrackingSystemNames();
    verifyAll();
    assertEquals(expected, actual);
}
Also used : Instance(javax.enterprise.inject.Instance) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Instance (javax.enterprise.inject.Instance)41 Test (org.junit.Test)14 List (java.util.List)12 Inject (javax.inject.Inject)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Optional (java.util.Optional)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Collectors (java.util.stream.Collectors)8 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 IOException (java.io.IOException)5 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)4 Path (javax.ws.rs.Path)4 MediaType (javax.ws.rs.core.MediaType)4 ProcessInstanceExporter (io.automatiko.engine.addons.process.management.export.ProcessInstanceExporter)3 ErrorInfoDTO (io.automatiko.engine.addons.process.management.model.ErrorInfoDTO)3 JsonExportedProcessInstance (io.automatiko.engine.addons.process.management.model.JsonExportedProcessInstance)3 ProcessDTO (io.automatiko.engine.addons.process.management.model.ProcessDTO)3 ProcessInstanceDTO (io.automatiko.engine.addons.process.management.model.ProcessInstanceDTO)3