Search in sources :

Example 1 with PluginBeanDefinition

use of org.pentaho.platform.api.engine.PluginBeanDefinition in project pentaho-platform by pentaho.

the class DefaultPluginManager method initializeBeanFactory.

/**
 * Initializes a bean factory for serving up instance of plugin classes.
 *
 * @return an instance of the factory that allows callers to continue to define more beans on it programmatically
 */
protected void initializeBeanFactory(final IPlatformPlugin plugin, final ClassLoader loader) throws PlatformPluginRegistrationException {
    if (!(loader instanceof PluginClassLoader)) {
        logger.warn("Can't determine plugin dir to load spring file because classloader is not of type PluginClassLoader.  " + // $NON-NLS-1$
        "This is since we are probably in a unit test");
        // $NON-NLS-1$
        return;
    }
    // 
    // Get the native factory (the factory that comes preconfigured via either Spring bean files or via JUnit test
    // 
    BeanFactory nativeBeanFactory = getNativeBeanFactory(plugin, loader);
    // 
    // Now create the definable factory for accepting old style bean definitions from IPluginProvider
    // 
    GenericApplicationContext beanFactory = null;
    if (nativeBeanFactory != null && nativeBeanFactory instanceof GenericApplicationContext) {
        beanFactory = (GenericApplicationContext) nativeBeanFactory;
    } else {
        beanFactory = new GenericApplicationContext();
        beanFactory.setClassLoader(loader);
        beanFactory.getBeanFactory().setBeanClassLoader(loader);
        if (nativeBeanFactory != null) {
            beanFactory.getBeanFactory().setParentBeanFactory(nativeBeanFactory);
        }
    }
    beanFactory.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessor());
    beanFactoryMap.put(plugin.getId(), beanFactory);
    // been made available to the plugin manager
    for (PluginBeanDefinition def : plugin.getBeans()) {
        // register by classname if id is null
        def.setBeanId((def.getBeanId() == null) ? def.getClassname() : def.getBeanId());
        assertUnique(plugin.getId(), def.getBeanId());
        // defining plugin beans the old way through the plugin provider ifc supports only prototype scope
        BeanDefinition beanDef = BeanDefinitionBuilder.rootBeanDefinition(def.getClassname()).setScope(BeanDefinition.SCOPE_PROTOTYPE).getBeanDefinition();
        beanFactory.registerBeanDefinition(def.getBeanId(), beanDef);
    }
    StandaloneSpringPentahoObjectFactory pentahoFactory = new StandaloneSpringPentahoObjectFactory("Plugin Factory ( " + plugin.getId() + " )");
    pentahoFactory.init(null, beanFactory);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) PentahoBeanScopeValidatorPostProcessor(org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) StandaloneSpringPentahoObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)

Example 2 with PluginBeanDefinition

use of org.pentaho.platform.api.engine.PluginBeanDefinition in project pentaho-platform by pentaho.

the class PentahoSystemPluginManager method createBeanFactory.

private GenericApplicationContext createBeanFactory(IPlatformPlugin plugin, ClassLoader classloader) throws PlatformPluginRegistrationException {
    if (!(classloader instanceof PluginClassLoader)) {
        throw new PlatformPluginRegistrationException("Can't determine plugin dir to load spring file because classloader is not of type PluginClassLoader.  " + "This is since we are probably in a unit test");
    }
    // 
    // Get the native factory (the factory that comes preconfigured via either Spring bean files or via JUnit test
    // 
    BeanFactory nativeBeanFactory = getNativeBeanFactory(plugin, classloader);
    // 
    // Now create the definable factory for accepting old style bean definitions from IPluginProvider
    // 
    GenericApplicationContext beanFactory = null;
    if (nativeBeanFactory != null && nativeBeanFactory instanceof GenericApplicationContext) {
        beanFactory = (GenericApplicationContext) nativeBeanFactory;
    } else {
        beanFactory = new GenericApplicationContext();
        beanFactory.setClassLoader(classloader);
        beanFactory.getBeanFactory().setBeanClassLoader(classloader);
        if (nativeBeanFactory != null) {
            beanFactory.getBeanFactory().setParentBeanFactory(nativeBeanFactory);
        }
    }
    beanFactory.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessor());
    // been made available to the plugin manager
    for (PluginBeanDefinition def : plugin.getBeans()) {
        // register by classname if id is null
        def.setBeanId((def.getBeanId() == null) ? def.getClassname() : def.getBeanId());
        try {
            assertUnique(beanFactory, plugin.getId(), def.getBeanId());
        } catch (PlatformPluginRegistrationException e) {
            logger.error(MessageFormat.format("Unable to register plugin bean, a bean by the id {0} is already defined in plugin: {1}", def.getBeanId(), plugin.getId()));
            continue;
        }
        // defining plugin beans the old way through the plugin provider ifc supports only prototype scope
        BeanDefinition beanDef = BeanDefinitionBuilder.rootBeanDefinition(def.getClassname()).setScope(BeanDefinition.SCOPE_PROTOTYPE).getBeanDefinition();
        beanFactory.registerBeanDefinition(def.getBeanId(), beanDef);
    }
    return beanFactory;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) PentahoBeanScopeValidatorPostProcessor(org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) PlatformPluginRegistrationException(org.pentaho.platform.api.engine.PlatformPluginRegistrationException)

Example 3 with PluginBeanDefinition

use of org.pentaho.platform.api.engine.PluginBeanDefinition in project pentaho-platform by pentaho.

the class SystemPathPluginProviderIT method testLoadBeanDefinition.

@SuppressWarnings("deprecation")
@Test
public void testLoadBeanDefinition() throws PlatformPluginRegistrationException {
    microPlatform.init();
    List<IPlatformPlugin> plugins = provider.getPlugins(new StandaloneSession());
    IPlatformPlugin plugin = (IPlatformPlugin) CollectionUtils.find(plugins, new PluginNameMatcherPredicate("Plugin 1"));
    assertNotNull("Plugin 1 should have been found", plugin);
    Collection<PluginBeanDefinition> beans = plugin.getBeans();
    assertEquals("FooComponent was not loaded", 1, CollectionUtils.countMatches(beans, new Predicate() {

        public boolean evaluate(Object object) {
            PluginBeanDefinition bean = (PluginBeanDefinition) object;
            return bean.getBeanId().equals("FooComponent") && bean.getClassname().equals("org.pentaho.test.platform.plugin.pluginmgr.FooComponent");
        }
    }));
    assertEquals("genericBean was not loaded", 1, CollectionUtils.countMatches(beans, new Predicate() {

        public boolean evaluate(Object object) {
            PluginBeanDefinition bean = (PluginBeanDefinition) object;
            return bean.getBeanId().equals("genericBean") && bean.getClassname().equals("java.lang.Object");
        }
    }));
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) IPlatformPlugin(org.pentaho.platform.api.engine.IPlatformPlugin) Predicate(org.apache.commons.collections.Predicate) Test(org.junit.Test)

Aggregations

PluginBeanDefinition (org.pentaho.platform.api.engine.PluginBeanDefinition)3 PentahoBeanScopeValidatorPostProcessor (org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 Predicate (org.apache.commons.collections.Predicate)1 Test (org.junit.Test)1 IPlatformPlugin (org.pentaho.platform.api.engine.IPlatformPlugin)1 PlatformPluginRegistrationException (org.pentaho.platform.api.engine.PlatformPluginRegistrationException)1 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)1 StandaloneSpringPentahoObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)1