Search in sources :

Example 1 with ConfigurationException

use of org.walkmod.conf.ConfigurationException in project walkmod-core by walkmod.

the class XMLConfigurationProvider method loadReaderConfig.

public void loadReaderConfig(Element element, ChainConfig ac) throws ConfigurationException {
    ReaderConfig readerConfig = new ReaderConfig();
    if ("reader".equals(element.getNodeName())) {
        if ("".equals(element.getAttribute("path"))) {
            throw new ConfigurationException("Invalid reader definition: " + "A path attribute must be specified");
        }
        readerConfig.setPath(element.getAttribute("path"));
        if ("".equals(element.getAttribute("type"))) {
            readerConfig.setType(null);
        } else {
            readerConfig.setType(element.getAttribute("type"));
        }
        readerConfig.setParameters(getParams(element));
        NodeList childs = element.getChildNodes();
        if (childs != null) {
            int max = childs.getLength();
            List<String> excludes = new LinkedList<String>();
            List<String> includes = new LinkedList<String>();
            for (int i = 0; i < max; i++) {
                Node n = childs.item(i);
                String nodeName = n.getNodeName();
                if ("exclude".equals(nodeName)) {
                    Element exclude = (Element) n;
                    excludes.add(exclude.getAttribute("wildcard"));
                } else if ("include".equals(nodeName)) {
                    Element include = (Element) n;
                    includes.add(include.getAttribute("wildcard"));
                } else {
                    throw new ConfigurationException("Invalid reader definition. Only exclude or include tags are supported");
                }
            }
            if (!excludes.isEmpty()) {
                String[] excludesArray = new String[excludes.size()];
                int j = 0;
                for (String exclude : excludes) {
                    excludesArray[j] = exclude;
                    j++;
                }
                readerConfig.setExcludes(excludesArray);
            }
            if (!includes.isEmpty()) {
                String[] includesArray = new String[includes.size()];
                int j = 0;
                for (String include : includes) {
                    includesArray[j] = include;
                    j++;
                }
                readerConfig.setIncludes(includesArray);
            }
        }
    } else {
        throw new ConfigurationException("Invalid architecture definition. " + "A reader element must be defined in the architecture element " + ac.getName());
    }
    ac.setReaderConfig(readerConfig);
}
Also used : ConfigurationException(org.walkmod.conf.ConfigurationException) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) LinkedList(java.util.LinkedList)

Example 2 with ConfigurationException

use of org.walkmod.conf.ConfigurationException 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 3 with ConfigurationException

use of org.walkmod.conf.ConfigurationException in project walkmod-core by walkmod.

the class IvyConfigurationProvider method initIvy.

/**
 * Ivy configuration initialization
 *
 * @throws ParseException
 *             If an error occurs when loading ivy settings file
 *             (ivysettings.xml)
 * @throws IOException
 *             If an error occurs when reading ivy settings file
 *             (ivysettings.xml)
 * @throws ConfigurationException
 *             If ivy settings file (ivysettings.xml) is not found in
 *             classpath
 */
public void initIvy() throws ParseException, IOException, ConfigurationException {
    if (ivy == null) {
        // creates clear ivy settings
        IvySettings ivySettings = new IvySettings();
        File settingsFile = new File(IVY_SETTINGS_FILE);
        if (settingsFile.exists()) {
            ivySettings.load(settingsFile);
        } else {
            URL settingsURL = ClassLoader.getSystemResource(IVY_SETTINGS_FILE);
            if (settingsURL == null) {
                // file not found in System classloader, we try the current one
                settingsURL = this.getClass().getClassLoader().getResource(IVY_SETTINGS_FILE);
                // when invoking toURI()
                if (settingsURL == null)
                    throw new ConfigurationException("Ivy settings file (" + IVY_SETTINGS_FILE + ") could not be found in classpath");
            }
            ivySettings.load(settingsURL);
        }
        // creates an Ivy instance with settings
        ivy = Ivy.newInstance(ivySettings);
    }
    ivyfile = File.createTempFile("ivy", ".xml");
    ivyfile.deleteOnExit();
    applyVerbose();
    String[] confs = new String[] { "default" };
    resolveOptions = new ResolveOptions().setConfs(confs);
    if (isOffLine) {
        resolveOptions = resolveOptions.setUseCacheOnly(true);
    } else {
        Map<String, Object> params = configuration.getParameters();
        if (params != null) {
            Object value = params.get("offline");
            if (value != null) {
                String offlineOpt = value.toString();
                if (offlineOpt != null) {
                    boolean offline = Boolean.parseBoolean(offlineOpt);
                    if (offline) {
                        resolveOptions = resolveOptions.setUseCacheOnly(true);
                    }
                }
            }
        }
    }
}
Also used : ConfigurationException(org.walkmod.conf.ConfigurationException) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) URL(java.net.URL)

Example 4 with ConfigurationException

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

use of org.walkmod.conf.ConfigurationException in project walkmod-core by walkmod.

the class IvyConfigurationProvider method resolveArtifacts.

public Collection<File> resolveArtifacts() throws Exception {
    if (ivy != null) {
        if (lastResolvedArtifacts.isEmpty() || !lastResolvedArtifacts.containsAll(artifacts)) {
            lastResolvedArtifacts = artifacts;
        } else {
            return artifactFiles;
        }
        XmlModuleDescriptorWriter.write(md, ivyfile);
        ResolveReport report = ivy.resolve(ivyfile.toURL(), resolveOptions);
        if (!report.hasError()) {
            ArtifactDownloadReport[] artifacts = report.getAllArtifactsReports();
            Collection<File> result = new LinkedList<File>();
            for (ArtifactDownloadReport item : artifacts) {
                result.add(item.getLocalFile());
            }
            artifactFiles = result;
            return result;
        } else {
            artifactFiles.clear();
            List problems = report.getAllProblemMessages();
            if (problems == null || problems.isEmpty()) {
                throw new ConfigurationException("Ivy can not resolve the artifacts. Undefined cause");
            } else {
                String msg = "";
                Iterator it = problems.iterator();
                while (it.hasNext()) {
                    String error = it.next().toString();
                    LOG.warn(error);
                    if ("".equals(msg)) {
                        msg = error;
                    } else {
                        msg = msg + ";" + error;
                    }
                }
                throw new ConfigurationException("Ivy can not resolve the artifacts. Cause: " + msg);
            }
        }
    }
    return null;
}
Also used : ResolveReport(org.apache.ivy.core.report.ResolveReport) ConfigurationException(org.walkmod.conf.ConfigurationException) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) File(java.io.File)

Aggregations

ConfigurationException (org.walkmod.conf.ConfigurationException)15 File (java.io.File)7 LinkedList (java.util.LinkedList)7 NodeList (org.w3c.dom.NodeList)6 IOException (java.io.IOException)5 URL (java.net.URL)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)5 PluginConfig (org.walkmod.conf.entities.PluginConfig)4 InputStream (java.io.InputStream)3 MalformedURLException (java.net.MalformedURLException)3 Document (org.w3c.dom.Document)3 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)3 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)3 InputSource (org.xml.sax.InputSource)3 ChainConfig (org.walkmod.conf.entities.ChainConfig)2 ChainConfigImpl (org.walkmod.conf.entities.impl.ChainConfigImpl)2 ParserConfigImpl (org.walkmod.conf.entities.impl.ParserConfigImpl)2 WalkerConfigImpl (org.walkmod.conf.entities.impl.WalkerConfigImpl)2 JSONException (com.alibaba.fastjson.JSONException)1