Search in sources :

Example 1 with CompositeComponentBeanInfo

use of com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo in project mojarra by eclipse-ee4j.

the class CompositeComponentAttributesELResolverTest method testGetValue.

/**
 * Test issue #2508.
 */
@Test
public void testGetValue() throws Exception {
    ELContext elContext1 = EasyMock.createNiceMock(ELContext.class);
    FacesContext facesContext1 = EasyMock.createNiceMock(FacesContext.class);
    ELContext elContext2 = EasyMock.createNiceMock(ELContext.class);
    FacesContext facesContext2 = EasyMock.createNiceMock(FacesContext.class);
    HashMap<Object, Object> ctxAttributes1 = new HashMap<Object, Object>();
    UIPanel composite = new UIPanel();
    CompositeComponentBeanInfo compositeBeanInfo = new CompositeComponentBeanInfo();
    BeanDescriptor beanDescriptor = new BeanDescriptor(composite.getClass());
    compositeBeanInfo.setBeanDescriptor(beanDescriptor);
    composite.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, "dummy");
    composite.getAttributes().put(UIComponent.BEANINFO_KEY, compositeBeanInfo);
    String property = "attrs";
    expect(elContext1.getContext(FacesContext.class)).andReturn(facesContext1);
    expect(facesContext1.getAttributes()).andReturn(ctxAttributes1);
    expect(elContext2.getContext(FacesContext.class)).andReturn(facesContext2);
    expect(facesContext2.getAttributes()).andReturn(ctxAttributes1);
    replay(elContext1, facesContext1, elContext2, facesContext2);
    CompositeComponentAttributesELResolver elResolver = new CompositeComponentAttributesELResolver();
    Map<String, Object> evalMap1 = (Map<String, Object>) elResolver.getValue(elContext1, composite, property);
    assertNotNull(evalMap1);
    Map<String, Object> evalMap2 = (Map<String, Object>) elResolver.getValue(elContext2, composite, property);
    assertNotNull(evalMap2);
    Field ctxField1 = evalMap1.getClass().getDeclaredField("ctx");
    ctxField1.setAccessible(true);
    Field ctxField2 = evalMap2.getClass().getDeclaredField("ctx");
    ctxField2.setAccessible(true);
    assertTrue(evalMap1 == evalMap2);
    assertTrue(facesContext1 != ctxField1.get(evalMap1));
    assertTrue(facesContext2 == ctxField1.get(evalMap1));
    assertTrue(facesContext1 != ctxField2.get(evalMap2));
    assertTrue(facesContext2 == ctxField2.get(evalMap2));
    verify(elContext1, facesContext1, elContext2, facesContext2);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) CompositeComponentBeanInfo(com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo) HashMap(java.util.HashMap) UIPanel(jakarta.faces.component.UIPanel) Field(java.lang.reflect.Field) ELContext(jakarta.el.ELContext) BeanDescriptor(java.beans.BeanDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with CompositeComponentBeanInfo

use of com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method createComponentMetadata.

private BeanInfo createComponentMetadata(FacesContext context, Resource ccResource) {
    // PENDING this implementation is terribly wasteful.
    // Must find a better way.
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FACELET_CONTEXT_KEY);
    DefaultFaceletFactory factory = (DefaultFaceletFactory) RequestStateManager.get(context, FACELET_FACTORY);
    VariableMapper orig = faceletContext.getVariableMapper();
    // Create tmp and facetComponent
    UIComponent tmp = context.getApplication().createComponent("jakarta.faces.NamingContainer");
    UIPanel facetComponent = (UIPanel) context.getApplication().createComponent("jakarta.faces.Panel");
    // PENDING I think this can be skipped because we don't render
    // this component instance.
    facetComponent.setRendererType("jakarta.faces.Group");
    // PENDING This could possibly be skipped too. However, I think
    // this is important because other tag handlers, within
    // <cc:interface> expect it will be there.
    tmp.getFacets().put(COMPOSITE_FACET_NAME, facetComponent);
    // We have to put the resource in here just so the classes that eventually
    // get called by facelets have access to it.
    tmp.getAttributes().put(COMPONENT_RESOURCE_KEY, ccResource);
    Facelet facelet;
    try {
        facelet = factory.getFacelet(context, ccResource.getURL());
        VariableMapper wrapper = new VariableMapperWrapper(orig) {

            @Override
            public ValueExpression resolveVariable(String variable) {
                // PENDING is this needed?
                return super.resolveVariable(variable);
            }
        };
        faceletContext.setVariableMapper(wrapper);
        context.getAttributes().put(IS_BUILDING_METADATA, TRUE);
        // Because mojarra currently requires a <cc:interface>
        // element within the compcomp markup, we can rely on the
        // fact that its tag handler, InterfaceHandler.apply(), is
        // called. In this method, we first imbue facetComponent
        // with any config information present on the <cc:interface>
        // element.
        // Then we do the normal facelet thing:
        // this.nextHandler.apply(). This causes any child tag
        // handlers of the <cc:interface> to be called. The
        // compcomp spec says each such tag handler is responsible
        // for adding to the compcomp metadata, referenced from the
        // facetComponent parent.
        facelet.apply(context, facetComponent);
    // When facelet.apply() returns (and therefore
    // InterfaceHandler.apply() returns), the compcomp metadata
    // pointed to by facetComponent is fully populated.
    } catch (Exception e) {
        if (e instanceof FacesException) {
            throw (FacesException) e;
        } else {
            throw new FacesException(e);
        }
    } finally {
        context.getAttributes().remove(IS_BUILDING_METADATA);
        faceletContext.setVariableMapper(orig);
    }
    // not yet implemented.
    return (CompositeComponentBeanInfo) tmp.getAttributes().get(BEANINFO_KEY);
}
Also used : DefaultFaceletFactory(com.sun.faces.facelets.impl.DefaultFaceletFactory) Facelet(jakarta.faces.view.facelets.Facelet) CompositeComponentBeanInfo(com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo) VariableMapper(jakarta.el.VariableMapper) UIComponent(jakarta.faces.component.UIComponent) UIPanel(jakarta.faces.component.UIPanel) VariableMapperWrapper(com.sun.faces.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException) FileNotFoundException(java.io.FileNotFoundException) FacesException(jakarta.faces.FacesException)

Aggregations

CompositeComponentBeanInfo (com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo)2 UIPanel (jakarta.faces.component.UIPanel)2 VariableMapperWrapper (com.sun.faces.facelets.el.VariableMapperWrapper)1 DefaultFaceletFactory (com.sun.faces.facelets.impl.DefaultFaceletFactory)1 ELContext (jakarta.el.ELContext)1 VariableMapper (jakarta.el.VariableMapper)1 FacesException (jakarta.faces.FacesException)1 UIComponent (jakarta.faces.component.UIComponent)1 FacesContext (jakarta.faces.context.FacesContext)1 Facelet (jakarta.faces.view.facelets.Facelet)1 FaceletContext (jakarta.faces.view.facelets.FaceletContext)1 BeanDescriptor (java.beans.BeanDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1