jef.ui.console.DefaultBatchConsoleShell.java Source code

Java tutorial

Introduction

Here is the source code for jef.ui.console.DefaultBatchConsoleShell.java

Source

/*
 * JEF - Copyright 2009-2010 Jiyi (mr.jiyi@gmail.com)
 *
 * 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 jef.ui.console;

import java.util.ArrayList;
import java.util.List;

import jef.common.log.LogUtil;
import jef.ui.ConsoleShell;

import org.apache.commons.lang.ArrayUtils;

public abstract class DefaultBatchConsoleShell extends AbstractConsoleShell {
    public DefaultBatchConsoleShell(ConsoleShell parent) {
        super(parent);
    }

    protected List<String> commandPool = new ArrayList<String>();

    protected int poolSize() {
        return commandPool.size();
    }

    public final ShellResult performCommand(String str, String... params) {
        int type = appendCommand(str);
        if (type == RETURN_READY) {
            try {
                executeEnd(commandPool.toArray(ArrayUtils.EMPTY_STRING_ARRAY), str);
                commandPool.clear();
            } catch (Throwable e) {
                LogUtil.exception(e);
            }
            if (isMultiBatch())
                return ShellResult.CONTINUE;
            return ShellResult.TERMINATE;
        } else if (type == RETURN_CONTINUE) {
            return ShellResult.CONTINUE;
        } else if (type == RETURN_TERMINATE) {
            commandPool.clear();
            return ShellResult.TERMINATE;
        } else {
            commandPool.clear();
            if (isMultiBatch())
                return ShellResult.CONTINUE;
            return ShellResult.TERMINATE;
        }
    }

    /**
     * ??Shell???
     * 
     * @return
     */
    protected boolean isMultiBatch() {
        return false;
    }

    public static final int RETURN_READY = -1;
    public static final int RETURN_CANCEL = 1;
    public static final int RETURN_CONTINUE = 0;
    public static final int RETURN_TERMINATE = -2;

    /**
     * 
     * @param str
     * @return
     * RETURN_READY batch??
     * RETURN_CANCEL batch???
     * RETURN_CONTINUE batch
     * RETURN_TERMINATE batch??shell
     */
    protected abstract int appendCommand(String str);

    protected abstract void executeEnd(String[] strings, String str);
}