Search in sources :

Example 1 with ChainConfigImpl

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

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

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

the class AddTransformationYMLAction method doAction.

@Override
public void doAction(JsonNode chainsNode) throws Exception {
    ArrayNode transformationsNode = null;
    boolean isMultiModule = chainsNode.has("modules");
    ObjectMapper mapper = provider.getObjectMapper();
    if (!isMultiModule) {
        boolean validChainName = chain != null && !"".equals(chain) && !"default".equals(chain);
        if (!chainsNode.has("chains")) {
            if (chainsNode.has("transformations")) {
                JsonNode aux = chainsNode.get("transformations");
                if (aux.isArray()) {
                    transformationsNode = (ArrayNode) aux;
                }
                if (!validChainName) {
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    if (transformationsNode == null) {
                        transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    }
                    auxRoot.set("transformations", transformationsNode);
                } else {
                    // reset the root
                    chainsNode = new ObjectNode(mapper.getNodeFactory());
                    ObjectNode auxRoot = (ObjectNode) chainsNode;
                    // the default chain list added
                    ObjectNode chainObject = new ObjectNode(mapper.getNodeFactory());
                    chainObject.set("name", new TextNode("default"));
                    chainObject.set("transformations", transformationsNode);
                    ArrayNode chainsListNode = new ArrayNode(mapper.getNodeFactory());
                    // the requested chain added
                    ObjectNode newChain = new ObjectNode(mapper.getNodeFactory());
                    newChain.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        newChain.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                    newChain.set("transformations", transformationsNode);
                    if (before == null || !"default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    chainsListNode.add(newChain);
                    if (before != null && "default".equals(before)) {
                        chainsListNode.add(chainObject);
                    }
                    auxRoot.set("chains", chainsListNode);
                }
            } else {
                ObjectNode auxRoot = (ObjectNode) chainsNode;
                transformationsNode = new ArrayNode(mapper.getNodeFactory());
                boolean writeChainInfo = validChainName;
                if (!writeChainInfo) {
                    writeChainInfo = path != null && !"".equals(path.trim());
                    chain = "default";
                }
                if (writeChainInfo) {
                    ArrayNode auxChainsList = new ArrayNode(mapper.getNodeFactory());
                    ObjectNode aux = new ObjectNode(mapper.getNodeFactory());
                    auxChainsList.add(aux);
                    aux.set("name", new TextNode(chain));
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("reader", readerNode);
                        populateWriterReader(readerNode, path, null, null, null, null);
                    }
                    auxRoot.set("chains", auxChainsList);
                    if (path != null && !"".equals(path.trim())) {
                        ObjectNode writerNode = new ObjectNode(mapper.getNodeFactory());
                        aux.set("writer", writerNode);
                        populateWriterReader(writerNode, path, null, null, null, null);
                    }
                    auxRoot = aux;
                }
                auxRoot.set("transformations", transformationsNode);
            }
        } else {
            if (validChainName) {
                JsonNode aux = chainsNode.get("chains");
                boolean found = false;
                if (aux.isArray()) {
                    Iterator<JsonNode> it = aux.elements();
                    while (it.hasNext()) {
                        JsonNode next = it.next();
                        if (next.has("name")) {
                            String id = next.get("name").asText();
                            if (chain.equals(id)) {
                                found = true;
                                if (next.has("transformations")) {
                                    JsonNode auxTrans = next.get("transformations");
                                    if (auxTrans.isArray()) {
                                        transformationsNode = (ArrayNode) auxTrans;
                                    } else {
                                        throw new Exception("The chain [" + chain + "] does not have a valid transformations node");
                                    }
                                } else if (next.isObject()) {
                                    ObjectNode auxNext = (ObjectNode) next;
                                    transformationsNode = new ArrayNode(mapper.getNodeFactory());
                                    auxNext.set("transformations", transformationsNode);
                                } else {
                                    throw new Exception("The chain [" + chain + "] does not have a valid structure");
                                }
                            }
                        }
                    }
                    if (!found) {
                        ChainConfig chainCfg = new ChainConfigImpl();
                        chainCfg.setName(chain);
                        WalkerConfig walkerCfg = new WalkerConfigImpl();
                        List<TransformationConfig> transfs = new LinkedList<TransformationConfig>();
                        transfs.add(transformationCfg);
                        walkerCfg.setTransformations(transfs);
                        chainCfg.setWalkerConfig(walkerCfg);
                        provider.addChainConfig(chainCfg, false, before);
                        return;
                    }
                }
            } else {
                ObjectNode node = new ObjectNode(mapper.getNodeFactory());
                node.set("name", new TextNode(chain));
                ArrayNode transNodes = new ArrayNode(mapper.getNodeFactory());
                node.set("transformations", transNodes);
                ArrayNode array = (ArrayNode) chainsNode.get("chains");
                array.add(node);
                ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
                transNodes.add(transformationNode);
                createTransformation(transformationNode, transformationCfg);
                return;
            }
        }
        if (transformationsNode != null) {
            ObjectNode transformationNode = new ObjectNode(mapper.getNodeFactory());
            if (order != null && order < transformationsNode.size()) {
                transformationsNode.insert(order, transformationNode);
            } else {
                transformationsNode.add(transformationNode);
            }
            createTransformation(transformationNode, transformationCfg);
            provider.write(chainsNode);
            return;
        } else if (chain != null) {
            throw new Exception("The chain [" + chain + "] does not exists");
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with ChainConfigImpl

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

the class DynamicConfigurationProvider method load.

@Override
public void load() throws ConfigurationException {
    config.prepareInitializers();
    if (chains != null) {
        for (String chain : chains) {
            if (StringUtils.isNotEmpty(chain)) {
                PluginConfig plugin = config.resolvePlugin(chain);
                config.getPlugins().add(plugin);
            }
        }
        config.preparePlugins();
    }
    String path = options.getPath();
    if (chains != null) {
        for (String chain : chains) {
            if (StringUtils.isNotEmpty(chain)) {
                ChainConfig cc = config.getChainConfig(chain);
                if (cc == null) {
                    cc = new ChainConfigImpl(new TransformationConfigImpl(chain));
                    config.addChainConfig(cc);
                }
            }
        }
    }
    if (StringUtils.isNotEmpty(path)) {
        for (ChainConfig cc : config.getChainConfigs()) {
            cc.setPath(path);
        }
    }
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) TransformationConfigImpl(org.walkmod.conf.entities.impl.TransformationConfigImpl) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Example 5 with ChainConfigImpl

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

the class XMLConfigurationProvider method loadChains.

public void loadChains() throws ConfigurationException {
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    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();
            if ("chain".equals(nodeName)) {
                ChainConfig ac = new ChainConfigImpl();
                if ("".equals(child.getAttribute("name"))) {
                    if (i == 0) {
                        ac.setName("chain_" + (i + 1));
                    } else {
                        ac.setName("chain_" + (i + 1));
                    }
                } else {
                    ac.setName(child.getAttribute("name"));
                }
                NodeList childrenModel = child.getChildNodes();
                ac.setParameters(getParams(child));
                int index = 0;
                if ("reader".equals(childrenModel.item(index).getNodeName())) {
                    loadReaderConfig((Element) childrenModel.item(index), ac);
                    index++;
                } else {
                    addDefaultReaderConfig(ac);
                }
                if (index >= childrenModel.getLength()) {
                    throw new ConfigurationException("Invalid architecture definition for the " + "element" + ac.getName());
                }
                if ("walker".equals(childrenModel.item(index).getNodeName())) {
                    loadWalkerConfig((Element) childrenModel.item(index), ac);
                    index++;
                } else if ("transformation".equals(childrenModel.item(index).getNodeName())) {
                    addDefaultWalker(ac, child);
                } else {
                    throw new ConfigurationException("Invalid transformation chain. A walker or at least one transformation must be specified");
                }
                if (index > childrenModel.getLength()) {
                    throw new ConfigurationException("Invalid architecture definition for the " + "element" + ac.getName());
                }
                boolean found = false;
                while (index < childrenModel.getLength() && !found) {
                    if ("writer".equals(childrenModel.item(index).getNodeName())) {
                        found = true;
                        loadWriter((Element) childrenModel.item(index), ac);
                    }
                    index++;
                }
                if (!found) {
                    addDefaultWriterConfig(ac);
                }
                configuration.addChainConfig(ac);
            } else if ("transformation".equals(nodeName)) {
                ChainConfig ac = new ChainConfigImpl();
                ac.setName("default");
                List<TransformationConfig> transformationConfigs = getTransformationItems(rootElement, true);
                WalkerConfig wc = new WalkerConfigImpl();
                wc.setType(null);
                wc.setParserConfig(new ParserConfigImpl());
                wc.setTransformations(transformationConfigs);
                addDefaultReaderConfig(ac);
                ac.setWalkerConfig(wc);
                addDefaultWriterConfig(ac);
                configuration.addChainConfig(ac);
                i = i + transformationConfigs.size() - 1;
            }
        }
    }
    LOG.debug("Transformation chains loaded");
}
Also used : ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) ChainConfig(org.walkmod.conf.entities.ChainConfig) ConfigurationException(org.walkmod.conf.ConfigurationException) List(java.util.List) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList)

Aggregations

ChainConfig (org.walkmod.conf.entities.ChainConfig)8 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)8 LinkedList (java.util.LinkedList)5 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)5 Configuration (org.walkmod.conf.entities.Configuration)4 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)4 Element (org.w3c.dom.Element)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)3 WalkerConfigImpl (org.walkmod.conf.entities.impl.WalkerConfigImpl)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 File (java.io.File)2 Test (org.junit.Test)2 Document (org.w3c.dom.Document)2 ConfigurationException (org.walkmod.conf.ConfigurationException)2 MergePolicyConfig (org.walkmod.conf.entities.MergePolicyConfig)2 PluginConfig (org.walkmod.conf.entities.PluginConfig)2 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)2 WriterConfig (org.walkmod.conf.entities.WriterConfig)2