Search in sources :

Example 11 with PlasticMethod

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;
        }
    };
}
Also used : ValueEncoder(org.apache.tapestry5.ValueEncoder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) JSONObject(org.apache.tapestry5.json.JSONObject) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 12 with PlasticMethod

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);
}
Also used : Cached(org.apache.tapestry5.annotations.Cached) PerThreadValue(org.apache.tapestry5.ioc.services.PerThreadValue)

Aggregations

PlasticMethod (org.apache.tapestry5.plastic.PlasticMethod)7 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)3 ComponentResources (org.apache.tapestry5.ComponentResources)2 ValueEncoder (org.apache.tapestry5.ValueEncoder)2 Import (org.apache.tapestry5.annotations.Import)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 InstructionBuilder (org.apache.tapestry5.plastic.InstructionBuilder)2 InstructionBuilderCallback (org.apache.tapestry5.plastic.InstructionBuilderCallback)2 MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)2 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)2 SessionTracker (com.flowlogix.web.mixins.SessionTracker)1 AJAX (com.flowlogix.web.services.annotations.AJAX)1 IOException (java.io.IOException)1 Array (java.lang.reflect.Array)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 List (java.util.List)1