Search in sources :

Example 1 with ChainAdapter

use of org.walkmod.ChainAdapter in project walkmod-core by walkmod.

the class ConfigurationImpl method executeChain.

public void executeChain(String userDir, Options options, ChainAdapterFactory apf, String name) {
    if (options.getIncludes() != null || options.getExcludes() != null) {
        Collection<ChainConfig> chains = getChainConfigs();
        if (chains != null) {
            for (ChainConfig cc : chains) {
                if (options.getIncludes() != null) {
                    String[] includes = options.getIncludes().toArray(new String[options.getIncludes().size()]);
                    cc.getReaderConfig().setIncludes(includes);
                }
                if (options.getExcludes() != null) {
                    String[] excludes = options.getExcludes().toArray(new String[options.getExcludes().size()]);
                    cc.getReaderConfig().setExcludes(excludes);
                }
            }
        }
    }
    ChainAdapter ap = apf.createChainProxy(this, name);
    if (ap == null) {
        if (options.isVerbose()) {
            log.error("The chain " + name + " is not found");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
    } else {
        long startTime = System.currentTimeMillis();
        long endTime = startTime;
        DecimalFormat myFormatter = new DecimalFormat("###.###");
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.US);
        if (options.isVerbose()) {
            log.info("** THE TRANSFORMATION CHAIN " + name + " STARTS **");
            System.out.print("----------------------------------------");
            System.out.println("----------------------------------------");
        }
        int num = 0;
        try {
            int size = getChainConfigs().size();
            ap.execute();
            // we check if some other chain config has been added and execute them
            if (getChainConfigs().size() > size) {
                LinkedList<ChainConfig> aux = new LinkedList<ChainConfig>(getChainConfigs());
                Iterator<ChainConfig> it = aux.listIterator(size);
                while (it.hasNext()) {
                    ChainConfig tcfg = it.next();
                    ChainAdapter auxAp = apf.createChainProxy(this, tcfg.getName());
                    auxAp.execute();
                }
            }
            num = ap.getWalkerAdapter().getWalker().getNumModifications();
            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("----------------------------------------");
                } else {
                    if (Summary.getInstance().getWrittenFiles().isEmpty()) {
                        log.info("**No sources changed**");
                    }
                }
                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");
                if (ap.getWalkerAdapter().getWalker().reportChanges()) {
                    log.info("Total modified files: " + num);
                }
                System.out.print("----------------------------------------");
                System.out.println("----------------------------------------");
            }
        } catch (Throwable e) {
            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);
                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 (" + name + ") FAILS", e);
                } else {
                    log.error("TRANSFORMATION CHAIN (" + name + ") FAILS. Execute walkmod with -e to see the error details.");
                }
            } else {
                throw new WalkModException(e);
            }
            return;
        }
    }
}
Also used : WalkModException(org.walkmod.exceptions.WalkModException) DecimalFormat(java.text.DecimalFormat) LinkedList(java.util.LinkedList) Date(java.util.Date) ChainConfig(org.walkmod.conf.entities.ChainConfig) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ChainAdapter(org.walkmod.ChainAdapter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ChainAdapter

use of org.walkmod.ChainAdapter 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)

Example 3 with ChainAdapter

use of org.walkmod.ChainAdapter in project walkmod-core by walkmod.

the class DefaultChainAdapterFactory method createChainProxy.

@Override
public ChainAdapter createChainProxy(Configuration configuration, String chain) {
    Collection<ChainConfig> acs = configuration.getChainConfigs();
    if (acs != null) {
        Iterator<ChainConfig> it = acs.iterator();
        boolean end = false;
        ChainConfig acfg = null;
        while (it.hasNext() && !end) {
            acfg = it.next();
            end = chain.equals(acfg.getName());
        }
        if (end) {
            LOG.debug("Chain " + chain + " found");
            ChainAdapter ap = new DefaultChainAdapter();
            ap.setChainConfig(acfg);
            ap.setChainInvocation(new DefaultChainInvocation());
            ap.prepare();
            return ap;
        }
    }
    return null;
}
Also used : ChainAdapter(org.walkmod.ChainAdapter) ChainConfig(org.walkmod.conf.entities.ChainConfig)

Aggregations

ChainAdapter (org.walkmod.ChainAdapter)3 ChainConfig (org.walkmod.conf.entities.ChainConfig)3 DateFormat (java.text.DateFormat)2 DecimalFormat (java.text.DecimalFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 WalkModException (org.walkmod.exceptions.WalkModException)2