GithubHelp home page GithubHelp logo

Comments (2)

decebals avatar decebals commented on June 2, 2024

@ramymagdy
Your code is OK but I don't like that we cannot extract something reusable components (for example ExtensionsInjector, SpringPluginManager) that can be used out of the box by someone else.

I tried to move forward the project in this direction.
For example the new version for ExtensionInjector could be:

public class ExtensionsInjector {

    private static final Logger log = LoggerFactory.getLogger(ExtensionsInjector.class);

    private final PluginManager pluginManager;

    public ExtensionsInjector(PluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }

    @PostConstruct
    public void injectExtensions(SingletonBeanRegistry beanRegistry) {
        ExtensionFactory extensionFactory = pluginManager.getExtensionFactory();

        // add extensions from classpath (non plugin)
        Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null);
        for (String extensionClassName : extensionClassNames) {
            try {
                log.debug("Register extension '{}' as bean", extensionClassName);
                Class<?> extensionClass = getClass().getClassLoader().loadClass(extensionClassName);
                registerExtension(extensionFactory.create(extensionClass), beanRegistry);
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
            }
        }

        // add extensions for each started plugin
        List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
        for (PluginWrapper plugin : startedPlugins) {
            log.debug("Registering extensions of the plugin '{}' as beans", plugin.getPluginId());
            extensionClassNames = pluginManager.getExtensionClassNames(plugin.getPluginId());
            for (String extensionClassName : extensionClassNames) {
                try {
                    log.debug("Register extension '{}' as bean", extensionClassName);
                    Class<?> extensionClass = plugin.getPluginClassLoader().loadClass(extensionClassName);
                    registerExtension(extensionFactory.create(extensionClass), beanRegistry);
                } catch (ClassNotFoundException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    protected void registerExtension(Object extension, SingletonBeanRegistry beanRegistry) {
        beanRegistry.registerSingleton(extension.getClass().getName(), extension);
    }

}

Also tried to create a SpringPluginManager that it's a decorator (see decorator/wrapper pattern) over a PluginManager and that comes with a concrete implementation (SpringExtensionFactory) for createExtensionFactory() method.

I am not a Spring expert and from this reason I don't know how to resolve the problem in an elegant and simple mode.

from pf4j-spring.

decebals avatar decebals commented on June 2, 2024

I think that actual code (4fc56f8) resolve this issue.

from pf4j-spring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.