Search in sources :

Example 1 with Configuration

use of org.walkmod.conf.entities.Configuration 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 2 with Configuration

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

the class PrintPluginsCommand method execute.

public void execute() {
    if (help) {
        command.usage("init");
    } else {
        try {
            WalkModFacade facade = new WalkModFacade(OptionsBuilder.options());
            Configuration cfg = facade.getConfiguration();
            Collection<PluginConfig> installedPlugins = null;
            if (cfg != null) {
                installedPlugins = cfg.getPlugins();
            }
            URL searchURL = new URL(MVN_SEARCH_URL);
            InputStream is = null;
            Map<String, Boolean> installedList = new LinkedHashMap<String, Boolean>();
            Map<String, String> pluginsList = new LinkedHashMap<String, String>();
            Map<String, String> pluginsURLs = new LinkedHashMap<String, String>();
            try {
                is = searchURL.openStream();
                String content = readInputStreamAsString(is);
                DefaultJSONParser parser = new DefaultJSONParser(content);
                JSONObject object = parser.parseObject();
                parser.close();
                JSONArray artifactList = (object.getJSONObject("response")).getJSONArray("docs");
                for (int i = 0; i < artifactList.size(); i++) {
                    JSONObject artifact = artifactList.getJSONObject(i);
                    String artifactId = artifact.getString("a");
                    if (artifactId.startsWith("walkmod-") && artifactId.endsWith("-plugin")) {
                        String groupId = artifact.getString("g");
                        if (!"org.walkmod.maven.plugins".equals(groupId)) {
                            String latestVersion = artifact.getString("latestVersion");
                            String pom = artifactId + "-" + latestVersion + ".pom";
                            String directory = groupId.replaceAll("\\.", "/");
                            URL artifactDetailsURL = new URL(ARTIFACT_DETAILS_URL + directory + "/" + artifactId + "/" + latestVersion + "/" + pom);
                            InputStream projectIs = null;
                            try {
                                projectIs = artifactDetailsURL.openStream();
                            } catch (Exception e) {
                            }
                            if (projectIs != null) {
                                try {
                                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                                    Document doc = dBuilder.parse(projectIs);
                                    NodeList nList = doc.getElementsByTagName("description");
                                    String description = "unavailable description";
                                    if (nList.getLength() == 1) {
                                        description = nList.item(0).getTextContent();
                                    }
                                    String id = "";
                                    if (!groupId.equals("org.walkmod")) {
                                        id = groupId + ":";
                                    }
                                    id += artifactId.substring("walkmod-".length(), artifactId.length() - "-plugin".length());
                                    if (Character.isLowerCase(description.charAt(0))) {
                                        description = Character.toUpperCase(description.charAt(0)) + description.substring(1, description.length());
                                    }
                                    if (!description.endsWith(".")) {
                                        description = description + ".";
                                    }
                                    pluginsList.put(id, description);
                                    nList = doc.getChildNodes().item(0).getChildNodes();
                                    int max = nList.getLength();
                                    String url = "unavailable url";
                                    for (int j = 0; j < max; j++) {
                                        String name = nList.item(j).getNodeName();
                                        if (name.equals("url")) {
                                            url = nList.item(j).getTextContent();
                                            j = max;
                                        }
                                    }
                                    pluginsURLs.put(id, url);
                                    PluginConfig equivalentPluginCfg = new PluginConfigImpl();
                                    equivalentPluginCfg.setGroupId(groupId);
                                    equivalentPluginCfg.setArtifactId(artifactId);
                                    boolean isInstalled = (installedPlugins != null && installedPlugins.contains(equivalentPluginCfg));
                                    installedList.put(id, isInstalled);
                                } finally {
                                    projectIs.close();
                                }
                            }
                        }
                    }
                }
            } finally {
                is.close();
            }
            Set<String> keys = pluginsList.keySet();
            List<String> sortedKeys = new LinkedList<String>(keys);
            Collections.sort(sortedKeys);
            at = new V2_AsciiTable();
            at.addRule();
            at.addRow("PLUGIN NAME (ID)", "RDY", "DESCRIPTION");
            at.addStrongRule();
            for (String key : sortedKeys) {
                String installed = "";
                if (installedList.get(key)) {
                    installed = "*";
                }
                at.addRow(key, installed, pluginsList.get(key) + "\n\nURL:" + pluginsURLs.get(key));
                at.addRule();
            }
        } catch (Exception e) {
            throw new WalkModException("Invalid plugins URL", e);
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(org.walkmod.conf.entities.Configuration) Document(org.w3c.dom.Document) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModException(org.walkmod.exceptions.WalkModException) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) JSONArray(com.alibaba.fastjson.JSONArray) WalkModException(org.walkmod.exceptions.WalkModException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PluginConfig(org.walkmod.conf.entities.PluginConfig) JSONObject(com.alibaba.fastjson.JSONObject) DocumentBuilder(javax.xml.parsers.DocumentBuilder) WalkModFacade(org.walkmod.WalkModFacade) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 3 with Configuration

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

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

the class WalkModFacade method readConfig.

private Configuration readConfig(ConfigurationProvider... cp) throws InvalidConfigurationException {
    Configuration config = null;
    try {
        ConfigurationManager cfgManager = new ConfigurationManager(cfg, cp);
        config = cfgManager.getConfiguration();
        config.setParameters(options.getMutableCopyOfDynamicArgs());
    } catch (Exception e) {
        printConfigError(e);
    }
    return config;
}
Also used : Configuration(org.walkmod.conf.entities.Configuration) ConfigurationManager(org.walkmod.conf.ConfigurationManager) WalkModException(org.walkmod.exceptions.WalkModException) IOException(java.io.IOException) InvalidConfigurationException(org.walkmod.exceptions.InvalidConfigurationException)

Example 5 with Configuration

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

Configuration (org.walkmod.conf.entities.Configuration)58 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)48 Test (org.junit.Test)43 File (java.io.File)38 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)23 LinkedList (java.util.LinkedList)21 AddTransformationCommand (org.walkmod.commands.AddTransformationCommand)21 ChainConfig (org.walkmod.conf.entities.ChainConfig)11 PluginConfig (org.walkmod.conf.entities.PluginConfig)10 HashMap (java.util.HashMap)6 WalkModException (org.walkmod.exceptions.WalkModException)6 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)5 IOException (java.io.IOException)5 WalkModFacade (org.walkmod.WalkModFacade)5 ConfigurationManager (org.walkmod.conf.ConfigurationManager)5 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)5 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)5 Document (org.w3c.dom.Document)4 NodeList (org.w3c.dom.NodeList)4 InvalidConfigurationException (org.walkmod.exceptions.InvalidConfigurationException)4