Search in sources :

Example 6 with WalkModException

use of org.walkmod.exceptions.WalkModException in project walkmod-core by walkmod.

the class DefaultChainWalkerAdapter method prepare.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare() throws WalkModException {
    walker.setResource(getModel());
    walker.setRootNamespace(config.getRootNamespace());
    ChainWriter mw = ap.getChainWriter();
    mw.setExcludes(config.getChainConfig().getWriterConfig().getExcludes());
    mw.setIncludes(config.getChainConfig().getWriterConfig().getIncludes());
    walker.setWriter(ap.getChainWriter());
    walker.setChainConfig(config.getChainConfig());
    ChainConfig ac = config.getChainConfig();
    Object visitor;
    Configuration c = ac.getConfiguration();
    Parser parser = null;
    String parserType = config.getParserConfig().getType();
    if (parserType != null) {
        Object parserInstance = c.getBean(parserType, config.getParserConfig().getParameters());
        if (parserInstance != null) {
            if (parserInstance instanceof Parser) {
                parser = (Parser) parserInstance;
                walker.setParser(parser);
            } else {
                throw new WalkModException("The parser " + parserType + " must implement " + Parser.class.getName());
            }
        } else {
            throw new WalkModException("The parser " + parserType + " does not exist.");
        }
    }
    Collection<TransformationConfig> cfgs = getTransformationConfig();
    if (cfgs != null) {
        for (TransformationConfig config : cfgs) {
            setName(config.getName());
            visitor = config.getVisitorInstance();
            if (visitor == null || "".equals(config.getType())) {
                visitor = c.getBean(config.getType(), config.getParameters());
            }
            if (visitor instanceof ResourceModifier) {
                ((ResourceModifier) visitor).setResource(getModel());
            }
            if (visitor instanceof ParserAware) {
                ((ParserAware) visitor).setParser(parser);
            }
            if (visitor != null) {
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].walker = " + walker.getClass().getName());
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].visitor = " + visitor.getClass().getName());
                visitors.add(visitor);
            } else {
                walker = null;
                visitor = null;
                LOG.debug("Transformation[" + getName() + "] without walker and visitor");
            }
        }
    }
    walker.setVisitors(visitors);
    wi.init(this);
}
Also used : ChainWriter(org.walkmod.ChainWriter) ResourceModifier(org.walkmod.ResourceModifier) WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ParserAware(org.walkmod.walkers.ParserAware) ChainConfig(org.walkmod.conf.entities.ChainConfig) Parser(org.walkmod.walkers.Parser)

Example 7 with WalkModException

use of org.walkmod.exceptions.WalkModException in project walkmod-core by walkmod.

the class DefaultChainWalkerInvocation method invoke.

@Override
public void invoke() throws WalkModException {
    ChainWalker walker = walkerAdapter.getWalker();
    if (walker != null) {
        try {
            walker.execute();
        } catch (Exception e) {
            throw new WalkModException("An exeception has been produced during the " + walkerAdapter.getName() + " transformation", e);
        }
    }
    LOG.debug("The transformation [" + walkerAdapter.getName() + "] has been executed");
}
Also used : ChainWalker(org.walkmod.ChainWalker) WalkModException(org.walkmod.exceptions.WalkModException) WalkModException(org.walkmod.exceptions.WalkModException)

Example 8 with WalkModException

use of org.walkmod.exceptions.WalkModException 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 9 with WalkModException

use of org.walkmod.exceptions.WalkModException in project walkmod-core by walkmod.

the class WalkModFacade method install.

/**
 * Downloads the list of declared plugins in the configuration file using Ivy. Ignores the
 * ConfigurationProvided if passed in the constructor.
 *
 * @throws InvalidConfigurationException
 *             if the walkmod configuration is invalid and it is working in no verbose mode.
 */
public void install() throws InvalidConfigurationException {
    if (cfg.exists()) {
        if (options.isVerbose()) {
            log.info(cfg.getAbsoluteFile() + " [ok]");
        }
        // Uses Ivy always
        ConfigurationProvider cp = new IvyConfigurationProvider(options.isOffline(), options.isVerbose());
        if (options.isVerbose()) {
            log.info("** THE PLUGIN INSTALLATION STARTS **");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
        long startTime = System.currentTimeMillis();
        long endTime = startTime;
        DecimalFormat myFormatter = new DecimalFormat("###.###");
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US);
        boolean error = false;
        try {
            userDir = new File(System.getProperty("user.dir")).getAbsolutePath();
            System.setProperty("user.dir", options.getExecutionDirectory().getCanonicalPath());
            ConfigurationManager cfgManager = new ConfigurationManager(cfg, cp);
            Configuration cf = cfgManager.getConfiguration();
            List<String> modules = cf.getModules();
            if (modules != null && !modules.isEmpty()) {
                for (String module : modules) {
                    File aux = new File(module).getAbsoluteFile();
                    if (aux.isDirectory()) {
                        if (options.isVerbose()) {
                            log.info("** MODULE " + aux.getAbsoluteFile() + " [ok] **");
                        }
                        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options(options).executionDirectory(aux).build());
                        facade.install();
                    } else {
                        log.error("The module " + aux.getAbsolutePath() + " is not an existing directory");
                    }
                }
            }
        } catch (Exception e) {
            System.setProperty("user.dir", userDir);
            if (options.isVerbose()) {
                error = true;
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("PLUGIN INSTALLATION FAILS");
                System.out.println();
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                if (options.isPrintErrors()) {
                    log.error("Plugin installations fails", e);
                } else {
                    log.info("Plugin installations fails. Please, execute walkmod with -e to see the details");
                }
                if (options.isThrowException()) {
                    RuntimeException re = new RuntimeException();
                    re.setStackTrace(e.getStackTrace());
                    throw re;
                }
            } else {
                throw new InvalidConfigurationException(e);
            }
        }
        if (!error) {
            System.setProperty("user.dir", userDir);
            if (options.isVerbose()) {
                endTime = System.currentTimeMillis();
                double time = 0;
                if (endTime > startTime) {
                    time = (double) (endTime - startTime) / (double) 1000;
                }
                String timeMsg = myFormatter.format(time);
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                System.out.println();
                log.info("PLUGIN INSTALLATION COMPLETE");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
                log.info("Total time: " + timeMsg + " seconds");
                log.info("Finished at: " + df.format(new Date()));
                log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
            }
        }
    } else {
        if (options.isVerbose()) {
            log.error(cfg.getAbsolutePath() + " does not exist. The root directory of your project must contain a walkmod.xml");
        } else {
            throw new WalkModException(cfg.getAbsolutePath() + " does not exist. The root directory of your project must contain a walkmod.xml");
        }
    }
}
Also used : WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) DynamicModulesConfigurationProvider(org.walkmod.conf.providers.DynamicModulesConfigurationProvider) ConfigurationProvider(org.walkmod.conf.ConfigurationProvider) ProjectConfigurationProvider(org.walkmod.conf.ProjectConfigurationProvider) DynamicConfigurationProvider(org.walkmod.conf.providers.DynamicConfigurationProvider) IvyConfigurationProvider(org.walkmod.conf.providers.IvyConfigurationProvider) DecimalFormat(java.text.DecimalFormat) WalkModException(org.walkmod.exceptions.WalkModException) IOException(java.io.IOException) InvalidConfigurationException(org.walkmod.exceptions.InvalidConfigurationException) Date(java.util.Date) InvalidConfigurationException(org.walkmod.exceptions.InvalidConfigurationException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) ConfigurationManager(org.walkmod.conf.ConfigurationManager) IvyConfigurationProvider(org.walkmod.conf.providers.IvyConfigurationProvider)

Example 10 with WalkModException

use of org.walkmod.exceptions.WalkModException in project walkmod-core by walkmod.

the class ConfigurationImpl method executeAllChains.

public void executeAllChains(Options options, ChainAdapterFactory apf) {
    Collection<ChainConfig> tcgfs = getChainConfigs();
    if (tcgfs != null) {
        if (options.isVerbose()) {
            log.info("** STARTING TRANSFORMATIONS CHAINS **");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
        long startTime = System.currentTimeMillis();
        long endTime = startTime;
        DecimalFormat myFormatter = new DecimalFormat("###.###");
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US);
        int num = 0;
        Iterator<ChainConfig> it = tcgfs.iterator();
        int pos = 1;
        while (it.hasNext()) {
            ChainConfig tcfg = it.next();
            if (tcgfs.size() > 1) {
                if (options.isVerbose()) {
                    String label = "";
                    if (tcfg.getName() != null && !tcfg.getName().startsWith("chain_")) {
                        label = "[" + tcfg.getName() + "](" + pos + "/" + tcgfs.size() + ") ";
                    } else {
                        label = "(" + pos + "/" + tcgfs.size() + ")";
                    }
                    log.info("TRANSFORMATION CHAIN " + label);
                    System.out.println();
                }
            }
            try {
                if (options.getIncludes() != null) {
                    String[] includes = options.getIncludes().toArray(new String[options.getIncludes().size()]);
                    tcfg.getReaderConfig().setIncludes(includes);
                }
                if (options.getExcludes() != null) {
                    String[] excludes = options.getExcludes().toArray(new String[options.getExcludes().size()]);
                    tcfg.getReaderConfig().setExcludes(excludes);
                }
                ChainAdapter ap = apf.createChainProxy(this, tcfg.getName());
                ap.execute();
                num += ap.getWalkerAdapter().getWalker().getNumModifications();
                pos++;
                if (options.isVerbose()) {
                    if (Summary.getInstance().getWrittenFiles().isEmpty()) {
                        log.info("**No sources changed**");
                    }
                    if (it.hasNext()) {
                        System.out.println();
                    }
                }
            } catch (Throwable e) {
                if (options.isVerbose()) {
                    endTime = System.currentTimeMillis();
                    double time = 0;
                    if (endTime > startTime) {
                        time = (double) (endTime - startTime) / (double) 1000;
                    }
                    String timeMsg = myFormatter.format(time);
                    if (num != 0) {
                        System.out.print("----------------------------------------");
                        System.out.println("----------------------------------------");
                    }
                    log.info("TRANSFORMATION CHAIN FAILS");
                    System.out.println();
                    System.out.print("----------------------------------------");
                    System.out.println("----------------------------------------");
                    log.info("Total time: " + timeMsg + " seconds");
                    log.info("Finished at: " + df.format(new Date()));
                    log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
                    System.out.print("----------------------------------------");
                    System.out.println("----------------------------------------");
                    log.info("Please, see the walkmod log file for details");
                    if (options.isPrintErrors()) {
                        log.error("TRANSFORMATION CHAIN (" + tcfg.getName() + ") FAILS", e);
                    } else {
                        log.error("TRANSFORMATION CHAIN (" + tcfg.getName() + ") FAILS. Execute walkmod with -e to see the error details.");
                    }
                    if (options.isThrowException()) {
                        RuntimeException re = new RuntimeException();
                        re.setStackTrace(e.getStackTrace());
                        throw re;
                    }
                } else {
                    throw new WalkModException(e);
                }
                return;
            }
            if (!it.hasNext()) {
                if (tcgfs.size() >= pos) {
                    LinkedList<ChainConfig> aux = new LinkedList<ChainConfig>(tcgfs);
                    it = aux.listIterator(pos - 1);
                }
            }
        }
        if (options.isVerbose()) {
            endTime = System.currentTimeMillis();
            double time = 0;
            if (endTime > startTime) {
                time = (double) (endTime - startTime) / (double) 1000;
            }
            String timeMsg = myFormatter.format(time);
            if (num != 0) {
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
            }
            System.out.println();
            log.info("TRANSFORMATION CHAIN SUCCESS");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
            log.info("Total time: " + timeMsg + " seconds");
            log.info("Finished at: " + df.format(new Date()));
            log.info("Final memory: " + (Runtime.getRuntime().freeMemory()) / 1048576 + " M/ " + (Runtime.getRuntime().totalMemory() / 1048576) + " M");
            log.info("Total modified files: " + num);
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
    }
}
Also used : WalkModException(org.walkmod.exceptions.WalkModException) DecimalFormat(java.text.DecimalFormat) Date(java.util.Date) LinkedList(java.util.LinkedList) ChainConfig(org.walkmod.conf.entities.ChainConfig) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ChainAdapter(org.walkmod.ChainAdapter) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

WalkModException (org.walkmod.exceptions.WalkModException)14 LinkedList (java.util.LinkedList)4 File (java.io.File)3 DateFormat (java.text.DateFormat)3 DecimalFormat (java.text.DecimalFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 ChainConfig (org.walkmod.conf.entities.ChainConfig)3 Configuration (org.walkmod.conf.entities.Configuration)3 IOException (java.io.IOException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 ScriptException (javax.script.ScriptException)2 ChainAdapter (org.walkmod.ChainAdapter)2 ChainWalker (org.walkmod.ChainWalker)2 ChainWriter (org.walkmod.ChainWriter)2 ConfigurationProvider (org.walkmod.conf.ConfigurationProvider)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)1