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);
}
}
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));
}
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();
}
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));
}
Aggregations