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