samplecode.tools.BasicToolCompletedProcessing.java Source code

Java tutorial

Introduction

Here is the source code for samplecode.tools.BasicToolCompletedProcessing.java

Source

/*
 * Copyright 2008-2011 UnboundID Corp. All Rights Reserved.
 */
/*
 * Copyright (C) 2008-2011 UnboundID Corp. This program is free
 * software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License (GPLv2 only) or the terms of the GNU
 * Lesser General Public License (LGPLv2.1 only) as published by the
 * Free Software Foundation. This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details. You
 * should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses>.
 */

package samplecode.tools;

import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.util.CommandLineTool;
import org.apache.commons.logging.Log;
import samplecode.annotation.Author;
import samplecode.annotation.CodeVersion;
import samplecode.annotation.Since;
import samplecode.util.StaticData;

import java.io.PrintStream;

import static com.unboundid.util.Validator.ensureNotNull;

/**
 * Provides a generic message for logging when a {@link CommandLineTool}
 * object has completed processing.
 * {@link CommandLineTool#doToolProcessing()} does not throw an
 * exception but provides a result code.
 */
@Author("terry.gardner@unboundid.com")
@Since("Dec 24, 2011")
@CodeVersion("1.4")
public class BasicToolCompletedProcessing implements ToolCompletedProcessing {

    private static final String KEY_COMPLETED_MSG_FORMAT = "tool-completed-message-fmt";

    /**
     * Creates a {@code BasicToolCompletedProcessing} with default state.
     *
     * @param tool
     *   the {@code CommandLineTool} that has completed processing.
     *   {@code tool} is not permitted to be {@code null}.
     * @param resultCode
     *   the result code returned from the tool. @code resultCode}
     *   is not permitted to be {@code null}.
     */
    public BasicToolCompletedProcessing(final CommandLineTool tool, final ResultCode resultCode) {
        ensureNotNull(tool, resultCode);

        this.tool = tool;
        this.resultCode = resultCode;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String createMsg() {
        final String fmt = StaticData.getResourceBundle().getString(KEY_COMPLETED_MSG_FORMAT);
        return String.format(fmt, tool.getToolName(), resultCode);
    }

    /**
     * {@inheritDoc}
     * <p/>
     * precondition: result code, tool, outStream and errStream cannot be
     * {@code null}.
     */
    @Override
    public void displayMessage(PrintStream outStream, PrintStream errStream) {
        if (outStream == null) {
            throw new IllegalArgumentException("outStream must not be null.");
        }
        if (errStream == null) {
            throw new IllegalArgumentException("errStream must not be null.");
        }
        PrintStream str;
        if (resultCode.equals(ResultCode.SUCCESS)) {
            str = outStream;
        } else {
            str = errStream;
        }
        str.println(createMsg());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void displayMessage(final Log logger) {
        if (resultCode.equals(ResultCode.SUCCESS)) {
            logger.info(createMsg());
        } else {
            logger.fatal(createMsg());
        }
    }

    // the result code generated by the processing of CommandLineTool
    private final ResultCode resultCode;

    // The tool the completed processing.
    private final CommandLineTool tool;

}