All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.template.velocity.spring.view.reactive.ReactiveVelocityToolboxView Maven / Gradle / Ivy

package org.zodiac.template.velocity.spring.view.reactive;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.velocity.context.Context;
import org.apache.velocity.tools.ToolboxFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.server.ServerWebExchange;
import org.zodiac.commons.util.Classes;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import org.zodiac.template.velocity.view.reactive.ReactiveVelocityToolView;
import org.zodiac.template.velocity.view.reactive.ReactiveViewToolContext;
import org.zodiac.template.velocity.view.reactive.ReactiveViewToolManager;

public class ReactiveVelocityToolboxView extends ReactiveVelocityView {

    private ToolboxFactory toolboxFactory;
    private boolean userOverwrite = true;

    public ReactiveVelocityToolboxView() {
        super();
    }

    public ReactiveVelocityToolboxView(String url) {
        super(url);
    }

    public void setToolboxFactory(ToolboxFactory toolboxFactory) {
        this.toolboxFactory = toolboxFactory;
    }

    protected boolean isUserOverwrite() {
        return userOverwrite;
    }

    public void setUserOverwrite(boolean userOverwrite) {
        this.userOverwrite = userOverwrite;
    }

    protected ToolboxFactory getToolboxFactory() {
        return toolboxFactory;
    }

    @Override
    protected Context getTemplateContext(Map model, ServerWebExchange exchange) {
        ReactiveViewToolContext velocityContext = new ReactiveViewToolContext(getVelocityEngine(), exchange);
        velocityContext.putAll(model);
        if (getToolboxFactory() != null) {
            ReactiveViewToolManager toolboxManager = new ReactiveVelocityToolView(exchange);
            toolboxManager.setVelocityEngine(getVelocityEngine());
            toolboxManager.setToolboxFactory(getToolboxFactory());
            toolboxManager.publishApplicationTools();
            toolboxManager.publishToolboxes(exchange);
            velocityContext.addToolbox(toolboxManager.getApplicationToolbox());
            velocityContext.setUserCanOverwriteTools(userOverwrite);
        }
        if (this.getToolAttributes() != null) {
            for (Map.Entry> entry : this.getToolAttributes().entrySet()) {
                String attributeName = entry.getKey();
                Class toolClass = entry.getValue();
                try {
                    Object tool = toolClass.newInstance();
                    initTool(tool, velocityContext);
                    velocityContext.put(attributeName, tool);
                } catch (Exception ex) {
                   log.error("Could not instantiate Velocity tool '{}', caused by {} .", attributeName, ExceptionUtil.stackTrace(ex));
                }
            }
        }
        return velocityContext;
    }

    protected void initTool(Object tool, Context velocityContext) throws Exception {
        /*Velocity Tools 1.3: a class-level "init(Object)" method.*/
        Method initMethod = Classes.getMethodIfAvailable(tool.getClass(), "init", Object.class);
        if (initMethod != null) {
            ReflectionUtils.invokeMethod(initMethod, tool, velocityContext);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy