001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base.events;
020
021import java.io.IOException;
022import java.io.Writer;
023import java.util.Arrays;
024import org.jgrapes.core.Channel;
025import org.jgrapes.core.Components;
026
027/**
028 * A {@link ConsoleCommand} created directly from the JSON RPC method
029 * name and parameters.
030 */
031public class SimpleConsoleCommand extends ConsoleCommand {
032
033    private String method;
034    private Object[] params;
035
036    /**
037     * Instantiates a new web console command.
038     *
039     * @param method the method
040     * @param parameters the parameterss
041     */
042    public SimpleConsoleCommand(String method, Object... parameters) {
043        this.method = method;
044        this.params = Arrays.copyOf(parameters, parameters.length);
045    }
046
047    /**
048     * Instantiates a new web console page command.
049     *
050     * @param method the method
051     */
052    public SimpleConsoleCommand(String method) {
053        this(method, new Object[0]);
054    }
055
056    /**
057     * Sets the parameters.
058     *
059     * @param parameters the new parameters
060     */
061    public void setParameters(Object... parameters) {
062        this.params = Arrays.copyOf(parameters, parameters.length);
063    }
064
065    @Override
066    public void toJson(Writer writer) throws IOException {
067        toJson(writer, method, params);
068    }
069
070    /*
071     * (non-Javadoc)
072     * 
073     * @see java.lang.Object#toString()
074     */
075    @Override
076    public String toString() {
077        StringBuilder builder = new StringBuilder();
078        builder.append(Components.objectName(this))
079            .append(" [method=")
080            .append(method);
081        if (channels() != null) {
082            builder.append(", channels=")
083                .append(Channel.toString(channels()));
084        }
085        builder.append(']');
086        return builder.toString();
087    }
088
089}