Java Code Examples for org.apache.deltaspike.cdise.api.ContextControl#startContext()

The following examples show how to use org.apache.deltaspike.cdise.api.ContextControl#startContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleSchedulerExample.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();

    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);

    GlobalResultHolder globalResultHolder =
        BeanProvider.getContextualReference(GlobalResultHolder.class);

    while (globalResultHolder.getCount() < 100)
    {
        Thread.sleep(500);
        LOG.info("current count: " + globalResultHolder.getCount());
    }
    LOG.info("completed!");

    contextControl.stopContext(ApplicationScoped.class);
    cdiContainer.shutdown();
}
 
Example 2
Source File: ConfigExample.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();

    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);

    SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);

    LOG.info("configured int-value #1: " + settingsBean.getIntProperty1());
    LOG.info("configured long-value #2: " + settingsBean.getProperty2());
    LOG.info("configured inverse-value #2: " + settingsBean.getInverseProperty());
    LOG.info("configured location (custom config): " + settingsBean.getLocationId().name());
    
    cdiContainer.shutdown();
}
 
Example 3
Source File: HAAbstractUnitTest.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
protected void startContainer()
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);
    contextControl.startContext(SessionScoped.class);
    contextControl.startContext(RequestScoped.class);
}
 
Example 4
Source File: CdiServletRequestListener.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent)
{
    LOG.log(Level.FINER,"Incoming request.");
    ContextControl contextControl = getContextControl();
    servletRequestEvent.getServletRequest().setAttribute(CDI_REQ_CONTEXT, contextControl);
    contextControl.startContext(RequestScoped.class);
}
 
Example 5
Source File: CdiCucumberTestRunner.java    From database-rider with Apache License 2.0 4 votes vote down vote up
private void startScopes(CdiContainer container,
                         Class testClass,
                         Method testMethod,
                         Class<? extends Annotation>... restrictedScopes) {

    ContextControl contextControl = container.getContextControl();

    List<Class<? extends Annotation>> scopeClasses = new ArrayList<Class<? extends Annotation>>();

    Collections.addAll(scopeClasses, this.testControl.startScopes());

    if (scopeClasses.isEmpty()) {
        addScopesForDefaultBehavior(scopeClasses);
    } else {
        List<TestControlValidator> testControlValidatorList =
                ServiceUtils.loadServiceImplementations(TestControlValidator.class);

        for (TestControlValidator testControlValidator : testControlValidatorList) {
            if (testControlValidator instanceof TestAware) {
                if (testMethod != null) {
                    ((TestAware) testControlValidator).setTestMethod(testMethod);
                }
                ((TestAware) testControlValidator).setTestClass(testClass);
            }
            try {
                testControlValidator.validate(this.testControl);
            } finally {
                if (testControlValidator instanceof TestAware) {
                    ((TestAware) testControlValidator).setTestClass(null);
                    ((TestAware) testControlValidator).setTestMethod(null);
                }
            }
        }
    }

    for (Class<? extends Annotation> scopeAnnotation : scopeClasses) {
        if (this.parent != null && this.parent.isScopeStarted(scopeAnnotation)) {
            continue;
        }

        if (isRestrictedScope(scopeAnnotation, restrictedScopes)) {
            continue;
        }

        try {
            //force a clean context - TODO discuss onScopeStopped call
            contextControl.stopContext(scopeAnnotation);

            contextControl.startContext(scopeAnnotation);
            this.startedScopes.add(scopeAnnotation);

            onScopeStarted(scopeAnnotation);
        } catch (RuntimeException e) {
            Logger logger = Logger.getLogger(CdiCucumberTestRunner.class.getName());
            logger.setLevel(Level.SEVERE);
            logger.log(Level.SEVERE, "failed to start scope @" + scopeAnnotation.getName(), e);
        }
    }

}
 
Example 6
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private void startScopes(CdiContainer container,
                         Class testClass,
                         Method testMethod,
                         Class<? extends Annotation>... restrictedScopes)
{
    try
    {
        automaticScopeHandlingActive.set(TRUE);

        ContextControl contextControl = container.getContextControl();

        List<Class<? extends Annotation>> scopeClasses = new ArrayList<Class<? extends Annotation>>();

        Collections.addAll(scopeClasses, this.testControl.startScopes());

        if (scopeClasses.isEmpty())
        {
            addScopesForDefaultBehavior(scopeClasses);
        }
        else
        {
            List<TestControlValidator> testControlValidatorList =
                ServiceUtils.loadServiceImplementations(TestControlValidator.class);

            for (TestControlValidator testControlValidator : testControlValidatorList)
            {
                if (testControlValidator instanceof TestAware)
                {
                    if (testMethod != null)
                    {
                        ((TestAware)testControlValidator).setTestMethod(testMethod);
                    }
                    ((TestAware)testControlValidator).setTestClass(testClass);
                }
                try
                {
                    testControlValidator.validate(this.testControl);
                }
                finally
                {
                    if (testControlValidator instanceof TestAware)
                    {
                        ((TestAware)testControlValidator).setTestClass(null);
                        ((TestAware)testControlValidator).setTestMethod(null);
                    }
                }
            }
        }

        for (Class<? extends Annotation> scopeAnnotation : scopeClasses)
        {
            if (this.parent != null && this.parent.isScopeStarted(scopeAnnotation))
            {
                continue;
            }

            if (isRestrictedScope(scopeAnnotation, restrictedScopes))
            {
                continue;
            }

            try
            {
                //force a clean context - TODO discuss onScopeStopped call
                contextControl.stopContext(scopeAnnotation);

                contextControl.startContext(scopeAnnotation);
                this.startedScopes.add(scopeAnnotation);

                onScopeStarted(scopeAnnotation);
            }
            catch (RuntimeException e)
            {
                Logger logger = Logger.getLogger(CdiTestRunner.class.getName());
                logger.setLevel(Level.SEVERE);
                logger.log(Level.SEVERE, "failed to start scope @" + scopeAnnotation.getName(), e);
            }
        }
    }
    finally
    {
        automaticScopeHandlingActive.set(null);
        automaticScopeHandlingActive.remove();
    }
}
 
Example 7
Source File: ContainerCtrlTckTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Test
public void testParallelThreadExecution() throws Exception
{
    final CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);

    cc.boot();
    cc.getContextControl().startContexts();

    final BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);

    final AtomicInteger numErrors = new AtomicInteger(0);
    final ContextControl contextControl = cc.getContextControl();

    Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                contextControl.startContext(SessionScoped.class);
                contextControl.startContext(RequestScoped.class);


                Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
                Bean<?> bean = bm.resolve(beans);

                CarRepair carRepair = (CarRepair)
                        bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
                Assert.assertNotNull(carRepair);

                for (int i = 0; i < 100000; i++)
                {
                    // we need the threads doing something ;)
                    Assert.assertNotNull(carRepair.getCar());
                    Assert.assertNotNull(carRepair.getCar().getUser());
                    Assert.assertNull(carRepair.getCar().getUser().getName());
                }
                contextControl.stopContext(RequestScoped.class);
                contextControl.stopContext(SessionScoped.class);
            }
            catch (Throwable e)
            {
                log.log(Level.SEVERE, "An exception happened on a new worker thread", e);
                numErrors.incrementAndGet();
            }
        }
    };


    Thread[] threads = new Thread[NUM_THREADS];
    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i] = new Thread(runnable);
    }

    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i].start();
    }

    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i].join();
    }

    Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get());


    cc.shutdown();
}
 
Example 8
Source File: SingletonRulesJUnitTest.java    From drools-workshop with Apache License 2.0 2 votes vote down vote up
@Test
@InSequence(0)
@Ignore
public void helloRequestScoped() {
    Assert.assertNotNull(myRequestBean);
    ContextControl ctxCtrl = BeanProvider.getContextualReference(ContextControl.class);


    ctxCtrl.startContext(RequestScoped.class);


    int myBeanHashcode = myRequestBean.getMyBean().hashCode();
    int myKieSessionHashcode = myRequestBean.getkSession().hashCode();

    int result = myRequestBean.doSomething("hello 0");
    Assert.assertEquals(1, result);

    ctxCtrl.stopContext(RequestScoped.class);
    ctxCtrl.startContext(RequestScoped.class);
    

    Assert.assertNotEquals(myBeanHashcode, myRequestBean.getMyBean().hashCode());
    Assert.assertNotEquals(myKieSessionHashcode, myRequestBean.getkSession().hashCode());


    myBeanHashcode = myRequestBean.getMyBean().hashCode();
    myKieSessionHashcode = myRequestBean.getkSession().hashCode();

    result = myRequestBean.doSomething("hello 1");
    Assert.assertEquals(1, result);
    ctxCtrl.stopContext(RequestScoped.class);
    ctxCtrl.startContext(RequestScoped.class);

    
    Assert.assertNotEquals(myBeanHashcode, myRequestBean.getMyBean().hashCode());
    Assert.assertNotEquals(myKieSessionHashcode, myRequestBean.getkSession().hashCode());

    myBeanHashcode = myRequestBean.getMyBean().hashCode();
    myKieSessionHashcode = myRequestBean.getkSession().hashCode();

    result = myRequestBean.doSomething("hello 2");
    Assert.assertEquals(1, result);
   
    myBeanHashcode = myRequestBean.getMyBean().hashCode();
    myKieSessionHashcode = myRequestBean.getkSession().hashCode();
    
    ctxCtrl.stopContext(RequestScoped.class);
    ctxCtrl.startContext(RequestScoped.class);

    Assert.assertNotEquals(myBeanHashcode, myRequestBean.getMyBean().hashCode());
    Assert.assertNotEquals(myKieSessionHashcode, myRequestBean.getkSession().hashCode());

}