All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.plugin.parser.ParserExecutor Maven / Gradle / Ivy

/*
 * #%L
 * I18n :: Maven Plugin
 * %%
 * Copyright (C) 2007 - 2017 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package org.nuiton.i18n.plugin.parser;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.maven.plugin.logging.Log;
import org.nuiton.i18n.spi.GetterFile;
import org.nuiton.plugin.PluginHelper;

/**
 * @author poussin
 * @since 1.2.2
 */
public class ParserExecutor extends ThreadPoolExecutor implements I18nParseMojoConfiguration {

    /**
     * the incoming configuration (from mojo which contains shared result and
     * logger)
     */
    protected final I18nParseMojoConfiguration configuration;

    /** list of files consumed */
    private final List treatedFiles;

    /** list of files touched (says having at least one i18n key) */
    private final List touchedFiles;

    /** number of files registred to consume */
    private int nbFiles;

    ParserExecutor(I18nParseMojoConfiguration configuration) {
        super(8, 10, 1L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        this.configuration = configuration;
        touchedFiles = new ArrayList<>();
        treatedFiles = new ArrayList<>();
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        ParserTask i18n = (ParserTask) r;
        i18n.registerResult(treatedFiles, touchedFiles, getGetterFile());
    }

    @Override
    public boolean isVerbose() {
        return getConfiguration().isVerbose();
    }

    @Override
    public boolean isSilent() {
        return getConfiguration().isSilent();
    }

    @Override
    public boolean isShowTouchedFiles() {
        return getConfiguration().isShowTouchedFiles();
    }

    @Override
    public Log getLog() {
        return getConfiguration().getLog();
    }

    @Override
    public GetterFile getGetterFile() {
        return getConfiguration().getGetterFile();
    }

    List getTreatedFiles() {
        return treatedFiles;
    }

    List getTouchedFiles() {
        return touchedFiles;
    }

    /** clean internal state after usage of the thread. */
    void clear() {
        treatedFiles.clear();
        touchedFiles.clear();
        nbFiles = 0;
    }

    /**
     * Add a file to be consumed.
     *
     * @param parser the parser of the file
     * @param files  files to parse
     */
    void addFile(FileParser parser, File... files) {

        for (File f : files) {

            nbFiles++;
            if (isVerbose()) {
                getLog().info("[" + nbFiles + "] " + f);
            }
            execute(new ParserTask(parser, f));
        }
    }

    /**
     * Ask the thread to stop.
     * 

* It will finish all incoming files (but will not accept more files to * parse) *

* Note: The method does not return until all files are not * consumed. * * @throws InterruptedException if something wrong while waiting end of * executor */ void terminatesAndWaits() throws InterruptedException { // ask executor to terminate shutdown(); if (isVerbose()) { if (nbFiles == 0) { // no file consumed getLog().info("No file consumed."); } else { getLog().info(String.format("Will waits until all files (%d) are consumed (still %d file(s) to consume)", nbFiles, getNbFilesToTreat())); } } try { // wait until all submited jobs are terminated // i don't want timeout, i think 2 days is good :) awaitTermination(2, TimeUnit.DAYS); } catch (InterruptedException e) { getLog().error(e); } if (getLog().isDebugEnabled()) { getLog().debug(String.format("A task was consumed, still %d file(s) to consume.", getNbFilesToTreat())); } if (isVerbose()) { getLog().info("Executor is terminated."); } } protected I18nParseMojoConfiguration getConfiguration() { return configuration; } private int getNbFilesToTreat() { return nbFiles - treatedFiles.size(); } /** * This is a task to parse a {@link #file}. *

* The task will be executed in the executor service created in the thread. */ class ParserTask implements Runnable { /** the file parser */ protected final FileParser parser; /** the file to parse */ protected final File file; /** starting time */ long startingTime; /** ending time */ long endingTime; ParserTask(FileParser parser, File file) { this.parser = parser; this.file = file; } @Override public void run() { startingTime = System.nanoTime(); if (getLog().isDebugEnabled()) { getLog().debug(String.format("starting action for %s", file)); } try { parser.parseFile(file); } catch (Exception e) { if (getLog().isErrorEnabled()) { getLog().error(String.format("could not parse file %s", file), e); } } finally { if (getLog().isDebugEnabled()) { getLog().debug(String.format("ending action for %s", file)); } endingTime = System.nanoTime(); } } @Override public String toString() { return super.toString() + " - " + file; } protected File getFile() { return file; } long getDelay() { return endingTime - startingTime; } void destroy() { parser.destroy(); } @Override protected void finalize() throws Throwable { super.finalize(); destroy(); } /** * Register the result of the parsing of the {@link #file} after {@link * #run()} method was invoked. *

* This method should be invoked by the executor as an ending hook. * * @param treatedFiles list of files already treated * @param touchedFiles list of files already touched * @param result shared result. */ synchronized void registerResult(List treatedFiles, List touchedFiles, GetterFile result) { try { treatedFiles.add(file); if (getLog().isDebugEnabled()) { String delay = PluginHelper.convertTime(getDelay()); getLog().debug(String.format("[%d] %s in %s", treatedFiles.size(), file, delay)); } if (parser.isTouched()) { // mark file as touched touchedFiles.add(file); if (isShowTouchedFiles()) { getLog().info("touch " + file); } if (isVerbose()) { String delay = PluginHelper.convertTime(getDelay()); getLog().info(String.format("[%d] file(s) touched %s in %s", treatedFiles.size(), file, delay)); } // merge file result with // merge result result.addKeys(parser.getResult()); } } finally { // destroy runner destroy(); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy