Search in sources :

Example 6 with Parameter

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

the class PageLoaderImpl method parameter.

private void parameter(AssemblerContext context) {
    final ParameterToken token = context.next(ParameterToken.class);
    context.add(new PageAssemblyAction() {

        public void execute(PageAssembly pageAssembly) {
            String parameterName = token.name;
            ComponentPageElement element = pageAssembly.createdElement.peek();
            Location location = token.getLocation();
            BlockImpl block = new BlockImpl(location, interner.format("Parameter %s of %s", parameterName, element.getCompleteId()));
            Binding binding = new LiteralBinding(location, "block parameter " + parameterName, block);
            EmbeddedComponentAssembler embeddedAssembler = pageAssembly.embeddedAssembler.peek();
            ParameterBinder binder = embeddedAssembler.createParameterBinder(parameterName);
            if (binder == null) {
                throw new UnknownValueException(String.format("Component %s does not include a formal parameter '%s' (and does not support informal parameters).", element.getCompleteId(), parameterName), location, null, new AvailableValues("Formal parameters", embeddedAssembler.getFormalParameterNames()));
            }
            binder.bind(pageAssembly.createdElement.peek(), binding);
            pageAssembly.bodyElement.push(block);
        }
    });
    consumeToEndElementAndPopBodyElement(context);
}
Also used : LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) Binding(org.apache.tapestry5.Binding) LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues) Location(org.apache.tapestry5.commons.Location)

Example 7 with Parameter

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

the class ReloadTests method beforeStartup.

@BeforeTest(groups = { "beforeStartup" })
public void beforeStartup(XmlTest xmlTest) throws Exception {
    String uid = Long.toHexString(System.currentTimeMillis());
    webappDir = new File(System.getProperty("java.io.tmpdir"), uid);
    webinfDir = new File(webappDir, "WEB-INF");
    classesDir = new File(webinfDir, "classes");
    pagesDir = new File(classesDir, PACKAGE.replace('.', '/'));
    pagesDir.mkdirs();
    copy("web.xml", webinfDir, "web.xml");
    copy("Index.1.tml", webappDir, "Index.tml");
    copy("Index.1.properties", pagesDir, "Index.properties");
    helper = new ClassCreationHelper(classesDir.getAbsolutePath());
    createIndexClass(100);
    // overwrite the web-app-folder parameter
    xmlTest.addParameter(TapestryTestConstants.WEB_APP_FOLDER_PARAMETER, webappDir.getAbsolutePath());
}
Also used : ClassCreationHelper(org.apache.tapestry5.internal.services.ClassCreationHelper) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with Parameter

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

the class OnEventWorker method implementDispatchMethod.

private void implementDispatchMethod(final PlasticClass plasticClass, final boolean isRoot, final MutableComponentModel model, final Flow<EventHandlerMethod> eventHandlerMethods) {
    plasticClass.introduceMethod(TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION).changeImplementation(new InstructionBuilderCallback() {

        public void doBuild(InstructionBuilder builder) {
            builder.startVariable("boolean", new LocalVariableCallback() {

                public void doBuild(LocalVariable resultVariable, InstructionBuilder builder) {
                    if (!isRoot) {
                        // As a subclass, there will be a base class implementation (possibly empty).
                        builder.loadThis().loadArguments().invokeSpecial(plasticClass.getSuperClassName(), TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION);
                        // First store the result of the super() call into the variable.
                        builder.storeVariable(resultVariable);
                        builder.loadArgument(0).invoke(Event.class, boolean.class, "isAborted");
                        builder.when(Condition.NON_ZERO, RETURN_TRUE);
                    } else {
                        // No event handler method has yet been invoked.
                        builder.loadConstant(false).storeVariable(resultVariable);
                    }
                    boolean hasRestEndpointEventHandlerMethod = false;
                    JSONArray restEndpointEventHandlerMethods = null;
                    for (EventHandlerMethod method : eventHandlerMethods) {
                        method.buildMatchAndInvocation(builder, resultVariable);
                        model.addEventHandler(method.eventType);
                        if (method.handleActivationEventContext) {
                            model.doHandleActivationEventContext();
                        }
                        // We're collecting this info for all components, even considering REST
                        // events are only triggered in pages, because we can have REST event
                        // handler methods in base classes too, and we need this info
                        // for generating complete, correct OpenAPI documentation.
                        final OnEvent onEvent = method.method.getAnnotation(OnEvent.class);
                        final String methodName = method.method.getDescription().methodName;
                        if (isRestEndpointEventHandlerMethod(onEvent, methodName)) {
                            hasRestEndpointEventHandlerMethod = true;
                            if (restEndpointEventHandlerMethods == null) {
                                restEndpointEventHandlerMethods = new JSONArray();
                            }
                            JSONObject methodMeta = new JSONObject();
                            methodMeta.put("name", methodName);
                            JSONArray parameters = new JSONArray();
                            for (MethodParameter parameter : method.method.getParameters()) {
                                parameters.add(parameter.getType());
                            }
                            methodMeta.put("parameters", parameters);
                            restEndpointEventHandlerMethods.add(methodMeta);
                        }
                    }
                    // memory by not setting it to all component models.
                    if (model.isPage()) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHOD_PRESENT, hasRestEndpointEventHandlerMethod ? InternalConstants.TRUE : InternalConstants.FALSE);
                    }
                    // methods in components, something that would be ignored anyway.
                    if (restEndpointEventHandlerMethods != null) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHODS, restEndpointEventHandlerMethods.toCompactString());
                    }
                    builder.loadVariable(resultVariable).returnResult();
                }
            });
        }
    });
}
Also used : LocalVariableCallback(org.apache.tapestry5.plastic.LocalVariableCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) JSONObject(org.apache.tapestry5.json.JSONObject) LocalVariable(org.apache.tapestry5.plastic.LocalVariable) JSONArray(org.apache.tapestry5.json.JSONArray) MethodParameter(org.apache.tapestry5.plastic.MethodParameter) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback) OnEvent(org.apache.tapestry5.annotations.OnEvent)

Example 9 with Parameter

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

the class BindParameterWorker method identifyParameterName.

private String identifyParameterName(ComponentResources resources, String firstGuess, String... otherGuesses) {
    ComponentModel model = resources.getContainerResources().getComponentModel();
    List<String> guesses = CollectionFactory.newList();
    guesses.add(firstGuess);
    for (String name : otherGuesses) {
        guesses.add(name);
    }
    for (String name : guesses) {
        if (model.isFormalParameter(name))
            return name;
        if (isPublishedParameter(model, name))
            return name;
    }
    String message = String.format("Containing component %s does not contain a formal parameter or a published parameter %s %s.", model.getComponentClassName(), guesses.size() == 1 ? "matching" : "matching any of", InternalUtils.joinSorted(guesses));
    List<String> formalAndPublishedParameters = CollectionFactory.newList(model.getParameterNames());
    formalAndPublishedParameters.addAll(getPublishedParameters(model));
    throw new UnknownValueException(message, new AvailableValues("Formal and published parameters", formalAndPublishedParameters));
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues)

Example 10 with Parameter

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

the class BindParameterWorker method convertFieldIntoContainerBoundParameter.

private void convertFieldIntoContainerBoundParameter(PlasticField field) {
    BindParameter annotation = field.getAnnotation(BindParameter.class);
    field.claim(annotation);
    final String[] possibleNames = annotation.value();
    final String fieldTypeName = field.getTypeName();
    final String fieldName = field.getName();
    ComputedValue<FieldConduit<Object>> computedConduit = new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            try {
                return createConduit(resources, fieldTypeName, fieldName, possibleNames);
            } catch (Exception ex) {
                throw new TapestryException(String.format("Failure binding parameter field '%s' of mixin %s (type %s): %s", fieldName, resources.getCompleteId(), resources.getComponentModel().getComponentClassName(), ExceptionUtils.toMessage(ex)), ex);
            }
        }
    };
    field.setComputedConduit(computedConduit);
}
Also used : BindParameter(org.apache.tapestry5.annotations.BindParameter) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)12 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)6 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)6 RequestParameter (org.apache.tapestry5.annotations.RequestParameter)5 JSONObject (org.apache.tapestry5.json.JSONObject)5 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)5 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)4 Test (org.testng.annotations.Test)4 Binding (org.apache.tapestry5.Binding)3 ComponentResources (org.apache.tapestry5.ComponentResources)3 ActivationContextParameter (org.apache.tapestry5.annotations.ActivationContextParameter)3 Location (org.apache.tapestry5.commons.Location)3 Resource (org.apache.tapestry5.commons.Resource)3 AvailableValues (org.apache.tapestry5.commons.util.AvailableValues)3 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)3 Logger (org.slf4j.Logger)3 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 EventContext (org.apache.tapestry5.EventContext)2 ValueEncoder (org.apache.tapestry5.ValueEncoder)2