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

com.cmeza.java.jar.plugin.manager.JarPluginManager Maven / Gradle / Ivy

package com.cmeza.java.jar.plugin.manager;

import com.cmeza.java.jar.plugin.JarPlugin;
import com.cmeza.java.jar.plugin.exceptions.JarPluginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;

public final class JarPluginManager implements JarPlugin {
    private static final Logger log = LoggerFactory.getLogger(JarPluginManager.class);
    private final Builder builder;

    private JarPluginManager(Builder builder) {
        this.builder = builder;
    }

    public static  JarPluginManager.Builder builder(Class clazz) {
        return new JarPluginManager.Builder<>(clazz);
    }

    @Override
    public Optional loadJar(File pluginFile) {
        if (Objects.isNull(pluginFile) || !pluginFile.exists()) {
            throw new JarPluginException("Jar File not found.");
        }

        if (!pluginFile.isFile()) {
            throw new JarPluginException(pluginFile.getAbsolutePath() + " it's not a jar");
        }

        PluginExecute pluginExecute = new PluginExecute<>(pluginFile, builder);

        return pluginExecute.execute();
    }

    @Override
    public List loadJarDirectory(File pluginDirectory) {
        if (Objects.isNull(pluginDirectory) || !pluginDirectory.exists() || !pluginDirectory.isDirectory()) {
            throw new JarPluginException("Jar Directory not found.");
        }

        log.debug("Search for .jar file in the folder: {}", pluginDirectory);

        File[] pluginFiles = pluginDirectory.listFiles((d, n) -> n.endsWith(".jar"));
        if (Objects.isNull(pluginFiles) || pluginFiles.length == 0) {
            log.warn("jars not found in: {}", pluginDirectory);
            return Collections.emptyList();
        }

        List result = new LinkedList<>();
        for (File pluginFile : pluginFiles) {
            Optional res = loadJar(pluginFile);
            res.ifPresent(result::add);
        }
        return result;
    }

    public static class Builder {
        private final Class classType;
        private String entryName = "Plugin-Entry";
        private String disabledPluginSuffix = ".disabled.jar";

        public Builder(Class classType) {
            this.classType = classType;
        }

        public String getEntryName() {
            return entryName;
        }

        public Builder setEntryName(String entryName) {
            this.entryName = entryName;
            return this;
        }

        public String getDisabledPluginSuffix() {
            return disabledPluginSuffix;
        }

        public Builder setDisabledPluginSuffix(String disabledPluginSuffix) {
            this.disabledPluginSuffix = disabledPluginSuffix;
            return this;
        }

        public Class getClassType() {
            return classType;
        }

        public JarPlugin build() {
            if (Objects.isNull(classType)) {
                throw new JarPluginException("ClassType is required");
            }
            if (!classType.isInterface()) {
                throw new JarPluginException("ClassType has to be an interface");
            }
            if (Objects.isNull(entryName) || entryName.isEmpty()) {
                throw new JarPluginException("Entry name is required");
            }
            if (Objects.isNull(disabledPluginSuffix) || disabledPluginSuffix.isEmpty()) {
                throw new JarPluginException("DisabledPluginSuffix is required");
            }
            return new JarPluginManager<>(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy