Search in sources :

Example 1 with MergePolicyConfig

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

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

the class XMLConfigurationProvider method loadMergePolicies.

private void loadMergePolicies() {
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    Collection<MergePolicyConfig> mergePolicies = new LinkedList<MergePolicyConfig>();
    for (int i = 0; i < childSize; i++) {
        Node childNode = children.item(i);
        if ("merge-policies".equals(childNode.getNodeName())) {
            Element child = (Element) childNode;
            NodeList policiesNodes = child.getChildNodes();
            int policiesSize = policiesNodes.getLength();
            for (int j = 0; j < policiesSize; j++) {
                Node policyNode = policiesNodes.item(j);
                if ("policy".equals(policyNode.getNodeName())) {
                    Element policyElem = (Element) policyNode;
                    MergePolicyConfig policy = new MergePolicyConfigImpl();
                    policy.setName(policyElem.getAttribute("name"));
                    String defaultOP = policyElem.getAttribute("default-object-policy");
                    if (!"".equals(defaultOP.trim())) {
                        policy.setDefaultObjectPolicy(defaultOP);
                    } else {
                        policy.setDefaultObjectPolicy(null);
                    }
                    String defaultTP = policyElem.getAttribute("default-type-policy");
                    if (!"".equals(defaultTP)) {
                        policy.setDefaultTypePolicy(defaultTP);
                    } else {
                        policy.setDefaultTypePolicy(null);
                    }
                    NodeList entriesNodes = policyElem.getChildNodes();
                    int entriesSize = entriesNodes.getLength();
                    Map<String, String> policyEntries = new HashMap<String, String>();
                    policy.setPolicyEntries(policyEntries);
                    mergePolicies.add(policy);
                    for (int k = 0; k < entriesSize; k++) {
                        Node entry = entriesNodes.item(k);
                        if ("policy-entry".equals(entry.getNodeName())) {
                            Element entryElem = (Element) entry;
                            String otype = entryElem.getAttribute("object-type");
                            String ptype = entryElem.getAttribute("policy-type");
                            if (!("".equals(otype.trim())) && !("".equals(ptype.trim()))) {
                                policyEntries.put(otype, ptype);
                            }
                        }
                    }
                }
            }
        }
    }
    configuration.setMergePolicies(mergePolicies);
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) MergePolicyConfigImpl(org.walkmod.conf.entities.impl.MergePolicyConfigImpl) LinkedList(java.util.LinkedList)

Example 3 with MergePolicyConfig

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

the class DefaultConfigurationAdapter method prepare.

@Override
public void prepare() {
    Collection<ProviderConfig> providers = config.getProviderConfigurations();
    if (providers != null) {
        for (ProviderConfig pc : providers) {
            Object aux = config.getBean(pc.getType(), pc.getParameters());
            if (aux instanceof ConfigurationProvider) {
                ConfigurationProvider cp = ((ConfigurationProvider) aux);
                cp.init(config);
                cp.load();
            }
        }
    }
    Collection<MergePolicyConfig> mergePolicies = config.getMergePolicies();
    if (mergePolicies != null) {
        Map<String, MergeEngine> mergeEngines = new HashMap<String, MergeEngine>();
        config.setMergeEngines(mergeEngines);
        for (MergePolicyConfig mpc : mergePolicies) {
            MergeEngine me = new MergeEngine();
            mergeEngines.put(mpc.getName(), me);
            String dopTypeLabel = mpc.getDefaultObjectPolicy();
            Object dop = null;
            Object top = null;
            if (dopTypeLabel != null) {
                dop = config.getBean(dopTypeLabel, null);
            }
            if (dop != null && dop instanceof MergePolicy<?>) {
                me.setDefaultObjectMergePolicy((MergePolicy) dop);
            }
            String topTypeLabel = mpc.getDefaultTypePolicy();
            if (topTypeLabel != null) {
                top = config.getBean(topTypeLabel, null);
            }
            if ((top != null) && top instanceof MergePolicy<?>) {
                me.setDefaultTypeMergePolicy((MergePolicy) top);
            }
            Map<String, String> policyEntries = mpc.getPolicyEntries();
            Class<?> oType = null;
            Object pType = null;
            Map<Class<?>, MergePolicy> resolvedEntries = new HashMap<Class<?>, MergePolicy>();
            if (policyEntries != null && !policyEntries.isEmpty()) {
                for (Map.Entry<String, String> entry : policyEntries.entrySet()) {
                    try {
                        oType = config.getClassLoader().loadClass(entry.getKey());
                    } catch (ClassNotFoundException e) {
                        throw new WalkModException("Invalid policy entry for " + entry.getKey());
                    }
                    pType = config.getBean(entry.getValue(), null);
                    if (pType instanceof MergePolicy) {
                        resolvedEntries.put(oType, (MergePolicy) pType);
                    }
                }
            }
            me.setPolicyConfiguration(resolvedEntries);
        }
    }
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) WalkModException(org.walkmod.exceptions.WalkModException) HashMap(java.util.HashMap) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) ConfigurationProvider(org.walkmod.conf.ConfigurationProvider) MergeEngine(org.walkmod.merger.MergeEngine) MergePolicy(org.walkmod.merger.MergePolicy) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with MergePolicyConfig

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

the class LanguageConfigurationProvider method loadMergePolicies.

private void loadMergePolicies() {
    Element rootElement = document.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    int childSize = children.getLength();
    Collection<MergePolicyConfig> mergePolicies = configuration.getMergePolicies();
    if (mergePolicies == null) {
        mergePolicies = new LinkedList<MergePolicyConfig>();
        configuration.setMergePolicies(mergePolicies);
    }
    MergePolicyConfig policy = null;
    for (int j = 0; j < childSize; j++) {
        Node childNode = children.item(j);
        if ("policy".equals(childNode.getNodeName())) {
            Element policyElem = (Element) childNode;
            policy = new MergePolicyConfigImpl();
            policy.setName(DEFAULT_MERGE_ENGINE_NAME);
            String defaultOP = policyElem.getAttribute("default-object-policy");
            if (!"".equals(defaultOP.trim())) {
                policy.setDefaultObjectPolicy(defaultOP);
            } else {
                policy.setDefaultObjectPolicy(null);
            }
            String defaultTP = policyElem.getAttribute("default-type-policy");
            if (!"".equals(defaultTP)) {
                policy.setDefaultTypePolicy(defaultTP);
            } else {
                policy.setDefaultTypePolicy(null);
            }
            NodeList entriesNodes = policyElem.getChildNodes();
            int entriesSize = entriesNodes.getLength();
            Map<String, String> policyEntries = new HashMap<String, String>();
            policy.setPolicyEntries(policyEntries);
            for (int k = 0; k < entriesSize; k++) {
                Node entry = entriesNodes.item(k);
                if ("policy-entry".equals(entry.getNodeName())) {
                    Element entryElem = (Element) entry;
                    String otype = entryElem.getAttribute("object-type");
                    String ptype = entryElem.getAttribute("policy-type");
                    if (!("".equals(otype.trim())) && !("".equals(ptype.trim()))) {
                        policyEntries.put(otype, ptype);
                    }
                }
            }
        }
    }
    if (policy != null) {
        mergePolicies.add(policy);
    }
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) MergePolicyConfigImpl(org.walkmod.conf.entities.impl.MergePolicyConfigImpl)

Example 5 with MergePolicyConfig

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

the class LanguageConfigurationProviderTest method testNullOverwriting.

@Test
public void testNullOverwriting() {
    LanguageConfigurationProvider provider = new LanguageConfigurationProvider();
    Configuration conf = new ConfigurationImpl();
    ChainConfig cc = new ChainConfigImpl();
    cc.setName("test-chain");
    ReaderConfig reader = new ReaderConfig();
    WalkerConfig walker = new WalkerConfigImpl();
    TransformationConfig transformation = new TransformationConfigImpl();
    transformation.isMergeable(true);
    List<TransformationConfig> transf = new LinkedList<TransformationConfig>();
    transf.add(transformation);
    walker.setParserConfig(new ParserConfigImpl());
    walker.setTransformations(transf);
    WriterConfig writer = new WriterConfigImpl();
    cc.setReaderConfig(reader);
    cc.setWalkerConfig(walker);
    cc.setWriterConfig(writer);
    conf.addChainConfig(cc);
    provider.init(conf);
    provider.load();
    Assert.assertNotNull(reader.getPath());
    Assert.assertNotNull(reader.getType());
    Assert.assertNotNull(walker.getType());
    Assert.assertNotNull(walker.getParserConfig().getType());
    Assert.assertNotNull(writer.getPath());
    Assert.assertNotNull(writer.getType());
    Assert.assertNotNull(transformation.getMergePolicy());
    Assert.assertNotNull(conf.getMergePolicies());
    Collection<MergePolicyConfig> mergec = conf.getMergePolicies();
    Assert.assertEquals(1, mergec.size());
    MergePolicyConfig mpc = mergec.iterator().next();
    Assert.assertNotNull(mpc.getDefaultObjectPolicy());
    Assert.assertNotNull(mpc.getDefaultTypePolicy());
    Map<String, String> entries = mpc.getPolicyEntries();
    Assert.assertEquals(2, entries.size());
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) ParserConfigImpl(org.walkmod.conf.entities.impl.ParserConfigImpl) Configuration(org.walkmod.conf.entities.Configuration) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ChainConfigImpl(org.walkmod.conf.entities.impl.ChainConfigImpl) WalkerConfigImpl(org.walkmod.conf.entities.impl.WalkerConfigImpl) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) WriterConfig(org.walkmod.conf.entities.WriterConfig) TransformationConfigImpl(org.walkmod.conf.entities.impl.TransformationConfigImpl) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ConfigurationImpl(org.walkmod.conf.entities.impl.ConfigurationImpl) WriterConfigImpl(org.walkmod.conf.entities.impl.WriterConfigImpl) Test(org.junit.Test)

Aggregations

MergePolicyConfig (org.walkmod.conf.entities.MergePolicyConfig)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)3 MergePolicyConfigImpl (org.walkmod.conf.entities.impl.MergePolicyConfigImpl)3 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ChainConfig (org.walkmod.conf.entities.ChainConfig)2 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)2 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)2 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 File (java.io.File)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 ConfigurationException (org.walkmod.conf.ConfigurationException)1