use of org.apache.tapestry5.plastic.PlasticMethod in project tapestry-5 by apache.
the class OnEventWorker method createQueryParameterProvider.
private EventHandlerMethodParameterProvider createQueryParameterProvider(PlasticMethod method, final int parameterIndex, final String parameterName, final String parameterTypeName, final boolean allowBlank) {
final String methodIdentifier = method.getMethodIdentifier();
return new EventHandlerMethodParameterProvider() {
@SuppressWarnings("unchecked")
public Object valueForEventHandlerMethodParameter(ComponentEvent event) {
try {
Class parameterType = classCache.forName(parameterTypeName);
boolean isArray = parameterType.isArray();
if (isArray) {
parameterType = parameterType.getComponentType();
}
ValueEncoder valueEncoder = valueEncoderSource.getValueEncoder(parameterType);
String parameterValue = request.getParameter(parameterName);
if (!allowBlank && InternalUtils.isBlank(parameterValue))
throw new RuntimeException(String.format("The value for query parameter '%s' was blank, but a non-blank value is needed.", parameterName));
Object value;
if (!isArray) {
value = coerce(parameterName, parameterType, parameterValue, valueEncoder, allowBlank);
} else {
String[] parameterValues = request.getParameters(parameterName);
Object[] array = (Object[]) Array.newInstance(parameterType, parameterValues.length);
for (int i = 0; i < parameterValues.length; i++) {
array[i] = coerce(parameterName, parameterType, parameterValues[i], valueEncoder, allowBlank);
}
value = array;
}
return value;
} catch (Exception ex) {
throw new RuntimeException(String.format("Unable process query parameter '%s' as parameter #%d of event handler method %s: %s", parameterName, parameterIndex + 1, methodIdentifier, ExceptionUtils.toMessage(ex)), ex);
}
}
private Object coerce(final String parameterName, Class parameterType, String parameterValue, ValueEncoder valueEncoder, boolean allowBlank) {
if (!allowBlank && InternalUtils.isBlank(parameterValue)) {
throw new RuntimeException(String.format("The value for query parameter '%s' was blank, but a non-blank value is needed.", parameterName));
}
Object value = valueEncoder.toValue(parameterValue);
if (parameterType.isPrimitive() && value == null)
throw new RuntimeException(String.format("Query parameter '%s' evaluates to null, but the event method parameter is type %s, a primitive.", parameterName, parameterType.getName()));
return value;
}
};
}
use of org.apache.tapestry5.plastic.PlasticMethod in project tapestry-5 by apache.
the class CachedWorker method adviseMethod.
private void adviseMethod(PlasticClass plasticClass, PlasticMethod method) {
// Every instance of the clas srequires its own per-thread value. This handles the case of multiple
// pages containing the component, or the same page containing the component multiple times.
PlasticField cacheField = plasticClass.introduceField(PerThreadValue.class, "cache$" + method.getDescription().methodName);
cacheField.injectComputed(new ComputedValue<PerThreadValue>() {
public PerThreadValue get(InstanceContext context) {
// Each instance will get a new PerThreadValue
return perThreadManager.createValue();
}
});
Cached annotation = method.getAnnotation(Cached.class);
MethodResultCacheFactory factory = createFactory(plasticClass, annotation.watch(), method);
MethodAdvice advice = createAdvice(cacheField, factory);
method.addAdvice(advice);
}
Aggregations