Search in sources :

Example 1 with ChainConfig

use of org.walkmod.conf.entities.ChainConfig 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 ChainConfig

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

the class AddIncludesOrExcludesXMLAction method doAction.

@Override
public void doAction() throws Exception {
    Document document = provider.getDocument();
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    boolean chainFound = false;
    boolean containsChains = false;
    if (chain == null) {
        chain = "default";
    }
    for (int i = 0; i < childSize; i++) {
        Node childNode = children.item(i);
        if (childNode instanceof Element) {
            Element child = (Element) childNode;
            final String nodeName = child.getNodeName();
            String writerPath = "src/main/java";
            if ("chain".equals(nodeName)) {
                containsChains = true;
                String name = child.getAttribute("name");
                if (name.equals(chain)) {
                    chainFound = true;
                    NodeList chainChildren = child.getChildNodes();
                    int chainChildrenSize = chainChildren.getLength();
                    boolean existsReader = false;
                    boolean existsWriter = false;
                    for (int j = 0; j < chainChildrenSize; j++) {
                        Node chainChild = chainChildren.item(j);
                        if (chainChild instanceof Element) {
                            Element elementChain = (Element) chainChild;
                            String elementName = elementChain.getNodeName();
                            if (elementName.equals("reader") && setToReader) {
                                existsReader = true;
                                updateElement(elementChain);
                                writerPath = elementChain.getAttribute("path");
                            } else if (elementName.equals("writer") && setToWriter) {
                                existsWriter = true;
                                updateElement(elementChain);
                            }
                        }
                    }
                    if (!existsReader && setToReader) {
                        Element reader = document.createElement("reader");
                        reader.setAttribute("path", "src/main/java");
                        updateElement(reader);
                        if (chainChildrenSize == 0) {
                            child.appendChild(reader);
                        } else {
                            child.insertBefore(reader, chainChildren.item(0));
                        }
                    }
                    if (!existsWriter && setToWriter) {
                        Element writer = document.createElement("writer");
                        writer.setAttribute("path", writerPath);
                        updateElement(writer);
                        child.appendChild(writer);
                    }
                }
            }
        }
    }
    if (!chainFound) {
        ChainConfig chainCfg = null;
        if (!containsChains) {
            Configuration configuration = new ConfigurationImpl();
            provider.setConfiguration(configuration);
            // we write specifically a default chain, and
            // afterwards, we
            // add the requested one.
            provider.loadChains();
            Collection<ChainConfig> chainCfgs = configuration.getChainConfigs();
            chainCfg = chainCfgs.iterator().next();
            NodeList child = rootElement.getChildNodes();
            int limit = child.getLength();
            for (int i = 0; i < limit; i++) {
                Node item = child.item(i);
                if (item instanceof Element) {
                    Element auxElem = (Element) item;
                    if (auxElem.getNodeName().equals("transformation")) {
                        rootElement.removeChild(auxElem);
                    }
                }
            }
            if (!chain.equals("default")) {
                rootElement.appendChild(createChainElement(chainCfg));
                chainCfg = new ChainConfigImpl();
                chainCfg.setName(chain);
                provider.addDefaultReaderConfig(chainCfg);
                provider.addDefaultWriterConfig(chainCfg);
                provider.addDefaultWalker(chainCfg);
            }
        } else {
            chainCfg = new ChainConfigImpl();
            chainCfg.setName(chain);
            provider.addDefaultReaderConfig(chainCfg);
            provider.addDefaultWriterConfig(chainCfg);
            provider.addDefaultWalker(chainCfg);
        }
        if (setToReader) {
            ReaderConfig rcfg = chainCfg.getReaderConfig();
            String[] aux = new String[includes.size()];
            rcfg.setIncludes(includes.toArray(aux));
            chainCfg.setReaderConfig(rcfg);
        }
        if (setToWriter) {
            WriterConfig wcfg = chainCfg.getWriterConfig();
            String[] aux = new String[includes.size()];
            wcfg.setIncludes(includes.toArray(aux));
            chainCfg.setWriterConfig(wcfg);
        }
        rootElement.appendChild(createChainElement(chainCfg));
    }
    provider.persist();
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) Document(org.w3c.dom.Document) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl)

Example 3 with ChainConfig

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

the class LanguageConfigurationProvider method updateNulls.

private void updateNulls() {
    Collection<ChainConfig> ccs = configuration.getChainConfigs();
    Element rootElement = document.getDocumentElement();
    if (ccs != null) {
        for (ChainConfig cc : ccs) {
            ReaderConfig rc = cc.getReaderConfig();
            if (rc.getType() == null) {
                rc.setType(rootElement.getAttribute("reader"));
            }
            if (rc.getPath() == null) {
                rc.setPath(rootElement.getAttribute("path"));
            }
            WriterConfig wc = cc.getWriterConfig();
            if (wc.getType() == null) {
                wc.setType(rootElement.getAttribute("writer"));
            }
            if (wc.getPath() == null) {
                wc.setPath(rootElement.getAttribute("path"));
            }
            WalkerConfig walkc = cc.getWalkerConfig();
            if (walkc.getType() == null) {
                walkc.setType(rootElement.getAttribute("walker"));
            }
            if (walkc.getParserConfig().getType() == null) {
                if (!"".equals(rootElement.getAttribute("parser"))) {
                    walkc.getParserConfig().setType(rootElement.getAttribute("parser"));
                }
            }
            List<TransformationConfig> transformations = walkc.getTransformations();
            if (transformations != null) {
                for (TransformationConfig tc : transformations) {
                    if (tc.isMergeable()) {
                        if (tc.getMergePolicy() == null) {
                            tc.setMergePolicy(DEFAULT_MERGE_ENGINE_NAME);
                        }
                    }
                }
            }
            wc.setPatcherType(rootElement.getAttribute("patcher"));
        }
    }
}
Also used : WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Example 4 with ChainConfig

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

the class ConfigurationImpl method executeChain.

public void executeChain(String userDir, Options options, ChainAdapterFactory apf, String name) {
    if (options.getIncludes() != null || options.getExcludes() != null) {
        Collection<ChainConfig> chains = getChainConfigs();
        if (chains != null) {
            for (ChainConfig cc : chains) {
                if (options.getIncludes() != null) {
                    String[] includes = options.getIncludes().toArray(new String[options.getIncludes().size()]);
                    cc.getReaderConfig().setIncludes(includes);
                }
                if (options.getExcludes() != null) {
                    String[] excludes = options.getExcludes().toArray(new String[options.getExcludes().size()]);
                    cc.getReaderConfig().setExcludes(excludes);
                }
            }
        }
    }
    ChainAdapter ap = apf.createChainProxy(this, name);
    if (ap == null) {
        if (options.isVerbose()) {
            log.error("The chain " + name + " is not found");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
    } else {
        long startTime = System.currentTimeMillis();
        long endTime = startTime;
        DecimalFormat myFormatter = new DecimalFormat("###.###");
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US);
        if (options.isVerbose()) {
            log.info("** THE TRANSFORMATION CHAIN " + name + " STARTS **");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
        int num = 0;
        try {
            int size = getChainConfigs().size();
            ap.execute();
            // we check if some other chain config has been added and execute them
            if (getChainConfigs().size() > size) {
                LinkedList<ChainConfig> aux = new LinkedList<ChainConfig>(getChainConfigs());
                Iterator<ChainConfig> it = aux.listIterator(size);
                while (it.hasNext()) {
                    ChainConfig tcfg = it.next();
                    ChainAdapter auxAp = apf.createChainProxy(this, tcfg.getName());
                    auxAp.execute();
                }
            }
            num = ap.getWalkerAdapter().getWalker().getNumModifications();
            if (options.isVerbose()) {
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                if (num != 0) {
                    System.out.print("----------------------------------------");
                    System.out.println("----------------------------------------");
                } else {
                    if (Summary.getInstance().getWrittenFiles().isEmpty()) {
                        log.info("**No sources changed**");
                    }
                }
                System.out.println();
                log.info("TRANSFORMATION CHAIN SUCCESS");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                if (ap.getWalkerAdapter().getWalker().reportChanges()) {
                    log.info("Total modified files: " + num);
                }
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
            }
        } catch (Throwable e) {
            System.setProperty("user.dir", userDir);
            if (options.isVerbose()) {
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                if (num != 0) {
                    System.out.print("----------------------------------------");
                    System.out.println("----------------------------------------");
                }
                log.info("TRANSFORMATION CHAIN FAILS");
                System.out.println();
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Please, see the walkmod log file for details");
                if (options.isPrintErrors()) {
                    log.error("TRANSFORMATION CHAIN (" + name + ") FAILS", e);
                } else {
                    log.error("TRANSFORMATION CHAIN (" + name + ") FAILS. Execute walkmod with -e to see the error details.");
                }
            } else {
                throw new WalkModException(e);
            }
            return;
        }
    }
}
Also used : WalkModException(org.walkmod.exceptions.WalkModException) DecimalFormat(java.text.DecimalFormat) LinkedList(java.util.LinkedList) Date(java.util.Date) ChainConfig(org.walkmod.conf.entities.ChainConfig) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ChainAdapter(org.walkmod.ChainAdapter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with ChainConfig

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

the class PrintChainsCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("chains");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().configurationFile(configurationFile).build());
        Configuration cfg = facade.getConfiguration();
        at = new V2_AsciiTable();
        at.addRule();
        at.addRow("CHAIN", "READER PATH", "WRITER PATH", "TRANSFORMATIONS");
        at.addStrongRule();
        if (cfg == null) {
            at.addRule();
            log.error("Sorry, the current directory does not contain a walkmod configuration file or it is invalid.");
        }
        if (cfg != null) {
            Collection<ChainConfig> chains = cfg.getChainConfigs();
            if (chains != null) {
                for (ChainConfig cc : chains) {
                    List<TransformationConfig> transformations = cc.getWalkerConfig().getTransformations();
                    int numTransformations = transformations.size();
                    int numReaderIncludesExcludes = 0;
                    int includesLength = 0;
                    String[] excludes = cc.getReaderConfig().getExcludes();
                    if (excludes != null) {
                        numReaderIncludesExcludes = excludes.length;
                    }
                    String[] includes = cc.getReaderConfig().getIncludes();
                    if (includes != null) {
                        includesLength = includes.length;
                        numReaderIncludesExcludes += includes.length;
                    }
                    int limit = numReaderIncludesExcludes + 1;
                    if (numTransformations > numReaderIncludesExcludes) {
                        limit = numTransformations;
                    }
                    int includesExcludesWriterLength = 0;
                    int includesWriterLength = 0;
                    String[] excludesWriter = cc.getWriterConfig().getExcludes();
                    if (excludesWriter != null) {
                        includesExcludesWriterLength += excludesWriter.length;
                        if (excludesWriter.length + 1 > limit) {
                            limit = excludesWriter.length + 1;
                        }
                    }
                    String[] includesWriter = cc.getWriterConfig().getIncludes();
                    if (includesWriter != null) {
                        includesExcludesWriterLength += includesWriter.length;
                        includesWriterLength = includesWriter.length;
                        if (includesWriter.length + 1 > limit) {
                            limit = includesWriter.length + 1;
                        }
                    }
                    for (int i = 0; i < limit; i++) {
                        TransformationConfig next = null;
                        String type = "";
                        if (i < numTransformations) {
                            next = transformations.get(i);
                            type = "- " + next.getType();
                        }
                        if (i == 0) {
                            at.addRow(cc.getName(), cc.getReaderConfig().getPath(), cc.getWriterConfig().getPath(), type);
                        } else {
                            String readerWildcard = "";
                            if (i - 1 < includesLength) {
                                readerWildcard = "> " + includes[i - 1];
                            } else {
                                if (i - 1 < numReaderIncludesExcludes) {
                                    readerWildcard = "< " + excludes[i - 1 + includesLength];
                                }
                            }
                            String writerWildcard = "";
                            if (includesWriter != null && i - 1 < includesWriter.length) {
                                writerWildcard = "> " + includesWriter[i - 1];
                            } else if (i - 1 < includesExcludesWriterLength) {
                                writerWildcard = "< " + excludesWriter[i - 1 + includesWriterLength];
                            }
                            at.addRow("", readerWildcard, writerWildcard, type);
                        }
                    }
                    at.addRule();
                }
            }
        }
    }
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Aggregations

ChainConfig (org.walkmod.conf.entities.ChainConfig)22 Configuration (org.walkmod.conf.entities.Configuration)11 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)11 LinkedList (java.util.LinkedList)8 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)8 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)8 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)6 Test (org.junit.Test)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 PluginConfig (org.walkmod.conf.entities.PluginConfig)4 Document (org.w3c.dom.Document)3 ChainAdapter (org.walkmod.ChainAdapter)3 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)3 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)3 WriterConfig (org.walkmod.conf.entities.WriterConfig)3 WalkModException (org.walkmod.exceptions.WalkModException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)2