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

com.smartling.maven.plugins.smartlingapi.mojo.AbstractSmartlingApiMojo Maven / Gradle / Ivy

The newest version!
package com.smartling.maven.plugins.smartlingapi.mojo;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.exceptions.ApiException;
import com.smartling.maven.plugins.smartlingapi.SmartlingApiMavenPluginUtils;

abstract class AbstractSmartlingApiMojo extends AbstractMojo
{
    private static final String SKIP_MOJO_EXECUTION_INFO_MESSAGE =
            SmartlingApiMavenPluginUtils.SMARTLING_API_PLUGIN_NAME + " skipping goal %s";


    /**
     * The enclosing maven project.
     */
    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    private MavenProject mavenProject;

    /**
     * Smartling API URL.
     */
    @Parameter(defaultValue = "https://api.smartling.com/v1", required = true)
    private String smartlingApiUrl;

    /**
     * Smartling API key.
     */
    @Parameter(required = true)
    private String apiKey;

    /**
     * Specifies if the build should fail if an error occurs.
     */
    @Parameter(defaultValue = "false")
    private boolean failsOnError;

    /**
     * Skip the goal.
     */
    @Parameter(defaultValue = "false")
    private boolean skip;


    protected MavenProject getMavenProject()
    {
        return mavenProject;
    }

    protected String getSmartlingApiUrl()
    {
        return smartlingApiUrl;
    }

    protected String getApiKey()
    {
        return apiKey;
    }

    protected boolean isFailsOnError()
    {
        return failsOnError;
    }

    protected boolean isSkip()
    {
        return skip;
    }


    public abstract String getGoalName();

    @Override
    public final void execute() throws MojoExecutionException, MojoFailureException
    {
        if (isSkip())
            getLog().info(String.format(SKIP_MOJO_EXECUTION_INFO_MESSAGE, getGoalName()));
        else
            doExecute();
    }

    public abstract void doExecute() throws MojoExecutionException, MojoFailureException;

    protected void handleError(final String errorMessage, final Exception cause) throws MojoFailureException
    {
        if (isFailsOnError())
            throw new MojoFailureException(errorMessage, cause);
        else
            getLog().warn(errorMessage, cause);
    }


    protected void checkApiResponseSuccess(final ApiResponse apiResponse) throws ApiException
    {
        if (!apiResponse.getCode().equals(SmartlingApiMavenPluginUtils.SMARTLING_API_SUCCESS_CODE))
        {
            throw new ApiException(String.format(
                    "Smartling API returned non-success status code: %s.\nMessages:\n{\n%s\n}",
                    apiResponse.getCode(),
                    StringUtils.join(apiResponse.getMessages(), "\n")));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy