Package org.apache.tapestry5.internal

Examples of org.apache.tapestry5.internal.InternalComponentResources


        train_getModel(ins, model);

        replay();

        InternalComponentResources resources = new InternalComponentResourcesImpl(page, element, null, null, null,
                null, ins, false);

        resources.bindParameter("bar", binding);

        assertNull(resources.getPropertyName("bar"));

        verify();
    }
View Full Code Here


        expect(binding.getPropertyName()).andReturn("foo");

        replay();

        InternalComponentResources resources = new InternalComponentResourcesImpl(page, element, null, null, null,
                null, ins, false);

        resources.bindParameter("bar", binding);

        assertEquals(resources.getPropertyName("bar"), "foo");

        verify();
    }
View Full Code Here

    public void field_read_and_write() throws Exception
    {
        ApplicationStateManager manager = mockApplicationStateManager();
        Logger logger = mockLogger();
        MutableComponentModel model = mockMutableComponentModel();
        InternalComponentResources resources = mockInternalComponentResources();
        ComponentClassCache cache = mockComponentClassCache();

        train_getLogger(model, logger);

        Class asoClass = SimpleASO.class;
View Full Code Here

    public void read_field_with_create_disabled() throws Exception
    {
        ApplicationStateManager manager = mockApplicationStateManager();
        Logger logger = mockLogger();
        MutableComponentModel model = mockMutableComponentModel();
        InternalComponentResources resources = mockInternalComponentResources();
        ComponentClassCache cache = mockComponentClassCache();

        train_getLogger(model, logger);

        Class asoClass = SimpleASO.class;
View Full Code Here

                if (binder != null)
                {
                    final String defaultBindingPrefix = binder.getDefaultBindingPrefix(metaDefaultBindingPrefix);

                    InternalComponentResources containerResources = pageAssembly.activeElement.peek()
                            .getComponentResources();

                    ComponentPageElement embeddedElement = pageAssembly.createdElement.peek();
                    InternalComponentResources embeddedResources = embeddedElement.getComponentResources();

                    Binding binding = elementFactory.newBinding(parameterName, containerResources, embeddedResources,
                            defaultBindingPrefix, parameterValue, location);

                    binder.bind(embeddedElement, binding);
View Full Code Here

        context.add(new PageAssemblyAction()
        {
            public void execute(PageAssembly pageAssembly)
            {
                InternalComponentResources resources = pageAssembly.activeElement.peek().getComponentResources();

                RenderCommand command = elementFactory.newAttributeElement(resources, token);

                pageAssembly.addRenderCommand(command);
            }
View Full Code Here

            final String fieldName, final String[] possibleNames)
    {
        if (!resources.isMixin())
            throw new TapestryException(TransformMessages.bindParameterOnlyOnMixin(fieldName, resources), null);

        InternalComponentResources containerResources = (InternalComponentResources) resources.getContainerResources();

        // Evaluate this early so that we get a fast fail.

        String containerParameterName = identifyParameterName(resources, InternalUtils.stripMemberName(fieldName),
                possibleNames);
View Full Code Here

    {
        return new ComponentValueProvider<ParameterConduit>()
        {
            public ParameterConduit get(ComponentResources resources)
            {
                final InternalComponentResources icr = (InternalComponentResources) resources;

                final String key = String.format("ParameterWorker:%s/%s", resources.getCompleteId(), parameterName);

                final Class fieldType = classCache.forName(fieldTypeName);

                // Rely on some code generation in the component to set the default binding from
                // the field, or from a default method.

                return new ParameterConduit()
                {
                    // Default value for parameter, computed *once* at
                    // page load time.

                    private Object defaultValue = classCache.defaultValueForType(fieldTypeName);

                    private Binding parameterBinding;

                    boolean loaded = false;

                    private boolean invariant = false;

                    {
                        // Inform the ComponentResources about the parameter conduit, so it can be
                        // shared with mixins.

                        icr.setParameterConduit(parameterName, this);
                    }

                    private ParameterState getState()
                    {
                        ParameterState state = (ParameterState) perThreadManager.get(key);

                        if (state == null)
                        {
                            state = new ParameterState();
                            state.value = defaultValue;
                            perThreadManager.put(key, state);
                        }

                        return state;
                    }

                    private boolean isLoaded()
                    {
                        return loaded;
                    }

                    public void set(Object newValue)
                    {
                        ParameterState state = getState();

                        // Assignments before the page is loaded ultimately exist to set the
                        // default value for the field. Often this is from the (original)
                        // constructor method, which is converted to a real method as part of the transformation.

                        if (!loaded)
                        {
                            state.value = newValue;
                            defaultValue = newValue;
                            return;
                        }

                        // This will catch read-only or unbound parameters.

                        writeToBinding(newValue);

                        state.value = newValue;

                        // If caching is enabled for the parameter (the typical case) and the
                        // component is currently rendering, then the result
                        // can be cached in this ParameterConduit (until the component finishes
                        // rendering).

                        state.cached = annotation.cache() && icr.isRendering();
                    }

                    private Object readFromBinding()
                    {
                        Object result = null;

                        try
                        {
                            Object boundValue = parameterBinding.get();

                            result = typeCoercer.coerce(boundValue, fieldType);
                        }
                        catch (RuntimeException ex)
                        {
                            throw new TapestryException(String.format(
                                    "Failure reading parameter '%s' of component %s: %s", parameterName,
                                    icr.getCompleteId(), InternalUtils.toMessage(ex)), parameterBinding, ex);
                        }

                        if (result != null || annotation.allowNull())
                            return result;

                        throw new TapestryException(
                                String.format(
                                        "Parameter '%s' of component %s is bound to null. This parameter is not allowed to be null.",
                                        parameterName, icr.getCompleteId()), parameterBinding, null);
                    }

                    private void writeToBinding(Object newValue)
                    {
                        // An unbound parameter acts like a simple field
                        // with no side effects.

                        if (parameterBinding == null)
                            return;

                        try
                        {
                            Object coerced = typeCoercer.coerce(newValue, parameterBinding.getBindingType());

                            parameterBinding.set(coerced);
                        }
                        catch (RuntimeException ex)
                        {
                            throw new TapestryException(String.format(
                                    "Failure writing parameter '%s' of component %s: %s", parameterName,
                                    icr.getCompleteId(), InternalUtils.toMessage(ex)), icr, ex);
                        }
                    }

                    public void reset()
                    {
                        if (!invariant)
                        {
                            getState().reset(defaultValue);
                        }
                    }

                    public void load()
                    {
                        logger.debug(String.format("%s loading parameter %s", icr.getCompleteId(), parameterName));

                        // If it's bound at this point, that's because of an explicit binding
                        // in the template or @Component annotation.

                        if (!icr.isBound(parameterName))
                        {
                            logger.debug(String.format("%s parameter %s not yet bound", icr.getCompleteId(),
                                    parameterName));

                            // Otherwise, construct a default binding, or use one provided from
                            // the component.

                            Binding binding = getDefaultBindingForParameter();

                            logger.debug(String.format("%s parameter %s bound to default %s", icr.getCompleteId(),
                                    parameterName, binding));

                            if (binding != null)
                                icr.bindParameter(parameterName, binding);
                        }

                        parameterBinding = icr.getBinding(parameterName);

                        loaded = true;

                        invariant = parameterBinding != null && parameterBinding.isInvariant();

                        getState().value = defaultValue;
                    }

                    public boolean isBound()
                    {
                        return parameterBinding != null;
                    }

                    public Object get()
                    {
                        if (!isLoaded()) { return defaultValue; }

                        ParameterState state = getState();

                        if (state.cached || !isBound()) { return state.value; }

                        // Read the parameter's binding and cast it to the
                        // field's type.

                        Object result = readFromBinding();

                        // If the value is invariant, we can cache it until at least the end of the request (before
                        // 5.2, it would be cached forever in the pooled instance).
                        // Otherwise, we
                        // we may want to cache it for the remainder of the component render (if the
                        // component is currently rendering).

                        if (invariant || (annotation.cache() && icr.isRendering()))
                        {
                            state.value = result;
                            state.cached = true;
                        }

View Full Code Here

        }
    }

    public void bindMixinParameter(String mixinId, String parameterName, Binding binding)
    {
        InternalComponentResources mixinResources = InternalUtils.get(mixinIdToComponentResources, mixinId);

        mixinResources.bindParameter(parameterName, binding);
    }
View Full Code Here

    {
        return new ComponentValueProvider<FieldValueConduit>()
        {
            public FieldValueConduit get(ComponentResources resources)
            {
                final InternalComponentResources icr = (InternalComponentResources) resources;

                return new ReadOnlyFieldValueConduit(resources, fieldName)
                {
                    public Object get()
                    {
                        return icr.getMixinByClassName(mixinClassName);
                    }
                };
            }
        };
    }
View Full Code Here

TOP

Related Classes of org.apache.tapestry5.internal.InternalComponentResources

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.