Search in sources :

Example 21 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryBase method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    // folder cannot be deleted at teardown shutdown hooks have not yet necessarily completed
    // parent folder must match jcrRepository.homeDir bean property in repository-test-override.spring.xml
    FileUtils.deleteDirectory(new File("/tmp/repository-future/jackrabbit-test-TRUNK"));
    PentahoSessionHolder.setStrategyName(PentahoSessionHolder.MODE_GLOBAL);
    // register repository spring context for correct work of <pen:list>
    final StandaloneSpringPentahoObjectFactory pentahoObjectFactory = new StandaloneSpringPentahoObjectFactory();
    GenericApplicationContext appCtx = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx);
    xmlReader.loadBeanDefinitions(new ClassPathResource("repository.spring.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("repository-test-override.spring.xml"));
    pentahoObjectFactory.init(null, appCtx);
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource) StandaloneSpringPentahoObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory) BeforeClass(org.junit.BeforeClass)

Example 22 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project pentaho-platform by pentaho.

the class DefaultPluginManager method reload.

@Override
public final boolean reload() {
    IPentahoSession session = PentahoSessionHolder.getSession();
    boolean anyErrors = false;
    IPluginProvider pluginProvider = PentahoSystem.get(IPluginProvider.class, "IPluginProvider", session);
    List<IPlatformPlugin> providedPlugins = null;
    try {
        synchronized (registeredPlugins) {
            this.unloadPlugins();
        }
        // the plugin may fail to load during getPlugins without an exception thrown if the provider
        // is capable of discovering the plugin fine but there are structural problems with the plugin
        // itself. In this case a warning should be logged by the provider, but, again, no exception
        // is expected.
        providedPlugins = pluginProvider.getPlugins(session);
    } catch (PlatformPluginRegistrationException e1) {
        String msg = // $NON-NLS-1$
        Messages.getInstance().getErrorString("PluginManager.ERROR_0012_PLUGIN_DISCOVERY_FAILED");
        Logger.error(getClass().toString(), msg, e1);
        PluginMessageLogger.add(msg);
        anyErrors = true;
    }
    synchronized (providedPlugins) {
        for (IPlatformPlugin plugin : providedPlugins) {
            try {
                registeredPlugins.put(plugin.getId(), plugin);
                ClassLoader loader = setPluginClassLoader(plugin);
                initializeBeanFactory(plugin, loader);
            } catch (Throwable t) {
                // this has been logged already
                anyErrors = true;
                String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0011_FAILED_TO_REGISTER_PLUGIN", // $NON-NLS-1$
                plugin.getId());
                Logger.error(getClass().toString(), msg, t);
                PluginMessageLogger.add(msg);
            }
        }
        registeredPlugins.clear();
        for (IPlatformPlugin plugin : providedPlugins) {
            try {
                GenericApplicationContext beanFactory = beanFactoryMap.get(plugin.getId());
                if (beanFactory != null) {
                    beanFactory.refresh();
                }
                registerPlugin(plugin);
                registeredPlugins.put(plugin.getId(), plugin);
            } catch (Throwable t) {
                // this has been logged already
                anyErrors = true;
                String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0011_FAILED_TO_REGISTER_PLUGIN", // $NON-NLS-1$
                plugin.getId());
                Logger.error(getClass().toString(), msg, t);
                PluginMessageLogger.add(msg);
            }
        }
    }
    IServiceManager svcManager = PentahoSystem.get(IServiceManager.class, null);
    if (svcManager != null) {
        try {
            svcManager.initServices();
        } catch (ServiceInitializationException e) {
            String msg = Messages.getInstance().getErrorString(// $NON-NLS-1$
            "PluginManager.ERROR_0022_SERVICE_INITIALIZATION_FAILED");
            Logger.error(getClass().toString(), msg, e);
            PluginMessageLogger.add(msg);
        }
    }
    return !anyErrors;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IPluginProvider(org.pentaho.platform.api.engine.IPluginProvider) PlatformPluginRegistrationException(org.pentaho.platform.api.engine.PlatformPluginRegistrationException) IServiceManager(org.pentaho.platform.api.engine.IServiceManager) IPlatformPlugin(org.pentaho.platform.api.engine.IPlatformPlugin) ServiceInitializationException(org.pentaho.platform.api.engine.ServiceInitializationException)

Example 23 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext 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 24 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project pentaho-platform by pentaho.

the class PentahoSystemPluginManager method reload.

@Override
public boolean reload(IPentahoSession session) {
    boolean anyErrors = false;
    IPluginProvider pluginProvider = PentahoSystem.get(IPluginProvider.class, "IPluginProvider", session);
    List<IPlatformPlugin> providedPlugins = Collections.emptyList();
    try {
        this.unloadPlugins();
        // the plugin may fail to load during getPlugins without an exception thrown if the provider
        // is capable of discovering the plugin fine but there are structural problems with the plugin
        // itself. In this case a warning should be logged by the provider, but, again, no exception
        // is expected.
        providedPlugins = pluginProvider.getPlugins(session);
    } catch (PlatformPluginRegistrationException e1) {
        String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0012_PLUGIN_DISCOVERY_FAILED");
        org.pentaho.platform.util.logging.Logger.error(getClass().toString(), msg, e1);
        PluginMessageLogger.add(msg);
        anyErrors = true;
    }
    for (IPlatformPlugin plugin : providedPlugins) {
        try {
            IPlatformPlugin existingPlugin = PentahoSystem.get(IPlatformPlugin.class, null, Collections.singletonMap(PLUGIN_ID, plugin.getId()));
            if (existingPlugin != null) {
                throw new PlatformPluginRegistrationException(Messages.getInstance().getErrorString("PluginManager.ERROR_0024_PLUGIN_ALREADY_LOADED_BY_SAME_NAME", plugin.getId()));
            }
            final ClassLoader classloader = createClassloader(plugin);
            // Register the classloader, Spring App Context and Object Factory with PentahoSystem
            IPentahoObjectRegistration handle = PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<IPlatformPlugin>(IPlatformPlugin.class).object(plugin).attributes(Collections.<String, Object>singletonMap(PLUGIN_ID, plugin.getId())).build(), IPlatformPlugin.class);
            registerReference(plugin.getId(), handle);
            handle = PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<ClassLoader>(ClassLoader.class).object(classloader).attributes(Collections.<String, Object>singletonMap(PLUGIN_ID, plugin.getId())).build(), ClassLoader.class);
            registerReference(plugin.getId(), handle);
            final GenericApplicationContext beanFactory = createBeanFactory(plugin, classloader);
            final StandaloneSpringPentahoObjectFactory pentahoFactory = new StandaloneSpringPentahoObjectFactory("Plugin Factory ( " + plugin.getId() + " )");
            pentahoFactory.init(null, beanFactory);
            beanFactory.refresh();
            handle = PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<GenericApplicationContext>(GenericApplicationContext.class).object(beanFactory).attributes(Collections.<String, Object>singletonMap(PLUGIN_ID, plugin.getId())).build(), IPentahoRegistrableObjectFactory.Types.ALL);
            registerReference(plugin.getId(), handle);
            handle = PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<IPentahoObjectFactory>(IPentahoObjectFactory.class).object(pentahoFactory).attributes(Collections.<String, Object>singletonMap(PLUGIN_ID, plugin.getId())).build(), IPentahoObjectFactory.class);
            registerReference(plugin.getId(), handle);
        } catch (Throwable t) {
            // this has been logged already
            anyErrors = true;
            String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0011_FAILED_TO_REGISTER_PLUGIN", plugin.getId());
            org.pentaho.platform.util.logging.Logger.error(getClass().toString(), msg, t);
            PluginMessageLogger.add(msg);
        }
    }
    for (IPlatformPlugin plugin : providedPlugins) {
        try {
            registerPlugin(plugin);
        } catch (Throwable t) {
            // this has been logged already
            anyErrors = true;
            String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0011_FAILED_TO_REGISTER_PLUGIN", plugin.getId());
            org.pentaho.platform.util.logging.Logger.error(getClass().toString(), msg, t);
            PluginMessageLogger.add(msg);
        }
    }
    IServiceManager svcManager = PentahoSystem.get(IServiceManager.class, null);
    if (svcManager != null) {
        try {
            svcManager.initServices();
        } catch (ServiceInitializationException e) {
            String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0022_SERVICE_INITIALIZATION_FAILED");
            org.pentaho.platform.util.logging.Logger.error(getClass().toString(), msg, e);
            PluginMessageLogger.add(msg);
        }
    }
    for (IPluginManagerListener listener : listeners) {
        listener.onReload();
    }
    return !anyErrors;
}
Also used : IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) IPluginProvider(org.pentaho.platform.api.engine.IPluginProvider) IPlatformPlugin(org.pentaho.platform.api.engine.IPlatformPlugin) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IPluginManagerListener(org.pentaho.platform.api.engine.IPluginManagerListener) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) PlatformPluginRegistrationException(org.pentaho.platform.api.engine.PlatformPluginRegistrationException) IServiceManager(org.pentaho.platform.api.engine.IServiceManager) StandaloneSpringPentahoObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory) ServiceInitializationException(org.pentaho.platform.api.engine.ServiceInitializationException)

Example 25 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project pentaho-platform by pentaho.

the class PentahoSystemPluginManager method getNativeBeanFactory.

protected BeanFactory getNativeBeanFactory(final IPlatformPlugin plugin, final ClassLoader loader) {
    BeanFactory nativeFactory = null;
    if (plugin.getBeanFactory() != null) {
        // then we are probably in a unit test so just use the preconfigured one
        BeanFactory testFactory = plugin.getBeanFactory();
        if (testFactory instanceof ConfigurableBeanFactory) {
            ((ConfigurableBeanFactory) testFactory).setBeanClassLoader(loader);
        } else {
            logger.warn(Messages.getInstance().getString("PluginManager.WARN_WRONG_BEAN_FACTORY_TYPE"));
        }
        nativeFactory = testFactory;
    } else {
        File f = new File(((PluginClassLoader) loader).getPluginDir(), "plugin.spring.xml");
        if (f.exists()) {
            logger.debug("Found plugin spring file @ " + f.getAbsolutePath());
            FileSystemResource fsr = new FileSystemResource(f);
            GenericApplicationContext appCtx = new GenericApplicationContext() {

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(loader);
                }

                @Override
                public ClassLoader getClassLoader() {
                    return loader;
                }
            };
            XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx);
            xmlReader.setBeanClassLoader(loader);
            xmlReader.loadBeanDefinitions(fsr);
            nativeFactory = appCtx;
        }
    }
    return nativeFactory;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) 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) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)378 Test (org.junit.jupiter.api.Test)193 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)106 Test (org.junit.Test)74 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)72 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 ClassPathResource (org.springframework.core.io.ClassPathResource)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 QueueChannel (org.springframework.integration.channel.QueueChannel)16 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)15 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)15 GenericMessage (org.springframework.messaging.support.GenericMessage)14 Properties (java.util.Properties)13 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)13 File (java.io.File)12 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 Message (org.springframework.messaging.Message)12 InputStreamResource (org.springframework.core.io.InputStreamResource)11 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10