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

com.xhinliang.jugg.plugin.preload.JuggPreloadHandler Maven / Gradle / Ivy

package com.xhinliang.jugg.plugin.preload;

import static com.google.common.collect.Iterables.isEmpty;
import static java.util.Collections.emptyList;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.xhinliang.jugg.context.CommandContext;
import com.xhinliang.jugg.exception.JuggRuntimeException;
import com.xhinliang.jugg.handler.IJuggHandler;
import com.xhinliang.jugg.parse.IJuggEvalKiller;
import com.xhinliang.jugg.plugin.help.JuggHelpable;

/**
 * @author xhinliang 
 * Created on 2019-03-21
 */
public class JuggPreloadHandler implements IJuggHandler, JuggHelpable {

    private static final Logger logger = LoggerFactory.getLogger(JuggPreloadHandler.class);
    private final IJuggEvalKiller evalKiller;

    private final Map preloaderMap;

    // map { username -> map { packageName -> isLoaded } }
    private final Map> preloadedMap;

    public JuggPreloadHandler(IJuggEvalKiller evalKiller) {
        this(evalKiller, ImmutableList. builder() //
                .add(new CollectionFunctionPreloader()) //
                .build() //
        );
    }

    public JuggPreloadHandler(IJuggEvalKiller evalKiller, List preloaderList) {
        this.evalKiller = evalKiller;
        this.preloaderMap = preloaderList.stream() //
                .collect(toMap(IJuggPreloader::packageName, identity(), (a, b) -> {
                    logger.info("IJuggPreloader duplicated a:{}, b:{}", a.packageName(), b.packageName());
                    logger.info("list:{}", preloaderList);
                    return a;
                }));
        this.preloadedMap = new ConcurrentHashMap<>();
    }

    public void handle(CommandContext context) {
        String result = this.generateResult(context);
        if (result != null) {
            context.setResult(result);
            context.setShouldEnd(true);
        }
    }

    @Nullable
    public String generateResult(CommandContext context) {
        String command = context.getCommand();

        if (command.equals("preload list")) {
            return list(context.getJuggUser().getUsername());
        }

        if (command.startsWith("preload ")) {
            String[] spliced = command.split(" ");
            if (spliced.length == 2) {
                return preload(context.getJuggUser().getUsername(), spliced[1]);
            } else {
                throw new JuggRuntimeException("[system] preload syntax error!");
            }
        }

        return null;
    }

    private String list(String username) {
        StringBuilder sb = new StringBuilder("list of preloaders\n");
        Map userLoadedMap = preloadedMap.computeIfAbsent(username, k -> new ConcurrentHashMap<>());
        preloaderMap.forEach((packageName, preloader) -> {
            if (userLoadedMap.getOrDefault(packageName, Boolean.FALSE)) {
                sb.append(String.format("+ %s -- %s\n", preloader.packageName(), preloader.desc()));
                sb.append(String.format("  %s -> %s\n", preloader.sampleInput(), preloader.sampleOutput()));
                sb.append("\n");
            } else {
                sb.append(String.format("- %s %s\n", preloader.packageName(), preloader.desc()));
                sb.append(String.format("  %s -> %s\n", preloader.sampleInput(), preloader.sampleOutput()));
                sb.append("\n");
            }
        });
        return sb.toString();
    }

    private String preload(String username, String packageName) {
        List scripts = Optional.ofNullable(preloaderMap.get(packageName)).map(IJuggPreloader::getScripts) //
                .orElse(emptyList());

        if (isEmpty(scripts)) {
            throw new JuggRuntimeException(String.format("[system] packageName:%s not found!", packageName));
        }

        boolean isLoaded = preloadedMap.computeIfAbsent(username, k -> new ConcurrentHashMap<>()) //
                .getOrDefault(packageName, Boolean.FALSE);
        if (isLoaded) {
            throw new JuggRuntimeException(String.format("[system] packageName:%s is already preloaded!", packageName));
        }

        scripts.forEach(script -> evalKiller.eval(script, username));
        preloadedMap.computeIfAbsent(username, k -> new ConcurrentHashMap<>()) //
                .put(packageName, Boolean.TRUE);
        return String.format("preload: %s", packageName);
    }

    public Map getPreloaderMap() {
        return preloaderMap;
    }

    @Override
    public String name() {
        return "preload";
    }

    @Override
    public Map patternToMessage() {
        return ImmutableMap. builder() //
                .put("preload list", "list all preloaders.") //
                .put("preload {{packageName}}", "preload package") //
                .build();

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy