Search in sources :

Example 1 with PluginConfig

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

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

the class IvyConfigurationProvider method load.

@Override
public void load() throws ConfigurationException {
    Collection<PluginConfig> plugins = configuration.getPlugins();
    PluginConfig plugin = null;
    Collection<File> jarsToLoad = new LinkedList<File>();
    ConfigurationException ce = null;
    try {
        if (plugins != null) {
            Iterator<PluginConfig> it = plugins.iterator();
            initIvy();
            while (it.hasNext()) {
                plugin = it.next();
                addArtifact(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion());
            }
            jarsToLoad = resolveArtifacts();
            URL[] urls = new URL[jarsToLoad.size()];
            int i = 0;
            for (File jar : jarsToLoad) {
                urls[i] = jar.toURI().toURL();
                i++;
            }
            URLClassLoader childClassLoader = new URLClassLoader(urls, configuration.getClassLoader());
            configuration.setClassLoader(childClassLoader);
        }
    } catch (Exception e) {
        if (!(e instanceof ConfigurationException)) {
            if (plugin == null) {
                ce = new ConfigurationException("Unable to initialize ivy configuration:" + e.getMessage());
            } else {
                ce = new ConfigurationException("Unable to resolve the plugin: " + plugin.getGroupId() + " : " + plugin.getArtifactId() + " : " + plugin.getVersion() + ". Reason : " + e.getMessage());
            }
        } else {
            ce = (ConfigurationException) e;
        }
        throw ce;
    }
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) ConfigurationException(org.walkmod.conf.ConfigurationException) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) ParseException(java.text.ParseException) IOException(java.io.IOException) ConfigurationException(org.walkmod.conf.ConfigurationException)

Example 3 with PluginConfig

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

the class XMLConfigurationProvider method loadPlugins.

private void loadPlugins() {
    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 ("plugins".equals(childNode.getNodeName())) {
            Element child = (Element) childNode;
            Collection<PluginConfig> plugins = new LinkedList<PluginConfig>();
            configuration.setPlugins(plugins);
            NodeList pluginNodes = child.getChildNodes();
            int pluginSize = pluginNodes.getLength();
            for (int j = 0; j < pluginSize; j++) {
                Node pluginNode = pluginNodes.item(j);
                if ("plugin".equals(pluginNode.getNodeName())) {
                    Element pluginElement = (Element) pluginNode;
                    PluginConfig pc = new PluginConfigImpl();
                    String groupId = pluginElement.getAttribute("groupId");
                    String artifactId = pluginElement.getAttribute("artifactId");
                    String version = pluginElement.getAttribute("version");
                    if (groupId == null) {
                        throw new ConfigurationException("Invalid plugin definition. A groupId is necessary.");
                    }
                    if (artifactId == null) {
                        throw new ConfigurationException("Invalid plugin definition. A artifactId is necessary.");
                    }
                    if (version == null) {
                        throw new ConfigurationException("Invalid plugin definition. A version is necessary.");
                    }
                    pc.setGroupId(groupId);
                    pc.setArtifactId(artifactId);
                    pc.setVersion(version);
                    plugins.add(pc);
                }
            }
        }
    }
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) ConfigurationException(org.walkmod.conf.ConfigurationException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) LinkedList(java.util.LinkedList)

Example 4 with PluginConfig

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

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

the class RemovePluginCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("rm-plugin");
    } else {
        List<PluginConfig> list = build();
        for (PluginConfig pc : list) {
            WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().printErrors(printErrors));
            facade.removePluginConfig(pc, recursive);
        }
    }
}
Also used : PluginConfig(org.walkmod.conf.entities.PluginConfig) WalkModFacade(org.walkmod.WalkModFacade)

Aggregations

PluginConfig (org.walkmod.conf.entities.PluginConfig)22 LinkedList (java.util.LinkedList)10 Configuration (org.walkmod.conf.entities.Configuration)10 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)10 Test (org.junit.Test)9 ConfigurationImpl (org.walkmod.conf.entities.impl.ConfigurationImpl)9 File (java.io.File)8 ConfigurationException (org.walkmod.conf.ConfigurationException)4 ChainConfig (org.walkmod.conf.entities.ChainConfig)4 IOException (java.io.IOException)3 URL (java.net.URL)3 NodeList (org.w3c.dom.NodeList)3 WalkModFacade (org.walkmod.WalkModFacade)3 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)3 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 AddPluginCommand (org.walkmod.commands.AddPluginCommand)2 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)2 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)2 JSONArray (com.alibaba.fastjson.JSONArray)1