001package com.ganteater.ae.maven.plugin;
002
003import java.io.File;
004import java.io.FileReader;
005import java.io.IOException;
006import java.util.Enumeration;
007import java.util.List;
008import java.util.Map;
009import java.util.Properties;
010import java.util.Set;
011
012import org.apache.commons.lang.StringUtils;
013import org.apache.maven.plugin.AbstractMojo;
014import org.apache.maven.plugin.MojoExecutionException;
015import org.apache.maven.plugin.descriptor.PluginDescriptor;
016import org.apache.maven.plugins.annotations.Parameter;
017import org.apache.maven.project.MavenProject;
018
019import com.ganteater.ae.AEWorkspace;
020import com.ganteater.ae.CommandException;
021import com.ganteater.ae.ConfigConstants;
022import com.ganteater.ae.RecipeRunner;
023
024public abstract class AEBase extends AbstractMojo {
025
026        @Parameter(readonly = true, defaultValue = "${project}")
027        protected MavenProject project;
028
029        @Parameter(defaultValue = "")
030        protected String aeDir;
031
032        @Parameter(defaultValue = "${plugin}", readonly = true)
033        protected PluginDescriptor plugin;
034
035        @Parameter
036        protected Map<String, String> variables;
037
038        @Parameter
039        protected List<File> propertiesFiles;
040
041        @Parameter
042        protected String config;
043
044        @Parameter
045        protected List<String> operationsDependencies;
046
047        protected Set<Class> operations;
048
049        @Parameter
050        protected String[] recipes;
051
052        protected void importVariables(AEWorkspace workspace) {
053                Map<String, Object> systemVariables = workspace.getSystemVariables();
054                if (variables != null) {
055                        variables.entrySet().stream().forEach(e -> systemVariables.put(e.getKey().toUpperCase(), e.getValue()));
056                }
057
058                if (propertiesFiles != null) {
059                        propertiesFiles.stream().forEach(file -> {
060                                Properties props = new Properties();
061                                try {
062                                        props.load(new FileReader(file));
063                                } catch (IOException e) {
064                                        e.printStackTrace();
065                                }
066                                Enumeration<Object> keys = props.keys();
067                                while (keys.hasMoreElements()) {
068                                        String key = (String) keys.nextElement();
069                                        systemVariables.put(key.toUpperCase(), props.get(key));
070                                }
071                        });
072                }
073        }
074
075        protected void runRecipes(AEWorkspace workspace, boolean async) throws MojoExecutionException, CommandException {
076                if (recipes == null) {
077                        String sysRecipes = System.getProperty(ConfigConstants.RECIPES_PARAM_NAME);
078                        if (sysRecipes != null) {
079                                recipes = StringUtils.split(sysRecipes, ',');
080                        }
081                }
082
083                if (recipes != null) {
084                        for (String name : recipes) {
085                                RecipeRunner runTask = workspace.runTask(name, async);
086                                if (!async && runTask.getStatus() == RecipeRunner.STATUS_FAILED) {
087                                        throw new MojoExecutionException("Status: Failed. See logs for details Reason for failure.");
088                                }
089                        }
090                }
091        }
092
093}