Java tutorial
/* * Copyright 2014 Decebal Suiu * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with * the License. You may obtain a copy of the License in the LICENSE file, or at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ package ro.fortsoft.pf4j.spring; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import ro.fortsoft.pf4j.PluginManager; import ro.fortsoft.pf4j.PluginWrapper; import java.util.List; import java.util.Set; /** * @author Decebal Suiu */ public class ExtensionsInjector implements BeanFactoryPostProcessor, ApplicationContextAware { private static final Logger log = LoggerFactory.getLogger(ExtensionsInjector.class); private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; injectExtensions(registry); } private void injectExtensions(BeanDefinitionRegistry registry) { PluginManager pluginManager = applicationContext.getBean(PluginManager.class); List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins(); for (PluginWrapper plugin : startedPlugins) { log.debug("Registering extensions of the plugin '{}' as beans", plugin.getPluginId()); Set<String> extensionClassNames = pluginManager.getExtensionClassNames(plugin.getPluginId()); for (String extensionClassName : extensionClassNames) { Class<?> extensionClass = null; try { log.debug("Register extension '{}' as bean", extensionClassName); extensionClass = plugin.getPluginClassLoader().loadClass(extensionClassName); BeanDefinition definition = new RootBeanDefinition(extensionClass); // optionally configure all bean properties, like scope, prototype/singleton, etc registry.registerBeanDefinition(extensionClassName, definition); } catch (ClassNotFoundException e) { log.error(e.getMessage(), e); } } } } }