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

net.segoia.scriptdao.core.DefaultCommandManager Maven / Gradle / Ivy

Go to download

Library to support dynamic command generation based on Apache Velocity templates

There is a newer version: 1.0.0
Show newest version
/**
 * script-dao - Library to support dynamic command generation based on Apache Velocity templates
 * Copyright (C) 2009  Adrian Cristian Ionescu - https://github.com/acionescu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.segoia.scriptdao.core;

import java.util.HashMap;
import java.util.Map;

import net.segoia.db.sql.SqlUtil;

public class DefaultCommandManager implements CommandManager {
    private String name;
    private CommandLoader commandLoader;
    private CommandBuilder commandBuilder;
    private CommandExecutor commandExecutor;
    private ConnectionManager connectionManager;
    private ResponseTranslator translator;
    private ClassLoader resourcesLoader;
    private Map commandTemplates = new HashMap();

    public DefaultCommandManager() {

    }

    public DefaultCommandManager(String name) {
	this.name = name;
    }

    public void init() throws Exception {
	load();
    }

    public void load() throws Exception {
	if (commandLoader != null) {
	    commandTemplates.clear();
	    CommandTemplate[] templates = commandLoader.loadCommands(resourcesLoader);
	    for (CommandTemplate template : templates) {
		if (!commandTemplates.containsKey(template.getName())) {
		    commandTemplates.put(template.getName(), template);
		} else {
		    CommandTemplate prevTemplate = commandTemplates.get(template.getName());
		    throw new RuntimeException("Could not add the command with name '" + template.getName()
			    + "' from '" + template.getSource() + "."
			    + "\nA command with this name was loaded previously from '" + prevTemplate.getSource()
			    + "'");
		}
	    }
	}
    }

    private CommandContext getCommandContext(ScriptDaoCommand command) {
	return new CommandContext(command, connectionManager);
    }

    private CommandContext getCommandContext(ScriptDaoCommand[] commands) {
	return new CommandContext(commands, connectionManager);
    }

    public String[] getCommandArguments(String commandName) {
	CommandTemplate template = commandTemplates.get(commandName);
	return template.getParameterNames();
    }

    public Object executeCommand(String commandName, Map arguments) throws Exception {
	ScriptDaoCommand command = getCommand(commandName, arguments);
	try {
	    return executeCommand(command);
	} catch (Exception e) {
	    throw new Exception("Error executing :\n " + command.getContent(), e);
	}
    }

    public String[] getAvailableCommands() {
	return commandTemplates.keySet().toArray(new String[0]);
    }

    public CommandBuilder getCommandBuilder() {
	return commandBuilder;
    }

    public void setCommandBuilder(CommandBuilder commandBuilder) {
	this.commandBuilder = commandBuilder;
    }

    public CommandExecutor getCommandExecutor() {
	return commandExecutor;
    }

    public void setCommandExecutor(CommandExecutor commandExecutor) {
	this.commandExecutor = commandExecutor;
    }

    public CommandLoader getCommandLoader() {
	return commandLoader;
    }

    public void setCommandLoader(CommandLoader commandLoader) {
	this.commandLoader = commandLoader;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
	this.name = name;
    }

    public String getName() {
	return name;
    }

    /**
     * @return the connectionManager
     */
    public ConnectionManager getConnectionManager() {
	return connectionManager;
    }

    /**
     * @param connectionManager
     *            the connectionManager to set
     */
    public void setConnectionManager(ConnectionManager connectionManager) {
	this.connectionManager = connectionManager;
    }

    /**
     * @return the translator
     */
    public ResponseTranslator getTranslator() {
	return translator;
    }

    /**
     * @param translator
     *            the translator to set
     */
    public void setTranslator(ResponseTranslator translator) {
	this.translator = translator;
    }

    public ScriptDaoCommand getCommand(String commandName, Map arguments) throws Exception {
	CommandTemplate template = commandTemplates.get(commandName);
	String script = template.getScript();
	String mergedScript = commandBuilder.buildCommand(script, arguments);
	ScriptDaoCommand command = new ScriptDaoCommand(commandName, mergedScript, template.getType(), arguments);
	return command;
    }

    public Object executeAsTransaction(ScriptDaoCommand[] commands) throws Exception {
	Object[] responses = null;
	if (connectionManager != null) {
	    responses = commandExecutor.executeAsTransaction(getCommandContext(commands));
	} else {
	    responses = commandExecutor.executeAsTransaction(commands);
	}
	return translate(responses);
    }

    public R executeCommand(ScriptDaoCommand command) throws Exception {
	Object r = null;
	if (connectionManager != null) {
	    r = commandExecutor.executeCommand(getCommandContext(command));
	} else {
	    r = commandExecutor.executeCommand(command);
	}
	return translate(r);
    }

    private R translate(Object response) {
	if (translator != null) {
	    return translator.translate(response);
	}
	return (R) response;
    }

    private Object translate(Object[] responses) {
	if (translator != null) {
	    return translator.translate(responses);
	}
	return responses;
    }

    public Map getCommandTemplates() {
	return commandTemplates;
    }

    /**
     * @return the resourcesLoader
     */
    public ClassLoader getResourcesLoader() {
	return resourcesLoader;
    }

    /**
     * @param resourcesLoader
     *            the resourcesLoader to set
     */
    public void setResourcesLoader(ClassLoader resourcesLoader) {
	this.resourcesLoader = resourcesLoader;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy