Java Code Examples for org.apache.deltaspike.core.api.provider.BeanProvider#getContextualReference()

The following examples show how to use org.apache.deltaspike.core.api.provider.BeanProvider#getContextualReference() . 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: InjectableConfigPropertyTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void injectionViaConfigProperty()
{
    SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);

    assertEquals(14, settingsBean.getProperty1());
    assertEquals(7L, settingsBean.getProperty2());
    assertEquals(-7L, settingsBean.getInverseProperty2());

    // also check the ones with defaultValue
    assertEquals("14", settingsBean.getProperty3Filled());
    assertEquals("myDefaultValue", settingsBean.getProperty3Defaulted());
    assertEquals(42, settingsBean.getProperty4Defaulted());

    assertEquals("some setting for prodDB", settingsBean.getDbConfig());
}
 
Example 2
Source File: SecurityUtils.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private static void processApplicationSecurityException(AccessDeniedException exception,
                                                        Class<? extends ViewConfig> errorView,
                                                        boolean allowNavigation)
{
    SecurityViolationHandler securityViolationHandler =
            BeanProvider.getContextualReference(SecurityViolationHandler.class, true);

    if (securityViolationHandler != null)
    {
        //optional (custom handler) - allows to handle custom implementations of SecurityViolation
        securityViolationHandler.processSecurityViolations(exception.getViolations());
    }
    else
    {
        addViolationsAsMessage(exception.getViolations());
    }

    if (allowNavigation)
    {
        BeanProvider.getContextualReference(ViewNavigationHandler.class).navigateTo(errorView);
    }
}
 
Example 3
Source File: JsfUtils.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static Set<RequestParameter> getViewConfigPageParameters()
{
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

    Set<RequestParameter> result = new HashSet<RequestParameter>();

    if (externalContext == null || //detection of early config for different mojarra versions
            externalContext.getRequestParameterValuesMap() == null || externalContext.getRequest() == null)
    {
        return result;
    }

    NavigationParameterContext navigationParameterContext =
            BeanProvider.getContextualReference(NavigationParameterContext.class);

    for (Map.Entry<String, String> entry : navigationParameterContext.getPageParameters().entrySet())
    {
        //TODO add multi-value support
        result.add(new RequestParameter(entry.getKey(), new String[]{entry.getValue()}));
    }

    return result;
}
 
Example 4
Source File: SecurityParameterBindingTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void afterInvocationAuthorizerWithoutReturnType()
{
    MethodInvocationParameter parameter = new MethodInvocationParameter();
    try
    {
        SecuredBean2 testBean = BeanProvider.getContextualReference(SecuredBean2.class, false);
        testBean.securityCheckAfterMethodInvocation(parameter);
        Assert.fail("AccessDeniedException expect, but was not thrown");
    }
    catch (AccessDeniedException e)
    {
        Assert.assertTrue(parameter.isMethodInvoked());
    }
}
 
Example 5
Source File: ScopedPartialBeanTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Test to demonstrate WELD-2084")
public void testPartialBeanWithApplicationScope() throws Throwable
{
    CustomerRepository repository = BeanProvider.getContextualReference(CustomerRepository.class);
    repository.save(null);
}
 
Example 6
Source File: DeltaSpikeContextExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * We can only initialize our contexts in AfterDeploymentValidation because
 * getBeans must not be invoked earlier than this phase to reduce randomness
 * caused by Beans no being fully registered yet.
 */
public void initializeDeltaSpikeContexts(@Observes AfterDeploymentValidation adv, BeanManager beanManager)
{
    if (!isActivated)
    {
        return;
    }

    WindowBeanHolder windowBeanHolder =
        BeanProvider.getContextualReference(beanManager, WindowBeanHolder.class, false);

    WindowIdHolder windowIdHolder =
        BeanProvider.getContextualReference(beanManager, WindowIdHolder.class, false);

    windowContext.init(windowBeanHolder, windowIdHolder);

    ConversationBeanHolder conversationBeanHolder =
        BeanProvider.getContextualReference(beanManager, ConversationBeanHolder.class, false);
    conversationContext.init(conversationBeanHolder);
    
    ViewAccessBeanHolder viewAccessBeanHolder =
        BeanProvider.getContextualReference(beanManager, ViewAccessBeanHolder.class, false);
    ViewAccessBeanAccessHistory viewAccessBeanAccessHistory =
        BeanProvider.getContextualReference(beanManager, ViewAccessBeanAccessHistory.class, false);
    ViewAccessViewHistory viewAccessViewHistory =
        BeanProvider.getContextualReference(beanManager, ViewAccessViewHistory.class, false);
    viewAccessScopedContext.init(viewAccessBeanHolder, viewAccessBeanAccessHistory, viewAccessViewHistory);
}
 
Example 7
Source File: ExcludeTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * beans de-/activated via expressions
 */
@Test
public void excludedIfExpressionMatch()
{
    ProdDbBean prodDbBean = BeanProvider.getContextualReference(ProdDbBean.class, true);

    Assert.assertNotNull(prodDbBean);

    DevDbBean devDbBean = BeanProvider.getContextualReference(DevDbBean.class, true);

    Assert.assertNull(devDbBean);
}
 
Example 8
Source File: GlobalAlternativeTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * The alternative configured in the low-level config should get used instead of the default implementation
 */
@Test
public void alternativeImplementationWithClassAsBaseType()
{
    BaseBean1 testBean = BeanProvider.getContextualReference(BaseBean1.class);

    Assert.assertEquals(SubBaseBean2.class.getName(), testBean.getClass().getName());
}
 
Example 9
Source File: SecurityBindingTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleInterceptorTestDenied()
{
    SecuredBean1 testBean = BeanProvider.getContextualReference(SecuredBean1.class, false);
    try {
        testBean.getBlockedResult();
        Assert.fail();
    } catch (AccessDeniedException e) {
        //expected
    }
}
 
Example 10
Source File: JsfUtils.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public static void tryToRestoreMessages(FacesContext facesContext)
{
    JsfModuleConfig jsfModuleConfig = BeanProvider.getContextualReference(JsfModuleConfig.class);

    if (!jsfModuleConfig.isAlwaysKeepMessages())
    {
        return;
    }

    try
    {
        WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class);

        @SuppressWarnings({ "unchecked" })
        List<FacesMessageEntry> facesMessageEntryList = windowMetaData.getFacesMessageEntryList();
        List<FacesMessage> originalMessageList = new ArrayList<FacesMessage>(facesContext.getMessageList());

        if (facesMessageEntryList != null)
        {
            for (FacesMessageEntry messageEntry : facesMessageEntryList)
            {
                if (isNewMessage(originalMessageList, messageEntry.getFacesMessage()))
                {
                    facesContext.addMessage(messageEntry.getComponentId(), messageEntry.getFacesMessage());
                }
            }
            facesMessageEntryList.clear();
        }
    }
    catch (ContextNotActiveException e)
    {
        //TODO discuss how we handle it
    }
}
 
Example 11
Source File: ExcludeTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * bean excluded based on a custom expression syntax
 */
@Test
public void excludedBasedOnCustomExpressionSyntax()
{
    CustomExpressionBasedNoBean noBean =
            BeanProvider.getContextualReference(CustomExpressionBasedNoBean.class, true);

    Assert.assertNull(noBean);
}
 
Example 12
Source File: ClassLevelInterceptorTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassLevelInterceptor() throws Exception
{
    // this test is known to not work under weld-2.0.0.Final and weld-2.0.0.SP1
    Assume.assumeTrue(!CdiContainerUnderTest.is(CONTAINER_WELD_2_0_0));

    PartialBean partialBean = BeanProvider.getContextualReference(PartialBean.class);
    CustomInterceptorState state = BeanProvider.getContextualReference(CustomInterceptorState.class);

    Assert.assertNotNull(partialBean);
    Assert.assertEquals("manual", partialBean.getManualResult());

    Assert.assertTrue(state.isIntercepted());
}
 
Example 13
Source File: SchedulerExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public <X> void scheduleJobs(@Observes AfterDeploymentValidation afterDeploymentValidation, BeanManager beanManager)
{
    if (!this.isActivated)
    {
        return;
    }

    SchedulerControl schedulerControl = BeanProvider.getContextualReference(SchedulerControl.class, true);
    if (schedulerControl != null && !schedulerControl.isSchedulerEnabled())
    {
        LOG.info("Scheduler has been disabled by " + ProxyUtils.getUnproxiedClass(schedulerControl.getClass()));
        return;
    }

    initScheduler(afterDeploymentValidation);

    if (this.scheduler == null)
    {
        return;
    }


    List<String> foundJobNames = new ArrayList<String>();

    for (Class jobClass : this.foundManagedJobClasses)
    {
        if (foundJobNames.contains(jobClass.getSimpleName()))
        {
            afterDeploymentValidation.addDeploymentProblem(
                new IllegalStateException("Multiple Job-Classes found with name " + jobClass.getSimpleName()));
        }

        foundJobNames.add(jobClass.getSimpleName());
        this.scheduler.registerNewJob(jobClass);
    }
}
 
Example 14
Source File: MinimalMessagesTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpressionLanguageIntegration()
{
    ElPickedUpMessages elMessage =
            (ElPickedUpMessages) BeanProvider.getContextualReference("elPickedUpMessages");
    Assert.assertNotNull(elMessage);
    Assert.assertEquals("Hello DeltaSpike", elMessage.sayHello("DeltaSpike"));
    Assert.assertEquals("Hello null", elMessage.sayHello(null));
    Assert.assertEquals("Text", elMessage.text());
}
 
Example 15
Source File: CallbackDescriptor.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
protected Object getTargetObjectByName(String beanName)
{
    return BeanProvider.getContextualReference(beanName, true);
}
 
Example 16
Source File: SecuredAnnotationTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Test
public void interceptorTestWithStereotypeOk()
{
    SecuredBean2 testBean = BeanProvider.getContextualReference(SecuredBean2.class, false);
    Assert.assertEquals("result", testBean.getResult());
}
 
Example 17
Source File: JsfRequestLifecyclePhaseListener.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private JsfRequestLifecycleBroadcaster resolveBroadcaster()
{
    //cdi has to inject the events,...
    return BeanProvider.getContextualReference(JsfRequestLifecycleBroadcaster.class);
}
 
Example 18
Source File: SecuredAnnotationTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleInterceptorTestParentOk()
{
    SecuredBean1 testBean = BeanProvider.getContextualReference(SecuredBean1.class, false);
    Assert.assertEquals("allfine", testBean.someFineMethodFromParent());
}
 
Example 19
Source File: SecuredAnnotationTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleInterceptorTestOk()
{
    SecuredBean1 testBean = BeanProvider.getContextualReference(SecuredBean1.class, false);
    Assert.assertEquals("result", testBean.getResult());
}
 
Example 20
Source File: BeanProviderHelper.java    From BeanTest with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a reference of the given bean class.
 * 
 * @param <T>
 *            the type of the bean.
 * @param beanClass
 *            the class of the bean whose reference should be returned.
 * @param qualifiers
 *            qualifiers for narrowing the bean instance. This attribute is not required.
 * @return the reference of the given bean class.
 */
public <T> T getBean(Class<T> beanClass, Annotation... qualifiers) {
    if (cdiContainer == null) {
        bootstrapCdiContainer();
    }
    return BeanProvider.getContextualReference(beanClass, qualifiers);
}