Search in sources :

Example 1 with PentahoBeanScopeValidatorPostProcessor

use of org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor in project pentaho-platform by pentaho.

the class StandaloneSpringPentahoObjectFactory method init.

/**
 * Initializes this object factory by creating a self-contained Spring {@link ApplicationContext} if one is not passed
 * in.
 *
 * @param configFile the Spring bean definition XML file
 * @param context    the {@link ApplicationContext} object, if null, then this method will create one
 */
public void init(String configFile, Object context) {
    if (context == null) {
        // beanFactory = new FileSystemXmlApplicationContext(configFile);
        FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext(configFile);
        appCtx.refresh();
        appCtx.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessor());
        Scope requestScope = new ThreadLocalScope();
        appCtx.getBeanFactory().registerScope("request", requestScope);
        Scope sessionScope = new ThreadLocalScope();
        appCtx.getBeanFactory().registerScope("session", sessionScope);
        beanFactory = appCtx;
    } else {
        if (!(context instanceof ConfigurableApplicationContext)) {
            String msg = Messages.getInstance().getErrorString(// $NON-NLS-1$
            "StandalonePentahoObjectFactory.ERROR_0001_CONTEXT_NOT_SUPPORTED", getClass().getSimpleName(), "GenericApplicationContext", // $NON-NLS-1$
            context.getClass().getName());
            throw new IllegalArgumentException(msg);
        }
        ConfigurableApplicationContext configAppCtx = (ConfigurableApplicationContext) context;
        if (configAppCtx.getBeanFactory().getRegisteredScope("request") == null) {
            Scope requestScope = new ThreadLocalScope();
            configAppCtx.getBeanFactory().registerScope("request", requestScope);
        }
        if (configAppCtx.getBeanFactory().getRegisteredScope("session") == null) {
            Scope sessionScope = new ThreadLocalScope();
            configAppCtx.getBeanFactory().registerScope("session", sessionScope);
        }
        setBeanFactory(configAppCtx);
    }
}
Also used : FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Scope(org.springframework.beans.factory.config.Scope) PentahoBeanScopeValidatorPostProcessor(org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor)

Example 2 with PentahoBeanScopeValidatorPostProcessor

use of org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor 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 3 with PentahoBeanScopeValidatorPostProcessor

use of org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor in project pentaho-platform by pentaho.

the class JAXRSServlet method getAppContext.

protected ApplicationContext getAppContext() {
    ConfigurableWebApplicationContext wac = new XmlWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            return new FileSystemResource(new File(path));
        }
    };
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getServletName());
    String springFile = PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$ //$NON-NLS-2$
    "system" + File.separator + "pentahoServices.spring.xml");
    wac.setConfigLocations(new String[] { springFile });
    wac.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessor());
    wac.refresh();
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) PentahoBeanScopeValidatorPostProcessor(org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File)

Example 4 with PentahoBeanScopeValidatorPostProcessor

use of org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor 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)

Aggregations

PentahoBeanScopeValidatorPostProcessor (org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor)4 PluginBeanDefinition (org.pentaho.platform.api.engine.PluginBeanDefinition)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 File (java.io.File)1 PlatformPluginRegistrationException (org.pentaho.platform.api.engine.PlatformPluginRegistrationException)1 StandaloneSpringPentahoObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)1 Scope (org.springframework.beans.factory.config.Scope)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)1 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)1