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);
}
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);
}
};
}
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);
}
};
}
};
}
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();
}
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;
}
Aggregations