Search in sources :

Example 76 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project wildfly by wildfly.

the class WeldClassIntrospector method createInstance.

@Override
public ManagedReference createInstance(final Object instance) {
    final BeanManager beanManager = this.beanManager.getValue();
    final InjectionTarget injectionTarget = getInjectionTarget(instance.getClass());
    final CreationalContext context = beanManager.createCreationalContext(null);
    injectionTarget.inject(instance, context);
    injectionTarget.postConstruct(instance);
    return new WeldManagedReference(injectionTarget, context, instance);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 77 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project kie-wb-common by kiegroup.

the class PackageNameWhiteListLoaderTest method setUp.

@Before
public void setUp() throws Exception {
    final SimpleFileSystemProvider fs = new SimpleFileSystemProvider();
    // Bootstrap WELD container
    weld = new Weld();
    final BeanManager beanManager = weld.initialize().getBeanManager();
    // Instantiate Paths used in tests for Path conversion
    final Bean pathsBean = (Bean) beanManager.getBeans(Paths.class).iterator().next();
    final CreationalContext cc = beanManager.createCreationalContext(pathsBean);
    Paths paths = (Paths) beanManager.getReference(pathsBean, Paths.class, cc);
    // Ensure URLs use the default:// scheme
    fs.forceAsDefault();
    tempFiles = new TempFiles();
    final File tempFile = tempFiles.createTempFile("white-list");
    final org.uberfire.java.nio.file.Path nioPackagePath = fs.getPath(tempFile.toURI());
    pathToWhiteList = paths.convert(nioPackagePath);
    loader = new PackageNameWhiteListLoader(packageNameSearchProvider, ioService);
}
Also used : Weld(org.jboss.weld.environment.se.Weld) Bean(javax.enterprise.inject.spi.Bean) SimpleFileSystemProvider(org.uberfire.java.nio.fs.file.SimpleFileSystemProvider) CreationalContext(javax.enterprise.context.spi.CreationalContext) Paths(org.uberfire.backend.server.util.Paths) BeanManager(javax.enterprise.inject.spi.BeanManager) File(java.io.File) TempFiles(org.guvnor.test.TempFiles) Before(org.junit.Before)

Example 78 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project microservice_framework by CJSCommonPlatform.

the class SynchronousDirectAdapterCache method adapterFromContext.

private SynchronousDirectAdapter adapterFromContext(final String component) {
    final Bean<?> bean = beanManager.getBeans(SynchronousDirectAdapter.class).stream().filter(b -> component.equals(b.getBeanClass().getAnnotation(DirectAdapter.class).value())).findAny().orElseThrow(() -> new IllegalArgumentException(format("Direct adapter for component %s not found", component)));
    final CreationalContext ctx = beanManager.createCreationalContext(bean);
    return (SynchronousDirectAdapter) beanManager.getReference(bean, SynchronousDirectAdapter.class, ctx);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) DirectAdapter(uk.gov.justice.services.core.annotation.DirectAdapter)

Example 79 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project core by weld.

the class BeanManagerImpl method getInjectableReference.

/**
 * Get a reference, registering the injection point used.
 *
 * @param injectionPoint the injection point to register
 * @param resolvedBean the bean to get a reference to
 * @param creationalContext the creationalContext
 * @return the injectable reference
 */
public Object getInjectableReference(InjectionPoint injectionPoint, Bean<?> resolvedBean, CreationalContext<?> creationalContext) {
    Preconditions.checkArgumentNotNull(resolvedBean, "resolvedBean");
    Preconditions.checkArgumentNotNull(creationalContext, CREATIONAL_CONTEXT);
    boolean registerInjectionPoint = isRegisterableInjectionPoint(injectionPoint);
    boolean delegateInjectionPoint = injectionPoint != null && injectionPoint.isDelegate();
    final ThreadLocalStackReference<InjectionPoint> stack = currentInjectionPoint.pushConditionally(injectionPoint, registerInjectionPoint);
    try {
        Type requestedType = null;
        if (injectionPoint != null) {
            requestedType = injectionPoint.getType();
        }
        if (clientProxyOptimization && injectionPoint != null && injectionPoint.getBean() != null) {
            // For certain combinations of scopes, the container is permitted to optimize an injectable reference lookup
            // This should also partially solve circular @PostConstruct invocation
            CreationalContextImpl<?> weldCreationalContext = null;
            Bean<?> bean = injectionPoint.getBean();
            // Do not optimize for self injection
            if (!bean.equals(resolvedBean)) {
                if (creationalContext instanceof CreationalContextImpl) {
                    weldCreationalContext = (CreationalContextImpl<?>) creationalContext;
                }
                if (weldCreationalContext != null && Dependent.class.equals(bean.getScope()) && isNormalScope(resolvedBean.getScope())) {
                    bean = findNormalScopedDependant(weldCreationalContext);
                }
                if (InjectionPoints.isInjectableReferenceLookupOptimizationAllowed(bean, resolvedBean)) {
                    if (weldCreationalContext != null) {
                        final Object incompleteInstance = weldCreationalContext.getIncompleteInstance(resolvedBean);
                        if (incompleteInstance != null) {
                            return incompleteInstance;
                        }
                    }
                    Context context = internalGetContext(resolvedBean.getScope());
                    if (context != null) {
                        @SuppressWarnings({ "unchecked", "rawtypes" }) final Object existinInstance = context.get(Reflections.<Contextual>cast(resolvedBean));
                        if (existinInstance != null) {
                            return existinInstance;
                        }
                    }
                }
            }
        }
        return getReference(resolvedBean, requestedType, creationalContext, delegateInjectionPoint);
    } finally {
        stack.pop();
    }
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) Context(javax.enterprise.context.spi.Context) SlimAnnotatedType(org.jboss.weld.annotated.slim.SlimAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) EnhancedAnnotatedType(org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType) AnnotatedTypeValidator.validateAnnotatedType(org.jboss.weld.annotated.AnnotatedTypeValidator.validateAnnotatedType) InterceptionType(javax.enterprise.inject.spi.InterceptionType) CurrentInjectionPoint(org.jboss.weld.injection.CurrentInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) EmptyInjectionPoint(org.jboss.weld.injection.EmptyInjectionPoint) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) CreationalContextImpl(org.jboss.weld.contexts.CreationalContextImpl)

Example 80 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project core by weld.

the class SimpleInterceptorTest method testSimpleInterceptor.

@Test
public void testSimpleInterceptor() {
    Bean bean = beanManager.getBeans(SimpleBeanImpl.class).iterator().next();
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    SimpleBeanImpl simpleBean = (SimpleBeanImpl) bean.create(creationalContext);
    String result = simpleBean.doSomething();
    assert "decorated-Hello!-decorated".equals(result);
    bean.destroy(simpleBean, creationalContext);
    assert SimpleInterceptor.aroundInvokeCalled;
    assert !SimpleInterceptor.postConstructCalled;
    assert !SimpleInterceptor.preDestroyCalled;
    assert TwoBindingsInterceptor.aroundInvokeCalled;
    assert SimpleBeanImpl.postConstructCalled;
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Aggregations

CreationalContext (javax.enterprise.context.spi.CreationalContext)129 Bean (javax.enterprise.inject.spi.Bean)110 Test (org.junit.Test)83 URL (java.net.URL)67 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 Module (org.guvnor.common.services.project.model.Module)13 BeanManager (javax.enterprise.inject.spi.BeanManager)10 BuildResults (org.guvnor.common.services.project.builder.model.BuildResults)6 IncrementalBuildResults (org.guvnor.common.services.project.builder.model.IncrementalBuildResults)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 Context (javax.enterprise.context.spi.Context)4 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)4 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 Annotation (java.lang.annotation.Annotation)3 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3