Search in sources :

Example 1 with IPentahoObjectRegistration

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

the class OSGIRuntimeObjectFactory method registerReference.

public <T> IPentahoObjectRegistration registerReference(final IPentahoObjectReference<?> reference, OSGIPentahoObjectRegistration existingRegistration, Class<?>... classes) {
    if (this.bundleContext == null) {
        ObjectRegistration runtimeRegistration = (ObjectRegistration) super.registerReference(reference, classes);
        OSGIPentahoObjectRegistration osgiPentahoObjectRegistration = new OSGIPentahoObjectRegistration(runtimeRegistration);
        synchronized (deferredRegistrations) {
            deferredRegistrations.add(osgiPentahoObjectRegistration);
        }
        return osgiPentahoObjectRegistration;
    }
    Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
    hashtable.putAll(reference.getAttributes());
    List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
    for (Class<?> aClass : classes) {
        try {
            // references unless the IPentahoObjectReference is a Singleton scope
            if (reference instanceof SingletonPentahoObjectReference || reference instanceof SpringPentahoObjectReference && (hashtable.get("scope").equals("singleton"))) {
                ServiceFactory<Object> factory = new ServiceFactory<Object>() {

                    @Override
                    public Object getService(Bundle bundle, ServiceRegistration<Object> serviceRegistration) {
                        return reference.getObject();
                    }

                    @Override
                    public void ungetService(Bundle bundle, ServiceRegistration<Object> serviceRegistration, Object o) {
                    }
                };
                if (hashtable.containsKey("priority")) {
                    hashtable.put(Constants.SERVICE_RANKING, hashtable.get("priority"));
                }
                ServiceRegistration<?> serviceRegistration = bundleContext.registerService(aClass.getName(), factory, hashtable);
                registrations.add(serviceRegistration);
            } else {
                // Publish it as an IPentahoObjectReference instead
                Hashtable<String, Object> referenceHashTable = new Hashtable<>(hashtable);
                referenceHashTable.put(REFERENCE_CLASS, aClass.getName());
                ServiceRegistration<?> serviceRegistration = bundleContext.registerService(IPentahoObjectReference.class.getName(), reference, referenceHashTable);
                registrations.add(serviceRegistration);
            }
        } catch (ClassCastException e) {
            logger.error("Error Retriving object from OSGI, Class is not as expected", e);
        }
    }
    if (existingRegistration != null) {
        existingRegistration.setRegistrations(registrations);
        return existingRegistration;
    } else {
        return new OSGIPentahoObjectRegistration(registrations);
    }
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) ServiceFactory(org.osgi.framework.ServiceFactory) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) ArrayList(java.util.ArrayList) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) SpringPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.spring.SpringPentahoObjectReference) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with IPentahoObjectRegistration

use of org.pentaho.platform.api.engine.IPentahoObjectRegistration 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 3 with IPentahoObjectRegistration

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

the class PentahoSystemPluginManager method registerOverlays.

private void registerOverlays(IPlatformPlugin plugin) {
    int priority = plugin.getOverlays().size();
    for (XulOverlay overlay : plugin.getOverlays()) {
        // preserve ordering as it may be significant
        final IPentahoObjectRegistration referenceHandle = PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<XulOverlay>(XulOverlay.class).object(overlay).attributes(Collections.<String, Object>singletonMap(PLUGIN_ID, plugin.getId())).priority(priority).build(), XulOverlay.class);
        priority--;
        registerReference(plugin.getId(), referenceHandle);
    }
}
Also used : XulOverlay(org.pentaho.ui.xul.XulOverlay) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration)

Example 4 with IPentahoObjectRegistration

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

the class PentahoSystemPluginManager method registerContentGenerators.

private void registerContentGenerators(IPlatformPlugin plugin, ClassLoader loader, final GenericApplicationContext beanFactory) throws PlatformPluginRegistrationException {
    // register the content generators
    for (final IContentGeneratorInfo cgInfo : plugin.getContentGenerators()) {
        // define the bean in the factory
        BeanDefinition beanDef = BeanDefinitionBuilder.rootBeanDefinition(cgInfo.getClassname()).setScope(BeanDefinition.SCOPE_PROTOTYPE).getBeanDefinition();
        // register bean with alias of content generator id (old way)
        beanFactory.registerBeanDefinition(cgInfo.getId(), beanDef);
        // register bean with alias of type (with default perspective) as well (new way)
        beanFactory.registerAlias(cgInfo.getId(), cgInfo.getType());
        PluginMessageLogger.add(Messages.getInstance().getString("PluginManager.USER_CONTENT_GENERATOR_REGISTERED", cgInfo.getId(), plugin.getId()));
        final HashMap<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(PLUGIN_ID, plugin.getId());
        attributes.put(CONTENT_TYPE, cgInfo.getType());
        final IPentahoObjectRegistration referenceHandle = PentahoSystem.registerReference(new PrototypePentahoObjectReference.Builder<IContentGenerator>(IContentGenerator.class).creator(new IObjectCreator<IContentGenerator>() {

            @Override
            public IContentGenerator create(IPentahoSession session) {
                return (IContentGenerator) beanFactory.getBean(cgInfo.getId());
            }
        }).attributes(attributes).build(), IContentGenerator.class);
        registerReference(plugin.getId(), referenceHandle);
    }
    // The remaining operations require a beanFactory
    if (beanFactory == null) {
        return;
    }
    String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory.getBeanFactory(), IContentGenerator.class);
    ArrayList<String> ids = new ArrayList<String>();
    for (String beanName : names) {
        ids.add(beanName);
        Collections.addAll(ids, beanFactory.getAliases(beanName));
    }
    for (final String beanName : ids) {
        final HashMap<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(PLUGIN_ID, plugin.getId());
        attributes.put(CONTENT_TYPE, beanName);
        final IPentahoObjectRegistration referenceHandle = PentahoSystem.registerReference(new PrototypePentahoObjectReference.Builder<IContentGenerator>(IContentGenerator.class).creator(new IObjectCreator<IContentGenerator>() {

            @Override
            public IContentGenerator create(IPentahoSession session) {
                return (IContentGenerator) beanFactory.getBean(beanName);
            }
        }).attributes(attributes).build(), IContentGenerator.class);
        registerReference(plugin.getId(), referenceHandle);
    }
}
Also used : IContentGeneratorInfo(org.pentaho.platform.api.engine.IContentGeneratorInfo) IObjectCreator(org.pentaho.platform.api.engine.IObjectCreator) HashMap(java.util.HashMap) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ArrayList(java.util.ArrayList) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IContentGenerator(org.pentaho.platform.api.engine.IContentGenerator) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration)

Example 5 with IPentahoObjectRegistration

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

the class PentahoSystemPluginManager method unloadPlugins.

private void unloadPlugins() {
    // is called within the synchronized block in reload
    for (IPlatformPlugin plugin : PentahoSystem.getAll(IPlatformPlugin.class)) {
        try {
            plugin.unLoaded();
            // if a spring app context was registered for this plugin, remove and close it
            final GenericApplicationContext appContext = PentahoSystem.get(GenericApplicationContext.class, null, Collections.singletonMap(PLUGIN_ID, plugin.getId()));
            if (appContext != null) {
                final StandaloneSpringPentahoObjectFactory pentahoObjectFactory = StandaloneSpringPentahoObjectFactory.getInstance(appContext);
                if (pentahoObjectFactory != null) {
                    PentahoSystem.deregisterObjectFactory(pentahoObjectFactory);
                }
                appContext.close();
            }
        } catch (Throwable t) {
            // we do not want any type of exception to leak out and cause a problem here
            // A plugin unload should not adversely affect anything downstream, it should
            // log an error and otherwise fail silently
            String msg = Messages.getInstance().getErrorString("PluginManager.ERROR_0014_PLUGIN_FAILED_TO_PROPERLY_UNLOAD", // $NON-NLS-1$
            plugin.getId());
            logger.error(getClass().toString(), msg, t);
            PluginMessageLogger.add(msg);
        }
        final ClassLoader classLoader = PentahoSystem.get(ClassLoader.class, null, Collections.singletonMap(PLUGIN_ID, plugin.getId()));
        if (classLoader != null) {
            try {
                ((PluginClassLoader) classLoader).close();
            } catch (IOException e) {
                logger.error("errror closing plugin clasloader", e);
            }
        }
    }
    for (Map.Entry<String, IPentahoObjectRegistration> entry : handleRegistry.entries()) {
        entry.getValue().remove();
    }
    handleRegistry.clear();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IOException(java.io.IOException) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) Map(java.util.Map) HashMap(java.util.HashMap) IPlatformPlugin(org.pentaho.platform.api.engine.IPlatformPlugin) StandaloneSpringPentahoObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)

Aggregations

IPentahoObjectRegistration (org.pentaho.platform.api.engine.IPentahoObjectRegistration)10 SingletonPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference)4 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 IPlatformPlugin (org.pentaho.platform.api.engine.IPlatformPlugin)2 StandaloneSpringPentahoObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 IOException (java.io.IOException)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 WeakHashMap (java.util.WeakHashMap)1 Bundle (org.osgi.framework.Bundle)1 ServiceFactory (org.osgi.framework.ServiceFactory)1 ServiceReference (org.osgi.framework.ServiceReference)1 IContentGenerator (org.pentaho.platform.api.engine.IContentGenerator)1