Search in sources :

Example 1 with SingletonPentahoObjectReference

use of org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference 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 SingletonPentahoObjectReference

use of org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference in project pentaho-platform by pentaho.

the class RuntimeObjectFactoryTest method testGetObjectReferences.

@Test
public void testGetObjectReferences() throws Exception {
    IPentahoSession session = new StandaloneSession("joe");
    RuntimeObjectFactory factory = new RuntimeObjectFactory();
    final SingletonPentahoObjectReference<String> something1 = new SingletonPentahoObjectReference<String>(String.class, "Something1", Collections.<String, Object>emptyMap(), 0);
    final SingletonPentahoObjectReference<String> something2 = new SingletonPentahoObjectReference<String>(String.class, "Something2", Collections.<String, Object>emptyMap(), 1);
    factory.registerReference(something1);
    factory.registerReference(something2);
    List<String> out = factory.getAll(String.class, PentahoSessionHolder.getSession());
    assertEquals(2, out.size());
    List<IPentahoObjectReference<String>> refs = factory.getObjectReferences(String.class, session);
    assertSame(something1, refs.get(1));
    assertSame(something2, refs.get(0));
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) Test(org.junit.Test)

Example 3 with SingletonPentahoObjectReference

use of org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference in project pentaho-platform by pentaho.

the class OSGIRuntimeObjectFactoryTest method testObjectDefined.

@Test
public void testObjectDefined() throws Exception {
    assertFalse(objectFactory.objectDefined(String.class));
    SingletonPentahoObjectReference<String> ref = new SingletonPentahoObjectReference<String>(String.class, "Testing", Collections.<String, Object>singletonMap("foo", "bar"), 10);
    IPentahoObjectRegistration iPentahoObjectRegistration = objectFactory.registerReference(ref, String.class);
    String s = objectFactory.get(String.class, null);
    assertEquals("Testing", s);
    assertTrue(objectFactory.objectDefined(String.class));
    ServiceRegistration registration = mock(ServiceRegistration.class);
    ServiceRegistration registration2 = mock(ServiceRegistration.class);
    ServiceReference mockRef = mock(ServiceReference.class);
    when(bundleContext.registerService(eq(String.class.getName()), anyObject(), any(Dictionary.class))).thenReturn(registration);
    when(bundleContext.registerService(eq(IPentahoObjectReference.class.getName()), anyObject(), any(Dictionary.class))).thenReturn(registration2);
    objectFactory.setBundleContext(bundleContext);
    when(bundleContext.getServiceReference(String.class)).thenReturn(mockRef);
    assertTrue(objectFactory.objectDefined(String.class));
    iPentahoObjectRegistration.remove();
    verify(registration, times(1)).unregister();
}
Also used : Dictionary(java.util.Dictionary) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with SingletonPentahoObjectReference

use of org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference in project pentaho-platform by pentaho.

the class RuntimeObjectFactoryTest method testGetObjectReference.

@Test
public void testGetObjectReference() throws Exception {
    IPentahoSession session = new StandaloneSession("joe");
    RuntimeObjectFactory factory = new RuntimeObjectFactory();
    final SingletonPentahoObjectReference<String> something = new SingletonPentahoObjectReference<String>(String.class, "Something");
    factory.registerReference(something);
    assertSame(something, factory.getObjectReference(String.class, session));
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) Test(org.junit.Test)

Aggregations

SingletonPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference)4 Test (org.junit.Test)3 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 IPentahoObjectReference (org.pentaho.platform.api.engine.IPentahoObjectReference)2 IPentahoObjectRegistration (org.pentaho.platform.api.engine.IPentahoObjectRegistration)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 Bundle (org.osgi.framework.Bundle)1 ServiceFactory (org.osgi.framework.ServiceFactory)1 ServiceReference (org.osgi.framework.ServiceReference)1 SpringPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.spring.SpringPentahoObjectReference)1