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

base.Commands Maven / Gradle / Ivy

/**
 * Creative commons Attribution-NonCommercial license.
 *
 * http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
 *
 * NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
 */
package base;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import base.security.PermissionException;

public class Commands extends Command {

	private List commands = new LinkedList();
	private List failedCommands = new LinkedList();

	@Override
	public Command newWithParameters(Map parameters) throws IOException, PermissionException {
		throw new IllegalArgumentException("Commands can not be instantiated with parameter map.");
	}

	public void addCommand(Command c) {
		commands.add(c);
	}

	public void execute() throws IOException {
		for(Command c : commands) {
			try {
				c.run();
			} catch(IOException e) {
				failedCommands.add(new Entry(c, e));
			}
		}
		if(failedCommands.size() != 0) {
			throw new IOException("One or more of the command set failed.");
		}
	}

	public List getFailedCommands() {
		return failedCommands;
	}

	public String getJsonParameters() {
		StringBuffer s = new StringBuffer();
		s.append("\"commands\":[");
		for(Command c : commands) {
			s.append(c.toJson());
			s.append(",");
		}
		if(commands.size() != 0) {
			s = s.deleteCharAt(s.length()-1);
		}
		s.append("]");
		return s.toString();
	}

	public class Entry {

		public Command command;
		public IOException exception;

		public Entry(Command command, IOException exception) {
			this.command = command;
			this.exception = exception;
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy