use of org.pentaho.platform.engine.core.system.objfac.spring.SpringPentahoObjectReference 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);
}
}
Aggregations