Search in sources :

Example 1 with Plugin

use of org.pf4j.Plugin in project pf4j-spring by pf4j.

the class SpringExtensionFactory method create.

@Override
public Object create(Class<?> extensionClass) {
    Object extension = createWithoutSpring(extensionClass);
    if (autowire && extension != null) {
        // test for SpringBean
        PluginWrapper pluginWrapper = pluginManager.whichPlugin(extensionClass);
        if (pluginWrapper != null) {
            Plugin plugin = pluginWrapper.getPlugin();
            if (plugin instanceof SpringPlugin) {
                // autowire
                ApplicationContext pluginContext = ((SpringPlugin) plugin).getApplicationContext();
                pluginContext.getAutowireCapableBeanFactory().autowireBean(extension);
            }
        }
    }
    return extension;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) PluginWrapper(org.pf4j.PluginWrapper) Plugin(org.pf4j.Plugin)

Example 2 with Plugin

use of org.pf4j.Plugin in project pf4j-spring by pf4j.

the class SpringExtensionFactory method getApplicationContextBy.

/**
 * Retrieves springs {@link ApplicationContext} from the extensions plugin or the {@link #pluginManager}.
 * <p>
 * The ordering of checks is:
 * <ol>
 *     <li>If the given {@code extensionClass} belongs to a plugin that is a {@link SpringPlugin} the plugins context will be returned.</li>
 *     <li>Otherwise, if the given {@link #pluginManager} of this instance is a {@link SpringPluginManager} the managers context will be returned.</li>
 *     <li>If none of these checks fits, {@code null} is returned.</li>
 * </ol>
 *
 * @param extensionClass The class annotated with {@code @}{@link Extension}.
 * @param <T>            The Type of extension for that an {@link ApplicationContext} is requested.
 * @return the best fitting context, or {@code null}.
 */
protected <T> Optional<ApplicationContext> getApplicationContextBy(final Class<T> extensionClass) {
    final Plugin plugin = Optional.ofNullable(this.pluginManager.whichPlugin(extensionClass)).map(PluginWrapper::getPlugin).orElse(null);
    final ApplicationContext applicationContext;
    if (plugin instanceof SpringPlugin) {
        log.debug("  Extension class ' " + nameOf(extensionClass) + "' belongs to spring-plugin '" + nameOf(plugin) + "' and will be autowired by using its application context.");
        applicationContext = ((SpringPlugin) plugin).getApplicationContext();
    } else if (this.pluginManager instanceof SpringPluginManager) {
        log.debug("  Extension class ' " + nameOf(extensionClass) + "' belongs to a non spring-plugin (or main application)" + " '" + nameOf(plugin) + ", but the used PF4J plugin-manager is a spring-plugin-manager. Therefore" + " the extension class will be autowired by using the managers application contexts");
        applicationContext = ((SpringPluginManager) this.pluginManager).getApplicationContext();
    } else {
        log.warn("  No application contexts can be used for instantiating extension class '" + nameOf(extensionClass) + "'." + " This extension neither belongs to a PF4J spring-plugin (id: '" + nameOf(plugin) + "') nor is the used" + " plugin manager a spring-plugin-manager (used manager: '" + nameOf(this.pluginManager.getClass()) + "')." + " At perspective of PF4J this seems highly uncommon in combination with a factory which only reason for existence" + " is using spring (and its application context) and should at least be reviewed. In fact no autowiring can be" + " applied although autowire flag was set to 'true'. Instantiating will fallback to standard Java reflection.");
        applicationContext = null;
    }
    return Optional.ofNullable(applicationContext);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Plugin(org.pf4j.Plugin)

Aggregations

Plugin (org.pf4j.Plugin)2 ApplicationContext (org.springframework.context.ApplicationContext)2 PluginWrapper (org.pf4j.PluginWrapper)1