Search in sources :

Example 1 with Cached

use of org.apache.tapestry5.annotations.Cached in project tapestry-5 by apache.

the class TypeCoercerImpl method findOrCreateCoercion.

/**
 * Here's the real meat; we do a search of the space to find coercions, or a system of
 * coercions, that accomplish
 * the desired coercion.
 *
 * There's <strong>TREMENDOUS</strong> room to improve this algorithm. For example, inheritance lists could be
 * cached. Further, there's probably more ways to early prune the search. However, even with dozens or perhaps
 * hundreds of tuples, I suspect the search will still grind to a conclusion quickly.
 *
 * The order of operations should help ensure that the most efficient tuple chain is located. If you think about how
 * tuples are added to the queue, there are two factors: size (the number of steps in the coercion) and
 * "class distance" (that is, number of steps up the inheritance hiearchy). All the appropriate 1 step coercions
 * will be considered first, in class distance order. Along the way, we'll queue up all the 2 step coercions, again
 * in class distance order. By the time we reach some of those, we'll have begun queueing up the 3 step coercions, and
 * so forth, until we run out of input tuples we can use to fabricate multi-step compound coercions, or reach a
 * final response.
 *
 * This does create a good number of short lived temporary objects (the compound tuples), but that's what the GC is
 * really good at.
 *
 * @param sourceType
 * @param targetType
 * @return coercer from sourceType to targetType
 */
@SuppressWarnings("unchecked")
private Coercion findOrCreateCoercion(Class sourceType, Class targetType) {
    if (sourceType == Void.class) {
        return searchForNullCoercion(targetType);
    }
    // Trying to find exact match.
    Optional<CoercionTuple> maybeTuple = getTuples(sourceType, targetType).stream().filter((t) -> sourceType.equals(t.getSourceType()) && targetType.equals(t.getTargetType())).findFirst();
    if (maybeTuple.isPresent()) {
        return maybeTuple.get().getCoercion();
    }
    // These are instance variables because this method may be called concurrently.
    // On a true race, we may go to the work of seeking out and/or fabricating
    // a tuple twice, but it's more likely that different threads are looking
    // for different source/target coercions.
    Set<CoercionTuple.Key> consideredTuples = CollectionFactory.newSet();
    LinkedList<CoercionTuple> queue = CollectionFactory.newLinkedList();
    seedQueue(sourceType, targetType, consideredTuples, queue);
    while (!queue.isEmpty()) {
        CoercionTuple tuple = queue.removeFirst();
        // If the tuple results in a value type that is assignable to the desired target type,
        // we're done! Later, we may add a concept of "cost" (i.e. number of steps) or
        // "quality" (how close is the tuple target type to the desired target type). Cost
        // is currently implicit, as compound tuples are stored deeper in the queue,
        // so simpler coercions will be located earlier.
        Class tupleTargetType = tuple.getTargetType();
        if (targetType.isAssignableFrom(tupleTargetType)) {
            return tuple.getCoercion();
        }
        // So .. this tuple doesn't get us directly to the target type.
        // However, it *may* get us part of the way. Each of these
        // represents a coercion from the source type to an intermediate type.
        // Now we're going to look for conversions from the intermediate type
        // to some other type.
        queueIntermediates(sourceType, targetType, tuple, consideredTuples, queue);
    }
    throw new CoercionNotFoundException(String.format("Could not find a coercion from type %s to type %s.", sourceType.getName(), targetType.getName()), buildCoercionCatalog(), sourceType, targetType);
}
Also used : PlasticUtils(org.apache.tapestry5.plastic.PlasticUtils) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) InternalCommonsUtils(org.apache.tapestry5.commons.internal.util.InternalCommonsUtils) Collection(java.util.Collection) Set(java.util.Set) StringToEnumCoercion(org.apache.tapestry5.commons.util.StringToEnumCoercion) LockSupport(org.apache.tapestry5.commons.internal.util.LockSupport) List(java.util.List) F(org.apache.tapestry5.func.F) Coercion(org.apache.tapestry5.commons.services.Coercion) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CollectionFactory(org.apache.tapestry5.commons.util.CollectionFactory) Map(java.util.Map) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues) Optional(java.util.Optional) InheritanceSearch(org.apache.tapestry5.commons.internal.util.InheritanceSearch) LinkedList(java.util.LinkedList) Collections(java.util.Collections) WeakHashMap(java.util.WeakHashMap) CoercionFailedException(org.apache.tapestry5.commons.util.CoercionFailedException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple)

Example 2 with Cached

use of org.apache.tapestry5.annotations.Cached in project tapestry-5 by apache.

the class CachedWorker method createFactory.

private MethodResultCacheFactory createFactory(PlasticClass plasticClass, final String watch, PlasticMethod method) {
    // will suffice.
    if (watch.equals("")) {
        return nonWatchFactory;
    }
    // Because of the watch, its necessary to create a factory for instances of this component and method.
    final FieldHandle bindingFieldHandle = plasticClass.introduceField(Binding.class, "cache$watchBinding$" + method.getDescription().methodName).getHandle();
    // Each component instance will get its own Binding instance. That handles both different locales,
    // and reuse of a component (with a cached method) within a page or across pages. However, the binding can't be initialized
    // until the page loads.
    plasticClass.introduceInterface(PageLifecycleListener.class);
    plasticClass.introduceMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_DESCRIPTION).addAdvice(new MethodAdvice() {

        public void advise(MethodInvocation invocation) {
            ComponentResources resources = invocation.getInstanceContext().get(ComponentResources.class);
            Binding binding = bindingSource.newBinding("@Cached watch", resources, BindingConstants.PROP, watch);
            bindingFieldHandle.set(invocation.getInstance(), binding);
            invocation.proceed();
        }
    });
    return new MethodResultCacheFactory() {

        public MethodResultCache create(Object instance) {
            Binding binding = (Binding) bindingFieldHandle.get(instance);
            return new WatchedBindingMethodResultCache(binding);
        }
    };
}
Also used : Binding(org.apache.tapestry5.Binding) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 3 with Cached

use of org.apache.tapestry5.annotations.Cached in project tapestry-5 by apache.

the class ParameterWorker method createComputedParameterConduit.

@SuppressWarnings("all")
private ComputedValue<FieldConduit<Object>> createComputedParameterConduit(final String parameterName, final String fieldTypeName, final Parameter annotation, final MethodHandle defaultMethodHandle) {
    boolean primitive = PlasticUtils.isPrimitive(fieldTypeName);
    final boolean allowNull = annotation.allowNull() && !primitive;
    return new ComputedValue<FieldConduit<Object>>() {

        public ParameterConduit get(InstanceContext context) {
            final InternalComponentResources icr = context.get(InternalComponentResources.class);
            final Class fieldType = classCache.forName(fieldTypeName);
            final PerThreadValue<ParameterState> stateValue = perThreadManager.createValue();
            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);
                    icr.getPageLifecycleCallbackHub().addPageLoadedCallback(new Runnable() {

                        public void run() {
                            load();
                        }
                    });
                }

                private ParameterState getState() {
                    ParameterState state = stateValue.get();
                    if (state == null) {
                        state = new ParameterState();
                        state.value = defaultValue;
                        stateValue.set(state);
                    }
                    return state;
                }

                private boolean isLoaded() {
                    return loaded;
                }

                public void set(Object instance, InstanceContext context, Object newValue) {
                    ParameterState state = getState();
                    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;
                    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(), ExceptionUtils.toMessage(ex)), parameterBinding, ex);
                    }
                    if (result == null && !allowNull) {
                        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);
                    }
                    return result;
                }

                private void writeToBinding(Object newValue) {
                    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(), ExceptionUtils.toMessage(ex)), icr, ex);
                    }
                }

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

                public void load() {
                    if (logger.isDebugEnabled()) {
                        logger.debug("{} loading parameter {}", icr.getCompleteId(), parameterName);
                    }
                    if (!icr.isBound(parameterName)) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("{} parameter {} not yet bound", icr.getCompleteId(), parameterName);
                        }
                        // Otherwise, construct a default binding, or use one provided from
                        // the component.
                        Binding binding = getDefaultBindingForParameter();
                        if (logger.isDebugEnabled()) {
                            logger.debug("{} parameter {} bound to default {}", 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(Object instance, InstanceContext context) {
                    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 (invariant || (annotation.cache() && icr.isRendering())) {
                        state.value = result;
                        state.cached = true;
                    }
                    return result;
                }

                private Binding getDefaultBindingForParameter() {
                    if (InternalUtils.isNonBlank(annotation.value())) {
                        return bindingSource.newBinding("default " + parameterName, icr, annotation.defaultPrefix(), annotation.value());
                    }
                    if (annotation.autoconnect()) {
                        return defaultProvider.defaultBinding(parameterName, icr);
                    }
                    // Invoke the default method and install any value or Binding returned there.
                    invokeDefaultMethod();
                    return parameterBinding;
                }

                private void invokeDefaultMethod() {
                    if (defaultMethodHandle == null) {
                        return;
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("{} invoking method {} to obtain default for parameter {}", icr.getCompleteId(), defaultMethodHandle, parameterName);
                    }
                    MethodInvocationResult result = defaultMethodHandle.invoke(icr.getComponent());
                    result.rethrow();
                    Object defaultValue = result.getReturnValue();
                    if (defaultValue == null) {
                        return;
                    }
                    if (defaultValue instanceof Binding) {
                        parameterBinding = (Binding) defaultValue;
                        return;
                    }
                    parameterBinding = new LiteralBinding(null, "default " + parameterName, defaultValue);
                }
            };
        }
    };
}
Also used : LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) Binding(org.apache.tapestry5.Binding) LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 4 with Cached

use of org.apache.tapestry5.annotations.Cached in project tapestry-5 by apache.

the class ExpansionPageElementImplTest method invariant_binding_is_cached.

@Test
public void invariant_binding_is_cached() {
    Binding binding = mockBinding();
    TypeCoercer coercer = mockTypeCoercer();
    MarkupWriter writer = mockMarkupWriter();
    RenderQueue queue = mockRenderQueue();
    Object value = new Object();
    train_isInvariant(binding, true);
    replay();
    RenderCommand element = new ExpansionPageElement(binding, coercer);
    verify();
    train_get(binding, value);
    train_coerce(coercer, value, String.class, "STRING-VALUE");
    writer.write("STRING-VALUE");
    replay();
    element.render(writer, queue);
    verify();
    // It is now cached ...
    writer.write("STRING-VALUE");
    replay();
    element.render(writer, queue);
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) RenderCommand(org.apache.tapestry5.runtime.RenderCommand) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) MarkupWriter(org.apache.tapestry5.MarkupWriter) Test(org.testng.annotations.Test)

Example 5 with Cached

use of org.apache.tapestry5.annotations.Cached in project tapestry-5 by apache.

the class ComponentLibraries method getLibraryMappings.

@Cached
public List<LibraryMapping> getLibraryMappings() {
    List<LibraryMapping> mappings = new ArrayList<LibraryMapping>();
    // add all the library mappings, except the "" (empty string) one.
    for (LibraryMapping libraryMapping : componentClassResolver.getLibraryMappings()) {
        if (!"".equals(libraryMapping.libraryName)) {
            mappings.add(libraryMapping);
        }
    }
    Collections.sort(mappings, LIBRARY_MAPPING_COMPARATOR);
    return mappings;
}
Also used : ArrayList(java.util.ArrayList) LibraryMapping(org.apache.tapestry5.services.LibraryMapping) Cached(org.apache.tapestry5.annotations.Cached)

Aggregations

Test (org.testng.annotations.Test)7 ComponentResources (org.apache.tapestry5.ComponentResources)6 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)4 ComponentModel (org.apache.tapestry5.model.ComponentModel)4 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)4 Binding (org.apache.tapestry5.Binding)3 Cached (org.apache.tapestry5.annotations.Cached)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 WeakHashMap (java.util.WeakHashMap)1 Asset (org.apache.tapestry5.Asset)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 StreamResponse (org.apache.tapestry5.StreamResponse)1