Search in sources :

Example 1 with IObjectCreator

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

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

the class PrototypePentahoObjectReferenceTest method testReference.

@Test
public void testReference() throws Exception {
    PrototypePentahoObjectReference<UUID> sessionRef = new PrototypePentahoObjectReference.Builder<UUID>(UUID.class).creator(new IObjectCreator<UUID>() {

        @Override
        public UUID create(IPentahoSession session) {
            return UUID.randomUUID();
        }
    }).build();
    IPentahoSession s1 = new StandaloneSession("joe");
    IPentahoSession s2 = new StandaloneSession("admin");
    PentahoSessionHolder.setSession(s1);
    UUID s1Uuid = sessionRef.getObject();
    PentahoSessionHolder.setSession(s2);
    UUID s2Uuid = sessionRef.getObject();
    assertNotSame(s1Uuid, s2Uuid);
    PentahoSessionHolder.setSession(s1);
    UUID s1UuidAgain = sessionRef.getObject();
    assertNotSame(s1Uuid, s1UuidAgain);
}
Also used : IObjectCreator(org.pentaho.platform.api.engine.IObjectCreator) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with IObjectCreator

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

the class SessionBoundPentahoObjectReferenceTest method testReference.

@Test
public void testReference() throws Exception {
    SessionBoundPentahoObjectReference<UUID> sessionRef = new SessionBoundPentahoObjectReference.Builder<UUID>(UUID.class).creator(new IObjectCreator<UUID>() {

        @Override
        public UUID create(IPentahoSession session) {
            return UUID.randomUUID();
        }
    }).build();
    IPentahoSession s1 = new StandaloneSession("joe");
    IPentahoSession s2 = new StandaloneSession("admin");
    PentahoSessionHolder.setSession(s1);
    UUID s1Uuid = sessionRef.getObject();
    PentahoSessionHolder.setSession(s2);
    UUID s2Uuid = sessionRef.getObject();
    assertNotSame(s1Uuid, s2Uuid);
    PentahoSessionHolder.setSession(s1);
    UUID s1UuidAgain = sessionRef.getObject();
    assertSame(s1Uuid, s1UuidAgain);
}
Also used : IObjectCreator(org.pentaho.platform.api.engine.IObjectCreator) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

IObjectCreator (org.pentaho.platform.api.engine.IObjectCreator)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 UUID (java.util.UUID)2 Test (org.junit.Test)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IContentGenerator (org.pentaho.platform.api.engine.IContentGenerator)1 IContentGeneratorInfo (org.pentaho.platform.api.engine.IContentGeneratorInfo)1 IPentahoObjectRegistration (org.pentaho.platform.api.engine.IPentahoObjectRegistration)1 PluginBeanDefinition (org.pentaho.platform.api.engine.PluginBeanDefinition)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1