Search in sources :

Example 1 with ProviderConfig

use of org.walkmod.conf.entities.ProviderConfig in project walkmod-core by walkmod.

the class YAMLConfigurationProvider method load.

@Override
public void load() throws ConfigurationException {
    File file = new File(fileName);
    try {
        JsonNode node = null;
        if (file.exists() && file.length() > 0) {
            node = mapper.readTree(file);
            configuration.prepareInitializers();
            if (node.has("plugins")) {
                Iterator<JsonNode> it = node.get("plugins").iterator();
                Collection<PluginConfig> pluginList = new LinkedList<PluginConfig>();
                while (it.hasNext()) {
                    JsonNode current = it.next();
                    String pluginId = current.asText();
                    String[] split = pluginId.split(":");
                    if (split.length > 3) {
                    } else {
                        String groupId, artifactId, version;
                        groupId = split[0];
                        artifactId = split[1];
                        version = split[2];
                        PluginConfig plugin = new PluginConfigImpl();
                        plugin.setGroupId(groupId);
                        plugin.setArtifactId(artifactId);
                        plugin.setVersion(version);
                        pluginList.add(plugin);
                    }
                }
                configuration.setPlugins(pluginList);
            }
            if (node.has("modules")) {
                Iterator<JsonNode> it = node.get("modules").iterator();
                List<String> modules = new LinkedList<String>();
                configuration.setModules(modules);
                while (it.hasNext()) {
                    JsonNode current = it.next();
                    modules.add(current.asText());
                }
                configuration.setModules(modules);
            }
            if (node.has("merge-policies")) {
                Iterator<JsonNode> it = node.get("merge-policies").iterator();
                Collection<MergePolicyConfig> mergePolicies = new LinkedList<MergePolicyConfig>();
                while (it.hasNext()) {
                    JsonNode next = it.next();
                    if (next.has("policy")) {
                        MergePolicyConfig mergeCfg = new MergePolicyConfigImpl();
                        mergeCfg.setName(next.get("name").asText());
                        mergeCfg.setDefaultObjectPolicy(next.get("default-object-policy").asText());
                        mergeCfg.setDefaultTypePolicy(next.get("default-type-policy").asText());
                        if (next.has("policy")) {
                            Iterator<JsonNode> it2 = next.get("policy").iterator();
                            Map<String, String> policies = new HashMap<String, String>();
                            while (it2.hasNext()) {
                                JsonNode nextPolicy = it2.next();
                                String objectType = nextPolicy.get("object-type").asText();
                                String policyType = nextPolicy.get("policy-type").asText();
                                policies.put(objectType, policyType);
                            }
                            mergeCfg.setPolicyEntries(policies);
                        }
                        mergePolicies.add(mergeCfg);
                    }
                }
                configuration.setMergePolicies(mergePolicies);
            }
            if (node.has("conf-providers")) {
                Iterator<JsonNode> it = node.get("conf-providers").iterator();
                Collection<ProviderConfig> provConfigs = new LinkedList<ProviderConfig>();
                while (it.hasNext()) {
                    JsonNode next = it.next();
                    ProviderConfig provCfg = new ProviderConfigImpl();
                    provCfg.setType(next.get("type").asText());
                    provCfg.setParameters(converter.getParams(next));
                    provConfigs.add(provCfg);
                }
                configuration.setProviderConfigurations(provConfigs);
            }
            if (node.has("chains")) {
                Iterator<JsonNode> it = node.get("chains").iterator();
                Collection<ChainConfig> chains = new LinkedList<ChainConfig>();
                int i = 0;
                while (it.hasNext()) {
                    ChainConfig chainCfg = new ChainConfigImpl();
                    JsonNode current = it.next();
                    if (current.has("name")) {
                        chainCfg.setName(current.get("name").asText());
                    } else {
                        chainCfg.setName("chain_" + i);
                    }
                    if (current.has("reader")) {
                        JsonNode reader = current.get("reader");
                        chainCfg.setReaderConfig(converter.getReader(reader));
                    } else {
                        addDefaultReaderConfig(chainCfg);
                    }
                    if (current.has("writer")) {
                        JsonNode writer = current.get("writer");
                        chainCfg.setWriterConfig(converter.getWriter(writer));
                    } else {
                        addDefaultWriterConfig(chainCfg);
                    }
                    if (current.has("walker")) {
                        chainCfg.setWalkerConfig(converter.getWalker(current));
                    } else {
                        addDefaultWalker(chainCfg);
                        if (current.has("transformations")) {
                            WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
                            walkerCfg.setTransformations(converter.getTransformationCfgs(current));
                        }
                    }
                    chains.add(chainCfg);
                }
                configuration.setChainConfigs(chains);
            } else if (node.has("transformations")) {
                Collection<ChainConfig> chains = new LinkedList<ChainConfig>();
                ChainConfig chainCfg = new ChainConfigImpl();
                chainCfg.setName("");
                addDefaultReaderConfig(chainCfg);
                addDefaultWalker(chainCfg);
                WalkerConfig walkerCfg = chainCfg.getWalkerConfig();
                walkerCfg.setTransformations(converter.getTransformationCfgs(node));
                addDefaultWriterConfig(chainCfg);
                chains.add(chainCfg);
                configuration.setChainConfigs(chains);
            }
        }
    } catch (JsonProcessingException e) {
        throw new ConfigurationException("Error parsing the " + fileName + " configuration", e);
    } catch (IOException e) {
        throw new ConfigurationException("Error reading the " + fileName + " configuration", e);
    }
    configuration.preparePlugins();
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) HashMap(java.util.HashMap) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig) ConfigurationException(org.walkmod.conf.ConfigurationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) IOException(java.io.IOException) MergePolicyConfigImpl(org.walkmod.conf.entities.impl.MergePolicyConfigImpl) LinkedList(java.util.LinkedList) PluginConfig(org.walkmod.conf.entities.PluginConfig) Collection(java.util.Collection) File(java.io.File) ProviderConfigImpl(org.walkmod.conf.entities.impl.ProviderConfigImpl)

Example 2 with ProviderConfig

use of org.walkmod.conf.entities.ProviderConfig in project walkmod-core by walkmod.

the class XMLConfigurationProvider method loadProviders.

private void loadProviders() {
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Collection<ProviderConfig> providers = new LinkedList<ProviderConfig>();
    int childSize = children.getLength();
    for (int i = 0; i < childSize; i++) {
        Node childNode = children.item(i);
        if ("conf-providers".equals(childNode.getNodeName())) {
            Element child = (Element) childNode;
            NodeList providersNodes = child.getChildNodes();
            int providersSize = providersNodes.getLength();
            for (int j = 0; j < providersSize; j++) {
                Node providerNode = providersNodes.item(j);
                if ("conf-provider".equals(providerNode.getNodeName())) {
                    Element providerElem = (Element) providerNode;
                    ProviderConfig pc = new ProviderConfigImpl();
                    pc.setType(providerElem.getAttribute("type"));
                    pc.setParameters(getParams(providerElem));
                    providers.add(pc);
                }
            }
        }
    }
    configuration.setProviderConfigurations(providers);
}
Also used : ProviderConfig(org.walkmod.conf.entities.ProviderConfig) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ProviderConfigImpl(org.walkmod.conf.entities.impl.ProviderConfigImpl) LinkedList(java.util.LinkedList)

Example 3 with ProviderConfig

use of org.walkmod.conf.entities.ProviderConfig in project walkmod-core by walkmod.

the class PrintProvidersCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        jcommander.usage("modules");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options());
        Configuration cfg = facade.getConfiguration();
        if (cfg == null) {
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
        }
        at = new V2_AsciiTable();
        at.addRule();
        at.addRow("CONFIGURATION PROVIDERS", "PARAMETERS");
        at.addRule();
        if (cfg != null) {
            Collection<ProviderConfig> providers = cfg.getProviderConfigurations();
            if (providers != null) {
                for (ProviderConfig provider : providers) {
                    Map<String, Object> params = provider.getParameters();
                    if (params == null) {
                        at.addRow(provider.getType(), "");
                    } else {
                        Set<String> keys = params.keySet();
                        int i = 0;
                        for (String key : keys) {
                            if (i == 0) {
                                at.addRow(provider.getType(), params.get(key).toString());
                            } else {
                                at.addRow("", params.get(key).toString());
                            }
                            i++;
                        }
                    }
                    at.addRule();
                }
            }
        }
        at.addRule();
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) WalkModFacade(org.walkmod.WalkModFacade)

Example 4 with ProviderConfig

use of org.walkmod.conf.entities.ProviderConfig in project walkmod-core by walkmod.

the class AddCfgProviderCommand method build.

public ProviderConfig build() throws Exception {
    ProviderConfig prov = new ProviderConfigImpl();
    prov.setType(type.get(0));
    if (params != null) {
        prov.setParameters(new HashMap<String, Object>(params));
    }
    return prov;
}
Also used : ProviderConfig(org.walkmod.conf.entities.ProviderConfig) ProviderConfigImpl(org.walkmod.conf.entities.impl.ProviderConfigImpl)

Example 5 with ProviderConfig

use of org.walkmod.conf.entities.ProviderConfig in project walkmod-core by walkmod.

the class XMLConfigurationProviderTest method testConfigProvidersConfigRecursively.

@Test
public void testConfigProvidersConfigRecursively() throws Exception {
    AddCfgProviderCommand command = new AddCfgProviderCommand("maven", null);
    File aux = new File("src/test/resources/xmlmultimodule");
    aux.mkdirs();
    File module0 = new File(aux, "module0");
    module0.mkdir();
    File xml = new File(aux, "walkmod.xml");
    File cfg = new File(module0, "walkmod.xml");
    XMLConfigurationProvider prov = new XMLConfigurationProvider(xml.getPath(), false);
    try {
        prov.createConfig();
        ProviderConfig provCfg = command.build();
        prov.addModules(Arrays.asList("module0"));
        prov.addProviderConfig(provCfg, true);
        String output = FileUtils.readFileToString(cfg);
        System.out.println(output);
        Assert.assertTrue(output.contains("maven"));
    } finally {
        if (xml.exists()) {
            xml.delete();
        }
    }
}
Also used : AddCfgProviderCommand(org.walkmod.commands.AddCfgProviderCommand) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) File(java.io.File) Test(org.junit.Test)

Aggregations

ProviderConfig (org.walkmod.conf.entities.ProviderConfig)13 File (java.io.File)7 Test (org.junit.Test)7 AddCfgProviderCommand (org.walkmod.commands.AddCfgProviderCommand)6 Configuration (org.walkmod.conf.entities.Configuration)5 LinkedList (java.util.LinkedList)4 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)4 ChainConfig (org.walkmod.conf.entities.ChainConfig)3 PluginConfig (org.walkmod.conf.entities.PluginConfig)3 ProviderConfigImpl (org.walkmod.conf.entities.impl.ProviderConfigImpl)3 HashMap (java.util.HashMap)2 MergePolicyConfig (org.walkmod.conf.entities.MergePolicyConfig)2 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1