org.shaf.shell.action.ActionContext.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.shell.action.ActionContext.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 org.shaf.shell.action;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.shaf.client.net.ClientController;
import org.shaf.client.net.ClientException;
import org.shaf.core.content.ProgressContent;
import org.shaf.core.content.StructuredContent;
import org.shaf.core.content.UnstructuredContent;
import org.shaf.core.util.ClassUtils;
import org.shaf.shell.io.Terminal;
import org.shaf.shell.view.CollectionView;
import org.shaf.shell.view.ConfigurationView;
import org.shaf.shell.view.MapView;
import org.shaf.shell.view.ProgressView;
import org.shaf.shell.view.StructuredView;
import org.shaf.shell.view.UnstructuredView;

import com.google.common.base.Strings;

/**
 * The collection of {@link Action action} resources.
 * 
 * @author Mykola Galushka
 */
public class ActionContext {

    // The property constants.
    private final static String SERVER_ADDRESS = "shaf.server.address";
    private final static String SERVER_USERNAME = "shaf.server.username";
    private final static String SERVER_PASSWORD = "shaf.server.password";

    /**
     * The reference to the actions register.
     */
    private final ActionRegister register;

    /**
     * The shell configuration.
     */
    private final Configuration config;

    /**
     * The terminal for output information.
     */
    private final Terminal terminal;

    /**
     * Constructs a new action resource.
     * 
     * @param register
     *            the reference to the actions register.
     * @param config
     *            the shell configuration.
     * @param terminal
     *            the terminal for output information.
     */
    public ActionContext(final ActionRegister register, final Configuration config, final Terminal terminal)
            throws Exception {
        this.register = register;
        this.config = config;
        this.terminal = terminal;
    }

    /**
     * Returns the action register.
     * 
     * @return the action register.
     */
    public ActionRegister getRegister() {
        return this.register;
    }

    /**
     * Returns the shell configuration.
     * 
     * @return the shell configuration.
     */
    public Configuration getConfig() {
        return this.config;
    }

    /**
     * Returns the client controller.
     * 
     * @return the client controller.
     * @throws ClientException
     *             if failed to initialize the client controller.
     */
    public ClientController getController() throws ClientException {
        return ClientController.getController(this.config.getString(SERVER_ADDRESS),
                this.config.getString(SERVER_USERNAME), this.config.getString(SERVER_PASSWORD));
    }

    /**
     * Shows the output content with title.
     * 
     * @param title
     *            the title to show.
     * @param content
     *            the content to show.
     */
    public void show(final String title, final boolean underscore, final Object content) {
        if (!Strings.isNullOrEmpty(title)) {
            /*
             * It the title is not null or empty, it should be printed to the
             * console before the returned content of executed command. The
             * title always separated with one "empty" line, and can be
             * underscored with '=' is according flag is set.
             */
            this.terminal.println("");
            this.terminal.println(title);

            if (underscore) {
                this.terminal.println(Strings.repeat("=", title.length()));
            }
        }

        /*
         * The return content can have s different type and as a result requires
         * its own visualization style. The next block of conditions try to
         * select an appropriate view for the returned content type and show its
         * data.
         */
        if (content instanceof ProgressContent) {
            new ProgressView(terminal).show((ProgressContent) content);
        } else if (content instanceof StructuredContent) {
            new StructuredView(terminal).show((StructuredContent) content);
        } else if (content instanceof UnstructuredContent) {
            new UnstructuredView(terminal).show((UnstructuredContent) content);
        } else if (ClassUtils.inherits(content, Configuration.class)) {
            new ConfigurationView(terminal).show((Configuration) content);
        } else if (ClassUtils.inherits(content, Map.class)) {
            new MapView(terminal).show((Map<?, ?>) content);
        } else if (ClassUtils.inherits(content, Collection.class)) {
            new CollectionView(terminal).show((Collection<?>) content);
        } else {
            this.terminal.print(content.toString());
        }
    }

    /**
     * Shows the output content without title.
     * 
     * @param content
     *            the content to show.
     */
    public void show(Object content) {
        this.show(null, false, content);
    }
}