Search in sources :

Example 1 with SpringPentahoObjectReference

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

Aggregations

ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Bundle (org.osgi.framework.Bundle)1 ServiceFactory (org.osgi.framework.ServiceFactory)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 IPentahoObjectReference (org.pentaho.platform.api.engine.IPentahoObjectReference)1 IPentahoObjectRegistration (org.pentaho.platform.api.engine.IPentahoObjectRegistration)1 SingletonPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference)1 SpringPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.spring.SpringPentahoObjectReference)1